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