source: libcfa/src/concurrency/locks.cfa @ 66b6f66

ADTast-experimental
Last change on this file since 66b6f66 was f5f2768, checked in by Peter A. Buhr <pabuhr@…>, 17 months ago

make _GNU_SOURCE default, change IO to use SOCKADDR_ARG and CONST_SOCKADDR_ARG, move sys/socket.h to first include because of anonymous naming problem

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