[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 ) {
|
---|
[82f4063] | 81 | insert_last( blocked_threads, *thrd );
|
---|
[848439f] | 82 | wait_count++;
|
---|
| 83 | unlock( lock );
|
---|
[eeb5023] | 84 | park( );
|
---|
[ac5816d] | 85 | }
|
---|
| 86 | // multi acquisition lock is held by current thread
|
---|
| 87 | else if ( owner == thrd && multi_acquisition ) {
|
---|
[848439f] | 88 | recursion_count++;
|
---|
| 89 | unlock( lock );
|
---|
[ac5816d] | 90 | }
|
---|
| 91 | // lock isn't held
|
---|
| 92 | else {
|
---|
| 93 | owner = thrd;
|
---|
[848439f] | 94 | recursion_count = 1;
|
---|
| 95 | unlock( lock );
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | bool try_lock( blocking_lock & this ) with( this ) {
|
---|
| 100 | bool ret = false;
|
---|
| 101 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
[ac5816d] | 102 |
|
---|
| 103 | // lock isn't held
|
---|
| 104 | if ( owner == 0p ) {
|
---|
[6a8882c] | 105 | owner = active_thread();
|
---|
| 106 | recursion_count = 1;
|
---|
[848439f] | 107 | ret = true;
|
---|
[ac5816d] | 108 | }
|
---|
| 109 | // multi acquisition lock is held by current thread
|
---|
| 110 | else if ( owner == active_thread() && multi_acquisition ) {
|
---|
[848439f] | 111 | recursion_count++;
|
---|
| 112 | ret = true;
|
---|
| 113 | }
|
---|
[ac5816d] | 114 |
|
---|
[848439f] | 115 | unlock( lock );
|
---|
| 116 | return ret;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[c18bf9e] | 119 | static void pop_and_set_new_owner( blocking_lock & this ) with( this ) {
|
---|
[e84ab3d] | 120 | thread$ * t = &try_pop_front( blocked_threads );
|
---|
[cad1df1] | 121 | owner = t;
|
---|
| 122 | recursion_count = ( t ? 1 : 0 );
|
---|
[f9b68d6] | 123 | if ( t ) wait_count--;
|
---|
[cad1df1] | 124 | unpark( t );
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | void unlock( blocking_lock & this ) with( this ) {
|
---|
| 128 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
[ac5816d] | 129 | /* paranoid */ verifyf( owner != 0p, "Attempt to release lock %p that isn't held", &this );
|
---|
[22b7579] | 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 );
|
---|
[ac5816d] | 132 |
|
---|
| 133 | // if recursion count is zero release lock and set new owner if one is waiting
|
---|
[848439f] | 134 | recursion_count--;
|
---|
[ac5816d] | 135 | if ( recursion_count == 0 ) {
|
---|
[cad1df1] | 136 | pop_and_set_new_owner( this );
|
---|
[848439f] | 137 | }
|
---|
| 138 | unlock( lock );
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | size_t wait_count( blocking_lock & this ) with( this ) {
|
---|
| 142 | return wait_count;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[e84ab3d] | 145 | void on_notify( blocking_lock & this, thread$ * t ) with( this ) {
|
---|
[ac5816d] | 146 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
| 147 | // lock held
|
---|
| 148 | if ( owner != 0p ) {
|
---|
[82f4063] | 149 | insert_last( blocked_threads, *t );
|
---|
[848439f] | 150 | wait_count++;
|
---|
| 151 | unlock( lock );
|
---|
[ac5816d] | 152 | }
|
---|
| 153 | // lock not held
|
---|
| 154 | else {
|
---|
[848439f] | 155 | owner = t;
|
---|
[6a8882c] | 156 | recursion_count = 1;
|
---|
[eeb5023] | 157 | unpark( t );
|
---|
[848439f] | 158 | unlock( lock );
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[22b7579] | 162 | size_t on_wait( blocking_lock & this ) with( this ) {
|
---|
[ac5816d] | 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 |
|
---|
[22b7579] | 167 | size_t ret = recursion_count;
|
---|
| 168 |
|
---|
[cad1df1] | 169 | pop_and_set_new_owner( this );
|
---|
[848439f] | 170 | unlock( lock );
|
---|
[22b7579] | 171 | return ret;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | void on_wakeup( blocking_lock & this, size_t recursion ) with( this ) {
|
---|
| 175 | recursion_count = recursion;
|
---|
[848439f] | 176 | }
|
---|
| 177 |
|
---|
[ac5816d] | 178 | //-----------------------------------------------------------------------------
|
---|
| 179 | // alarm node wrapper
|
---|
[fd54fef] | 180 | forall(L & | is_blocking_lock(L)) {
|
---|
[c20533ea] | 181 | struct alarm_node_wrap {
|
---|
| 182 | alarm_node_t alarm_node;
|
---|
| 183 | condition_variable(L) * cond;
|
---|
[90a10e8] | 184 | info_thread(L) * info_thd;
|
---|
[c20533ea] | 185 | };
|
---|
| 186 |
|
---|
[c457dc41] | 187 | void ?{}( alarm_node_wrap(L) & this, Duration alarm, Duration period, Alarm_Callback callback, condition_variable(L) * c, info_thread(L) * i ) {
|
---|
[c20533ea] | 188 | this.alarm_node{ callback, alarm, period };
|
---|
[ac5816d] | 189 | this.cond = c;
|
---|
[90a10e8] | 190 | this.info_thd = i;
|
---|
[c20533ea] | 191 | }
|
---|
| 192 |
|
---|
| 193 | void ^?{}( alarm_node_wrap(L) & this ) { }
|
---|
[848439f] | 194 |
|
---|
[c18bf9e] | 195 | static void timeout_handler ( alarm_node_wrap(L) & this ) with( this ) {
|
---|
[ac5816d] | 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
|
---|
[82f4063] | 202 | if ( (*info_thd)`isListed ) { // is thread on queue
|
---|
[90a10e8] | 203 | info_thd->signalled = false;
|
---|
[ac5816d] | 204 | // remove this thread O(1)
|
---|
[82f4063] | 205 | remove( *info_thd );
|
---|
[6a8882c] | 206 | cond->count--;
|
---|
[90a10e8] | 207 | if( info_thd->lock ) {
|
---|
[ac5816d] | 208 | // call lock's on_notify if a lock was passed
|
---|
[90a10e8] | 209 | on_notify(*info_thd->lock, info_thd->t);
|
---|
[ac5816d] | 210 | } else {
|
---|
| 211 | // otherwise wake thread
|
---|
[90a10e8] | 212 | unpark( info_thd->t );
|
---|
[ac5816d] | 213 | }
|
---|
| 214 | }
|
---|
| 215 | unlock( cond->lock );
|
---|
[848439f] | 216 | }
|
---|
| 217 |
|
---|
[797a193] | 218 | // this casts the alarm node to our wrapped type since we used type erasure
|
---|
[c18bf9e] | 219 | static void alarm_node_wrap_cast( alarm_node_t & a ) { timeout_handler( (alarm_node_wrap(L) &)a ); }
|
---|
[ae06e0b] | 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 ); }
|
---|
[c20533ea] | 252 | }
|
---|
| 253 |
|
---|
[ac5816d] | 254 | //-----------------------------------------------------------------------------
|
---|
[7f958c4] | 255 | // Synchronization Locks
|
---|
[fd54fef] | 256 | forall(L & | is_blocking_lock(L)) {
|
---|
[c20533ea] | 257 |
|
---|
[7f958c4] | 258 | //-----------------------------------------------------------------------------
|
---|
| 259 | // condition variable
|
---|
[848439f] | 260 | void ?{}( condition_variable(L) & this ){
|
---|
[eeb5023] | 261 | this.lock{};
|
---|
| 262 | this.blocked_threads{};
|
---|
| 263 | this.count = 0;
|
---|
[848439f] | 264 | }
|
---|
| 265 |
|
---|
[cad1df1] | 266 | void ^?{}( condition_variable(L) & this ){ }
|
---|
[848439f] | 267 |
|
---|
[c18bf9e] | 268 | static void process_popped( condition_variable(L) & this, info_thread(L) & popped ) with( this ) {
|
---|
[cad1df1] | 269 | if(&popped != 0p) {
|
---|
[dff1fd1] | 270 | popped.signalled = true;
|
---|
[eeb5023] | 271 | count--;
|
---|
[cad1df1] | 272 | if (popped.lock) {
|
---|
[ac5816d] | 273 | // if lock passed call on_notify
|
---|
| 274 | on_notify(*popped.lock, popped.t);
|
---|
[848439f] | 275 | } else {
|
---|
[ac5816d] | 276 | // otherwise wake thread
|
---|
| 277 | unpark(popped.t);
|
---|
[848439f] | 278 | }
|
---|
| 279 | }
|
---|
[cad1df1] | 280 | }
|
---|
| 281 |
|
---|
| 282 | bool notify_one( condition_variable(L) & this ) with( this ) {
|
---|
| 283 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
[82f4063] | 284 | bool ret = ! blocked_threads`isEmpty;
|
---|
| 285 | process_popped(this, try_pop_front( blocked_threads ));
|
---|
[848439f] | 286 | unlock( lock );
|
---|
| 287 | return ret;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
[eeb5023] | 290 | bool notify_all( condition_variable(L) & this ) with(this) {
|
---|
[848439f] | 291 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
[82f4063] | 292 | bool ret = ! blocked_threads`isEmpty;
|
---|
| 293 | while( ! blocked_threads`isEmpty ) {
|
---|
| 294 | process_popped(this, try_pop_front( blocked_threads ));
|
---|
[848439f] | 295 | }
|
---|
| 296 | unlock( lock );
|
---|
| 297 | return ret;
|
---|
| 298 | }
|
---|
| 299 |
|
---|
[eeb5023] | 300 | uintptr_t front( condition_variable(L) & this ) with(this) {
|
---|
[82f4063] | 301 | return blocked_threads`isEmpty ? NULL : blocked_threads`first.info;
|
---|
[848439f] | 302 | }
|
---|
| 303 |
|
---|
[22b7579] | 304 | bool empty( condition_variable(L) & this ) with(this) {
|
---|
| 305 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
[82f4063] | 306 | bool ret = blocked_threads`isEmpty;
|
---|
[22b7579] | 307 | unlock( lock );
|
---|
| 308 | return ret;
|
---|
| 309 | }
|
---|
[cad1df1] | 310 |
|
---|
| 311 | int counter( condition_variable(L) & this ) with(this) { return count; }
|
---|
[848439f] | 312 |
|
---|
[c18bf9e] | 313 | static size_t queue_and_get_recursion( condition_variable(L) & this, info_thread(L) * i ) with(this) {
|
---|
[ac5816d] | 314 | // add info_thread to waiting queue
|
---|
[82f4063] | 315 | insert_last( blocked_threads, *i );
|
---|
[cad1df1] | 316 | count++;
|
---|
| 317 | size_t recursion_count = 0;
|
---|
[ac5816d] | 318 | if (i->lock) {
|
---|
| 319 | // if lock was passed get recursion count to reset to after waking thread
|
---|
[22b7579] | 320 | recursion_count = on_wait( *i->lock );
|
---|
[cad1df1] | 321 | }
|
---|
| 322 | return recursion_count;
|
---|
[848439f] | 323 | }
|
---|
| 324 |
|
---|
[cad1df1] | 325 | // helper for wait()'s' with no timeout
|
---|
[c18bf9e] | 326 | static void queue_info_thread( condition_variable(L) & this, info_thread(L) & i ) with(this) {
|
---|
[848439f] | 327 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
[cad1df1] | 328 | size_t recursion_count = queue_and_get_recursion(this, &i);
|
---|
[848439f] | 329 | unlock( lock );
|
---|
[ac5816d] | 330 |
|
---|
| 331 | // blocks here
|
---|
| 332 | park( );
|
---|
| 333 |
|
---|
| 334 | // resets recursion count here after waking
|
---|
[22b7579] | 335 | if (i.lock) on_wakeup(*i.lock, recursion_count);
|
---|
[848439f] | 336 | }
|
---|
| 337 |
|
---|
[ac5816d] | 338 | #define WAIT( u, l ) \
|
---|
| 339 | info_thread( L ) i = { active_thread(), u, l }; \
|
---|
| 340 | queue_info_thread( this, i );
|
---|
| 341 |
|
---|
[eeb5023] | 342 | // helper for wait()'s' with a timeout
|
---|
[c18bf9e] | 343 | static void queue_info_thread_timeout( condition_variable(L) & this, info_thread(L) & info, Duration t, Alarm_Callback callback ) with(this) {
|
---|
[eeb5023] | 344 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
[cad1df1] | 345 | size_t recursion_count = queue_and_get_recursion(this, &info);
|
---|
[afd7faf] | 346 | alarm_node_wrap(L) node_wrap = { t, 0`s, callback, &this, &info };
|
---|
[848439f] | 347 | unlock( lock );
|
---|
| 348 |
|
---|
[4e83bb7] | 349 | // registers alarm outside cond lock to avoid deadlock
|
---|
| 350 | register_self( &node_wrap.alarm_node );
|
---|
| 351 |
|
---|
[ac5816d] | 352 | // blocks here
|
---|
| 353 | park();
|
---|
[848439f] | 354 |
|
---|
[ac5816d] | 355 | // unregisters alarm so it doesn't go off if this happens first
|
---|
| 356 | unregister_self( &node_wrap.alarm_node );
|
---|
[848439f] | 357 |
|
---|
[ac5816d] | 358 | // resets recursion count here after waking
|
---|
[22b7579] | 359 | if (info.lock) on_wakeup(*info.lock, recursion_count);
|
---|
[848439f] | 360 | }
|
---|
| 361 |
|
---|
[ac5816d] | 362 | #define WAIT_TIME( u, l, t ) \
|
---|
| 363 | info_thread( L ) i = { active_thread(), u, l }; \
|
---|
[afd7faf] | 364 | queue_info_thread_timeout(this, i, t, alarm_node_wrap_cast ); \
|
---|
[dff1fd1] | 365 | return i.signalled;
|
---|
[848439f] | 366 |
|
---|
[ac5816d] | 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 |
|
---|
[c457dc41] | 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 ) }
|
---|
[7f958c4] | 376 |
|
---|
| 377 | //-----------------------------------------------------------------------------
|
---|
| 378 | // fast_cond_var
|
---|
| 379 | void ?{}( fast_cond_var(L) & this ){
|
---|
[c18bf9e] | 380 | this.blocked_threads{};
|
---|
[7f958c4] | 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;
|
---|
[7d9598d8] | 415 | else assert(lock_used == &l);
|
---|
[7f958c4] | 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 | }
|
---|
[454f478] | 423 |
|
---|
[ae06e0b] | 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 );
|
---|
[4e83bb7] | 439 | popped.signalled = true;
|
---|
[ae06e0b] | 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 );
|
---|
[4e83bb7] | 451 | popped.signalled = true;
|
---|
[ae06e0b] | 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 |
|
---|
[4e83bb7] | 475 | // registers alarm outside cond lock to avoid deadlock
|
---|
| 476 | register_self( &node_wrap.alarm_node );
|
---|
| 477 |
|
---|
[ae06e0b] | 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 |
|
---|
[4e83bb7] | 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 |
|
---|
[ae06e0b] | 516 | bool wait( pthread_cond_var(L) & this, L & l, timespec t ) {
|
---|
[4e83bb7] | 517 | PTHREAD_WAIT_TIME( 0, &l , getDuration( t ) )
|
---|
[ae06e0b] | 518 | }
|
---|
| 519 |
|
---|
| 520 | bool wait( pthread_cond_var(L) & this, L & l, uintptr_t info, timespec t ) {
|
---|
[4e83bb7] | 521 | PTHREAD_WAIT_TIME( info, &l , getDuration( t ) )
|
---|
[ae06e0b] | 522 | }
|
---|
| 523 | }
|
---|
[454f478] | 524 | //-----------------------------------------------------------------------------
|
---|
| 525 | // Semaphore
|
---|
| 526 | void ?{}( semaphore & this, int count = 1 ) {
|
---|
| 527 | (this.lock){};
|
---|
| 528 | this.count = count;
|
---|
| 529 | (this.waiting){};
|
---|
| 530 | }
|
---|
| 531 | void ^?{}(semaphore & this) {}
|
---|
| 532 |
|
---|
| 533 | bool 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 |
|
---|
[e84ab3d] | 551 | thread$ * V (semaphore & this, const bool doUnpark ) with( this ) {
|
---|
| 552 | thread$ * thrd = 0p;
|
---|
[454f478] | 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
|
---|
[22b7579] | 563 | if( doUnpark ) unpark( thrd );
|
---|
| 564 |
|
---|
| 565 | return thrd;
|
---|
| 566 | }
|
---|
[454f478] | 567 |
|
---|
[22b7579] | 568 | bool V(semaphore & this) with( this ) {
|
---|
[e84ab3d] | 569 | thread$ * thrd = V(this, true);
|
---|
[454f478] | 570 | return thrd != 0p;
|
---|
| 571 | }
|
---|
| 572 |
|
---|
| 573 | bool V(semaphore & this, unsigned diff) with( this ) {
|
---|
[e84ab3d] | 574 | thread$ * thrd = 0p;
|
---|
[454f478] | 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;
|
---|
[f5f2768] | 585 | }
|
---|