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.
5 // +build darwin netbsd openbsd plan9 windows
9 // This implementation depends on OS-specific implementations of
11 // uintptr runtime_semacreate(void)
12 // Create a semaphore, which will be assigned to m->waitsema.
13 // The zero value is treated as absence of any semaphore,
14 // so be sure to return a non-zero value.
16 // int32 runtime_semasleep(int64 ns)
17 // If ns < 0, acquire m->waitsema and return 0.
18 // If ns >= 0, try to acquire m->waitsema for at most ns nanoseconds.
19 // Return 0 if the semaphore was acquired, -1 if interrupted or timed out.
21 // int32 runtime_semawakeup(M *mp)
22 // Wake up mp, which is or will soon be sleeping on mp->waitsema.
43 runtime_throw("runtime_lock: lock count");
45 // Speculative grab for lock.
46 if(runtime_casp((void**)&l->key, nil, (void*)LOCKED))
50 m->waitsema = runtime_semacreate();
52 // On uniprocessor's, no point spinning.
53 // On multiprocessors, spin for ACTIVE_SPIN attempts.
59 v = (uintptr)runtime_atomicloadp((void**)&l->key);
62 if(runtime_casp((void**)&l->key, (void*)v, (void*)(v|LOCKED)))
67 runtime_procyield(ACTIVE_SPIN_CNT);
68 else if(i<spin+PASSIVE_SPIN)
71 // Someone else has it.
72 // l->waitm points to a linked list of M's waiting
73 // for this lock, chained through m->nextwaitm.
76 m->nextwaitm = (void*)(v&~LOCKED);
77 if(runtime_casp((void**)&l->key, (void*)v, (void*)((uintptr)m|LOCKED)))
79 v = (uintptr)runtime_atomicloadp((void**)&l->key);
85 runtime_semasleep(-1);
93 runtime_unlock(Lock *l)
98 if(--runtime_m()->locks < 0)
99 runtime_throw("runtime_unlock: lock count");
102 v = (uintptr)runtime_atomicloadp((void**)&l->key);
104 if(runtime_casp((void**)&l->key, (void*)LOCKED, nil))
107 // Other M's are waiting for the lock.
109 mp = (void*)(v&~LOCKED);
110 if(runtime_casp((void**)&l->key, (void*)v, mp->nextwaitm)) {
111 // Dequeued an M. Wake it.
112 runtime_semawakeup(mp);
119 // One-time notifications.
121 runtime_noteclear(Note *n)
127 runtime_notewakeup(Note *n)
132 mp = runtime_atomicloadp((void**)&n->key);
133 while(!runtime_casp((void**)&n->key, mp, (void*)LOCKED));
135 // Successfully set waitm to LOCKED.
136 // What was it before?
138 // Nothing was waiting. Done.
139 } else if(mp == (M*)LOCKED) {
140 // Two notewakeups! Not allowed.
141 runtime_throw("notewakeup - double wakeup");
143 // Must be the waiting m. Wake it up.
144 runtime_semawakeup(mp);
149 runtime_notesleep(Note *n)
155 m->waitsema = runtime_semacreate();
156 if(!runtime_casp((void**)&n->key, nil, m)) { // must be LOCKED (got wakeup)
158 runtime_throw("notesleep - waitm out of sync");
163 runtime_setprof(false);
164 runtime_semasleep(-1);
166 runtime_setprof(true);
170 runtime_notetsleep(Note *n, int64 ns)
177 runtime_notesleep(n);
183 m->waitsema = runtime_semacreate();
185 // Register for wakeup on n->waitm.
186 if(!runtime_casp((void**)&n->key, nil, m)) { // must be LOCKED (got wakeup already)
188 runtime_throw("notetsleep - waitm out of sync");
193 runtime_setprof(false);
194 deadline = runtime_nanotime() + ns;
196 // Registered. Sleep.
197 if(runtime_semasleep(ns) >= 0) {
198 // Acquired semaphore, semawakeup unregistered us.
201 runtime_setprof(true);
205 // Interrupted or timed out. Still registered. Semaphore not acquired.
206 now = runtime_nanotime();
210 // Deadline hasn't arrived. Keep sleeping.
215 runtime_setprof(true);
217 // Deadline arrived. Still registered. Semaphore not acquired.
218 // Want to give up and return, but have to unregister first,
219 // so that any notewakeup racing with the return does not
220 // try to grant us the semaphore when we don't expect it.
222 mp = runtime_atomicloadp((void**)&n->key);
224 // No wakeup yet; unregister if possible.
225 if(runtime_casp((void**)&n->key, mp, nil))
227 } else if(mp == (M*)LOCKED) {
228 // Wakeup happened so semaphore is available.
229 // Grab it to avoid getting out of sync.
230 if(runtime_semasleep(-1) < 0)
231 runtime_throw("runtime: unable to acquire - semaphore out of sync");
234 runtime_throw("runtime: unexpected waitm - semaphore out of sync");