-
Notifications
You must be signed in to change notification settings - Fork 2
/
Atoms.go
45 lines (40 loc) · 1.07 KB
/
Atoms.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
package Go_Utils
import "sync/atomic"
// AtomicUint backed by uintptr
type AtomicUint struct {
v uintptr
}
func (u *AtomicUint) Load() uint {
return uint(atomic.LoadUintptr(&u.v))
}
func (u *AtomicUint) Store(v uint) {
atomic.StoreUintptr(&u.v, uintptr(v))
}
func (u *AtomicUint) Add(d uint) uint {
return uint(atomic.AddUintptr(&u.v, uintptr(d)))
}
func (u *AtomicUint) Swap(v uint) uint {
return uint(atomic.SwapUintptr(&u.v, uintptr(v)))
}
func (u *AtomicUint) CompareAndSwap(exp, v uint) bool {
return atomic.CompareAndSwapUintptr(&u.v, uintptr(exp), uintptr(v))
}
// AtomicInt backed by uintptr
type AtomicInt struct {
v uintptr
}
func (u *AtomicInt) Load() int {
return int(atomic.LoadUintptr(&u.v))
}
func (u *AtomicInt) Store(v int) {
atomic.StoreUintptr(&u.v, uintptr(v))
}
func (u *AtomicInt) Add(d int) int {
return int(atomic.AddUintptr(&u.v, uintptr(d)))
}
func (u *AtomicInt) Swap(v int) int {
return int(atomic.SwapUintptr(&u.v, uintptr(v)))
}
func (u *AtomicInt) CompareAndSwap(exp, v int) bool {
return atomic.CompareAndSwapUintptr(&u.v, uintptr(exp), uintptr(v))
}