source: libcfa/src/concurrency/locks.cfa@ 305aaef

ADT ast-experimental pthread-emulation qualifiedEnum
Last change on this file since 305aaef was 305aaef, checked in by caparsons <caparson@…>, 3 years ago

fixed multiple def issue

  • Property mode set to 100644
File size: 14.8 KB
Line 
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#define _GNU_SOURCE
19
20#include "locks.hfa"
21#include "kernel/private.hfa"
22
23#include <kernel.hfa>
24#include <stdlib.hfa>
25
26//-----------------------------------------------------------------------------
27// info_thread
28forall(L & | is_blocking_lock(L)) {
29 struct info_thread {
30 // used to put info_thread on a dl queue
31 inline dlink(info_thread(L));
32
33 // waiting thread
34 struct thread$ * t;
35
36 // shadow field
37 uintptr_t info;
38
39 // lock that is passed to wait() (if one is passed)
40 L * lock;
41
42 // true when signalled and false when timeout wakes thread
43 bool signalled;
44 };
45 P9_EMBEDDED( info_thread(L), dlink(info_thread(L)) )
46
47 void ?{}( info_thread(L) & this, thread$ * t, uintptr_t info, L * l ) {
48 this.t = t;
49 this.info = info;
50 this.lock = l;
51 }
52
53 void ^?{}( info_thread(L) & this ) {}
54}
55
56//-----------------------------------------------------------------------------
57// Blocking Locks
58void ?{}( blocking_lock & this, bool multi_acquisition, bool strict_owner ) {
59 this.lock{};
60 this.blocked_threads{};
61 this.wait_count = 0;
62 this.multi_acquisition = multi_acquisition;
63 this.strict_owner = strict_owner;
64 this.owner = 0p;
65 this.recursion_count = 0;
66}
67
68void ^?{}( blocking_lock & this ) {}
69
70
71void lock( blocking_lock & this ) with( this ) {
72 lock( lock __cfaabi_dbg_ctx2 );
73 thread$ * thrd = active_thread();
74
75 // single acquisition lock is held by current thread
76 /* paranoid */ verifyf( owner != thrd || multi_acquisition, "Single acquisition lock holder (%p) attempted to reacquire the lock %p resulting in a deadlock.", owner, &this );
77
78 // lock is held by some other thread
79 if ( owner != 0p && owner != thrd ) {
80 insert_last( blocked_threads, *thrd );
81 wait_count++;
82 unlock( lock );
83 park( );
84 }
85 // multi acquisition lock is held by current thread
86 else if ( owner == thrd && multi_acquisition ) {
87 recursion_count++;
88 unlock( lock );
89 }
90 // lock isn't held
91 else {
92 owner = thrd;
93 recursion_count = 1;
94 unlock( lock );
95 }
96}
97
98bool try_lock( blocking_lock & this ) with( this ) {
99 bool ret = false;
100 lock( lock __cfaabi_dbg_ctx2 );
101
102 // lock isn't held
103 if ( owner == 0p ) {
104 owner = active_thread();
105 recursion_count = 1;
106 ret = true;
107 }
108 // multi acquisition lock is held by current thread
109 else if ( owner == active_thread() && multi_acquisition ) {
110 recursion_count++;
111 ret = true;
112 }
113
114 unlock( lock );
115 return ret;
116}
117
118void pop_and_set_new_owner( blocking_lock & this ) with( this ) {
119 thread$ * t = &try_pop_front( blocked_threads );
120 owner = t;
121 recursion_count = ( t ? 1 : 0 );
122 if ( t ) wait_count--;
123 unpark( t );
124}
125
126void unlock( blocking_lock & this ) with( this ) {
127 lock( lock __cfaabi_dbg_ctx2 );
128 /* paranoid */ verifyf( owner != 0p, "Attempt to release lock %p that isn't held", &this );
129 /* paranoid */ verifyf( owner == active_thread() || !strict_owner , "Thread %p other than the owner %p attempted to release owner lock %p", owner, active_thread(), &this );
130 /* 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 );
131
132 // if recursion count is zero release lock and set new owner if one is waiting
133 recursion_count--;
134 if ( recursion_count == 0 ) {
135 pop_and_set_new_owner( this );
136 }
137 unlock( lock );
138}
139
140size_t wait_count( blocking_lock & this ) with( this ) {
141 return wait_count;
142}
143
144void on_notify( blocking_lock & this, thread$ * t ) with( this ) {
145 lock( lock __cfaabi_dbg_ctx2 );
146 // lock held
147 if ( owner != 0p ) {
148 insert_last( blocked_threads, *t );
149 wait_count++;
150 unlock( lock );
151 }
152 // lock not held
153 else {
154 owner = t;
155 recursion_count = 1;
156 unpark( t );
157 unlock( lock );
158 }
159}
160
161size_t on_wait( blocking_lock & this ) with( this ) {
162 lock( lock __cfaabi_dbg_ctx2 );
163 /* paranoid */ verifyf( owner != 0p, "Attempt to release lock %p that isn't held", &this );
164 /* paranoid */ verifyf( owner == active_thread() || !strict_owner, "Thread %p other than the owner %p attempted to release owner lock %p", owner, active_thread(), &this );
165
166 size_t ret = recursion_count;
167
168 pop_and_set_new_owner( this );
169 unlock( lock );
170 return ret;
171}
172
173void on_wakeup( blocking_lock & this, size_t recursion ) with( this ) {
174 recursion_count = recursion;
175}
176
177//-----------------------------------------------------------------------------
178// simple_owner_lock
179
180static inline void lock(simple_owner_lock & this) with(this) {
181 if (owner == active_thread()) {
182 recursion_count++;
183 return;
184 }
185 lock( lock __cfaabi_dbg_ctx2 );
186
187 if (owner != 0p) {
188 insert_last( blocked_threads, *active_thread() );
189 unlock( lock );
190 park( );
191 return;
192 }
193 owner = active_thread();
194 recursion_count = 1;
195 unlock( lock );
196}
197
198void pop_and_set_new_owner( simple_owner_lock & this ) with( this ) {
199 thread$ * t = &try_pop_front( blocked_threads );
200 owner = t;
201 recursion_count = ( t ? 1 : 0 );
202 unpark( t );
203}
204
205static inline void unlock(simple_owner_lock & this) with(this) {
206 lock( lock __cfaabi_dbg_ctx2 );
207 /* paranoid */ verifyf( owner != 0p, "Attempt to release lock %p that isn't held", &this );
208 /* paranoid */ verifyf( owner == active_thread(), "Thread %p other than the owner %p attempted to release owner lock %p", owner, active_thread(), &this );
209 // if recursion count is zero release lock and set new owner if one is waiting
210 recursion_count--;
211 if ( recursion_count == 0 ) {
212 pop_and_set_new_owner( this );
213 }
214 unlock( lock );
215}
216
217static inline void on_notify(simple_owner_lock & this, struct thread$ * t ) with(this) {
218 lock( lock __cfaabi_dbg_ctx2 );
219 // lock held
220 if ( owner != 0p ) {
221 insert_last( blocked_threads, *t );
222 unlock( lock );
223 }
224 // lock not held
225 else {
226 owner = t;
227 recursion_count = 1;
228 unpark( t );
229 unlock( lock );
230 }
231}
232
233static inline size_t on_wait(simple_owner_lock & this) with(this) {
234 lock( lock __cfaabi_dbg_ctx2 );
235 /* paranoid */ verifyf( owner != 0p, "Attempt to release lock %p that isn't held", &this );
236 /* paranoid */ verifyf( owner == active_thread(), "Thread %p other than the owner %p attempted to release owner lock %p", owner, active_thread(), &this );
237
238 size_t ret = recursion_count;
239
240 pop_and_set_new_owner( this );
241
242 unlock( lock );
243 return ret;
244}
245
246static inline void on_wakeup(simple_owner_lock & this, size_t recursion ) with(this) { recursion_count = recursion; }
247
248
249//-----------------------------------------------------------------------------
250// alarm node wrapper
251forall(L & | is_blocking_lock(L)) {
252 struct alarm_node_wrap {
253 alarm_node_t alarm_node;
254 condition_variable(L) * cond;
255 info_thread(L) * info_thd;
256 };
257
258 void ?{}( alarm_node_wrap(L) & this, Duration alarm, Duration period, Alarm_Callback callback, condition_variable(L) * c, info_thread(L) * i ) {
259 this.alarm_node{ callback, alarm, period };
260 this.cond = c;
261 this.info_thd = i;
262 }
263
264 void ^?{}( alarm_node_wrap(L) & this ) { }
265
266 void timeout_handler ( alarm_node_wrap(L) & this ) with( this ) {
267 // This condition_variable member is called from the kernel, and therefore, cannot block, but it can spin.
268 lock( cond->lock __cfaabi_dbg_ctx2 );
269
270 // this check is necessary to avoid a race condition since this timeout handler
271 // may still be called after a thread has been removed from the queue but
272 // before the alarm is unregistered
273 if ( (*info_thd)`isListed ) { // is thread on queue
274 info_thd->signalled = false;
275 // remove this thread O(1)
276 remove( *info_thd );
277 cond->count--;
278 if( info_thd->lock ) {
279 // call lock's on_notify if a lock was passed
280 on_notify(*info_thd->lock, info_thd->t);
281 } else {
282 // otherwise wake thread
283 unpark( info_thd->t );
284 }
285 }
286 unlock( cond->lock );
287 }
288
289 // this casts the alarm node to our wrapped type since we used type erasure
290 void alarm_node_wrap_cast( alarm_node_t & a ) { timeout_handler( (alarm_node_wrap(L) &)a ); }
291}
292
293//-----------------------------------------------------------------------------
294// Synchronization Locks
295forall(L & | is_blocking_lock(L)) {
296
297 //-----------------------------------------------------------------------------
298 // condition variable
299 void ?{}( condition_variable(L) & this ){
300 this.lock{};
301 this.blocked_threads{};
302 this.count = 0;
303 }
304
305 void ^?{}( condition_variable(L) & this ){ }
306
307 void process_popped( condition_variable(L) & this, info_thread(L) & popped ) with( this ) {
308 if(&popped != 0p) {
309 popped.signalled = true;
310 count--;
311 if (popped.lock) {
312 // if lock passed call on_notify
313 on_notify(*popped.lock, popped.t);
314 } else {
315 // otherwise wake thread
316 unpark(popped.t);
317 }
318 }
319 }
320
321 bool notify_one( condition_variable(L) & this ) with( this ) {
322 lock( lock __cfaabi_dbg_ctx2 );
323 bool ret = ! blocked_threads`isEmpty;
324 process_popped(this, try_pop_front( blocked_threads ));
325 unlock( lock );
326 return ret;
327 }
328
329 bool notify_all( condition_variable(L) & this ) with(this) {
330 lock( lock __cfaabi_dbg_ctx2 );
331 bool ret = ! blocked_threads`isEmpty;
332 while( ! blocked_threads`isEmpty ) {
333 process_popped(this, try_pop_front( blocked_threads ));
334 }
335 unlock( lock );
336 return ret;
337 }
338
339 uintptr_t front( condition_variable(L) & this ) with(this) {
340 return blocked_threads`isEmpty ? NULL : blocked_threads`first.info;
341 }
342
343 bool empty( condition_variable(L) & this ) with(this) {
344 lock( lock __cfaabi_dbg_ctx2 );
345 bool ret = blocked_threads`isEmpty;
346 unlock( lock );
347 return ret;
348 }
349
350 int counter( condition_variable(L) & this ) with(this) { return count; }
351
352 size_t queue_and_get_recursion( condition_variable(L) & this, info_thread(L) * i ) with(this) {
353 // add info_thread to waiting queue
354 insert_last( blocked_threads, *i );
355 count++;
356 size_t recursion_count = 0;
357 if (i->lock) {
358 // if lock was passed get recursion count to reset to after waking thread
359 recursion_count = on_wait( *i->lock );
360 }
361 return recursion_count;
362 }
363
364 // helper for wait()'s' with no timeout
365 void queue_info_thread( condition_variable(L) & this, info_thread(L) & i ) with(this) {
366 lock( lock __cfaabi_dbg_ctx2 );
367 size_t recursion_count = queue_and_get_recursion(this, &i);
368 unlock( lock );
369
370 // blocks here
371 park( );
372
373 // resets recursion count here after waking
374 if (i.lock) on_wakeup(*i.lock, recursion_count);
375 }
376
377 #define WAIT( u, l ) \
378 info_thread( L ) i = { active_thread(), u, l }; \
379 queue_info_thread( this, i );
380
381 // helper for wait()'s' with a timeout
382 void queue_info_thread_timeout( condition_variable(L) & this, info_thread(L) & info, Duration t, Alarm_Callback callback ) with(this) {
383 lock( lock __cfaabi_dbg_ctx2 );
384 size_t recursion_count = queue_and_get_recursion(this, &info);
385 alarm_node_wrap(L) node_wrap = { t, 0`s, callback, &this, &info };
386 register_self( &node_wrap.alarm_node );
387 unlock( lock );
388
389 // blocks here
390 park();
391
392 // unregisters alarm so it doesn't go off if this happens first
393 unregister_self( &node_wrap.alarm_node );
394
395 // resets recursion count here after waking
396 if (info.lock) on_wakeup(*info.lock, recursion_count);
397 }
398
399 #define WAIT_TIME( u, l, t ) \
400 info_thread( L ) i = { active_thread(), u, l }; \
401 queue_info_thread_timeout(this, i, t, alarm_node_wrap_cast ); \
402 return i.signalled;
403
404 void wait( condition_variable(L) & this ) with(this) { WAIT( 0, 0p ) }
405 void wait( condition_variable(L) & this, uintptr_t info ) with(this) { WAIT( info, 0p ) }
406 void wait( condition_variable(L) & this, L & l ) with(this) { WAIT( 0, &l ) }
407 void wait( condition_variable(L) & this, L & l, uintptr_t info ) with(this) { WAIT( info, &l ) }
408
409 bool wait( condition_variable(L) & this, Duration duration ) with(this) { WAIT_TIME( 0 , 0p , duration ) }
410 bool wait( condition_variable(L) & this, uintptr_t info, Duration duration ) with(this) { WAIT_TIME( info, 0p , duration ) }
411 bool wait( condition_variable(L) & this, L & l, Duration duration ) with(this) { WAIT_TIME( 0 , &l , duration ) }
412 bool wait( condition_variable(L) & this, L & l, uintptr_t info, Duration duration ) with(this) { WAIT_TIME( info, &l , duration ) }
413
414 //-----------------------------------------------------------------------------
415 // fast_cond_var
416 void ?{}( fast_cond_var(L) & this ){
417 this.blocked_threads{};
418 #ifdef __CFA_DEBUG__
419 this.lock_used = 0p;
420 #endif
421 }
422 void ^?{}( fast_cond_var(L) & this ){ }
423
424 bool notify_one( fast_cond_var(L) & this ) with(this) {
425 bool ret = ! blocked_threads`isEmpty;
426 if ( ret ) {
427 info_thread(L) & popped = try_pop_front( blocked_threads );
428 on_notify(*popped.lock, popped.t);
429 }
430 return ret;
431 }
432 bool notify_all( fast_cond_var(L) & this ) with(this) {
433 bool ret = ! blocked_threads`isEmpty;
434 while( ! blocked_threads`isEmpty ) {
435 info_thread(L) & popped = try_pop_front( blocked_threads );
436 on_notify(*popped.lock, popped.t);
437 }
438 return ret;
439 }
440
441 uintptr_t front( fast_cond_var(L) & this ) with(this) { return blocked_threads`isEmpty ? NULL : blocked_threads`first.info; }
442 bool empty ( fast_cond_var(L) & this ) with(this) { return blocked_threads`isEmpty; }
443
444 void wait( fast_cond_var(L) & this, L & l ) {
445 wait( this, l, 0 );
446 }
447
448 void wait( fast_cond_var(L) & this, L & l, uintptr_t info ) with(this) {
449 // brand cond lock with lock
450 #ifdef __CFA_DEBUG__
451 if ( lock_used == 0p ) lock_used = &l;
452 else { assert(lock_used == &l); }
453 #endif
454 info_thread( L ) i = { active_thread(), info, &l };
455 insert_last( blocked_threads, i );
456 size_t recursion_count = on_wait( *i.lock );
457 park( );
458 on_wakeup(*i.lock, recursion_count);
459 }
460}
461
462//-----------------------------------------------------------------------------
463// Semaphore
464void ?{}( semaphore & this, int count = 1 ) {
465 (this.lock){};
466 this.count = count;
467 (this.waiting){};
468}
469void ^?{}(semaphore & this) {}
470
471bool P(semaphore & this) with( this ){
472 lock( lock __cfaabi_dbg_ctx2 );
473 count -= 1;
474 if ( count < 0 ) {
475 // queue current task
476 append( waiting, active_thread() );
477
478 // atomically release spin lock and block
479 unlock( lock );
480 park();
481 return true;
482 }
483 else {
484 unlock( lock );
485 return false;
486 }
487}
488
489thread$ * V (semaphore & this, const bool doUnpark ) with( this ) {
490 thread$ * thrd = 0p;
491 lock( lock __cfaabi_dbg_ctx2 );
492 count += 1;
493 if ( count <= 0 ) {
494 // remove task at head of waiting list
495 thrd = pop_head( waiting );
496 }
497
498 unlock( lock );
499
500 // make new owner
501 if( doUnpark ) unpark( thrd );
502
503 return thrd;
504}
505
506bool V(semaphore & this) with( this ) {
507 thread$ * thrd = V(this, true);
508 return thrd != 0p;
509}
510
511bool V(semaphore & this, unsigned diff) with( this ) {
512 thread$ * thrd = 0p;
513 lock( lock __cfaabi_dbg_ctx2 );
514 int release = max(-count, (int)diff);
515 count += diff;
516 for(release) {
517 unpark( pop_head( waiting ) );
518 }
519
520 unlock( lock );
521
522 return thrd != 0p;
523}
Note: See TracBrowser for help on using the repository browser.