-
Notifications
You must be signed in to change notification settings - Fork 12
/
atomic.go
51 lines (43 loc) · 1.06 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 gohalt
import (
"math"
"sync/atomic"
)
func atomicBAdd(number *uint64, delta uint64) (res uint64) {
prev := atomic.LoadUint64(number)
if res = atomic.AddUint64(number, delta); res < prev {
res = math.MaxUint64
atomic.StoreUint64(number, res)
}
return
}
func atomicBSub(number *uint64, delta uint64) (res uint64) {
prev := atomic.LoadUint64(number)
if res = atomic.AddUint64(number, ^uint64(delta-1)); res > prev {
res = 0
atomic.StoreUint64(number, res)
}
return
}
func atomicBIncr(number *uint64) (res uint64) {
if res = atomic.AddUint64(number, 1); res == 0 {
res = atomic.AddUint64(number, ^uint64(0))
}
return
}
func atomicBDecr(number *uint64) (res uint64) {
if res = atomic.AddUint64(number, ^uint64(0)); res == math.MaxUint64 {
res = atomic.AddUint64(number, 1)
}
return
}
func atomicIncr(number *uint64) uint64 {
return atomic.AddUint64(number, 1)
}
func atomicSet(number *uint64, value uint64) uint64 {
atomic.StoreUint64(number, value)
return value
}
func atomicGet(number *uint64) uint64 {
return atomic.LoadUint64(number)
}