-
Notifications
You must be signed in to change notification settings - Fork 0
/
cas.go
65 lines (59 loc) · 1002 Bytes
/
cas.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package sync
import (
"runtime"
"sync/atomic"
)
// POINT 自旋当前次数协程让出CPU
const POINT = 10
// AddInt32 CAS增加int32
func AddInt32(val, add int32) {
count := 0
for {
count++
if atomic.CompareAndSwapInt32(&val, val, (val + add)) {
if count%POINT == 0 {
runtime.Gosched()
}
continue
}
}
}
// AddInt64 CAS增加int64
func AddInt64(val, add int64) {
count := 0
for {
count++
if atomic.CompareAndSwapInt64(&val, val, (val + add)) {
if count%POINT == 0 {
runtime.Gosched()
}
continue
}
}
}
// AddUint32 CAS增加uint32
func AddUint32(val, add uint32) {
count := 0
for {
count++
if atomic.CompareAndSwapUint32(&val, val, (val + add)) {
if count%POINT == 0 {
runtime.Gosched()
}
continue
}
}
}
// AddUint64 CAS增加uint64
func AddUint64(val, add uint64) {
count := 0
for {
count++
if atomic.CompareAndSwapUint64(&val, val, (val + add)) {
if count%POINT == 0 {
runtime.Gosched()
}
continue
}
}
}