| [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" | 
|---|
| [708ae38] | 21 | #include "kernel/private.hfa" | 
|---|
| [848439f] | 22 |  | 
|---|
|  | 23 | #include <kernel.hfa> | 
|---|
|  | 24 | #include <stdlib.hfa> | 
|---|
|  | 25 |  | 
|---|
| [ac5816d] | 26 | //----------------------------------------------------------------------------- | 
|---|
|  | 27 | // info_thread | 
|---|
| [fd54fef] | 28 | forall(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] | 58 | void ?{}( 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] | 68 | void ^?{}( blocking_lock & this ) {} | 
|---|
| [ab1b971] | 69 |  | 
|---|
| [848439f] | 70 |  | 
|---|
|  | 71 | void 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 |  | 
|---|
|  | 98 | bool 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] | 118 | void 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 |  | 
|---|
|  | 126 | void 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 |  | 
|---|
|  | 140 | size_t wait_count( blocking_lock & this ) with( this ) { | 
|---|
|  | 141 | return wait_count; | 
|---|
|  | 142 | } | 
|---|
|  | 143 |  | 
|---|
| [e84ab3d] | 144 | void 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] | 161 | size_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 |  | 
|---|
|  | 173 | void on_wakeup( blocking_lock & this, size_t recursion ) with( this ) { | 
|---|
|  | 174 | recursion_count = recursion; | 
|---|
| [848439f] | 175 | } | 
|---|
|  | 176 |  | 
|---|
| [ac5816d] | 177 | //----------------------------------------------------------------------------- | 
|---|
|  | 178 | // alarm node wrapper | 
|---|
| [fd54fef] | 179 | forall(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 | //----------------------------------------------------------------------------- | 
|---|
| [7f958c4] | 222 | // Synchronization Locks | 
|---|
| [fd54fef] | 223 | forall(L & | is_blocking_lock(L)) { | 
|---|
| [c20533ea] | 224 |  | 
|---|
| [7f958c4] | 225 | //----------------------------------------------------------------------------- | 
|---|
|  | 226 | // condition variable | 
|---|
| [848439f] | 227 | void ?{}( condition_variable(L) & this ){ | 
|---|
| [eeb5023] | 228 | this.lock{}; | 
|---|
|  | 229 | this.blocked_threads{}; | 
|---|
|  | 230 | this.count = 0; | 
|---|
| [848439f] | 231 | } | 
|---|
|  | 232 |  | 
|---|
| [cad1df1] | 233 | void ^?{}( condition_variable(L) & this ){ } | 
|---|
| [848439f] | 234 |  | 
|---|
| [cad1df1] | 235 | void process_popped( condition_variable(L) & this, info_thread(L) & popped ) with( this ) { | 
|---|
|  | 236 | if(&popped != 0p) { | 
|---|
| [dff1fd1] | 237 | popped.signalled = true; | 
|---|
| [eeb5023] | 238 | count--; | 
|---|
| [cad1df1] | 239 | if (popped.lock) { | 
|---|
| [ac5816d] | 240 | // if lock passed call on_notify | 
|---|
|  | 241 | on_notify(*popped.lock, popped.t); | 
|---|
| [848439f] | 242 | } else { | 
|---|
| [ac5816d] | 243 | // otherwise wake thread | 
|---|
|  | 244 | unpark(popped.t); | 
|---|
| [848439f] | 245 | } | 
|---|
|  | 246 | } | 
|---|
| [cad1df1] | 247 | } | 
|---|
|  | 248 |  | 
|---|
|  | 249 | bool notify_one( condition_variable(L) & this ) with( this ) { | 
|---|
|  | 250 | lock( lock __cfaabi_dbg_ctx2 ); | 
|---|
| [82f4063] | 251 | bool ret = ! blocked_threads`isEmpty; | 
|---|
|  | 252 | process_popped(this, try_pop_front( blocked_threads )); | 
|---|
| [848439f] | 253 | unlock( lock ); | 
|---|
|  | 254 | return ret; | 
|---|
|  | 255 | } | 
|---|
|  | 256 |  | 
|---|
| [eeb5023] | 257 | bool notify_all( condition_variable(L) & this ) with(this) { | 
|---|
| [848439f] | 258 | lock( lock __cfaabi_dbg_ctx2 ); | 
|---|
| [82f4063] | 259 | bool ret = ! blocked_threads`isEmpty; | 
|---|
|  | 260 | while( ! blocked_threads`isEmpty ) { | 
|---|
|  | 261 | process_popped(this, try_pop_front( blocked_threads )); | 
|---|
| [848439f] | 262 | } | 
|---|
|  | 263 | unlock( lock ); | 
|---|
|  | 264 | return ret; | 
|---|
|  | 265 | } | 
|---|
|  | 266 |  | 
|---|
| [eeb5023] | 267 | uintptr_t front( condition_variable(L) & this ) with(this) { | 
|---|
| [82f4063] | 268 | return blocked_threads`isEmpty ? NULL : blocked_threads`first.info; | 
|---|
| [848439f] | 269 | } | 
|---|
|  | 270 |  | 
|---|
| [22b7579] | 271 | bool empty( condition_variable(L) & this ) with(this) { | 
|---|
|  | 272 | lock( lock __cfaabi_dbg_ctx2 ); | 
|---|
| [82f4063] | 273 | bool ret = blocked_threads`isEmpty; | 
|---|
| [22b7579] | 274 | unlock( lock ); | 
|---|
|  | 275 | return ret; | 
|---|
|  | 276 | } | 
|---|
| [cad1df1] | 277 |  | 
|---|
|  | 278 | int counter( condition_variable(L) & this ) with(this) { return count; } | 
|---|
| [848439f] | 279 |  | 
|---|
| [cad1df1] | 280 | size_t queue_and_get_recursion( condition_variable(L) & this, info_thread(L) * i ) with(this) { | 
|---|
| [ac5816d] | 281 | // add info_thread to waiting queue | 
|---|
| [82f4063] | 282 | insert_last( blocked_threads, *i ); | 
|---|
| [cad1df1] | 283 | count++; | 
|---|
|  | 284 | size_t recursion_count = 0; | 
|---|
| [ac5816d] | 285 | if (i->lock) { | 
|---|
|  | 286 | // if lock was passed get recursion count to reset to after waking thread | 
|---|
| [22b7579] | 287 | recursion_count = on_wait( *i->lock ); | 
|---|
| [cad1df1] | 288 | } | 
|---|
|  | 289 | return recursion_count; | 
|---|
| [848439f] | 290 | } | 
|---|
|  | 291 |  | 
|---|
| [cad1df1] | 292 | // helper for wait()'s' with no timeout | 
|---|
| [eeb5023] | 293 | void queue_info_thread( condition_variable(L) & this, info_thread(L) & i ) with(this) { | 
|---|
| [848439f] | 294 | lock( lock __cfaabi_dbg_ctx2 ); | 
|---|
| [cad1df1] | 295 | size_t recursion_count = queue_and_get_recursion(this, &i); | 
|---|
| [848439f] | 296 | unlock( lock ); | 
|---|
| [ac5816d] | 297 |  | 
|---|
|  | 298 | // blocks here | 
|---|
|  | 299 | park( ); | 
|---|
|  | 300 |  | 
|---|
|  | 301 | // resets recursion count here after waking | 
|---|
| [22b7579] | 302 | if (i.lock) on_wakeup(*i.lock, recursion_count); | 
|---|
| [848439f] | 303 | } | 
|---|
|  | 304 |  | 
|---|
| [ac5816d] | 305 | #define WAIT( u, l ) \ | 
|---|
|  | 306 | info_thread( L ) i = { active_thread(), u, l }; \ | 
|---|
|  | 307 | queue_info_thread( this, i ); | 
|---|
|  | 308 |  | 
|---|
| [eeb5023] | 309 | // helper for wait()'s' with a timeout | 
|---|
| [afd7faf] | 310 | void queue_info_thread_timeout( condition_variable(L) & this, info_thread(L) & info, Duration t, Alarm_Callback callback ) with(this) { | 
|---|
| [eeb5023] | 311 | lock( lock __cfaabi_dbg_ctx2 ); | 
|---|
| [cad1df1] | 312 | size_t recursion_count = queue_and_get_recursion(this, &info); | 
|---|
| [afd7faf] | 313 | alarm_node_wrap(L) node_wrap = { t, 0`s, callback, &this, &info }; | 
|---|
| [eeb5023] | 314 | register_self( &node_wrap.alarm_node ); | 
|---|
| [848439f] | 315 | unlock( lock ); | 
|---|
|  | 316 |  | 
|---|
| [ac5816d] | 317 | // blocks here | 
|---|
|  | 318 | park(); | 
|---|
| [848439f] | 319 |  | 
|---|
| [ac5816d] | 320 | // unregisters alarm so it doesn't go off if this happens first | 
|---|
|  | 321 | unregister_self( &node_wrap.alarm_node ); | 
|---|
| [848439f] | 322 |  | 
|---|
| [ac5816d] | 323 | // resets recursion count here after waking | 
|---|
| [22b7579] | 324 | if (info.lock) on_wakeup(*info.lock, recursion_count); | 
|---|
| [848439f] | 325 | } | 
|---|
|  | 326 |  | 
|---|
| [ac5816d] | 327 | #define WAIT_TIME( u, l, t ) \ | 
|---|
|  | 328 | info_thread( L ) i = { active_thread(), u, l }; \ | 
|---|
| [afd7faf] | 329 | queue_info_thread_timeout(this, i, t, alarm_node_wrap_cast ); \ | 
|---|
| [dff1fd1] | 330 | return i.signalled; | 
|---|
| [848439f] | 331 |  | 
|---|
| [ac5816d] | 332 | void wait( condition_variable(L) & this                        ) with(this) { WAIT( 0, 0p    ) } | 
|---|
|  | 333 | void wait( condition_variable(L) & this, uintptr_t info        ) with(this) { WAIT( info, 0p ) } | 
|---|
|  | 334 | void wait( condition_variable(L) & this, L & l                 ) with(this) { WAIT( 0, &l    ) } | 
|---|
|  | 335 | void wait( condition_variable(L) & this, L & l, uintptr_t info ) with(this) { WAIT( info, &l ) } | 
|---|
|  | 336 |  | 
|---|
| [c457dc41] | 337 | bool wait( condition_variable(L) & this, Duration duration                        ) with(this) { WAIT_TIME( 0   , 0p , duration ) } | 
|---|
|  | 338 | bool wait( condition_variable(L) & this, uintptr_t info, Duration duration        ) with(this) { WAIT_TIME( info, 0p , duration ) } | 
|---|
|  | 339 | bool wait( condition_variable(L) & this, L & l, Duration duration                 ) with(this) { WAIT_TIME( 0   , &l , duration ) } | 
|---|
|  | 340 | bool wait( condition_variable(L) & this, L & l, uintptr_t info, Duration duration ) with(this) { WAIT_TIME( info, &l , duration ) } | 
|---|
| [7f958c4] | 341 |  | 
|---|
|  | 342 | //----------------------------------------------------------------------------- | 
|---|
|  | 343 | // fast_cond_var | 
|---|
|  | 344 | void  ?{}( fast_cond_var(L) & this ){ | 
|---|
|  | 345 | this.blocked_threads{}; | 
|---|
|  | 346 | #ifdef __CFA_DEBUG__ | 
|---|
|  | 347 | this.lock_used = 0p; | 
|---|
|  | 348 | #endif | 
|---|
|  | 349 | } | 
|---|
|  | 350 | void ^?{}( fast_cond_var(L) & this ){ } | 
|---|
|  | 351 |  | 
|---|
|  | 352 | bool notify_one( fast_cond_var(L) & this ) with(this) { | 
|---|
|  | 353 | bool ret = ! blocked_threads`isEmpty; | 
|---|
|  | 354 | if ( ret ) { | 
|---|
|  | 355 | info_thread(L) & popped = try_pop_front( blocked_threads ); | 
|---|
|  | 356 | on_notify(*popped.lock, popped.t); | 
|---|
|  | 357 | } | 
|---|
|  | 358 | return ret; | 
|---|
|  | 359 | } | 
|---|
|  | 360 | bool notify_all( fast_cond_var(L) & this ) with(this) { | 
|---|
|  | 361 | bool ret = ! blocked_threads`isEmpty; | 
|---|
|  | 362 | while( ! blocked_threads`isEmpty ) { | 
|---|
|  | 363 | info_thread(L) & popped = try_pop_front( blocked_threads ); | 
|---|
|  | 364 | on_notify(*popped.lock, popped.t); | 
|---|
|  | 365 | } | 
|---|
|  | 366 | return ret; | 
|---|
|  | 367 | } | 
|---|
|  | 368 |  | 
|---|
|  | 369 | uintptr_t front( fast_cond_var(L) & this ) with(this) { return blocked_threads`isEmpty ? NULL : blocked_threads`first.info; } | 
|---|
|  | 370 | bool empty ( fast_cond_var(L) & this ) with(this) { return blocked_threads`isEmpty; } | 
|---|
|  | 371 |  | 
|---|
|  | 372 | void wait( fast_cond_var(L) & this, L & l ) { | 
|---|
|  | 373 | wait( this, l, 0 ); | 
|---|
|  | 374 | } | 
|---|
|  | 375 |  | 
|---|
|  | 376 | void wait( fast_cond_var(L) & this, L & l, uintptr_t info ) with(this) { | 
|---|
|  | 377 | // brand cond lock with lock | 
|---|
|  | 378 | #ifdef __CFA_DEBUG__ | 
|---|
|  | 379 | if ( lock_used == 0p ) lock_used = &l; | 
|---|
|  | 380 | else { assert(lock_used == &l); } | 
|---|
|  | 381 | #endif | 
|---|
|  | 382 | info_thread( L ) i = { active_thread(), info, &l }; | 
|---|
|  | 383 | insert_last( blocked_threads, i ); | 
|---|
|  | 384 | size_t recursion_count = on_wait( *i.lock ); | 
|---|
|  | 385 | park( ); | 
|---|
|  | 386 | on_wakeup(*i.lock, recursion_count); | 
|---|
|  | 387 | } | 
|---|
| [848439f] | 388 | } | 
|---|
| [454f478] | 389 |  | 
|---|
|  | 390 | //----------------------------------------------------------------------------- | 
|---|
|  | 391 | // Semaphore | 
|---|
|  | 392 | void  ?{}( semaphore & this, int count = 1 ) { | 
|---|
|  | 393 | (this.lock){}; | 
|---|
|  | 394 | this.count = count; | 
|---|
|  | 395 | (this.waiting){}; | 
|---|
|  | 396 | } | 
|---|
|  | 397 | void ^?{}(semaphore & this) {} | 
|---|
|  | 398 |  | 
|---|
|  | 399 | bool P(semaphore & this) with( this ){ | 
|---|
|  | 400 | lock( lock __cfaabi_dbg_ctx2 ); | 
|---|
|  | 401 | count -= 1; | 
|---|
|  | 402 | if ( count < 0 ) { | 
|---|
|  | 403 | // queue current task | 
|---|
|  | 404 | append( waiting, active_thread() ); | 
|---|
|  | 405 |  | 
|---|
|  | 406 | // atomically release spin lock and block | 
|---|
|  | 407 | unlock( lock ); | 
|---|
|  | 408 | park(); | 
|---|
|  | 409 | return true; | 
|---|
|  | 410 | } | 
|---|
|  | 411 | else { | 
|---|
|  | 412 | unlock( lock ); | 
|---|
|  | 413 | return false; | 
|---|
|  | 414 | } | 
|---|
|  | 415 | } | 
|---|
|  | 416 |  | 
|---|
| [e84ab3d] | 417 | thread$ * V (semaphore & this, const bool doUnpark ) with( this ) { | 
|---|
|  | 418 | thread$ * thrd = 0p; | 
|---|
| [454f478] | 419 | lock( lock __cfaabi_dbg_ctx2 ); | 
|---|
|  | 420 | count += 1; | 
|---|
|  | 421 | if ( count <= 0 ) { | 
|---|
|  | 422 | // remove task at head of waiting list | 
|---|
|  | 423 | thrd = pop_head( waiting ); | 
|---|
|  | 424 | } | 
|---|
|  | 425 |  | 
|---|
|  | 426 | unlock( lock ); | 
|---|
|  | 427 |  | 
|---|
|  | 428 | // make new owner | 
|---|
| [22b7579] | 429 | if( doUnpark ) unpark( thrd ); | 
|---|
|  | 430 |  | 
|---|
|  | 431 | return thrd; | 
|---|
|  | 432 | } | 
|---|
| [454f478] | 433 |  | 
|---|
| [22b7579] | 434 | bool V(semaphore & this) with( this ) { | 
|---|
| [e84ab3d] | 435 | thread$ * thrd = V(this, true); | 
|---|
| [454f478] | 436 | return thrd != 0p; | 
|---|
|  | 437 | } | 
|---|
|  | 438 |  | 
|---|
|  | 439 | bool V(semaphore & this, unsigned diff) with( this ) { | 
|---|
| [e84ab3d] | 440 | thread$ * thrd = 0p; | 
|---|
| [454f478] | 441 | lock( lock __cfaabi_dbg_ctx2 ); | 
|---|
|  | 442 | int release = max(-count, (int)diff); | 
|---|
|  | 443 | count += diff; | 
|---|
|  | 444 | for(release) { | 
|---|
|  | 445 | unpark( pop_head( waiting ) ); | 
|---|
|  | 446 | } | 
|---|
|  | 447 |  | 
|---|
|  | 448 | unlock( lock ); | 
|---|
|  | 449 |  | 
|---|
|  | 450 | return thrd != 0p; | 
|---|
|  | 451 | } | 
|---|