atomic-cooperative.mx raw

   1  //go:build moxie.unicore
   2  
   3  package task
   4  
   5  // Atomics implementation for cooperative systems. The atomic types here aren't
   6  // actually atomic, they assume that accesses cannot be interrupted by a
   7  // different goroutine or interrupt happening at the same time.
   8  
   9  type atomicIntegerType interface {
  10  	uintptr | uint32 | uint64
  11  }
  12  
  13  type pseudoAtomic[T atomicIntegerType] struct {
  14  	v T
  15  }
  16  
  17  func (x *pseudoAtomic[T]) Add(delta T) T { x.v += delta; return x.v }
  18  func (x *pseudoAtomic[T]) Load() T       { return x.v }
  19  func (x *pseudoAtomic[T]) Store(val T)   { x.v = val }
  20  func (x *pseudoAtomic[T]) CompareAndSwap(old, new T) (swapped bool) {
  21  	if x.v != old {
  22  		return false
  23  	}
  24  	x.v = new
  25  	return true
  26  }
  27  func (x *pseudoAtomic[T]) Swap(new T) (old T) {
  28  	old = x.v
  29  	x.v = new
  30  	return
  31  }
  32  
  33  // Uintptr is an atomic uintptr when multithreading is enabled, and a plain old
  34  // uintptr otherwise.
  35  type Uintptr = pseudoAtomic[uintptr]
  36  
  37  // Uint32 is an atomic uint32 when multithreading is enabled, and a plain old
  38  // uint32 otherwise.
  39  type Uint32 = pseudoAtomic[uint32]
  40  
  41  // Uint64 is an atomic uint64 when multithreading is enabled, and a plain old
  42  // uint64 otherwise.
  43  type Uint64 = pseudoAtomic[uint64]
  44