mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-17 03:03:27 +00:00
internal/task: add non-atomic atomic operations
This adds some non-atomic types that have the same interface as the ones in sync/atomic. We currently don't need these to be atomic (because the scheduler is entirely cooperative), but once we add support for a scheduler with multiple threads and/or preemptive scheduling we can trivially add some type aliases under a different build tag in the future when real atomicity is needed for threading.
This commit is contained in:
@@ -0,0 +1,41 @@
|
|||||||
|
package task
|
||||||
|
|
||||||
|
// Atomics implementation for cooperative systems. The atomic types here aren't
|
||||||
|
// actually atomic, they assume that accesses cannot be interrupted by a
|
||||||
|
// different goroutine or interrupt happening at the same time.
|
||||||
|
|
||||||
|
type atomicIntegerType interface {
|
||||||
|
uintptr | uint32 | uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
type pseudoAtomic[T atomicIntegerType] struct {
|
||||||
|
v T
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *pseudoAtomic[T]) Add(delta T) T { x.v += delta; return x.v }
|
||||||
|
func (x *pseudoAtomic[T]) Load() T { return x.v }
|
||||||
|
func (x *pseudoAtomic[T]) Store(val T) { x.v = val }
|
||||||
|
func (x *pseudoAtomic[T]) CompareAndSwap(old, new T) (swapped bool) {
|
||||||
|
if x.v != old {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
x.v = new
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
func (x *pseudoAtomic[T]) Swap(new T) (old T) {
|
||||||
|
old = x.v
|
||||||
|
x.v = new
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Uintptr is an atomic uintptr when multithreading is enabled, and a plain old
|
||||||
|
// uintptr otherwise.
|
||||||
|
type Uintptr = pseudoAtomic[uintptr]
|
||||||
|
|
||||||
|
// Uint32 is an atomic uint32 when multithreading is enabled, and a plain old
|
||||||
|
// uint32 otherwise.
|
||||||
|
type Uint32 = pseudoAtomic[uint32]
|
||||||
|
|
||||||
|
// Uint64 is an atomic uint64 when multithreading is enabled, and a plain old
|
||||||
|
// uint64 otherwise.
|
||||||
|
type Uint64 = pseudoAtomic[uint64]
|
||||||
Reference in New Issue
Block a user