source: libcfa/src/concurrency/locks.cfa @ e5d9274

pthread-emulationqualifiedEnum
Last change on this file since e5d9274 was e5d9274, checked in by caparsons <caparson@…>, 6 months ago

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

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