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

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since f42fc13 was f9b68d6, checked in by caparsons <caparson@…>, 3 years ago

made spacing consistent

  • Property mode set to 100644
File size: 11.3 KB
RevLine 
[ab1b971]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__
[43784ac]18#define _GNU_SOURCE
[ab1b971]19
[848439f]20#include "locks.hfa"
21#include "kernel_private.hfa"
22
23#include <kernel.hfa>
24#include <stdlib.hfa>
25
[ac5816d]26//-----------------------------------------------------------------------------
27// info_thread
[fd54fef]28forall(L & | is_blocking_lock(L)) {
[ac5816d]29        struct info_thread {
[82f4063]30                // used to put info_thread on a dl queue
31                inline dlink(info_thread(L));
[ac5816d]32
33                // waiting thread
[e84ab3d]34                struct thread$ * t;
[ac5816d]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        };
[82f4063]45        P9_EMBEDDED( info_thread(L), dlink(info_thread(L)) )
[848439f]46
[e84ab3d]47        void ?{}( info_thread(L) & this, thread$ * t, uintptr_t info, L * l ) {
[848439f]48                this.t = t;
49                this.info = info;
[ac5816d]50                this.lock = l;
[848439f]51        }
52
[ac5816d]53        void ^?{}( info_thread(L) & this ) {}
[848439f]54}
[cad1df1]55
[ac5816d]56//-----------------------------------------------------------------------------
57// Blocking Locks
[848439f]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
[cad1df1]68void ^?{}( blocking_lock & this ) {}
[ab1b971]69
[848439f]70
71void lock( blocking_lock & this ) with( this ) {
72        lock( lock __cfaabi_dbg_ctx2 );
[e84ab3d]73        thread$ * thrd = active_thread();
[ac5816d]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 ) {
[82f4063]80                insert_last( blocked_threads, *thrd );
[848439f]81                wait_count++;
82                unlock( lock );
[eeb5023]83                park( );
[ac5816d]84        }
85        // multi acquisition lock is held by current thread
86        else if ( owner == thrd && multi_acquisition ) {
[848439f]87                recursion_count++;
88                unlock( lock );
[ac5816d]89        }
90        // lock isn't held
91        else {
92                owner = thrd;
[848439f]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 );
[ac5816d]101
102        // lock isn't held
103        if ( owner == 0p ) {
[6a8882c]104                owner = active_thread();
105                recursion_count = 1;
[848439f]106                ret = true;
[ac5816d]107        }
108        // multi acquisition lock is held by current thread
109        else if ( owner == active_thread() && multi_acquisition ) {
[848439f]110                recursion_count++;
111                ret = true;
112        }
[ac5816d]113
[848439f]114        unlock( lock );
115        return ret;
116}
117
[cad1df1]118void pop_and_set_new_owner( blocking_lock & this ) with( this ) {
[e84ab3d]119        thread$ * t = &try_pop_front( blocked_threads );
[cad1df1]120        owner = t;
121        recursion_count = ( t ? 1 : 0 );
[f9b68d6]122        if ( t ) wait_count--;
[cad1df1]123        unpark( t );
124}
125
126void unlock( blocking_lock & this ) with( this ) {
127        lock( lock __cfaabi_dbg_ctx2 );
[ac5816d]128        /* paranoid */ verifyf( owner != 0p, "Attempt to release lock %p that isn't held", &this );
[22b7579]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 );
[ac5816d]131
132        // if recursion count is zero release lock and set new owner if one is waiting
[848439f]133        recursion_count--;
[ac5816d]134        if ( recursion_count == 0 ) {
[cad1df1]135                pop_and_set_new_owner( this );
[848439f]136        }
137        unlock( lock );
138}
139
140size_t wait_count( blocking_lock & this ) with( this ) {
141        return wait_count;
142}
143
[e84ab3d]144void on_notify( blocking_lock & this, thread$ * t ) with( this ) {
[ac5816d]145        lock( lock __cfaabi_dbg_ctx2 );
146        // lock held
147        if ( owner != 0p ) {
[82f4063]148                insert_last( blocked_threads, *t );
[848439f]149                wait_count++;
150                unlock( lock );
[ac5816d]151        }
152        // lock not held
153        else {
[848439f]154                owner = t;
[6a8882c]155                recursion_count = 1;
[eeb5023]156                unpark( t );
[848439f]157                unlock( lock );
158        }
159}
160
[22b7579]161size_t on_wait( blocking_lock & this ) with( this ) {
[ac5816d]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
[22b7579]166        size_t ret = recursion_count;
167
[cad1df1]168        pop_and_set_new_owner( this );
[848439f]169        unlock( lock );
[22b7579]170        return ret;
171}
172
173void on_wakeup( blocking_lock & this, size_t recursion ) with( this ) {
174        recursion_count = recursion;
[848439f]175}
176
[ac5816d]177//-----------------------------------------------------------------------------
178// alarm node wrapper
[fd54fef]179forall(L & | is_blocking_lock(L)) {
[c20533ea]180        struct alarm_node_wrap {
181                alarm_node_t alarm_node;
182                condition_variable(L) * cond;
[90a10e8]183                info_thread(L) * info_thd;
[c20533ea]184        };
185
[c457dc41]186        void ?{}( alarm_node_wrap(L) & this, Duration alarm, Duration period, Alarm_Callback callback, condition_variable(L) * c, info_thread(L) * i ) {
[c20533ea]187                this.alarm_node{ callback, alarm, period };
[ac5816d]188                this.cond = c;
[90a10e8]189                this.info_thd = i;
[c20533ea]190        }
191
192        void ^?{}( alarm_node_wrap(L) & this ) { }
[848439f]193
[c5bbb9b]194        void timeout_handler ( alarm_node_wrap(L) & this ) with( this ) {
[ac5816d]195                // This condition_variable member is called from the kernel, and therefore, cannot block, but it can spin.
196                lock( cond->lock __cfaabi_dbg_ctx2 );
197
198                // this check is necessary to avoid a race condition since this timeout handler
199                //      may still be called after a thread has been removed from the queue but
200                //      before the alarm is unregistered
[82f4063]201                if ( (*info_thd)`isListed ) {   // is thread on queue
[90a10e8]202                        info_thd->signalled = false;
[ac5816d]203                        // remove this thread O(1)
[82f4063]204                        remove( *info_thd );
[6a8882c]205                        cond->count--;
[90a10e8]206                        if( info_thd->lock ) {
[ac5816d]207                                // call lock's on_notify if a lock was passed
[90a10e8]208                                on_notify(*info_thd->lock, info_thd->t);
[ac5816d]209                        } else {
210                                // otherwise wake thread
[90a10e8]211                                unpark( info_thd->t );
[ac5816d]212                        }
213                }
214                unlock( cond->lock );
[848439f]215        }
216
[797a193]217        // this casts the alarm node to our wrapped type since we used type erasure
[cad1df1]218        void alarm_node_wrap_cast( alarm_node_t & a ) { timeout_handler( (alarm_node_wrap(L) &)a ); }
[c20533ea]219}
220
[ac5816d]221//-----------------------------------------------------------------------------
222// condition variable
[fd54fef]223forall(L & | is_blocking_lock(L)) {
[c20533ea]224
[848439f]225        void ?{}( condition_variable(L) & this ){
[eeb5023]226                this.lock{};
227                this.blocked_threads{};
228                this.count = 0;
[848439f]229        }
230
[cad1df1]231        void ^?{}( condition_variable(L) & this ){ }
[848439f]232
[cad1df1]233        void process_popped( condition_variable(L) & this, info_thread(L) & popped ) with( this ) {
234                if(&popped != 0p) {
[dff1fd1]235                        popped.signalled = true;
[eeb5023]236                        count--;
[cad1df1]237                        if (popped.lock) {
[ac5816d]238                                // if lock passed call on_notify
239                                on_notify(*popped.lock, popped.t);
[848439f]240                        } else {
[ac5816d]241                                // otherwise wake thread
242                                unpark(popped.t);
[848439f]243                        }
244                }
[cad1df1]245        }
246
247        bool notify_one( condition_variable(L) & this ) with( this ) {
248                lock( lock __cfaabi_dbg_ctx2 );
[82f4063]249                bool ret = ! blocked_threads`isEmpty;
250                process_popped(this, try_pop_front( blocked_threads ));
[848439f]251                unlock( lock );
252                return ret;
253        }
254
[eeb5023]255        bool notify_all( condition_variable(L) & this ) with(this) {
[848439f]256                lock( lock __cfaabi_dbg_ctx2 );
[82f4063]257                bool ret = ! blocked_threads`isEmpty;
258                while( ! blocked_threads`isEmpty ) {
259                        process_popped(this, try_pop_front( blocked_threads ));
[848439f]260                }
261                unlock( lock );
262                return ret;
263        }
264
[eeb5023]265        uintptr_t front( condition_variable(L) & this ) with(this) {
[82f4063]266                return blocked_threads`isEmpty ? NULL : blocked_threads`first.info;
[848439f]267        }
268
[22b7579]269        bool empty( condition_variable(L) & this ) with(this) {
270                lock( lock __cfaabi_dbg_ctx2 );
[82f4063]271                bool ret = blocked_threads`isEmpty;
[22b7579]272                unlock( lock );
273                return ret;
274        }
[cad1df1]275
276        int counter( condition_variable(L) & this ) with(this) { return count; }
[848439f]277
[cad1df1]278        size_t queue_and_get_recursion( condition_variable(L) & this, info_thread(L) * i ) with(this) {
[ac5816d]279                // add info_thread to waiting queue
[82f4063]280                insert_last( blocked_threads, *i );
[cad1df1]281                count++;
282                size_t recursion_count = 0;
[ac5816d]283                if (i->lock) {
284                        // if lock was passed get recursion count to reset to after waking thread
[22b7579]285                        recursion_count = on_wait( *i->lock );
[cad1df1]286                }
287                return recursion_count;
[848439f]288        }
289
[cad1df1]290        // helper for wait()'s' with no timeout
[eeb5023]291        void queue_info_thread( condition_variable(L) & this, info_thread(L) & i ) with(this) {
[848439f]292                lock( lock __cfaabi_dbg_ctx2 );
[cad1df1]293                size_t recursion_count = queue_and_get_recursion(this, &i);
[848439f]294                unlock( lock );
[ac5816d]295
296                // blocks here
297                park( );
298
299                // resets recursion count here after waking
[22b7579]300                if (i.lock) on_wakeup(*i.lock, recursion_count);
[848439f]301        }
302
[ac5816d]303        #define WAIT( u, l ) \
304                info_thread( L ) i = { active_thread(), u, l }; \
305                queue_info_thread( this, i );
306
[eeb5023]307        // helper for wait()'s' with a timeout
[afd7faf]308        void queue_info_thread_timeout( condition_variable(L) & this, info_thread(L) & info, Duration t, Alarm_Callback callback ) with(this) {
[eeb5023]309                lock( lock __cfaabi_dbg_ctx2 );
[cad1df1]310                size_t recursion_count = queue_and_get_recursion(this, &info);
[afd7faf]311                alarm_node_wrap(L) node_wrap = { t, 0`s, callback, &this, &info };
[eeb5023]312                register_self( &node_wrap.alarm_node );
[848439f]313                unlock( lock );
314
[ac5816d]315                // blocks here
316                park();
[848439f]317
[ac5816d]318                // unregisters alarm so it doesn't go off if this happens first
319                unregister_self( &node_wrap.alarm_node );
[848439f]320
[ac5816d]321                // resets recursion count here after waking
[22b7579]322                if (info.lock) on_wakeup(*info.lock, recursion_count);
[848439f]323        }
324
[ac5816d]325        #define WAIT_TIME( u, l, t ) \
326                info_thread( L ) i = { active_thread(), u, l }; \
[afd7faf]327                queue_info_thread_timeout(this, i, t, alarm_node_wrap_cast ); \
[dff1fd1]328                return i.signalled;
[848439f]329
[ac5816d]330        void wait( condition_variable(L) & this                        ) with(this) { WAIT( 0, 0p    ) }
331        void wait( condition_variable(L) & this, uintptr_t info        ) with(this) { WAIT( info, 0p ) }
332        void wait( condition_variable(L) & this, L & l                 ) with(this) { WAIT( 0, &l    ) }
333        void wait( condition_variable(L) & this, L & l, uintptr_t info ) with(this) { WAIT( info, &l ) }
334
[c457dc41]335        bool wait( condition_variable(L) & this, Duration duration                        ) with(this) { WAIT_TIME( 0   , 0p , duration ) }
336        bool wait( condition_variable(L) & this, uintptr_t info, Duration duration        ) with(this) { WAIT_TIME( info, 0p , duration ) }
337        bool wait( condition_variable(L) & this, L & l, Duration duration                 ) with(this) { WAIT_TIME( 0   , &l , duration ) }
338        bool wait( condition_variable(L) & this, L & l, uintptr_t info, Duration duration ) with(this) { WAIT_TIME( info, &l , duration ) }
[848439f]339}
[454f478]340
341//-----------------------------------------------------------------------------
342// Semaphore
343void  ?{}( semaphore & this, int count = 1 ) {
344        (this.lock){};
345        this.count = count;
346        (this.waiting){};
347}
348void ^?{}(semaphore & this) {}
349
350bool P(semaphore & this) with( this ){
351        lock( lock __cfaabi_dbg_ctx2 );
352        count -= 1;
353        if ( count < 0 ) {
354                // queue current task
355                append( waiting, active_thread() );
356
357                // atomically release spin lock and block
358                unlock( lock );
359                park();
360                return true;
361        }
362        else {
363            unlock( lock );
364            return false;
365        }
366}
367
[e84ab3d]368thread$ * V (semaphore & this, const bool doUnpark ) with( this ) {
369        thread$ * thrd = 0p;
[454f478]370        lock( lock __cfaabi_dbg_ctx2 );
371        count += 1;
372        if ( count <= 0 ) {
373                // remove task at head of waiting list
374                thrd = pop_head( waiting );
375        }
376
377        unlock( lock );
378
379        // make new owner
[22b7579]380        if( doUnpark ) unpark( thrd );
381
382        return thrd;
383}
[454f478]384
[22b7579]385bool V(semaphore & this) with( this ) {
[e84ab3d]386        thread$ * thrd = V(this, true);
[454f478]387        return thrd != 0p;
388}
389
390bool V(semaphore & this, unsigned diff) with( this ) {
[e84ab3d]391        thread$ * thrd = 0p;
[454f478]392        lock( lock __cfaabi_dbg_ctx2 );
393        int release = max(-count, (int)diff);
394        count += diff;
395        for(release) {
396                unpark( pop_head( waiting ) );
397        }
398
399        unlock( lock );
400
401        return thrd != 0p;
402}
Note: See TracBrowser for help on using the repository browser.