Debian Codegraph @

libgo/go/runtime/lock_sema.go

raw
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build aix || darwin || hurd || netbsd || openbsd || plan9 || solaris || windows
6
7 package runtime
8
9 import (
10 "runtime/internal/atomic"
11 "unsafe"
12 )
13
14 // For gccgo, while we still have C runtime code, use go:linkname to
15 // export some functions.
16 //
17 //go:linkname lock
18 //go:linkname unlock
19 //go:linkname noteclear
20 //go:linkname notewakeup
21 //go:linkname notesleep
22 //go:linkname notetsleep
23 //go:linkname notetsleepg
24
25 // This implementation depends on OS-specific implementations of
26 //
27 // func semacreate(mp *m)
28 // Create a semaphore for mp, if it does not already have one.
29 //
30 // func semasleep(ns int64) int32
31 // If ns < 0, acquire m's semaphore and return 0.
32 // If ns >= 0, try to acquire m's semaphore for at most ns nanoseconds.
33 // Return 0 if the semaphore was acquired, -1 if interrupted or timed out.
34 //
35 // func semawakeup(mp *m)
36 // Wake up mp, which is or will soon be sleeping on its semaphore.
37 //
38 const (
39 mutex_locked uintptr = 1
40
41 active_spin = 4
42 active_spin_cnt = 30
43 passive_spin = 1
44 )
45
46 func lock(l *mutex) {
47 lockWithRank(l, getLockRank(l))
48 }
49
50 func lock2(l *mutex) {
51 gp := getg()
52 if gp.m.locks < 0 {
53 throw("runtime·lock: lock count")
54 }
55 gp.m.locks++
56
57 // Speculative grab for lock.
58 if atomic.Casuintptr(&l.key, 0, mutex_locked) {
59 return
60 }
61 semacreate(gp.m)
62
63 // On uniprocessor's, no point spinning.
64 // On multiprocessors, spin for ACTIVE_SPIN attempts.
65 spin := 0
66 if ncpu > 1 {
67 spin = active_spin
68 }
69 Loop:
70 for i := 0; ; i++ {
71 v := atomic.Loaduintptr(&l.key)
72 if v&mutex_locked == 0 {
73 // Unlocked. Try to lock.
74 if atomic.Casuintptr(&l.key, v, v|mutex_locked) {
75 return
76 }
77 i = 0
78 }
79 if i < spin {
80 procyield(active_spin_cnt)
81 } else if i < spin+passive_spin {
82 osyield()
83 } else {
84 // Someone else has it.
85 // l->waitm points to a linked list of M's waiting
86 // for this lock, chained through m->nextwaitm.
87 // Queue this M.
88 for {
89 gp.m.nextwaitm = muintptr(v &^ mutex_locked)
90 if atomic.Casuintptr(&l.key, v, uintptr(unsafe.Pointer(gp.m))|mutex_locked) {
91 break
92 }
93 v = atomic.Loaduintptr(&l.key)
94 if v&mutex_locked == 0 {
95 continue Loop
96 }
97 }
98 if v&mutex_locked != 0 {
99 // Queued. Wait.
100 semasleep(-1)
101 i = 0
102 }
103 }
104 }
105 }
106
107 func unlock(l *mutex) {
108 unlockWithRank(l)
109 }
110
111 //go:nowritebarrier
112 // We might not be holding a p in this code.
113 func unlock2(l *mutex) {
114 gp := getg()
115 var mp *m
116 for {
117 v := atomic.Loaduintptr(&l.key)
118 if v == mutex_locked {
119 if atomic.Casuintptr(&l.key, mutex_locked, 0) {
120 break
121 }
122 } else {
123 // Other M's are waiting for the lock.
124 // Dequeue an M.
125 mp = muintptr(v &^ mutex_locked).ptr()
126 if atomic.Casuintptr(&l.key, v, uintptr(mp.nextwaitm)) {
127 // Dequeued an M. Wake it.
128 semawakeup(mp)
129 break
130 }
131 }
132 }
133 gp.m.locks--
134 if gp.m.locks < 0 {
135 throw("runtime·unlock: lock count")
136 }
137 // if gp.m.locks == 0 && gp.preempt { // restore the preemption request in case we've cleared it in newstack
138 // gp.stackguard0 = stackPreempt
139 // }
140 }
141
142 // One-time notifications.
143 func noteclear(n *note) {
144 if GOOS == "aix" {
145 // On AIX, semaphores might not synchronize the memory in some
146 // rare cases. See issue #30189.
147 atomic.Storeuintptr(&n.key, 0)
148 } else {
149 n.key = 0
150 }
151 }
152
153 func notewakeup(n *note) {
154 var v uintptr
155 for {
156 v = atomic.Loaduintptr(&n.key)
157 if atomic.Casuintptr(&n.key, v, mutex_locked) {
158 break
159 }
160 }
161
162 // Successfully set waitm to locked.
163 // What was it before?
164 switch {
165 case v == 0:
166 // Nothing was waiting. Done.
167 case v == mutex_locked:
168 // Two notewakeups! Not allowed.
169 throw("notewakeup - double wakeup")
170 default:
171 // Must be the waiting m. Wake it up.
172 semawakeup((*m)(unsafe.Pointer(v)))
173 }
174 }
175
176 func notesleep(n *note) {
177 gp := getg()
178 if gp != gp.m.g0 {
179 throw("notesleep not on g0")
180 }
181 semacreate(gp.m)
182 if !atomic.Casuintptr(&n.key, 0, uintptr(unsafe.Pointer(gp.m))) {
183 // Must be locked (got wakeup).
184 if n.key != mutex_locked {
185 throw("notesleep - waitm out of sync")
186 }
187 return
188 }
189 // Queued. Sleep.
190 gp.m.blocked = true
191 if *cgo_yield == nil {
192 semasleep(-1)
193 } else {
194 // Sleep for an arbitrary-but-moderate interval to poll libc interceptors.
195 const ns = 10e6
196 for atomic.Loaduintptr(&n.key) == 0 {
197 semasleep(ns)
198 asmcgocall(*cgo_yield, nil)
199 }
200 }
201 gp.m.blocked = false
202 }
203
204 //go:nosplit
205 func notetsleep_internal(n *note, ns int64, gp *g, deadline int64) bool {
206 // gp and deadline are logically local variables, but they are written
207 // as parameters so that the stack space they require is charged
208 // to the caller.
209 // This reduces the nosplit footprint of notetsleep_internal.
210 gp = getg()
211
212 // Register for wakeup on n->waitm.
213 if !atomic.Casuintptr(&n.key, 0, uintptr(unsafe.Pointer(gp.m))) {
214 // Must be locked (got wakeup).
215 if n.key != mutex_locked {
216 throw("notetsleep - waitm out of sync")
217 }
218 return true
219 }
220 if ns < 0 {
221 // Queued. Sleep.
222 gp.m.blocked = true
223 if *cgo_yield == nil {
224 semasleep(-1)
225 } else {
226 // Sleep in arbitrary-but-moderate intervals to poll libc interceptors.
227 const ns = 10e6
228 for semasleep(ns) < 0 {
229 asmcgocall(*cgo_yield, nil)
230 }
231 }
232 gp.m.blocked = false
233 return true
234 }
235
236 deadline = nanotime() + ns
237 for {
238 // Registered. Sleep.
239 gp.m.blocked = true
240 if *cgo_yield != nil && ns > 10e6 {
241 ns = 10e6
242 }
243 if semasleep(ns) >= 0 {
244 gp.m.blocked = false
245 // Acquired semaphore, semawakeup unregistered us.
246 // Done.
247 return true
248 }
249 if *cgo_yield != nil {
250 asmcgocall(*cgo_yield, nil)
251 }
252 gp.m.blocked = false
253 // Interrupted or timed out. Still registered. Semaphore not acquired.
254 ns = deadline - nanotime()
255 if ns <= 0 {
256 break
257 }
258 // Deadline hasn't arrived. Keep sleeping.
259 }
260
261 // Deadline arrived. Still registered. Semaphore not acquired.
262 // Want to give up and return, but have to unregister first,
263 // so that any notewakeup racing with the return does not
264 // try to grant us the semaphore when we don't expect it.
265 for {
266 v := atomic.Loaduintptr(&n.key)
267 switch v {
268 case uintptr(unsafe.Pointer(gp.m)):
269 // No wakeup yet; unregister if possible.
270 if atomic.Casuintptr(&n.key, v, 0) {
271 return false
272 }
273 case mutex_locked:
274 // Wakeup happened so semaphore is available.
275 // Grab it to avoid getting out of sync.
276 gp.m.blocked = true
277 if semasleep(-1) < 0 {
278 throw("runtime: unable to acquire - semaphore out of sync")
279 }
280 gp.m.blocked = false
281 return true
282 default:
283 throw("runtime: unexpected waitm - semaphore out of sync")
284 }
285 }
286 }
287
288 func notetsleep(n *note, ns int64) bool {
289 gp := getg()
290 if gp != gp.m.g0 {
291 throw("notetsleep not on g0")
292 }
293 semacreate(gp.m)
294 return notetsleep_internal(n, ns, nil, 0)
295 }
296
297 // same as runtime·notetsleep, but called on user g (not g0)
298 // calls only nosplit functions between entersyscallblock/exitsyscall
299 func notetsleepg(n *note, ns int64) bool {
300 gp := getg()
301 if gp == gp.m.g0 {
302 throw("notetsleepg on g0")
303 }
304 semacreate(gp.m)
305 entersyscallblock()
306 ok := notetsleep_internal(n, ns, nil, 0)
307 exitsyscall()
308 return ok
309 }
310
311 func beforeIdle(int64, int64) (*g, bool) {
312 return nil, false
313 }
314
315 func checkTimeouts() {}