...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package atomic
16
17 import (
18 "internal/cpu"
19 "unsafe"
20 )
21
22
23 var lock struct {
24 state uint32
25 pad [cpu.CacheLinePadSize - 4]byte
26 }
27
28
29 func spinLock(state *uint32)
30
31
32 func spinUnlock(state *uint32)
33
34
35 func lockAndCheck(addr *uint64) {
36
37 if uintptr(unsafe.Pointer(addr))&7 != 0 {
38 panicUnaligned()
39 }
40
41 _ = *addr
42
43 spinLock(&lock.state)
44 }
45
46
47 func unlock() {
48 spinUnlock(&lock.state)
49 }
50
51
52 func Xadd64(addr *uint64, delta int64) (new uint64) {
53 lockAndCheck(addr)
54
55 new = *addr + uint64(delta)
56 *addr = new
57
58 unlock()
59 return
60 }
61
62
63 func Xchg64(addr *uint64, new uint64) (old uint64) {
64 lockAndCheck(addr)
65
66 old = *addr
67 *addr = new
68
69 unlock()
70 return
71 }
72
73
74 func Cas64(addr *uint64, old, new uint64) (swapped bool) {
75 lockAndCheck(addr)
76
77 if (*addr) == old {
78 *addr = new
79 unlock()
80 return true
81 }
82
83 unlock()
84 return false
85 }
86
87
88 func Load64(addr *uint64) (val uint64) {
89 lockAndCheck(addr)
90
91 val = *addr
92
93 unlock()
94 return
95 }
96
97
98 func Store64(addr *uint64, val uint64) {
99 lockAndCheck(addr)
100
101 *addr = val
102
103 unlock()
104 return
105 }
106
107
108 func Xadd(ptr *uint32, delta int32) uint32
109
110
111 func Xadduintptr(ptr *uintptr, delta uintptr) uintptr
112
113
114 func Xchg(ptr *uint32, new uint32) uint32
115
116
117 func Xchguintptr(ptr *uintptr, new uintptr) uintptr
118
119
120 func Load(ptr *uint32) uint32
121
122
123 func Load8(ptr *uint8) uint8
124
125
126 func Loadp(ptr unsafe.Pointer) unsafe.Pointer
127
128
129 func LoadAcq(ptr *uint32) uint32
130
131
132 func LoadAcquintptr(ptr *uintptr) uintptr
133
134
135 func And8(ptr *uint8, val uint8)
136
137
138 func Or8(ptr *uint8, val uint8)
139
140
141 func And(ptr *uint32, val uint32)
142
143
144 func Or(ptr *uint32, val uint32)
145
146
147 func Store(ptr *uint32, val uint32)
148
149
150 func Store8(ptr *uint8, val uint8)
151
152
153 func StorepNoWB(ptr unsafe.Pointer, val unsafe.Pointer)
154
155
156 func StoreRel(ptr *uint32, val uint32)
157
158
159 func StoreReluintptr(ptr *uintptr, val uintptr)
160
161
162 func CasRel(addr *uint32, old, new uint32) bool
163
View as plain text