-
Notifications
You must be signed in to change notification settings - Fork 946
/
Copy pathatomic-cooperative.go
43 lines (35 loc) · 1.16 KB
/
atomic-cooperative.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//go:build !scheduler.threads
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]