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