* gcc.dg/atomic-compare-exchange-1.c,
[official-gcc.git] / libgo / runtime / lock_sema.c
blobce435119323fdcc9a58882eec3f9b1ecb706da33
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
7 #include "runtime.h"
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.
25 enum
27 LOCKED = 1,
29 ACTIVE_SPIN = 4,
30 ACTIVE_SPIN_CNT = 30,
31 PASSIVE_SPIN = 1,
34 void
35 runtime_lock(Lock *l)
37 M *m;
38 uintptr v;
39 uint32 i, spin;
41 m = runtime_m();
42 if(m->locks++ < 0)
43 runtime_throw("runtime_lock: lock count");
45 // Speculative grab for lock.
46 if(runtime_casp((void**)&l->key, nil, (void*)LOCKED))
47 return;
49 if(m->waitsema == 0)
50 m->waitsema = runtime_semacreate();
52 // On uniprocessor's, no point spinning.
53 // On multiprocessors, spin for ACTIVE_SPIN attempts.
54 spin = 0;
55 if(runtime_ncpu > 1)
56 spin = ACTIVE_SPIN;
58 for(i=0;; i++) {
59 v = (uintptr)runtime_atomicloadp((void**)&l->key);
60 if((v&LOCKED) == 0) {
61 unlocked:
62 if(runtime_casp((void**)&l->key, (void*)v, (void*)(v|LOCKED)))
63 return;
64 i = 0;
66 if(i<spin)
67 runtime_procyield(ACTIVE_SPIN_CNT);
68 else if(i<spin+PASSIVE_SPIN)
69 runtime_osyield();
70 else {
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.
74 // Queue this M.
75 for(;;) {
76 m->nextwaitm = (void*)(v&~LOCKED);
77 if(runtime_casp((void**)&l->key, (void*)v, (void*)((uintptr)m|LOCKED)))
78 break;
79 v = (uintptr)runtime_atomicloadp((void**)&l->key);
80 if((v&LOCKED) == 0)
81 goto unlocked;
83 if(v&LOCKED) {
84 // Queued. Wait.
85 runtime_semasleep(-1);
86 i = 0;
92 void
93 runtime_unlock(Lock *l)
95 uintptr v;
96 M *mp;
98 for(;;) {
99 v = (uintptr)runtime_atomicloadp((void**)&l->key);
100 if(v == LOCKED) {
101 if(runtime_casp((void**)&l->key, (void*)LOCKED, nil))
102 break;
103 } else {
104 // Other M's are waiting for the lock.
105 // Dequeue an M.
106 mp = (void*)(v&~LOCKED);
107 if(runtime_casp((void**)&l->key, (void*)v, mp->nextwaitm)) {
108 // Dequeued an M. Wake it.
109 runtime_semawakeup(mp);
110 break;
115 if(--runtime_m()->locks < 0)
116 runtime_throw("runtime_unlock: lock count");
119 // One-time notifications.
120 void
121 runtime_noteclear(Note *n)
123 n->key = 0;
126 void
127 runtime_notewakeup(Note *n)
129 M *mp;
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?
137 if(mp == nil) {
138 // Nothing was waiting. Done.
139 } else if(mp == (M*)LOCKED) {
140 // Two notewakeups! Not allowed.
141 runtime_throw("notewakeup - double wakeup");
142 } else {
143 // Must be the waiting m. Wake it up.
144 runtime_semawakeup(mp);
148 void
149 runtime_notesleep(Note *n)
151 M *m;
153 m = runtime_m();
155 if(runtime_g() != m->g0)
156 runtime_throw("notesleep not on g0");
158 if(m->waitsema == 0)
159 m->waitsema = runtime_semacreate();
160 if(!runtime_casp((void**)&n->key, nil, m)) { // must be LOCKED (got wakeup)
161 if(n->key != LOCKED)
162 runtime_throw("notesleep - waitm out of sync");
163 return;
165 // Queued. Sleep.
166 runtime_semasleep(-1);
169 static bool
170 notetsleep(Note *n, int64 ns, int64 deadline, M *mp)
172 M *m;
174 m = runtime_m();
176 // Conceptually, deadline and mp are local variables.
177 // They are passed as arguments so that the space for them
178 // does not count against our nosplit stack sequence.
180 // Register for wakeup on n->waitm.
181 if(!runtime_casp((void**)&n->key, nil, m)) { // must be LOCKED (got wakeup already)
182 if(n->key != LOCKED)
183 runtime_throw("notetsleep - waitm out of sync");
184 return true;
187 if(ns < 0) {
188 // Queued. Sleep.
189 runtime_semasleep(-1);
190 return true;
193 deadline = runtime_nanotime() + ns;
194 for(;;) {
195 // Registered. Sleep.
196 if(runtime_semasleep(ns) >= 0) {
197 // Acquired semaphore, semawakeup unregistered us.
198 // Done.
199 return true;
202 // Interrupted or timed out. Still registered. Semaphore not acquired.
203 ns = deadline - runtime_nanotime();
204 if(ns <= 0)
205 break;
206 // Deadline hasn't arrived. Keep sleeping.
209 // Deadline arrived. Still registered. Semaphore not acquired.
210 // Want to give up and return, but have to unregister first,
211 // so that any notewakeup racing with the return does not
212 // try to grant us the semaphore when we don't expect it.
213 for(;;) {
214 mp = runtime_atomicloadp((void**)&n->key);
215 if(mp == m) {
216 // No wakeup yet; unregister if possible.
217 if(runtime_casp((void**)&n->key, mp, nil))
218 return false;
219 } else if(mp == (M*)LOCKED) {
220 // Wakeup happened so semaphore is available.
221 // Grab it to avoid getting out of sync.
222 if(runtime_semasleep(-1) < 0)
223 runtime_throw("runtime: unable to acquire - semaphore out of sync");
224 return true;
225 } else
226 runtime_throw("runtime: unexpected waitm - semaphore out of sync");
230 bool
231 runtime_notetsleep(Note *n, int64 ns)
233 M *m;
234 bool res;
236 m = runtime_m();
238 if(runtime_g() != m->g0 && !m->gcing)
239 runtime_throw("notetsleep not on g0");
241 if(m->waitsema == 0)
242 m->waitsema = runtime_semacreate();
244 res = notetsleep(n, ns, 0, nil);
245 return res;
248 // same as runtime_notetsleep, but called on user g (not g0)
249 // calls only nosplit functions between entersyscallblock/exitsyscall
250 bool
251 runtime_notetsleepg(Note *n, int64 ns)
253 M *m;
254 bool res;
256 m = runtime_m();
258 if(runtime_g() == m->g0)
259 runtime_throw("notetsleepg on g0");
261 if(m->waitsema == 0)
262 m->waitsema = runtime_semacreate();
264 runtime_entersyscallblock();
265 res = notetsleep(n, ns, 0, nil);
266 runtime_exitsyscall();
267 return res;