1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2021 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // locks.hfa -- PUBLIC |
---|
8 | // Runtime locks that used with the runtime thread system. |
---|
9 | // |
---|
10 | // Author : Colby Alexander Parsons |
---|
11 | // Created On : Thu Jan 21 19:46:50 2021 |
---|
12 | // Last Modified By : |
---|
13 | // Last Modified On : |
---|
14 | // Update Count : |
---|
15 | // |
---|
16 | |
---|
17 | #pragma once |
---|
18 | |
---|
19 | #include <stdbool.h> |
---|
20 | #include <stdio.h> |
---|
21 | |
---|
22 | #include "bits/weakso_locks.hfa" |
---|
23 | #include "containers/queueLockFree.hfa" |
---|
24 | #include "containers/list.hfa" |
---|
25 | |
---|
26 | #include "limits.hfa" |
---|
27 | #include "thread.hfa" |
---|
28 | |
---|
29 | #include "time_t.hfa" |
---|
30 | #include "time.hfa" |
---|
31 | |
---|
32 | //----------------------------------------------------------------------------- |
---|
33 | // Semaphores |
---|
34 | |
---|
35 | // '0-nary' semaphore |
---|
36 | // Similar to a counting semaphore except the value of one is never reached |
---|
37 | // as a consequence, a V() that would bring the value to 1 *spins* until |
---|
38 | // a P consumes it |
---|
39 | struct Semaphore0nary { |
---|
40 | __spinlock_t lock; // needed to protect |
---|
41 | mpsc_queue(thread$) queue; |
---|
42 | }; |
---|
43 | |
---|
44 | static inline bool P(Semaphore0nary & this, thread$ * thrd) { |
---|
45 | /* paranoid */ verify(!thrd`next); |
---|
46 | /* paranoid */ verify(!(&(*thrd)`next)); |
---|
47 | |
---|
48 | push(this.queue, thrd); |
---|
49 | return true; |
---|
50 | } |
---|
51 | |
---|
52 | static inline bool P(Semaphore0nary & this) { |
---|
53 | thread$ * thrd = active_thread(); |
---|
54 | P(this, thrd); |
---|
55 | park(); |
---|
56 | return true; |
---|
57 | } |
---|
58 | |
---|
59 | static inline thread$ * V(Semaphore0nary & this, bool doUnpark = true) { |
---|
60 | thread$ * next; |
---|
61 | lock(this.lock __cfaabi_dbg_ctx2); |
---|
62 | for (;;) { |
---|
63 | next = pop(this.queue); |
---|
64 | if (next) break; |
---|
65 | Pause(); |
---|
66 | } |
---|
67 | unlock(this.lock); |
---|
68 | |
---|
69 | if (doUnpark) unpark(next); |
---|
70 | return next; |
---|
71 | } |
---|
72 | |
---|
73 | // Wrapper used on top of any sempahore to avoid potential locking |
---|
74 | struct BinaryBenaphore { |
---|
75 | volatile ssize_t counter; |
---|
76 | }; |
---|
77 | |
---|
78 | static inline { |
---|
79 | void ?{}(BinaryBenaphore & this) { this.counter = 0; } |
---|
80 | void ?{}(BinaryBenaphore & this, zero_t) { this.counter = 0; } |
---|
81 | void ?{}(BinaryBenaphore & this, one_t ) { this.counter = 1; } |
---|
82 | |
---|
83 | // returns true if no blocking needed |
---|
84 | bool P(BinaryBenaphore & this) { |
---|
85 | return __atomic_fetch_sub(&this.counter, 1, __ATOMIC_SEQ_CST) > 0; |
---|
86 | } |
---|
87 | |
---|
88 | bool tryP(BinaryBenaphore & this) { |
---|
89 | ssize_t c = this.counter; |
---|
90 | /* paranoid */ verify( c > MIN ); |
---|
91 | return (c >= 1) && __atomic_compare_exchange_n(&this.counter, &c, c-1, false, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED); |
---|
92 | } |
---|
93 | |
---|
94 | // returns true if notify needed |
---|
95 | bool V(BinaryBenaphore & this) { |
---|
96 | ssize_t c = 0; |
---|
97 | for () { |
---|
98 | /* paranoid */ verify( this.counter < MAX ); |
---|
99 | if (__atomic_compare_exchange_n(&this.counter, &c, c+1, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) { |
---|
100 | if (c == 0) return true; |
---|
101 | /* paranoid */ verify(c < 0); |
---|
102 | return false; |
---|
103 | } else { |
---|
104 | if (c == 1) return true; |
---|
105 | /* paranoid */ verify(c < 1); |
---|
106 | Pause(); |
---|
107 | } |
---|
108 | } |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | // Binary Semaphore based on the BinaryBenaphore on top of the 0-nary Semaphore |
---|
113 | struct ThreadBenaphore { |
---|
114 | BinaryBenaphore ben; |
---|
115 | Semaphore0nary sem; |
---|
116 | }; |
---|
117 | |
---|
118 | static inline void ?{}(ThreadBenaphore & this) {} |
---|
119 | static inline void ?{}(ThreadBenaphore & this, zero_t) { (this.ben){ 0 }; } |
---|
120 | static inline void ?{}(ThreadBenaphore & this, one_t ) { (this.ben){ 1 }; } |
---|
121 | |
---|
122 | static inline bool P(ThreadBenaphore & this) { return P(this.ben) ? false : P(this.sem); } |
---|
123 | static inline bool tryP(ThreadBenaphore & this) { return tryP(this.ben); } |
---|
124 | static inline bool P(ThreadBenaphore & this, bool wait) { return wait ? P(this) : tryP(this); } |
---|
125 | |
---|
126 | static inline thread$ * V(ThreadBenaphore & this, bool doUnpark = true) { |
---|
127 | if (V(this.ben)) return 0p; |
---|
128 | return V(this.sem, doUnpark); |
---|
129 | } |
---|
130 | |
---|
131 | //----------------------------------------------------------------------------- |
---|
132 | // Semaphore |
---|
133 | struct semaphore { |
---|
134 | __spinlock_t lock; |
---|
135 | int count; |
---|
136 | __queue_t(thread$) waiting; |
---|
137 | }; |
---|
138 | |
---|
139 | void ?{}(semaphore & this, int count = 1); |
---|
140 | void ^?{}(semaphore & this); |
---|
141 | bool P (semaphore & this); |
---|
142 | bool V (semaphore & this); |
---|
143 | bool V (semaphore & this, unsigned count); |
---|
144 | thread$ * V (semaphore & this, bool ); |
---|
145 | |
---|
146 | //---------- |
---|
147 | struct single_acquisition_lock { |
---|
148 | inline blocking_lock; |
---|
149 | }; |
---|
150 | |
---|
151 | static inline void ?{}( single_acquisition_lock & this ) {((blocking_lock &)this){ false, false };} |
---|
152 | static inline void ^?{}( single_acquisition_lock & this ) {} |
---|
153 | static inline void lock ( single_acquisition_lock & this ) { lock ( (blocking_lock &)this ); } |
---|
154 | static inline bool try_lock ( single_acquisition_lock & this ) { return try_lock( (blocking_lock &)this ); } |
---|
155 | static inline void unlock ( single_acquisition_lock & this ) { unlock ( (blocking_lock &)this ); } |
---|
156 | static inline size_t on_wait ( single_acquisition_lock & this ) { return on_wait ( (blocking_lock &)this ); } |
---|
157 | static inline void on_wakeup( single_acquisition_lock & this, size_t v ) { on_wakeup ( (blocking_lock &)this, v ); } |
---|
158 | static inline void on_notify( single_acquisition_lock & this, struct thread$ * t ) { on_notify( (blocking_lock &)this, t ); } |
---|
159 | |
---|
160 | //---------- |
---|
161 | struct owner_lock { |
---|
162 | inline blocking_lock; |
---|
163 | }; |
---|
164 | |
---|
165 | static inline void ?{}( owner_lock & this ) {((blocking_lock &)this){ true, true };} |
---|
166 | static inline void ^?{}( owner_lock & this ) {} |
---|
167 | static inline void lock ( owner_lock & this ) { lock ( (blocking_lock &)this ); } |
---|
168 | static inline bool try_lock ( owner_lock & this ) { return try_lock( (blocking_lock &)this ); } |
---|
169 | static inline void unlock ( owner_lock & this ) { unlock ( (blocking_lock &)this ); } |
---|
170 | static inline size_t on_wait ( owner_lock & this ) { return on_wait ( (blocking_lock &)this ); } |
---|
171 | static inline void on_wakeup( owner_lock & this, size_t v ) { on_wakeup ( (blocking_lock &)this, v ); } |
---|
172 | static inline void on_notify( owner_lock & this, struct thread$ * t ) { on_notify( (blocking_lock &)this, t ); } |
---|
173 | |
---|
174 | struct fast_lock { |
---|
175 | thread$ * volatile owner; |
---|
176 | ThreadBenaphore sem; |
---|
177 | }; |
---|
178 | |
---|
179 | static inline void ?{}(fast_lock & this) __attribute__((deprecated("use linear_backoff_then_block_lock instead"))); |
---|
180 | static inline void ?{}(fast_lock & this) { this.owner = 0p; } |
---|
181 | |
---|
182 | static inline bool $try_lock(fast_lock & this, thread$ * thrd) { |
---|
183 | thread$ * exp = 0p; |
---|
184 | return __atomic_compare_exchange_n(&this.owner, &exp, thrd, false, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED); |
---|
185 | } |
---|
186 | |
---|
187 | static inline void lock( fast_lock & this ) __attribute__((deprecated("use linear_backoff_then_block_lock instead"), artificial)); |
---|
188 | static inline void lock( fast_lock & this ) { |
---|
189 | thread$ * thrd = active_thread(); |
---|
190 | /* paranoid */verify(thrd != this.owner); |
---|
191 | |
---|
192 | for (;;) { |
---|
193 | if ($try_lock(this, thrd)) return; |
---|
194 | P(this.sem); |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | static inline bool try_lock( fast_lock & this ) __attribute__((deprecated("use linear_backoff_then_block_lock instead"), artificial)); |
---|
199 | static inline bool try_lock ( fast_lock & this ) { |
---|
200 | thread$ * thrd = active_thread(); |
---|
201 | /* paranoid */ verify(thrd != this.owner); |
---|
202 | return $try_lock(this, thrd); |
---|
203 | } |
---|
204 | |
---|
205 | static inline thread$ * unlock( fast_lock & this ) __attribute__((deprecated("use linear_backoff_then_block_lock instead"), artificial)); |
---|
206 | static inline thread$ * unlock( fast_lock & this ) { |
---|
207 | /* paranoid */ verify(active_thread() == this.owner); |
---|
208 | |
---|
209 | // open 'owner' before unlocking anyone |
---|
210 | // so new and unlocked threads don't park incorrectly. |
---|
211 | // This may require additional fencing on ARM. |
---|
212 | this.owner = 0p; |
---|
213 | |
---|
214 | return V(this.sem); |
---|
215 | } |
---|
216 | |
---|
217 | static inline size_t on_wait( fast_lock & this ) { unlock(this); return 0; } |
---|
218 | static inline void on_wakeup( fast_lock & this, size_t ) { lock(this); } |
---|
219 | static inline void on_notify( fast_lock &, struct thread$ * t ) { unpark(t); } |
---|
220 | |
---|
221 | struct mcs_node { |
---|
222 | mcs_node * volatile next; |
---|
223 | single_sem sem; |
---|
224 | }; |
---|
225 | |
---|
226 | static inline void ?{}(mcs_node & this) { this.next = 0p; } |
---|
227 | |
---|
228 | static inline mcs_node * volatile & ?`next ( mcs_node * node ) { |
---|
229 | return node->next; |
---|
230 | } |
---|
231 | |
---|
232 | struct mcs_lock { |
---|
233 | mcs_queue(mcs_node) queue; |
---|
234 | }; |
---|
235 | |
---|
236 | static inline void lock(mcs_lock & l, mcs_node & n) { |
---|
237 | if(push(l.queue, &n)) |
---|
238 | wait(n.sem); |
---|
239 | } |
---|
240 | |
---|
241 | static inline void unlock(mcs_lock & l, mcs_node & n) { |
---|
242 | mcs_node * next = advance(l.queue, &n); |
---|
243 | if(next) post(next->sem); |
---|
244 | } |
---|
245 | |
---|
246 | struct linear_backoff_then_block_lock { |
---|
247 | // Spin lock used for mutual exclusion |
---|
248 | __spinlock_t spinlock; |
---|
249 | |
---|
250 | // Current thread owning the lock |
---|
251 | struct thread$ * owner; |
---|
252 | |
---|
253 | // List of blocked threads |
---|
254 | dlist( thread$ ) blocked_threads; |
---|
255 | |
---|
256 | // Used for comparing and exchanging |
---|
257 | volatile size_t lock_value; |
---|
258 | |
---|
259 | // used for linear backoff spinning |
---|
260 | int spin_start; |
---|
261 | int spin_end; |
---|
262 | int spin_count; |
---|
263 | |
---|
264 | // after unsuccessful linear backoff yield this many times |
---|
265 | int yield_count; |
---|
266 | }; |
---|
267 | |
---|
268 | static inline void ?{}( linear_backoff_then_block_lock & this, int spin_start, int spin_end, int spin_count, int yield_count ) { |
---|
269 | this.spinlock{}; |
---|
270 | this.blocked_threads{}; |
---|
271 | this.lock_value = 0; |
---|
272 | this.spin_start = spin_start; |
---|
273 | this.spin_end = spin_end; |
---|
274 | this.spin_count = spin_count; |
---|
275 | this.yield_count = yield_count; |
---|
276 | } |
---|
277 | static inline void ?{}( linear_backoff_then_block_lock & this ) { this{4, 1024, 16, 0}; } |
---|
278 | static inline void ^?{}( linear_backoff_then_block_lock & this ) {} |
---|
279 | static inline void ?{}( linear_backoff_then_block_lock & this, linear_backoff_then_block_lock this2 ) = void; |
---|
280 | static inline void ?=?( linear_backoff_then_block_lock & this, linear_backoff_then_block_lock this2 ) = void; |
---|
281 | |
---|
282 | static inline bool internal_try_lock(linear_backoff_then_block_lock & this, size_t & compare_val) with(this) { |
---|
283 | if (__atomic_compare_exchange_n(&lock_value, &compare_val, 1, false, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) { |
---|
284 | owner = active_thread(); |
---|
285 | return true; |
---|
286 | } |
---|
287 | return false; |
---|
288 | } |
---|
289 | |
---|
290 | static inline bool try_lock(linear_backoff_then_block_lock & this) { size_t compare_val = 0; return internal_try_lock(this, compare_val); } |
---|
291 | |
---|
292 | static inline bool try_lock_contention(linear_backoff_then_block_lock & this) with(this) { |
---|
293 | if (__atomic_exchange_n(&lock_value, 2, __ATOMIC_ACQUIRE) == 0) { |
---|
294 | owner = active_thread(); |
---|
295 | return true; |
---|
296 | } |
---|
297 | return false; |
---|
298 | } |
---|
299 | |
---|
300 | static inline bool block(linear_backoff_then_block_lock & this) with(this) { |
---|
301 | lock( spinlock __cfaabi_dbg_ctx2 ); |
---|
302 | if (lock_value != 2) { |
---|
303 | unlock( spinlock ); |
---|
304 | return true; |
---|
305 | } |
---|
306 | insert_last( blocked_threads, *active_thread() ); |
---|
307 | unlock( spinlock ); |
---|
308 | park( ); |
---|
309 | return true; |
---|
310 | } |
---|
311 | |
---|
312 | static inline bool lock(linear_backoff_then_block_lock & this) with(this) { |
---|
313 | // if owner just return |
---|
314 | if (active_thread() == owner) return true; |
---|
315 | size_t compare_val = 0; |
---|
316 | int spin = spin_start; |
---|
317 | // linear backoff |
---|
318 | for( ;; ) { |
---|
319 | compare_val = 0; |
---|
320 | if (internal_try_lock(this, compare_val)) return true; |
---|
321 | if (2 == compare_val) break; |
---|
322 | for (int i = 0; i < spin; i++) Pause(); |
---|
323 | if (spin >= spin_end) break; |
---|
324 | spin += spin; |
---|
325 | } |
---|
326 | |
---|
327 | if(2 != compare_val && try_lock_contention(this)) return true; |
---|
328 | // block until signalled |
---|
329 | while (block(this)) if(try_lock_contention(this)) return true; |
---|
330 | |
---|
331 | // this should never be reached as block(this) always returns true |
---|
332 | return false; |
---|
333 | } |
---|
334 | |
---|
335 | static inline void unlock(linear_backoff_then_block_lock & this) with(this) { |
---|
336 | verify(lock_value > 0); |
---|
337 | owner = 0p; |
---|
338 | if (__atomic_exchange_n(&lock_value, 0, __ATOMIC_RELEASE) == 1) return; |
---|
339 | lock( spinlock __cfaabi_dbg_ctx2 ); |
---|
340 | thread$ * t = &try_pop_front( blocked_threads ); |
---|
341 | unlock( spinlock ); |
---|
342 | unpark( t ); |
---|
343 | } |
---|
344 | |
---|
345 | static inline void on_notify(linear_backoff_then_block_lock & this, struct thread$ * t ) { unpark(t); } |
---|
346 | static inline size_t on_wait(linear_backoff_then_block_lock & this) { unlock(this); return 0; } |
---|
347 | static inline void on_wakeup(linear_backoff_then_block_lock & this, size_t recursion ) { lock(this); } |
---|
348 | |
---|
349 | //----------------------------------------------------------------------------- |
---|
350 | // is_blocking_lock |
---|
351 | trait is_blocking_lock(L & | sized(L)) { |
---|
352 | // For synchronization locks to use when acquiring |
---|
353 | void on_notify( L &, struct thread$ * ); |
---|
354 | |
---|
355 | // For synchronization locks to use when releasing |
---|
356 | size_t on_wait( L & ); |
---|
357 | |
---|
358 | // to set recursion count after getting signalled; |
---|
359 | void on_wakeup( L &, size_t recursion ); |
---|
360 | }; |
---|
361 | |
---|
362 | //----------------------------------------------------------------------------- |
---|
363 | // // info_thread |
---|
364 | // // the info thread is a wrapper around a thread used |
---|
365 | // // to store extra data for use in the condition variable |
---|
366 | forall(L & | is_blocking_lock(L)) { |
---|
367 | struct info_thread; |
---|
368 | |
---|
369 | // // for use by sequence |
---|
370 | // info_thread(L) *& Back( info_thread(L) * this ); |
---|
371 | // info_thread(L) *& Next( info_thread(L) * this ); |
---|
372 | } |
---|
373 | |
---|
374 | //----------------------------------------------------------------------------- |
---|
375 | // Synchronization Locks |
---|
376 | forall(L & | is_blocking_lock(L)) { |
---|
377 | struct condition_variable { |
---|
378 | // Spin lock used for mutual exclusion |
---|
379 | __spinlock_t lock; |
---|
380 | |
---|
381 | // List of blocked threads |
---|
382 | dlist( info_thread(L) ) blocked_threads; |
---|
383 | |
---|
384 | // Count of current blocked threads |
---|
385 | int count; |
---|
386 | }; |
---|
387 | |
---|
388 | |
---|
389 | void ?{}( condition_variable(L) & this ); |
---|
390 | void ^?{}( condition_variable(L) & this ); |
---|
391 | |
---|
392 | bool notify_one( condition_variable(L) & this ); |
---|
393 | bool notify_all( condition_variable(L) & this ); |
---|
394 | |
---|
395 | uintptr_t front( condition_variable(L) & this ); |
---|
396 | |
---|
397 | bool empty ( condition_variable(L) & this ); |
---|
398 | int counter( condition_variable(L) & this ); |
---|
399 | |
---|
400 | void wait( condition_variable(L) & this ); |
---|
401 | void wait( condition_variable(L) & this, uintptr_t info ); |
---|
402 | bool wait( condition_variable(L) & this, Duration duration ); |
---|
403 | bool wait( condition_variable(L) & this, uintptr_t info, Duration duration ); |
---|
404 | |
---|
405 | void wait( condition_variable(L) & this, L & l ); |
---|
406 | void wait( condition_variable(L) & this, L & l, uintptr_t info ); |
---|
407 | bool wait( condition_variable(L) & this, L & l, Duration duration ); |
---|
408 | bool wait( condition_variable(L) & this, L & l, uintptr_t info, Duration duration ); |
---|
409 | } |
---|