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 -- LIBCFATHREAD |
---|
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 | #define __cforall_thread__ |
---|
18 | |
---|
19 | #include "locks.hfa" |
---|
20 | #include "kernel/private.hfa" |
---|
21 | |
---|
22 | #include <kernel.hfa> |
---|
23 | #include <stdlib.hfa> |
---|
24 | |
---|
25 | #pragma GCC visibility push(default) |
---|
26 | |
---|
27 | //----------------------------------------------------------------------------- |
---|
28 | // info_thread |
---|
29 | forall(L & | is_blocking_lock(L)) { |
---|
30 | struct info_thread { |
---|
31 | // used to put info_thread on a dl queue |
---|
32 | inline dlink(info_thread(L)); |
---|
33 | |
---|
34 | // waiting thread |
---|
35 | struct thread$ * t; |
---|
36 | |
---|
37 | // shadow field |
---|
38 | uintptr_t info; |
---|
39 | |
---|
40 | // lock that is passed to wait() (if one is passed) |
---|
41 | L * lock; |
---|
42 | |
---|
43 | // true when signalled and false when timeout wakes thread |
---|
44 | bool signalled; |
---|
45 | }; |
---|
46 | P9_EMBEDDED( info_thread(L), dlink(info_thread(L)) ) |
---|
47 | |
---|
48 | void ?{}( info_thread(L) & this, thread$ * t, uintptr_t info, L * l ) { |
---|
49 | this.t = t; |
---|
50 | this.info = info; |
---|
51 | this.lock = l; |
---|
52 | } |
---|
53 | |
---|
54 | void ^?{}( info_thread(L) & this ) {} |
---|
55 | } |
---|
56 | |
---|
57 | //----------------------------------------------------------------------------- |
---|
58 | // Blocking Locks |
---|
59 | void ?{}( blocking_lock & this, bool multi_acquisition, bool strict_owner ) { |
---|
60 | this.lock{}; |
---|
61 | this.blocked_threads{}; |
---|
62 | this.wait_count = 0; |
---|
63 | this.multi_acquisition = multi_acquisition; |
---|
64 | this.strict_owner = strict_owner; |
---|
65 | this.owner = 0p; |
---|
66 | this.recursion_count = 0; |
---|
67 | } |
---|
68 | |
---|
69 | void ^?{}( blocking_lock & this ) {} |
---|
70 | |
---|
71 | |
---|
72 | void lock( blocking_lock & this ) with( this ) { |
---|
73 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
74 | thread$ * thrd = active_thread(); |
---|
75 | |
---|
76 | // single acquisition lock is held by current thread |
---|
77 | /* paranoid */ verifyf( owner != thrd || multi_acquisition, "Single acquisition lock holder (%p) attempted to reacquire the lock %p resulting in a deadlock.", owner, &this ); |
---|
78 | |
---|
79 | // lock is held by some other thread |
---|
80 | if ( owner != 0p && owner != thrd ) { |
---|
81 | select_node node; |
---|
82 | insert_last( blocked_threads, node ); |
---|
83 | wait_count++; |
---|
84 | unlock( lock ); |
---|
85 | park( ); |
---|
86 | return; |
---|
87 | } else if ( owner == thrd && multi_acquisition ) { // multi acquisition lock is held by current thread |
---|
88 | recursion_count++; |
---|
89 | } else { // lock isn't held |
---|
90 | owner = thrd; |
---|
91 | recursion_count = 1; |
---|
92 | } |
---|
93 | unlock( lock ); |
---|
94 | } |
---|
95 | |
---|
96 | bool try_lock( blocking_lock & this ) with( this ) { |
---|
97 | bool ret = false; |
---|
98 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
99 | |
---|
100 | // lock isn't held |
---|
101 | if ( owner == 0p ) { |
---|
102 | owner = active_thread(); |
---|
103 | recursion_count = 1; |
---|
104 | ret = true; |
---|
105 | } |
---|
106 | // multi acquisition lock is held by current thread |
---|
107 | else if ( owner == active_thread() && multi_acquisition ) { |
---|
108 | recursion_count++; |
---|
109 | ret = true; |
---|
110 | } |
---|
111 | |
---|
112 | unlock( lock ); |
---|
113 | return ret; |
---|
114 | } |
---|
115 | |
---|
116 | // static void pop_and_set_new_owner( blocking_lock & this ) with( this ) { |
---|
117 | // thread$ * t = &try_pop_front( blocked_threads ); |
---|
118 | // owner = t; |
---|
119 | // recursion_count = ( t ? 1 : 0 ); |
---|
120 | // if ( t ) wait_count--; |
---|
121 | // unpark( t ); |
---|
122 | // } |
---|
123 | |
---|
124 | static inline void pop_node( blocking_lock & this ) with( this ) { |
---|
125 | __handle_waituntil_OR( blocked_threads ); |
---|
126 | select_node * node = &try_pop_front( blocked_threads ); |
---|
127 | if ( node ) { |
---|
128 | wait_count--; |
---|
129 | owner = node->blocked_thread; |
---|
130 | recursion_count = 1; |
---|
131 | // if ( !node->clause_status || __make_select_node_available( *node ) ) unpark( node->blocked_thread ); |
---|
132 | wake_one( blocked_threads, *node ); |
---|
133 | } else { |
---|
134 | owner = 0p; |
---|
135 | recursion_count = 0; |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | void unlock( blocking_lock & this ) with( this ) { |
---|
140 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
141 | /* paranoid */ verifyf( owner != 0p, "Attempt to release lock %p that isn't held", &this ); |
---|
142 | /* paranoid */ verifyf( owner == active_thread() || !strict_owner , "Thread %p other than the owner %p attempted to release owner lock %p", owner, active_thread(), &this ); |
---|
143 | /* paranoid */ verifyf( recursion_count == 1 || multi_acquisition, "Thread %p attempted to release owner lock %p which is not recursive but has a recursive count of %zu", active_thread(), &this, recursion_count ); |
---|
144 | |
---|
145 | // if recursion count is zero release lock and set new owner if one is waiting |
---|
146 | recursion_count--; |
---|
147 | if ( recursion_count == 0 ) { |
---|
148 | pop_node( this ); |
---|
149 | } |
---|
150 | unlock( lock ); |
---|
151 | } |
---|
152 | |
---|
153 | size_t wait_count( blocking_lock & this ) with( this ) { |
---|
154 | return wait_count; |
---|
155 | } |
---|
156 | |
---|
157 | void on_notify( blocking_lock & this, thread$ * t ) with( this ) { |
---|
158 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
159 | // lock held |
---|
160 | if ( owner != 0p ) { |
---|
161 | insert_last( blocked_threads, *(select_node *)t->link_node ); |
---|
162 | wait_count++; |
---|
163 | } |
---|
164 | // lock not held |
---|
165 | else { |
---|
166 | owner = t; |
---|
167 | recursion_count = 1; |
---|
168 | unpark( t ); |
---|
169 | } |
---|
170 | unlock( lock ); |
---|
171 | } |
---|
172 | |
---|
173 | size_t on_wait( blocking_lock & this ) with( this ) { |
---|
174 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
175 | /* paranoid */ verifyf( owner != 0p, "Attempt to release lock %p that isn't held", &this ); |
---|
176 | /* paranoid */ verifyf( owner == active_thread() || !strict_owner, "Thread %p other than the owner %p attempted to release owner lock %p", owner, active_thread(), &this ); |
---|
177 | |
---|
178 | size_t ret = recursion_count; |
---|
179 | |
---|
180 | pop_node( this ); |
---|
181 | |
---|
182 | select_node node; |
---|
183 | active_thread()->link_node = (void *)&node; |
---|
184 | unlock( lock ); |
---|
185 | |
---|
186 | park(); |
---|
187 | |
---|
188 | return ret; |
---|
189 | } |
---|
190 | |
---|
191 | void on_wakeup( blocking_lock & this, size_t recursion ) with( this ) { |
---|
192 | recursion_count = recursion; |
---|
193 | } |
---|
194 | |
---|
195 | // waituntil() support |
---|
196 | bool register_select( blocking_lock & this, select_node & node ) with(this) { |
---|
197 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
198 | thread$ * thrd = active_thread(); |
---|
199 | |
---|
200 | // single acquisition lock is held by current thread |
---|
201 | /* paranoid */ verifyf( owner != thrd || multi_acquisition, "Single acquisition lock holder (%p) attempted to reacquire the lock %p resulting in a deadlock.", owner, &this ); |
---|
202 | |
---|
203 | if ( !node.park_counter && ( (owner == thrd && multi_acquisition) || owner == 0p ) ) { // OR special case |
---|
204 | if ( !__make_select_node_available( node ) ) { // we didn't win the race so give up on registering |
---|
205 | unlock( lock ); |
---|
206 | return false; |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | // lock is held by some other thread |
---|
211 | if ( owner != 0p && owner != thrd ) { |
---|
212 | insert_last( blocked_threads, node ); |
---|
213 | wait_count++; |
---|
214 | unlock( lock ); |
---|
215 | return false; |
---|
216 | } else if ( owner == thrd && multi_acquisition ) { // multi acquisition lock is held by current thread |
---|
217 | recursion_count++; |
---|
218 | } else { // lock isn't held |
---|
219 | owner = thrd; |
---|
220 | recursion_count = 1; |
---|
221 | } |
---|
222 | |
---|
223 | if ( node.park_counter ) __make_select_node_available( node ); |
---|
224 | unlock( lock ); |
---|
225 | return true; |
---|
226 | } |
---|
227 | |
---|
228 | bool unregister_select( blocking_lock & this, select_node & node ) with(this) { |
---|
229 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
230 | if ( node`isListed ) { |
---|
231 | remove( node ); |
---|
232 | wait_count--; |
---|
233 | unlock( lock ); |
---|
234 | return false; |
---|
235 | } |
---|
236 | |
---|
237 | if ( owner == active_thread() ) { |
---|
238 | /* paranoid */ verifyf( recursion_count == 1 || multi_acquisition, "Thread %p attempted to unlock owner lock %p in waituntil unregister, which is not recursive but has a recursive count of %zu", active_thread(), &this, recursion_count ); |
---|
239 | // if recursion count is zero release lock and set new owner if one is waiting |
---|
240 | recursion_count--; |
---|
241 | if ( recursion_count == 0 ) { |
---|
242 | pop_node( this ); |
---|
243 | } |
---|
244 | } |
---|
245 | unlock( lock ); |
---|
246 | return false; |
---|
247 | } |
---|
248 | |
---|
249 | bool on_selected( blocking_lock & this, select_node & node ) { return true; } |
---|
250 | |
---|
251 | //----------------------------------------------------------------------------- |
---|
252 | // alarm node wrapper |
---|
253 | forall(L & | is_blocking_lock(L)) { |
---|
254 | struct alarm_node_wrap { |
---|
255 | alarm_node_t alarm_node; |
---|
256 | condition_variable(L) * cond; |
---|
257 | info_thread(L) * info_thd; |
---|
258 | }; |
---|
259 | |
---|
260 | void ?{}( alarm_node_wrap(L) & this, Duration alarm, Duration period, Alarm_Callback callback, condition_variable(L) * c, info_thread(L) * i ) { |
---|
261 | this.alarm_node{ callback, alarm, period }; |
---|
262 | this.cond = c; |
---|
263 | this.info_thd = i; |
---|
264 | } |
---|
265 | |
---|
266 | void ^?{}( alarm_node_wrap(L) & this ) { } |
---|
267 | |
---|
268 | static void timeout_handler ( alarm_node_wrap(L) & this ) with( this ) { |
---|
269 | // This condition_variable member is called from the kernel, and therefore, cannot block, but it can spin. |
---|
270 | lock( cond->lock __cfaabi_dbg_ctx2 ); |
---|
271 | |
---|
272 | // this check is necessary to avoid a race condition since this timeout handler |
---|
273 | // may still be called after a thread has been removed from the queue but |
---|
274 | // before the alarm is unregistered |
---|
275 | if ( (*info_thd)`isListed ) { // is thread on queue |
---|
276 | info_thd->signalled = false; |
---|
277 | // remove this thread O(1) |
---|
278 | remove( *info_thd ); |
---|
279 | cond->count--; |
---|
280 | if( info_thd->lock ) { |
---|
281 | // call lock's on_notify if a lock was passed |
---|
282 | on_notify(*info_thd->lock, info_thd->t); |
---|
283 | } else { |
---|
284 | // otherwise wake thread |
---|
285 | unpark( info_thd->t ); |
---|
286 | } |
---|
287 | } |
---|
288 | unlock( cond->lock ); |
---|
289 | } |
---|
290 | |
---|
291 | // this casts the alarm node to our wrapped type since we used type erasure |
---|
292 | static void alarm_node_wrap_cast( alarm_node_t & a ) { timeout_handler( (alarm_node_wrap(L) &)a ); } |
---|
293 | |
---|
294 | struct pthread_alarm_node_wrap { |
---|
295 | alarm_node_t alarm_node; |
---|
296 | pthread_cond_var(L) * cond; |
---|
297 | info_thread(L) * info_thd; |
---|
298 | }; |
---|
299 | |
---|
300 | void ?{}( pthread_alarm_node_wrap(L) & this, Duration alarm, Duration period, Alarm_Callback callback, pthread_cond_var(L) * c, info_thread(L) * i ) { |
---|
301 | this.alarm_node{ callback, alarm, period }; |
---|
302 | this.cond = c; |
---|
303 | this.info_thd = i; |
---|
304 | } |
---|
305 | |
---|
306 | void ^?{}( pthread_alarm_node_wrap(L) & this ) { } |
---|
307 | |
---|
308 | static void timeout_handler ( pthread_alarm_node_wrap(L) & this ) with( this ) { |
---|
309 | // This pthread_cond_var member is called from the kernel, and therefore, cannot block, but it can spin. |
---|
310 | lock( cond->lock __cfaabi_dbg_ctx2 ); |
---|
311 | // this check is necessary to avoid a race condition since this timeout handler |
---|
312 | // may still be called after a thread has been removed from the queue but |
---|
313 | // before the alarm is unregistered |
---|
314 | if ( (*info_thd)`isListed ) { // is thread on queue |
---|
315 | info_thd->signalled = false; |
---|
316 | // remove this thread O(1) |
---|
317 | remove( *info_thd ); |
---|
318 | on_notify(*info_thd->lock, info_thd->t); |
---|
319 | } |
---|
320 | unlock( cond->lock ); |
---|
321 | } |
---|
322 | |
---|
323 | // this casts the alarm node to our wrapped type since we used type erasure |
---|
324 | static void pthread_alarm_node_wrap_cast( alarm_node_t & a ) { timeout_handler( (pthread_alarm_node_wrap(L) &)a ); } |
---|
325 | } |
---|
326 | |
---|
327 | //----------------------------------------------------------------------------- |
---|
328 | // Synchronization Locks |
---|
329 | forall(L & | is_blocking_lock(L)) { |
---|
330 | |
---|
331 | //----------------------------------------------------------------------------- |
---|
332 | // condition variable |
---|
333 | void ?{}( condition_variable(L) & this ){ |
---|
334 | this.lock{}; |
---|
335 | this.blocked_threads{}; |
---|
336 | this.count = 0; |
---|
337 | } |
---|
338 | |
---|
339 | void ^?{}( condition_variable(L) & this ){ } |
---|
340 | |
---|
341 | static void process_popped( condition_variable(L) & this, info_thread(L) & popped ) with( this ) { |
---|
342 | if(&popped != 0p) { |
---|
343 | popped.signalled = true; |
---|
344 | count--; |
---|
345 | if (popped.lock) { |
---|
346 | // if lock passed call on_notify |
---|
347 | on_notify(*popped.lock, popped.t); |
---|
348 | } else { |
---|
349 | // otherwise wake thread |
---|
350 | unpark(popped.t); |
---|
351 | } |
---|
352 | } |
---|
353 | } |
---|
354 | |
---|
355 | bool notify_one( condition_variable(L) & this ) with( this ) { |
---|
356 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
357 | bool ret = ! blocked_threads`isEmpty; |
---|
358 | process_popped(this, try_pop_front( blocked_threads )); |
---|
359 | unlock( lock ); |
---|
360 | return ret; |
---|
361 | } |
---|
362 | |
---|
363 | bool notify_all( condition_variable(L) & this ) with(this) { |
---|
364 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
365 | bool ret = ! blocked_threads`isEmpty; |
---|
366 | while( ! blocked_threads`isEmpty ) { |
---|
367 | process_popped(this, try_pop_front( blocked_threads )); |
---|
368 | } |
---|
369 | unlock( lock ); |
---|
370 | return ret; |
---|
371 | } |
---|
372 | |
---|
373 | uintptr_t front( condition_variable(L) & this ) with(this) { |
---|
374 | return blocked_threads`isEmpty ? NULL : blocked_threads`first.info; |
---|
375 | } |
---|
376 | |
---|
377 | bool empty( condition_variable(L) & this ) with(this) { |
---|
378 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
379 | bool ret = blocked_threads`isEmpty; |
---|
380 | unlock( lock ); |
---|
381 | return ret; |
---|
382 | } |
---|
383 | |
---|
384 | int counter( condition_variable(L) & this ) with(this) { return count; } |
---|
385 | |
---|
386 | static void enqueue_thread( condition_variable(L) & this, info_thread(L) * i ) with(this) { |
---|
387 | // add info_thread to waiting queue |
---|
388 | insert_last( blocked_threads, *i ); |
---|
389 | count++; |
---|
390 | // size_t recursion_count = 0; |
---|
391 | // if (i->lock) { |
---|
392 | // // if lock was passed get recursion count to reset to after waking thread |
---|
393 | // recursion_count = on_wait( *i->lock ); |
---|
394 | // } |
---|
395 | // return recursion_count; |
---|
396 | } |
---|
397 | |
---|
398 | static size_t block_and_get_recursion( info_thread(L) & i ) { |
---|
399 | size_t recursion_count = 0; |
---|
400 | if ( i.lock ) { |
---|
401 | // if lock was passed get recursion count to reset to after waking thread |
---|
402 | recursion_count = on_wait( *i.lock ); // this call blocks |
---|
403 | } else park( ); |
---|
404 | return recursion_count; |
---|
405 | } |
---|
406 | |
---|
407 | // helper for wait()'s' with no timeout |
---|
408 | static void queue_info_thread( condition_variable(L) & this, info_thread(L) & i ) with(this) { |
---|
409 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
410 | enqueue_thread( this, &i ); |
---|
411 | // size_t recursion_count = queue_and_get_recursion( this, &i ); |
---|
412 | unlock( lock ); |
---|
413 | |
---|
414 | // blocks here |
---|
415 | size_t recursion_count = block_and_get_recursion( i ); |
---|
416 | // park( ); |
---|
417 | |
---|
418 | // resets recursion count here after waking |
---|
419 | if ( i.lock ) on_wakeup( *i.lock, recursion_count ); |
---|
420 | } |
---|
421 | |
---|
422 | #define WAIT( u, l ) \ |
---|
423 | info_thread( L ) i = { active_thread(), u, l }; \ |
---|
424 | queue_info_thread( this, i ); |
---|
425 | |
---|
426 | // helper for wait()'s' with a timeout |
---|
427 | static void queue_info_thread_timeout( condition_variable(L) & this, info_thread(L) & info, Duration t, Alarm_Callback callback ) with(this) { |
---|
428 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
429 | enqueue_thread( this, &info ); |
---|
430 | // size_t recursion_count = queue_and_get_recursion( this, &info ); |
---|
431 | alarm_node_wrap(L) node_wrap = { t, 0`s, callback, &this, &info }; |
---|
432 | unlock( lock ); |
---|
433 | |
---|
434 | // registers alarm outside cond lock to avoid deadlock |
---|
435 | register_self( &node_wrap.alarm_node ); |
---|
436 | |
---|
437 | // blocks here |
---|
438 | size_t recursion_count = block_and_get_recursion( info ); |
---|
439 | // park(); |
---|
440 | |
---|
441 | // unregisters alarm so it doesn't go off if this happens first |
---|
442 | unregister_self( &node_wrap.alarm_node ); |
---|
443 | |
---|
444 | // resets recursion count here after waking |
---|
445 | if ( info.lock ) on_wakeup( *info.lock, recursion_count ); |
---|
446 | } |
---|
447 | |
---|
448 | #define WAIT_TIME( u, l, t ) \ |
---|
449 | info_thread( L ) i = { active_thread(), u, l }; \ |
---|
450 | queue_info_thread_timeout(this, i, t, alarm_node_wrap_cast ); \ |
---|
451 | return i.signalled; |
---|
452 | |
---|
453 | void wait( condition_variable(L) & this ) with(this) { WAIT( 0, 0p ) } |
---|
454 | void wait( condition_variable(L) & this, uintptr_t info ) with(this) { WAIT( info, 0p ) } |
---|
455 | void wait( condition_variable(L) & this, L & l ) with(this) { WAIT( 0, &l ) } |
---|
456 | void wait( condition_variable(L) & this, L & l, uintptr_t info ) with(this) { WAIT( info, &l ) } |
---|
457 | |
---|
458 | bool wait( condition_variable(L) & this, Duration duration ) with(this) { WAIT_TIME( 0 , 0p , duration ) } |
---|
459 | bool wait( condition_variable(L) & this, uintptr_t info, Duration duration ) with(this) { WAIT_TIME( info, 0p , duration ) } |
---|
460 | bool wait( condition_variable(L) & this, L & l, Duration duration ) with(this) { WAIT_TIME( 0 , &l , duration ) } |
---|
461 | bool wait( condition_variable(L) & this, L & l, uintptr_t info, Duration duration ) with(this) { WAIT_TIME( info, &l , duration ) } |
---|
462 | |
---|
463 | //----------------------------------------------------------------------------- |
---|
464 | // fast_cond_var |
---|
465 | void ?{}( fast_cond_var(L) & this ){ |
---|
466 | this.blocked_threads{}; |
---|
467 | #ifdef __CFA_DEBUG__ |
---|
468 | this.lock_used = 0p; |
---|
469 | #endif |
---|
470 | } |
---|
471 | void ^?{}( fast_cond_var(L) & this ){ } |
---|
472 | |
---|
473 | bool notify_one( fast_cond_var(L) & this ) with(this) { |
---|
474 | bool ret = ! blocked_threads`isEmpty; |
---|
475 | if ( ret ) { |
---|
476 | info_thread(L) & popped = try_pop_front( blocked_threads ); |
---|
477 | on_notify(*popped.lock, popped.t); |
---|
478 | } |
---|
479 | return ret; |
---|
480 | } |
---|
481 | bool notify_all( fast_cond_var(L) & this ) with(this) { |
---|
482 | bool ret = ! blocked_threads`isEmpty; |
---|
483 | while( ! blocked_threads`isEmpty ) { |
---|
484 | info_thread(L) & popped = try_pop_front( blocked_threads ); |
---|
485 | on_notify(*popped.lock, popped.t); |
---|
486 | } |
---|
487 | return ret; |
---|
488 | } |
---|
489 | |
---|
490 | uintptr_t front( fast_cond_var(L) & this ) with(this) { return blocked_threads`isEmpty ? NULL : blocked_threads`first.info; } |
---|
491 | bool empty ( fast_cond_var(L) & this ) with(this) { return blocked_threads`isEmpty; } |
---|
492 | |
---|
493 | void wait( fast_cond_var(L) & this, L & l ) { |
---|
494 | wait( this, l, 0 ); |
---|
495 | } |
---|
496 | |
---|
497 | void wait( fast_cond_var(L) & this, L & l, uintptr_t info ) with(this) { |
---|
498 | // brand cond lock with lock |
---|
499 | #ifdef __CFA_DEBUG__ |
---|
500 | if ( lock_used == 0p ) lock_used = &l; |
---|
501 | else assert(lock_used == &l); |
---|
502 | #endif |
---|
503 | info_thread( L ) i = { active_thread(), info, &l }; |
---|
504 | insert_last( blocked_threads, i ); |
---|
505 | size_t recursion_count = on_wait( *i.lock ); // blocks here |
---|
506 | // park( ); |
---|
507 | on_wakeup(*i.lock, recursion_count); |
---|
508 | } |
---|
509 | |
---|
510 | //----------------------------------------------------------------------------- |
---|
511 | // pthread_cond_var |
---|
512 | |
---|
513 | void ?{}( pthread_cond_var(L) & this ) with(this) { |
---|
514 | blocked_threads{}; |
---|
515 | lock{}; |
---|
516 | } |
---|
517 | |
---|
518 | void ^?{}( pthread_cond_var(L) & this ) { } |
---|
519 | |
---|
520 | bool notify_one( pthread_cond_var(L) & this ) with(this) { |
---|
521 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
522 | bool ret = ! blocked_threads`isEmpty; |
---|
523 | if ( ret ) { |
---|
524 | info_thread(L) & popped = try_pop_front( blocked_threads ); |
---|
525 | popped.signalled = true; |
---|
526 | on_notify(*popped.lock, popped.t); |
---|
527 | } |
---|
528 | unlock( lock ); |
---|
529 | return ret; |
---|
530 | } |
---|
531 | |
---|
532 | bool notify_all( pthread_cond_var(L) & this ) with(this) { |
---|
533 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
534 | bool ret = ! blocked_threads`isEmpty; |
---|
535 | while( ! blocked_threads`isEmpty ) { |
---|
536 | info_thread(L) & popped = try_pop_front( blocked_threads ); |
---|
537 | popped.signalled = true; |
---|
538 | on_notify(*popped.lock, popped.t); |
---|
539 | } |
---|
540 | unlock( lock ); |
---|
541 | return ret; |
---|
542 | } |
---|
543 | |
---|
544 | uintptr_t front( pthread_cond_var(L) & this ) with(this) { return blocked_threads`isEmpty ? NULL : blocked_threads`first.info; } |
---|
545 | bool empty ( pthread_cond_var(L) & this ) with(this) { return blocked_threads`isEmpty; } |
---|
546 | |
---|
547 | // static size_t queue_and_get_recursion( pthread_cond_var(L) & this, info_thread(L) * i ) with(this) { |
---|
548 | // // add info_thread to waiting queue |
---|
549 | // insert_last( blocked_threads, *i ); |
---|
550 | // size_t recursion_count = 0; |
---|
551 | // recursion_count = on_wait( *i->lock ); |
---|
552 | // return recursion_count; |
---|
553 | // } |
---|
554 | |
---|
555 | |
---|
556 | static void queue_info_thread_timeout( pthread_cond_var(L) & this, info_thread(L) & info, Duration t, Alarm_Callback callback ) with(this) { |
---|
557 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
558 | // size_t recursion_count = queue_and_get_recursion(this, &info); |
---|
559 | insert_last( blocked_threads, info ); |
---|
560 | pthread_alarm_node_wrap(L) node_wrap = { t, 0`s, callback, &this, &info }; |
---|
561 | unlock( lock ); |
---|
562 | |
---|
563 | // registers alarm outside cond lock to avoid deadlock |
---|
564 | register_self( &node_wrap.alarm_node ); |
---|
565 | |
---|
566 | // blocks here |
---|
567 | size_t recursion_count = block_and_get_recursion( info ); |
---|
568 | // park(); |
---|
569 | |
---|
570 | // unregisters alarm so it doesn't go off if this happens first |
---|
571 | unregister_self( &node_wrap.alarm_node ); |
---|
572 | |
---|
573 | // resets recursion count here after waking |
---|
574 | if ( info.lock ) on_wakeup( *info.lock, recursion_count ); |
---|
575 | } |
---|
576 | |
---|
577 | void wait( pthread_cond_var(L) & this, L & l ) with(this) { |
---|
578 | wait( this, l, 0 ); |
---|
579 | } |
---|
580 | |
---|
581 | void wait( pthread_cond_var(L) & this, L & l, uintptr_t info ) with(this) { |
---|
582 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
583 | info_thread( L ) i = { active_thread(), info, &l }; |
---|
584 | insert_last( blocked_threads, i ); |
---|
585 | // size_t recursion_count = queue_and_get_recursion( this, &i ); |
---|
586 | unlock( lock ); |
---|
587 | |
---|
588 | // blocks here |
---|
589 | size_t recursion_count = block_and_get_recursion( i ); |
---|
590 | // park(); |
---|
591 | on_wakeup( *i.lock, recursion_count ); |
---|
592 | } |
---|
593 | |
---|
594 | #define PTHREAD_WAIT_TIME( u, l, t ) \ |
---|
595 | info_thread( L ) i = { active_thread(), u, l }; \ |
---|
596 | queue_info_thread_timeout(this, i, t, pthread_alarm_node_wrap_cast ); \ |
---|
597 | return i.signalled; |
---|
598 | |
---|
599 | Duration getDuration(timespec t) { |
---|
600 | timespec currTime; |
---|
601 | clock_gettime(CLOCK_REALTIME, &currTime); |
---|
602 | Duration waitUntil = { t }; |
---|
603 | Duration currDur = { currTime }; |
---|
604 | if ( currDur >= waitUntil ) return currDur - waitUntil; |
---|
605 | Duration zero = { 0 }; |
---|
606 | return zero; |
---|
607 | } |
---|
608 | |
---|
609 | bool wait( pthread_cond_var(L) & this, L & l, timespec t ) { |
---|
610 | PTHREAD_WAIT_TIME( 0, &l , getDuration( t ) ) |
---|
611 | } |
---|
612 | |
---|
613 | bool wait( pthread_cond_var(L) & this, L & l, uintptr_t info, timespec t ) { |
---|
614 | PTHREAD_WAIT_TIME( info, &l , getDuration( t ) ) |
---|
615 | } |
---|
616 | } |
---|
617 | //----------------------------------------------------------------------------- |
---|
618 | // Semaphore |
---|
619 | void ?{}( semaphore & this, int count = 1 ) { |
---|
620 | (this.lock){}; |
---|
621 | this.count = count; |
---|
622 | (this.waiting){}; |
---|
623 | } |
---|
624 | void ^?{}(semaphore & this) {} |
---|
625 | |
---|
626 | bool P(semaphore & this) with( this ){ |
---|
627 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
628 | count -= 1; |
---|
629 | if ( count < 0 ) { |
---|
630 | // queue current task |
---|
631 | append( waiting, active_thread() ); |
---|
632 | |
---|
633 | // atomically release spin lock and block |
---|
634 | unlock( lock ); |
---|
635 | park(); |
---|
636 | return true; |
---|
637 | } |
---|
638 | else { |
---|
639 | unlock( lock ); |
---|
640 | return false; |
---|
641 | } |
---|
642 | } |
---|
643 | |
---|
644 | thread$ * V (semaphore & this, const bool doUnpark ) with( this ) { |
---|
645 | thread$ * thrd = 0p; |
---|
646 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
647 | count += 1; |
---|
648 | if ( count <= 0 ) { |
---|
649 | // remove task at head of waiting list |
---|
650 | thrd = pop_head( waiting ); |
---|
651 | } |
---|
652 | |
---|
653 | unlock( lock ); |
---|
654 | |
---|
655 | // make new owner |
---|
656 | if( doUnpark ) unpark( thrd ); |
---|
657 | |
---|
658 | return thrd; |
---|
659 | } |
---|
660 | |
---|
661 | bool V(semaphore & this) with( this ) { |
---|
662 | thread$ * thrd = V(this, true); |
---|
663 | return thrd != 0p; |
---|
664 | } |
---|
665 | |
---|
666 | bool V(semaphore & this, unsigned diff) with( this ) { |
---|
667 | thread$ * thrd = 0p; |
---|
668 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
669 | int release = max(-count, (int)diff); |
---|
670 | count += diff; |
---|
671 | for(release) { |
---|
672 | unpark( pop_head( waiting ) ); |
---|
673 | } |
---|
674 | |
---|
675 | unlock( lock ); |
---|
676 | |
---|
677 | return thrd != 0p; |
---|
678 | } |
---|