-
Notifications
You must be signed in to change notification settings - Fork 46
/
atomic.go
51 lines (42 loc) · 1.75 KB
/
atomic.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
44
45
46
47
48
49
50
51
package haxmap
import (
"sync/atomic"
"unsafe"
)
// noCopy implements sync.Locker so that go vet can trigger
// warnings when types embedding noCopy are copied.
type noCopy struct{}
func (c *noCopy) Lock() {}
func (c *noCopy) Unlock() {}
type atomicUint32 struct {
_ noCopy
v uint32
}
type atomicPointer[T any] struct {
_ noCopy
ptr unsafe.Pointer
}
type atomicUintptr struct {
_ noCopy
ptr uintptr
}
func (u *atomicUint32) Load() uint32 { return atomic.LoadUint32(&u.v) }
func (u *atomicUint32) Store(v uint32) { atomic.StoreUint32(&u.v, v) }
func (u *atomicUint32) Add(delta uint32) uint32 { return atomic.AddUint32(&u.v, delta) }
func (u *atomicUint32) Swap(v uint32) uint32 { return atomic.SwapUint32(&u.v, v) }
func (u *atomicUint32) CompareAndSwap(old, new uint32) bool {
return atomic.CompareAndSwapUint32(&u.v, old, new)
}
func (p *atomicPointer[T]) Load() *T { return (*T)(atomic.LoadPointer(&p.ptr)) }
func (p *atomicPointer[T]) Store(v *T) { atomic.StorePointer(&p.ptr, unsafe.Pointer(v)) }
func (p *atomicPointer[T]) Swap(v *T) *T { return (*T)(atomic.SwapPointer(&p.ptr, unsafe.Pointer(v))) }
func (p *atomicPointer[T]) CompareAndSwap(old, new *T) bool {
return atomic.CompareAndSwapPointer(&p.ptr, unsafe.Pointer(old), unsafe.Pointer(new))
}
func (u *atomicUintptr) Load() uintptr { return atomic.LoadUintptr(&u.ptr) }
func (u *atomicUintptr) Store(v uintptr) { atomic.StoreUintptr(&u.ptr, v) }
func (u *atomicUintptr) Add(delta uintptr) uintptr { return atomic.AddUintptr(&u.ptr, delta) }
func (u *atomicUintptr) Swap(v uintptr) uintptr { return atomic.SwapUintptr(&u.ptr, v) }
func (u *atomicUintptr) CompareAndSwap(old, new uintptr) bool {
return atomic.CompareAndSwapUintptr(&u.ptr, old, new)
}