[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"
|
---|
[708ae38] | 20 | #include "kernel/private.hfa"
|
---|
[848439f] | 21 |
|
---|
| 22 | #include <kernel.hfa>
|
---|
| 23 | #include <stdlib.hfa>
|
---|
| 24 |
|
---|
[c18bf9e] | 25 | #pragma GCC visibility push(default)
|
---|
| 26 |
|
---|
[ac5816d] | 27 | //-----------------------------------------------------------------------------
|
---|
| 28 | // info_thread
|
---|
[fd54fef] | 29 | forall(L & | is_blocking_lock(L)) {
|
---|
[ac5816d] | 30 | struct info_thread {
|
---|
[82f4063] | 31 | // used to put info_thread on a dl queue
|
---|
| 32 | inline dlink(info_thread(L));
|
---|
[ac5816d] | 33 |
|
---|
| 34 | // waiting thread
|
---|
[e84ab3d] | 35 | struct thread$ * t;
|
---|
[ac5816d] | 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 | };
|
---|
[82f4063] | 46 | P9_EMBEDDED( info_thread(L), dlink(info_thread(L)) )
|
---|
[848439f] | 47 |
|
---|
[e84ab3d] | 48 | void ?{}( info_thread(L) & this, thread$ * t, uintptr_t info, L * l ) {
|
---|
[848439f] | 49 | this.t = t;
|
---|
| 50 | this.info = info;
|
---|
[ac5816d] | 51 | this.lock = l;
|
---|
[848439f] | 52 | }
|
---|
| 53 |
|
---|
[ac5816d] | 54 | void ^?{}( info_thread(L) & this ) {}
|
---|
[848439f] | 55 | }
|
---|
[cad1df1] | 56 |
|
---|
[ac5816d] | 57 | //-----------------------------------------------------------------------------
|
---|
| 58 | // Blocking Locks
|
---|
[848439f] | 59 | void ?{}( 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 |
|
---|
[cad1df1] | 69 | void ^?{}( blocking_lock & this ) {}
|
---|
[ab1b971] | 70 |
|
---|
[848439f] | 71 |
|
---|
| 72 | void lock( blocking_lock & this ) with( this ) {
|
---|
| 73 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
[e84ab3d] | 74 | thread$ * thrd = active_thread();
|
---|
[ac5816d] | 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 ) {
|
---|
[beeff61e] | 81 | select_node node;
|
---|
| 82 | insert_last( blocked_threads, node );
|
---|
[848439f] | 83 | wait_count++;
|
---|
| 84 | unlock( lock );
|
---|
[eeb5023] | 85 | park( );
|
---|
[beeff61e] | 86 | return;
|
---|
| 87 | } else if ( owner == thrd && multi_acquisition ) { // multi acquisition lock is held by current thread
|
---|
[848439f] | 88 | recursion_count++;
|
---|
[beeff61e] | 89 | } else { // lock isn't held
|
---|
[ac5816d] | 90 | owner = thrd;
|
---|
[848439f] | 91 | recursion_count = 1;
|
---|
| 92 | }
|
---|
[beeff61e] | 93 | unlock( lock );
|
---|
[848439f] | 94 | }
|
---|
| 95 |
|
---|
| 96 | bool try_lock( blocking_lock & this ) with( this ) {
|
---|
| 97 | bool ret = false;
|
---|
| 98 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
[ac5816d] | 99 |
|
---|
| 100 | // lock isn't held
|
---|
| 101 | if ( owner == 0p ) {
|
---|
[6a8882c] | 102 | owner = active_thread();
|
---|
| 103 | recursion_count = 1;
|
---|
[848439f] | 104 | ret = true;
|
---|
[ac5816d] | 105 | }
|
---|
| 106 | // multi acquisition lock is held by current thread
|
---|
| 107 | else if ( owner == active_thread() && multi_acquisition ) {
|
---|
[848439f] | 108 | recursion_count++;
|
---|
| 109 | ret = true;
|
---|
| 110 | }
|
---|
[ac5816d] | 111 |
|
---|
[848439f] | 112 | unlock( lock );
|
---|
| 113 | return ret;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[beeff61e] | 116 | // static void pop_and_set_new_owner( blocking_lock & this ) with( this ) {
|
---|
| 117 | // thread$ * t = &try_pop_front( blocked_threads );
|
---|
| 118 | // owner = t;
|
---|
| 119 | // recursion_count = ( t ? 1 : 0 );
|
---|
| 120 | // if ( t ) wait_count--;
|
---|
| 121 | // unpark( t );
|
---|
| 122 | // }
|
---|
| 123 |
|
---|
| 124 | static inline void pop_node( blocking_lock & this ) with( this ) {
|
---|
| 125 | __handle_waituntil_OR( blocked_threads );
|
---|
| 126 | select_node * node = &try_pop_front( blocked_threads );
|
---|
| 127 | if ( node ) {
|
---|
| 128 | wait_count--;
|
---|
| 129 | owner = node->blocked_thread;
|
---|
| 130 | recursion_count = 1;
|
---|
| 131 | // if ( !node->clause_status || __make_select_node_available( *node ) ) unpark( node->blocked_thread );
|
---|
| 132 | wake_one( blocked_threads, *node );
|
---|
| 133 | } else {
|
---|
| 134 | owner = 0p;
|
---|
| 135 | recursion_count = 0;
|
---|
| 136 | }
|
---|
[cad1df1] | 137 | }
|
---|
| 138 |
|
---|
| 139 | void unlock( blocking_lock & this ) with( this ) {
|
---|
| 140 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
[ac5816d] | 141 | /* paranoid */ verifyf( owner != 0p, "Attempt to release lock %p that isn't held", &this );
|
---|
[22b7579] | 142 | /* paranoid */ verifyf( owner == active_thread() || !strict_owner , "Thread %p other than the owner %p attempted to release owner lock %p", owner, active_thread(), &this );
|
---|
| 143 | /* 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] | 144 |
|
---|
| 145 | // if recursion count is zero release lock and set new owner if one is waiting
|
---|
[848439f] | 146 | recursion_count--;
|
---|
[ac5816d] | 147 | if ( recursion_count == 0 ) {
|
---|
[beeff61e] | 148 | pop_node( this );
|
---|
[848439f] | 149 | }
|
---|
| 150 | unlock( lock );
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | size_t wait_count( blocking_lock & this ) with( this ) {
|
---|
| 154 | return wait_count;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[e84ab3d] | 157 | void on_notify( blocking_lock & this, thread$ * t ) with( this ) {
|
---|
[ac5816d] | 158 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
| 159 | // lock held
|
---|
| 160 | if ( owner != 0p ) {
|
---|
[beeff61e] | 161 | insert_last( blocked_threads, *(select_node *)t->link_node );
|
---|
[848439f] | 162 | wait_count++;
|
---|
[ac5816d] | 163 | }
|
---|
| 164 | // lock not held
|
---|
| 165 | else {
|
---|
[848439f] | 166 | owner = t;
|
---|
[6a8882c] | 167 | recursion_count = 1;
|
---|
[eeb5023] | 168 | unpark( t );
|
---|
[848439f] | 169 | }
|
---|
[beeff61e] | 170 | unlock( lock );
|
---|
[848439f] | 171 | }
|
---|
| 172 |
|
---|
[22b7579] | 173 | size_t on_wait( blocking_lock & this ) with( this ) {
|
---|
[ac5816d] | 174 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
| 175 | /* paranoid */ verifyf( owner != 0p, "Attempt to release lock %p that isn't held", &this );
|
---|
| 176 | /* paranoid */ verifyf( owner == active_thread() || !strict_owner, "Thread %p other than the owner %p attempted to release owner lock %p", owner, active_thread(), &this );
|
---|
| 177 |
|
---|
[22b7579] | 178 | size_t ret = recursion_count;
|
---|
| 179 |
|
---|
[beeff61e] | 180 | pop_node( this );
|
---|
| 181 |
|
---|
| 182 | select_node node;
|
---|
| 183 | active_thread()->link_node = (void *)&node;
|
---|
[848439f] | 184 | unlock( lock );
|
---|
[beeff61e] | 185 |
|
---|
| 186 | park();
|
---|
| 187 |
|
---|
[22b7579] | 188 | return ret;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | void on_wakeup( blocking_lock & this, size_t recursion ) with( this ) {
|
---|
| 192 | recursion_count = recursion;
|
---|
[848439f] | 193 | }
|
---|
| 194 |
|
---|
[beeff61e] | 195 | // waituntil() support
|
---|
| 196 | bool register_select( blocking_lock & this, select_node & node ) with(this) {
|
---|
| 197 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
| 198 | thread$ * thrd = active_thread();
|
---|
| 199 |
|
---|
| 200 | // single acquisition lock is held by current thread
|
---|
| 201 | /* paranoid */ verifyf( owner != thrd || multi_acquisition, "Single acquisition lock holder (%p) attempted to reacquire the lock %p resulting in a deadlock.", owner, &this );
|
---|
| 202 |
|
---|
| 203 | if ( !node.park_counter && ( (owner == thrd && multi_acquisition) || owner == 0p ) ) { // OR special case
|
---|
| 204 | if ( !__make_select_node_available( node ) ) { // we didn't win the race so give up on registering
|
---|
| 205 | unlock( lock );
|
---|
| 206 | return false;
|
---|
| 207 | }
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | // lock is held by some other thread
|
---|
| 211 | if ( owner != 0p && owner != thrd ) {
|
---|
| 212 | insert_last( blocked_threads, node );
|
---|
| 213 | wait_count++;
|
---|
| 214 | unlock( lock );
|
---|
| 215 | return false;
|
---|
| 216 | } else if ( owner == thrd && multi_acquisition ) { // multi acquisition lock is held by current thread
|
---|
| 217 | recursion_count++;
|
---|
| 218 | } else { // lock isn't held
|
---|
| 219 | owner = thrd;
|
---|
| 220 | recursion_count = 1;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | if ( node.park_counter ) __make_select_node_available( node );
|
---|
| 224 | unlock( lock );
|
---|
| 225 | return true;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | bool unregister_select( blocking_lock & this, select_node & node ) with(this) {
|
---|
| 229 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
| 230 | if ( node`isListed ) {
|
---|
| 231 | remove( node );
|
---|
| 232 | wait_count--;
|
---|
| 233 | unlock( lock );
|
---|
| 234 | return false;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | if ( owner == active_thread() ) {
|
---|
| 238 | /* paranoid */ verifyf( recursion_count == 1 || multi_acquisition, "Thread %p attempted to unlock owner lock %p in waituntil unregister, which is not recursive but has a recursive count of %zu", active_thread(), &this, recursion_count );
|
---|
| 239 | // if recursion count is zero release lock and set new owner if one is waiting
|
---|
| 240 | recursion_count--;
|
---|
| 241 | if ( recursion_count == 0 ) {
|
---|
| 242 | pop_node( this );
|
---|
| 243 | }
|
---|
| 244 | }
|
---|
| 245 | unlock( lock );
|
---|
| 246 | return false;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | bool on_selected( blocking_lock & this, select_node & node ) { return true; }
|
---|
| 250 |
|
---|
[ac5816d] | 251 | //-----------------------------------------------------------------------------
|
---|
| 252 | // alarm node wrapper
|
---|
[fd54fef] | 253 | forall(L & | is_blocking_lock(L)) {
|
---|
[c20533ea] | 254 | struct alarm_node_wrap {
|
---|
| 255 | alarm_node_t alarm_node;
|
---|
| 256 | condition_variable(L) * cond;
|
---|
[90a10e8] | 257 | info_thread(L) * info_thd;
|
---|
[c20533ea] | 258 | };
|
---|
| 259 |
|
---|
[c457dc41] | 260 | void ?{}( alarm_node_wrap(L) & this, Duration alarm, Duration period, Alarm_Callback callback, condition_variable(L) * c, info_thread(L) * i ) {
|
---|
[c20533ea] | 261 | this.alarm_node{ callback, alarm, period };
|
---|
[ac5816d] | 262 | this.cond = c;
|
---|
[90a10e8] | 263 | this.info_thd = i;
|
---|
[c20533ea] | 264 | }
|
---|
| 265 |
|
---|
| 266 | void ^?{}( alarm_node_wrap(L) & this ) { }
|
---|
[848439f] | 267 |
|
---|
[c18bf9e] | 268 | static void timeout_handler ( alarm_node_wrap(L) & this ) with( this ) {
|
---|
[ac5816d] | 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
|
---|
[82f4063] | 275 | if ( (*info_thd)`isListed ) { // is thread on queue
|
---|
[90a10e8] | 276 | info_thd->signalled = false;
|
---|
[ac5816d] | 277 | // remove this thread O(1)
|
---|
[82f4063] | 278 | remove( *info_thd );
|
---|
[6a8882c] | 279 | cond->count--;
|
---|
[90a10e8] | 280 | if( info_thd->lock ) {
|
---|
[ac5816d] | 281 | // call lock's on_notify if a lock was passed
|
---|
[90a10e8] | 282 | on_notify(*info_thd->lock, info_thd->t);
|
---|
[ac5816d] | 283 | } else {
|
---|
| 284 | // otherwise wake thread
|
---|
[90a10e8] | 285 | unpark( info_thd->t );
|
---|
[ac5816d] | 286 | }
|
---|
| 287 | }
|
---|
| 288 | unlock( cond->lock );
|
---|
[848439f] | 289 | }
|
---|
| 290 |
|
---|
[797a193] | 291 | // this casts the alarm node to our wrapped type since we used type erasure
|
---|
[c18bf9e] | 292 | static void alarm_node_wrap_cast( alarm_node_t & a ) { timeout_handler( (alarm_node_wrap(L) &)a ); }
|
---|
[ae06e0b] | 293 |
|
---|
| 294 | struct pthread_alarm_node_wrap {
|
---|
| 295 | alarm_node_t alarm_node;
|
---|
| 296 | pthread_cond_var(L) * cond;
|
---|
| 297 | info_thread(L) * info_thd;
|
---|
| 298 | };
|
---|
| 299 |
|
---|
| 300 | void ?{}( pthread_alarm_node_wrap(L) & this, Duration alarm, Duration period, Alarm_Callback callback, pthread_cond_var(L) * c, info_thread(L) * i ) {
|
---|
| 301 | this.alarm_node{ callback, alarm, period };
|
---|
| 302 | this.cond = c;
|
---|
| 303 | this.info_thd = i;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | void ^?{}( pthread_alarm_node_wrap(L) & this ) { }
|
---|
| 307 |
|
---|
| 308 | static void timeout_handler ( pthread_alarm_node_wrap(L) & this ) with( this ) {
|
---|
| 309 | // This pthread_cond_var member is called from the kernel, and therefore, cannot block, but it can spin.
|
---|
| 310 | lock( cond->lock __cfaabi_dbg_ctx2 );
|
---|
| 311 | // this check is necessary to avoid a race condition since this timeout handler
|
---|
| 312 | // may still be called after a thread has been removed from the queue but
|
---|
| 313 | // before the alarm is unregistered
|
---|
| 314 | if ( (*info_thd)`isListed ) { // is thread on queue
|
---|
| 315 | info_thd->signalled = false;
|
---|
| 316 | // remove this thread O(1)
|
---|
| 317 | remove( *info_thd );
|
---|
| 318 | on_notify(*info_thd->lock, info_thd->t);
|
---|
| 319 | }
|
---|
| 320 | unlock( cond->lock );
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | // this casts the alarm node to our wrapped type since we used type erasure
|
---|
| 324 | static void pthread_alarm_node_wrap_cast( alarm_node_t & a ) { timeout_handler( (pthread_alarm_node_wrap(L) &)a ); }
|
---|
[c20533ea] | 325 | }
|
---|
| 326 |
|
---|
[ac5816d] | 327 | //-----------------------------------------------------------------------------
|
---|
[7f958c4] | 328 | // Synchronization Locks
|
---|
[fd54fef] | 329 | forall(L & | is_blocking_lock(L)) {
|
---|
[c20533ea] | 330 |
|
---|
[7f958c4] | 331 | //-----------------------------------------------------------------------------
|
---|
| 332 | // condition variable
|
---|
[848439f] | 333 | void ?{}( condition_variable(L) & this ){
|
---|
[eeb5023] | 334 | this.lock{};
|
---|
| 335 | this.blocked_threads{};
|
---|
| 336 | this.count = 0;
|
---|
[848439f] | 337 | }
|
---|
| 338 |
|
---|
[cad1df1] | 339 | void ^?{}( condition_variable(L) & this ){ }
|
---|
[848439f] | 340 |
|
---|
[c18bf9e] | 341 | static void process_popped( condition_variable(L) & this, info_thread(L) & popped ) with( this ) {
|
---|
[cad1df1] | 342 | if(&popped != 0p) {
|
---|
[dff1fd1] | 343 | popped.signalled = true;
|
---|
[eeb5023] | 344 | count--;
|
---|
[cad1df1] | 345 | if (popped.lock) {
|
---|
[ac5816d] | 346 | // if lock passed call on_notify
|
---|
| 347 | on_notify(*popped.lock, popped.t);
|
---|
[848439f] | 348 | } else {
|
---|
[ac5816d] | 349 | // otherwise wake thread
|
---|
| 350 | unpark(popped.t);
|
---|
[848439f] | 351 | }
|
---|
| 352 | }
|
---|
[cad1df1] | 353 | }
|
---|
| 354 |
|
---|
| 355 | bool notify_one( condition_variable(L) & this ) with( this ) {
|
---|
| 356 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
[82f4063] | 357 | bool ret = ! blocked_threads`isEmpty;
|
---|
| 358 | process_popped(this, try_pop_front( blocked_threads ));
|
---|
[848439f] | 359 | unlock( lock );
|
---|
| 360 | return ret;
|
---|
| 361 | }
|
---|
| 362 |
|
---|
[eeb5023] | 363 | bool notify_all( condition_variable(L) & this ) with(this) {
|
---|
[848439f] | 364 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
[82f4063] | 365 | bool ret = ! blocked_threads`isEmpty;
|
---|
| 366 | while( ! blocked_threads`isEmpty ) {
|
---|
| 367 | process_popped(this, try_pop_front( blocked_threads ));
|
---|
[848439f] | 368 | }
|
---|
| 369 | unlock( lock );
|
---|
| 370 | return ret;
|
---|
| 371 | }
|
---|
| 372 |
|
---|
[eeb5023] | 373 | uintptr_t front( condition_variable(L) & this ) with(this) {
|
---|
[82f4063] | 374 | return blocked_threads`isEmpty ? NULL : blocked_threads`first.info;
|
---|
[848439f] | 375 | }
|
---|
| 376 |
|
---|
[22b7579] | 377 | bool empty( condition_variable(L) & this ) with(this) {
|
---|
| 378 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
[82f4063] | 379 | bool ret = blocked_threads`isEmpty;
|
---|
[22b7579] | 380 | unlock( lock );
|
---|
| 381 | return ret;
|
---|
| 382 | }
|
---|
[cad1df1] | 383 |
|
---|
| 384 | int counter( condition_variable(L) & this ) with(this) { return count; }
|
---|
[848439f] | 385 |
|
---|
[beeff61e] | 386 | static void enqueue_thread( condition_variable(L) & this, info_thread(L) * i ) with(this) {
|
---|
[ac5816d] | 387 | // add info_thread to waiting queue
|
---|
[82f4063] | 388 | insert_last( blocked_threads, *i );
|
---|
[cad1df1] | 389 | count++;
|
---|
[beeff61e] | 390 | // size_t recursion_count = 0;
|
---|
| 391 | // if (i->lock) {
|
---|
| 392 | // // if lock was passed get recursion count to reset to after waking thread
|
---|
| 393 | // recursion_count = on_wait( *i->lock );
|
---|
| 394 | // }
|
---|
| 395 | // return recursion_count;
|
---|
[848439f] | 396 | }
|
---|
| 397 |
|
---|
[beeff61e] | 398 | static size_t block_and_get_recursion( info_thread(L) & i ) {
|
---|
| 399 | size_t recursion_count = 0;
|
---|
| 400 | if ( i.lock ) {
|
---|
| 401 | // if lock was passed get recursion count to reset to after waking thread
|
---|
| 402 | recursion_count = on_wait( *i.lock ); // this call blocks
|
---|
| 403 | } else park( );
|
---|
| 404 | return recursion_count;
|
---|
| 405 | }
|
---|
| 406 |
|
---|
[cad1df1] | 407 | // helper for wait()'s' with no timeout
|
---|
[c18bf9e] | 408 | static void queue_info_thread( condition_variable(L) & this, info_thread(L) & i ) with(this) {
|
---|
[848439f] | 409 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
[beeff61e] | 410 | enqueue_thread( this, &i );
|
---|
| 411 | // size_t recursion_count = queue_and_get_recursion( this, &i );
|
---|
[848439f] | 412 | unlock( lock );
|
---|
[ac5816d] | 413 |
|
---|
| 414 | // blocks here
|
---|
[beeff61e] | 415 | size_t recursion_count = block_and_get_recursion( i );
|
---|
| 416 | // park( );
|
---|
[ac5816d] | 417 |
|
---|
| 418 | // resets recursion count here after waking
|
---|
[beeff61e] | 419 | if ( i.lock ) on_wakeup( *i.lock, recursion_count );
|
---|
[848439f] | 420 | }
|
---|
| 421 |
|
---|
[ac5816d] | 422 | #define WAIT( u, l ) \
|
---|
| 423 | info_thread( L ) i = { active_thread(), u, l }; \
|
---|
| 424 | queue_info_thread( this, i );
|
---|
| 425 |
|
---|
[eeb5023] | 426 | // helper for wait()'s' with a timeout
|
---|
[c18bf9e] | 427 | static void queue_info_thread_timeout( condition_variable(L) & this, info_thread(L) & info, Duration t, Alarm_Callback callback ) with(this) {
|
---|
[eeb5023] | 428 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
[beeff61e] | 429 | enqueue_thread( this, &info );
|
---|
| 430 | // size_t recursion_count = queue_and_get_recursion( this, &info );
|
---|
[afd7faf] | 431 | alarm_node_wrap(L) node_wrap = { t, 0`s, callback, &this, &info };
|
---|
[848439f] | 432 | unlock( lock );
|
---|
| 433 |
|
---|
[4e83bb7] | 434 | // registers alarm outside cond lock to avoid deadlock
|
---|
| 435 | register_self( &node_wrap.alarm_node );
|
---|
| 436 |
|
---|
[ac5816d] | 437 | // blocks here
|
---|
[beeff61e] | 438 | size_t recursion_count = block_and_get_recursion( info );
|
---|
| 439 | // park();
|
---|
[848439f] | 440 |
|
---|
[ac5816d] | 441 | // unregisters alarm so it doesn't go off if this happens first
|
---|
| 442 | unregister_self( &node_wrap.alarm_node );
|
---|
[848439f] | 443 |
|
---|
[ac5816d] | 444 | // resets recursion count here after waking
|
---|
[beeff61e] | 445 | if ( info.lock ) on_wakeup( *info.lock, recursion_count );
|
---|
[848439f] | 446 | }
|
---|
| 447 |
|
---|
[ac5816d] | 448 | #define WAIT_TIME( u, l, t ) \
|
---|
| 449 | info_thread( L ) i = { active_thread(), u, l }; \
|
---|
[afd7faf] | 450 | queue_info_thread_timeout(this, i, t, alarm_node_wrap_cast ); \
|
---|
[dff1fd1] | 451 | return i.signalled;
|
---|
[848439f] | 452 |
|
---|
[ac5816d] | 453 | void wait( condition_variable(L) & this ) with(this) { WAIT( 0, 0p ) }
|
---|
| 454 | void wait( condition_variable(L) & this, uintptr_t info ) with(this) { WAIT( info, 0p ) }
|
---|
| 455 | void wait( condition_variable(L) & this, L & l ) with(this) { WAIT( 0, &l ) }
|
---|
| 456 | void wait( condition_variable(L) & this, L & l, uintptr_t info ) with(this) { WAIT( info, &l ) }
|
---|
| 457 |
|
---|
[c457dc41] | 458 | bool wait( condition_variable(L) & this, Duration duration ) with(this) { WAIT_TIME( 0 , 0p , duration ) }
|
---|
| 459 | bool wait( condition_variable(L) & this, uintptr_t info, Duration duration ) with(this) { WAIT_TIME( info, 0p , duration ) }
|
---|
| 460 | bool wait( condition_variable(L) & this, L & l, Duration duration ) with(this) { WAIT_TIME( 0 , &l , duration ) }
|
---|
| 461 | bool wait( condition_variable(L) & this, L & l, uintptr_t info, Duration duration ) with(this) { WAIT_TIME( info, &l , duration ) }
|
---|
[7f958c4] | 462 |
|
---|
| 463 | //-----------------------------------------------------------------------------
|
---|
| 464 | // fast_cond_var
|
---|
| 465 | void ?{}( fast_cond_var(L) & this ){
|
---|
[c18bf9e] | 466 | this.blocked_threads{};
|
---|
[7f958c4] | 467 | #ifdef __CFA_DEBUG__
|
---|
| 468 | this.lock_used = 0p;
|
---|
| 469 | #endif
|
---|
| 470 | }
|
---|
| 471 | void ^?{}( fast_cond_var(L) & this ){ }
|
---|
| 472 |
|
---|
| 473 | bool notify_one( fast_cond_var(L) & this ) with(this) {
|
---|
| 474 | bool ret = ! blocked_threads`isEmpty;
|
---|
| 475 | if ( ret ) {
|
---|
| 476 | info_thread(L) & popped = try_pop_front( blocked_threads );
|
---|
| 477 | on_notify(*popped.lock, popped.t);
|
---|
| 478 | }
|
---|
| 479 | return ret;
|
---|
| 480 | }
|
---|
| 481 | bool notify_all( fast_cond_var(L) & this ) with(this) {
|
---|
| 482 | bool ret = ! blocked_threads`isEmpty;
|
---|
| 483 | while( ! blocked_threads`isEmpty ) {
|
---|
| 484 | info_thread(L) & popped = try_pop_front( blocked_threads );
|
---|
| 485 | on_notify(*popped.lock, popped.t);
|
---|
| 486 | }
|
---|
| 487 | return ret;
|
---|
| 488 | }
|
---|
| 489 |
|
---|
| 490 | uintptr_t front( fast_cond_var(L) & this ) with(this) { return blocked_threads`isEmpty ? NULL : blocked_threads`first.info; }
|
---|
| 491 | bool empty ( fast_cond_var(L) & this ) with(this) { return blocked_threads`isEmpty; }
|
---|
| 492 |
|
---|
| 493 | void wait( fast_cond_var(L) & this, L & l ) {
|
---|
| 494 | wait( this, l, 0 );
|
---|
| 495 | }
|
---|
| 496 |
|
---|
| 497 | void wait( fast_cond_var(L) & this, L & l, uintptr_t info ) with(this) {
|
---|
| 498 | // brand cond lock with lock
|
---|
| 499 | #ifdef __CFA_DEBUG__
|
---|
| 500 | if ( lock_used == 0p ) lock_used = &l;
|
---|
[7d9598d8] | 501 | else assert(lock_used == &l);
|
---|
[7f958c4] | 502 | #endif
|
---|
| 503 | info_thread( L ) i = { active_thread(), info, &l };
|
---|
| 504 | insert_last( blocked_threads, i );
|
---|
[beeff61e] | 505 | size_t recursion_count = on_wait( *i.lock ); // blocks here
|
---|
| 506 | // park( );
|
---|
[7f958c4] | 507 | on_wakeup(*i.lock, recursion_count);
|
---|
| 508 | }
|
---|
[454f478] | 509 |
|
---|
[ae06e0b] | 510 | //-----------------------------------------------------------------------------
|
---|
| 511 | // pthread_cond_var
|
---|
| 512 |
|
---|
| 513 | void ?{}( pthread_cond_var(L) & this ) with(this) {
|
---|
| 514 | blocked_threads{};
|
---|
| 515 | lock{};
|
---|
| 516 | }
|
---|
| 517 |
|
---|
| 518 | void ^?{}( pthread_cond_var(L) & this ) { }
|
---|
| 519 |
|
---|
| 520 | bool notify_one( pthread_cond_var(L) & this ) with(this) {
|
---|
| 521 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
| 522 | bool ret = ! blocked_threads`isEmpty;
|
---|
| 523 | if ( ret ) {
|
---|
| 524 | info_thread(L) & popped = try_pop_front( blocked_threads );
|
---|
[4e83bb7] | 525 | popped.signalled = true;
|
---|
[ae06e0b] | 526 | on_notify(*popped.lock, popped.t);
|
---|
| 527 | }
|
---|
| 528 | unlock( lock );
|
---|
| 529 | return ret;
|
---|
| 530 | }
|
---|
| 531 |
|
---|
| 532 | bool notify_all( pthread_cond_var(L) & this ) with(this) {
|
---|
| 533 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
| 534 | bool ret = ! blocked_threads`isEmpty;
|
---|
| 535 | while( ! blocked_threads`isEmpty ) {
|
---|
| 536 | info_thread(L) & popped = try_pop_front( blocked_threads );
|
---|
[4e83bb7] | 537 | popped.signalled = true;
|
---|
[ae06e0b] | 538 | on_notify(*popped.lock, popped.t);
|
---|
| 539 | }
|
---|
| 540 | unlock( lock );
|
---|
| 541 | return ret;
|
---|
| 542 | }
|
---|
| 543 |
|
---|
| 544 | uintptr_t front( pthread_cond_var(L) & this ) with(this) { return blocked_threads`isEmpty ? NULL : blocked_threads`first.info; }
|
---|
| 545 | bool empty ( pthread_cond_var(L) & this ) with(this) { return blocked_threads`isEmpty; }
|
---|
| 546 |
|
---|
[beeff61e] | 547 | // static size_t queue_and_get_recursion( pthread_cond_var(L) & this, info_thread(L) * i ) with(this) {
|
---|
| 548 | // // add info_thread to waiting queue
|
---|
| 549 | // insert_last( blocked_threads, *i );
|
---|
| 550 | // size_t recursion_count = 0;
|
---|
| 551 | // recursion_count = on_wait( *i->lock );
|
---|
| 552 | // return recursion_count;
|
---|
| 553 | // }
|
---|
| 554 |
|
---|
[ae06e0b] | 555 |
|
---|
| 556 | static void queue_info_thread_timeout( pthread_cond_var(L) & this, info_thread(L) & info, Duration t, Alarm_Callback callback ) with(this) {
|
---|
| 557 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
[beeff61e] | 558 | // size_t recursion_count = queue_and_get_recursion(this, &info);
|
---|
| 559 | insert_last( blocked_threads, info );
|
---|
[ae06e0b] | 560 | pthread_alarm_node_wrap(L) node_wrap = { t, 0`s, callback, &this, &info };
|
---|
| 561 | unlock( lock );
|
---|
| 562 |
|
---|
[4e83bb7] | 563 | // registers alarm outside cond lock to avoid deadlock
|
---|
| 564 | register_self( &node_wrap.alarm_node );
|
---|
| 565 |
|
---|
[ae06e0b] | 566 | // blocks here
|
---|
[beeff61e] | 567 | size_t recursion_count = block_and_get_recursion( info );
|
---|
| 568 | // park();
|
---|
[ae06e0b] | 569 |
|
---|
| 570 | // unregisters alarm so it doesn't go off if this happens first
|
---|
| 571 | unregister_self( &node_wrap.alarm_node );
|
---|
| 572 |
|
---|
| 573 | // resets recursion count here after waking
|
---|
[beeff61e] | 574 | if ( info.lock ) on_wakeup( *info.lock, recursion_count );
|
---|
[ae06e0b] | 575 | }
|
---|
| 576 |
|
---|
| 577 | void wait( pthread_cond_var(L) & this, L & l ) with(this) {
|
---|
| 578 | wait( this, l, 0 );
|
---|
| 579 | }
|
---|
| 580 |
|
---|
| 581 | void wait( pthread_cond_var(L) & this, L & l, uintptr_t info ) with(this) {
|
---|
| 582 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
| 583 | info_thread( L ) i = { active_thread(), info, &l };
|
---|
[beeff61e] | 584 | insert_last( blocked_threads, i );
|
---|
| 585 | // size_t recursion_count = queue_and_get_recursion( this, &i );
|
---|
[ae06e0b] | 586 | unlock( lock );
|
---|
[beeff61e] | 587 |
|
---|
| 588 | // blocks here
|
---|
| 589 | size_t recursion_count = block_and_get_recursion( i );
|
---|
| 590 | // park();
|
---|
| 591 | on_wakeup( *i.lock, recursion_count );
|
---|
[ae06e0b] | 592 | }
|
---|
| 593 |
|
---|
| 594 | #define PTHREAD_WAIT_TIME( u, l, t ) \
|
---|
| 595 | info_thread( L ) i = { active_thread(), u, l }; \
|
---|
| 596 | queue_info_thread_timeout(this, i, t, pthread_alarm_node_wrap_cast ); \
|
---|
| 597 | return i.signalled;
|
---|
| 598 |
|
---|
[4e83bb7] | 599 | Duration getDuration(timespec t) {
|
---|
| 600 | timespec currTime;
|
---|
| 601 | clock_gettime(CLOCK_REALTIME, &currTime);
|
---|
| 602 | Duration waitUntil = { t };
|
---|
| 603 | Duration currDur = { currTime };
|
---|
| 604 | if ( currDur >= waitUntil ) return currDur - waitUntil;
|
---|
| 605 | Duration zero = { 0 };
|
---|
| 606 | return zero;
|
---|
| 607 | }
|
---|
| 608 |
|
---|
[ae06e0b] | 609 | bool wait( pthread_cond_var(L) & this, L & l, timespec t ) {
|
---|
[4e83bb7] | 610 | PTHREAD_WAIT_TIME( 0, &l , getDuration( t ) )
|
---|
[ae06e0b] | 611 | }
|
---|
| 612 |
|
---|
| 613 | bool wait( pthread_cond_var(L) & this, L & l, uintptr_t info, timespec t ) {
|
---|
[4e83bb7] | 614 | PTHREAD_WAIT_TIME( info, &l , getDuration( t ) )
|
---|
[ae06e0b] | 615 | }
|
---|
| 616 | }
|
---|
[454f478] | 617 | //-----------------------------------------------------------------------------
|
---|
| 618 | // Semaphore
|
---|
| 619 | void ?{}( semaphore & this, int count = 1 ) {
|
---|
| 620 | (this.lock){};
|
---|
| 621 | this.count = count;
|
---|
| 622 | (this.waiting){};
|
---|
| 623 | }
|
---|
| 624 | void ^?{}(semaphore & this) {}
|
---|
| 625 |
|
---|
| 626 | bool P(semaphore & this) with( this ){
|
---|
| 627 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
| 628 | count -= 1;
|
---|
| 629 | if ( count < 0 ) {
|
---|
| 630 | // queue current task
|
---|
| 631 | append( waiting, active_thread() );
|
---|
| 632 |
|
---|
| 633 | // atomically release spin lock and block
|
---|
| 634 | unlock( lock );
|
---|
| 635 | park();
|
---|
| 636 | return true;
|
---|
| 637 | }
|
---|
| 638 | else {
|
---|
| 639 | unlock( lock );
|
---|
| 640 | return false;
|
---|
| 641 | }
|
---|
| 642 | }
|
---|
| 643 |
|
---|
[e84ab3d] | 644 | thread$ * V (semaphore & this, const bool doUnpark ) with( this ) {
|
---|
| 645 | thread$ * thrd = 0p;
|
---|
[454f478] | 646 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
| 647 | count += 1;
|
---|
| 648 | if ( count <= 0 ) {
|
---|
| 649 | // remove task at head of waiting list
|
---|
| 650 | thrd = pop_head( waiting );
|
---|
| 651 | }
|
---|
| 652 |
|
---|
| 653 | unlock( lock );
|
---|
| 654 |
|
---|
| 655 | // make new owner
|
---|
[22b7579] | 656 | if( doUnpark ) unpark( thrd );
|
---|
| 657 |
|
---|
| 658 | return thrd;
|
---|
| 659 | }
|
---|
[454f478] | 660 |
|
---|
[22b7579] | 661 | bool V(semaphore & this) with( this ) {
|
---|
[e84ab3d] | 662 | thread$ * thrd = V(this, true);
|
---|
[454f478] | 663 | return thrd != 0p;
|
---|
| 664 | }
|
---|
| 665 |
|
---|
| 666 | bool V(semaphore & this, unsigned diff) with( this ) {
|
---|
[e84ab3d] | 667 | thread$ * thrd = 0p;
|
---|
[454f478] | 668 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
| 669 | int release = max(-count, (int)diff);
|
---|
| 670 | count += diff;
|
---|
| 671 | for(release) {
|
---|
| 672 | unpark( pop_head( waiting ) );
|
---|
| 673 | }
|
---|
| 674 |
|
---|
| 675 | unlock( lock );
|
---|
| 676 |
|
---|
| 677 | return thrd != 0p;
|
---|
[f5f2768] | 678 | }
|
---|