[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 | |
---|
[c18bf9e] | 26 | #pragma GCC visibility push(default) |
---|
| 27 | |
---|
[ac5816d] | 28 | //----------------------------------------------------------------------------- |
---|
| 29 | // info_thread |
---|
[fd54fef] | 30 | forall(L & | is_blocking_lock(L)) { |
---|
[ac5816d] | 31 | struct info_thread { |
---|
[82f4063] | 32 | // used to put info_thread on a dl queue |
---|
| 33 | inline dlink(info_thread(L)); |
---|
[ac5816d] | 34 | |
---|
| 35 | // waiting thread |
---|
[e84ab3d] | 36 | struct thread$ * t; |
---|
[ac5816d] | 37 | |
---|
| 38 | // shadow field |
---|
| 39 | uintptr_t info; |
---|
| 40 | |
---|
| 41 | // lock that is passed to wait() (if one is passed) |
---|
| 42 | L * lock; |
---|
| 43 | |
---|
| 44 | // true when signalled and false when timeout wakes thread |
---|
| 45 | bool signalled; |
---|
| 46 | }; |
---|
[82f4063] | 47 | P9_EMBEDDED( info_thread(L), dlink(info_thread(L)) ) |
---|
[848439f] | 48 | |
---|
[e84ab3d] | 49 | void ?{}( info_thread(L) & this, thread$ * t, uintptr_t info, L * l ) { |
---|
[848439f] | 50 | this.t = t; |
---|
| 51 | this.info = info; |
---|
[ac5816d] | 52 | this.lock = l; |
---|
[848439f] | 53 | } |
---|
| 54 | |
---|
[ac5816d] | 55 | void ^?{}( info_thread(L) & this ) {} |
---|
[848439f] | 56 | } |
---|
[cad1df1] | 57 | |
---|
[ac5816d] | 58 | //----------------------------------------------------------------------------- |
---|
| 59 | // Blocking Locks |
---|
[848439f] | 60 | void ?{}( blocking_lock & this, bool multi_acquisition, bool strict_owner ) { |
---|
| 61 | this.lock{}; |
---|
| 62 | this.blocked_threads{}; |
---|
| 63 | this.wait_count = 0; |
---|
| 64 | this.multi_acquisition = multi_acquisition; |
---|
| 65 | this.strict_owner = strict_owner; |
---|
| 66 | this.owner = 0p; |
---|
| 67 | this.recursion_count = 0; |
---|
| 68 | } |
---|
| 69 | |
---|
[cad1df1] | 70 | void ^?{}( blocking_lock & this ) {} |
---|
[ab1b971] | 71 | |
---|
[848439f] | 72 | |
---|
| 73 | void lock( blocking_lock & this ) with( this ) { |
---|
| 74 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
[e84ab3d] | 75 | thread$ * thrd = active_thread(); |
---|
[ac5816d] | 76 | |
---|
| 77 | // single acquisition lock is held by current thread |
---|
| 78 | /* paranoid */ verifyf( owner != thrd || multi_acquisition, "Single acquisition lock holder (%p) attempted to reacquire the lock %p resulting in a deadlock.", owner, &this ); |
---|
| 79 | |
---|
| 80 | // lock is held by some other thread |
---|
| 81 | if ( owner != 0p && owner != thrd ) { |
---|
[82f4063] | 82 | insert_last( blocked_threads, *thrd ); |
---|
[848439f] | 83 | wait_count++; |
---|
| 84 | unlock( lock ); |
---|
[eeb5023] | 85 | park( ); |
---|
[ac5816d] | 86 | } |
---|
| 87 | // multi acquisition lock is held by current thread |
---|
| 88 | else if ( owner == thrd && multi_acquisition ) { |
---|
[848439f] | 89 | recursion_count++; |
---|
| 90 | unlock( lock ); |
---|
[ac5816d] | 91 | } |
---|
| 92 | // lock isn't held |
---|
| 93 | else { |
---|
| 94 | owner = thrd; |
---|
[848439f] | 95 | recursion_count = 1; |
---|
| 96 | unlock( lock ); |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | bool try_lock( blocking_lock & this ) with( this ) { |
---|
| 101 | bool ret = false; |
---|
| 102 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
[ac5816d] | 103 | |
---|
| 104 | // lock isn't held |
---|
| 105 | if ( owner == 0p ) { |
---|
[6a8882c] | 106 | owner = active_thread(); |
---|
| 107 | recursion_count = 1; |
---|
[848439f] | 108 | ret = true; |
---|
[ac5816d] | 109 | } |
---|
| 110 | // multi acquisition lock is held by current thread |
---|
| 111 | else if ( owner == active_thread() && multi_acquisition ) { |
---|
[848439f] | 112 | recursion_count++; |
---|
| 113 | ret = true; |
---|
| 114 | } |
---|
[ac5816d] | 115 | |
---|
[848439f] | 116 | unlock( lock ); |
---|
| 117 | return ret; |
---|
| 118 | } |
---|
| 119 | |
---|
[c18bf9e] | 120 | static void pop_and_set_new_owner( blocking_lock & this ) with( this ) { |
---|
[e84ab3d] | 121 | thread$ * t = &try_pop_front( blocked_threads ); |
---|
[cad1df1] | 122 | owner = t; |
---|
| 123 | recursion_count = ( t ? 1 : 0 ); |
---|
[f9b68d6] | 124 | if ( t ) wait_count--; |
---|
[cad1df1] | 125 | unpark( t ); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | void unlock( blocking_lock & this ) with( this ) { |
---|
| 129 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
[ac5816d] | 130 | /* paranoid */ verifyf( owner != 0p, "Attempt to release lock %p that isn't held", &this ); |
---|
[22b7579] | 131 | /* paranoid */ verifyf( owner == active_thread() || !strict_owner , "Thread %p other than the owner %p attempted to release owner lock %p", owner, active_thread(), &this ); |
---|
| 132 | /* 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] | 133 | |
---|
| 134 | // if recursion count is zero release lock and set new owner if one is waiting |
---|
[848439f] | 135 | recursion_count--; |
---|
[ac5816d] | 136 | if ( recursion_count == 0 ) { |
---|
[cad1df1] | 137 | pop_and_set_new_owner( this ); |
---|
[848439f] | 138 | } |
---|
| 139 | unlock( lock ); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | size_t wait_count( blocking_lock & this ) with( this ) { |
---|
| 143 | return wait_count; |
---|
| 144 | } |
---|
| 145 | |
---|
[e84ab3d] | 146 | void on_notify( blocking_lock & this, thread$ * t ) with( this ) { |
---|
[ac5816d] | 147 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 148 | // lock held |
---|
| 149 | if ( owner != 0p ) { |
---|
[82f4063] | 150 | insert_last( blocked_threads, *t ); |
---|
[848439f] | 151 | wait_count++; |
---|
| 152 | unlock( lock ); |
---|
[ac5816d] | 153 | } |
---|
| 154 | // lock not held |
---|
| 155 | else { |
---|
[848439f] | 156 | owner = t; |
---|
[6a8882c] | 157 | recursion_count = 1; |
---|
[eeb5023] | 158 | unpark( t ); |
---|
[848439f] | 159 | unlock( lock ); |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | |
---|
[22b7579] | 163 | size_t on_wait( blocking_lock & this ) with( this ) { |
---|
[ac5816d] | 164 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 165 | /* paranoid */ verifyf( owner != 0p, "Attempt to release lock %p that isn't held", &this ); |
---|
| 166 | /* paranoid */ verifyf( owner == active_thread() || !strict_owner, "Thread %p other than the owner %p attempted to release owner lock %p", owner, active_thread(), &this ); |
---|
| 167 | |
---|
[22b7579] | 168 | size_t ret = recursion_count; |
---|
| 169 | |
---|
[cad1df1] | 170 | pop_and_set_new_owner( this ); |
---|
[848439f] | 171 | unlock( lock ); |
---|
[22b7579] | 172 | return ret; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | void on_wakeup( blocking_lock & this, size_t recursion ) with( this ) { |
---|
| 176 | recursion_count = recursion; |
---|
[848439f] | 177 | } |
---|
| 178 | |
---|
[ac5816d] | 179 | //----------------------------------------------------------------------------- |
---|
| 180 | // alarm node wrapper |
---|
[fd54fef] | 181 | forall(L & | is_blocking_lock(L)) { |
---|
[c20533ea] | 182 | struct alarm_node_wrap { |
---|
| 183 | alarm_node_t alarm_node; |
---|
| 184 | condition_variable(L) * cond; |
---|
[90a10e8] | 185 | info_thread(L) * info_thd; |
---|
[c20533ea] | 186 | }; |
---|
| 187 | |
---|
[c457dc41] | 188 | void ?{}( alarm_node_wrap(L) & this, Duration alarm, Duration period, Alarm_Callback callback, condition_variable(L) * c, info_thread(L) * i ) { |
---|
[c20533ea] | 189 | this.alarm_node{ callback, alarm, period }; |
---|
[ac5816d] | 190 | this.cond = c; |
---|
[90a10e8] | 191 | this.info_thd = i; |
---|
[c20533ea] | 192 | } |
---|
| 193 | |
---|
| 194 | void ^?{}( alarm_node_wrap(L) & this ) { } |
---|
[848439f] | 195 | |
---|
[c18bf9e] | 196 | static void timeout_handler ( alarm_node_wrap(L) & this ) with( this ) { |
---|
[ac5816d] | 197 | // This condition_variable member is called from the kernel, and therefore, cannot block, but it can spin. |
---|
| 198 | lock( cond->lock __cfaabi_dbg_ctx2 ); |
---|
| 199 | |
---|
| 200 | // this check is necessary to avoid a race condition since this timeout handler |
---|
| 201 | // may still be called after a thread has been removed from the queue but |
---|
| 202 | // before the alarm is unregistered |
---|
[82f4063] | 203 | if ( (*info_thd)`isListed ) { // is thread on queue |
---|
[90a10e8] | 204 | info_thd->signalled = false; |
---|
[ac5816d] | 205 | // remove this thread O(1) |
---|
[82f4063] | 206 | remove( *info_thd ); |
---|
[6a8882c] | 207 | cond->count--; |
---|
[90a10e8] | 208 | if( info_thd->lock ) { |
---|
[ac5816d] | 209 | // call lock's on_notify if a lock was passed |
---|
[90a10e8] | 210 | on_notify(*info_thd->lock, info_thd->t); |
---|
[ac5816d] | 211 | } else { |
---|
| 212 | // otherwise wake thread |
---|
[90a10e8] | 213 | unpark( info_thd->t ); |
---|
[ac5816d] | 214 | } |
---|
| 215 | } |
---|
| 216 | unlock( cond->lock ); |
---|
[848439f] | 217 | } |
---|
| 218 | |
---|
[797a193] | 219 | // this casts the alarm node to our wrapped type since we used type erasure |
---|
[c18bf9e] | 220 | static void alarm_node_wrap_cast( alarm_node_t & a ) { timeout_handler( (alarm_node_wrap(L) &)a ); } |
---|
[ae06e0b] | 221 | |
---|
| 222 | struct pthread_alarm_node_wrap { |
---|
| 223 | alarm_node_t alarm_node; |
---|
| 224 | pthread_cond_var(L) * cond; |
---|
| 225 | info_thread(L) * info_thd; |
---|
| 226 | }; |
---|
| 227 | |
---|
| 228 | void ?{}( pthread_alarm_node_wrap(L) & this, Duration alarm, Duration period, Alarm_Callback callback, pthread_cond_var(L) * c, info_thread(L) * i ) { |
---|
| 229 | this.alarm_node{ callback, alarm, period }; |
---|
| 230 | this.cond = c; |
---|
| 231 | this.info_thd = i; |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | void ^?{}( pthread_alarm_node_wrap(L) & this ) { } |
---|
| 235 | |
---|
| 236 | static void timeout_handler ( pthread_alarm_node_wrap(L) & this ) with( this ) { |
---|
| 237 | // This pthread_cond_var member is called from the kernel, and therefore, cannot block, but it can spin. |
---|
| 238 | lock( cond->lock __cfaabi_dbg_ctx2 ); |
---|
| 239 | |
---|
| 240 | // this check is necessary to avoid a race condition since this timeout handler |
---|
| 241 | // may still be called after a thread has been removed from the queue but |
---|
| 242 | // before the alarm is unregistered |
---|
| 243 | if ( (*info_thd)`isListed ) { // is thread on queue |
---|
| 244 | info_thd->signalled = false; |
---|
| 245 | // remove this thread O(1) |
---|
| 246 | remove( *info_thd ); |
---|
| 247 | on_notify(*info_thd->lock, info_thd->t); |
---|
| 248 | } |
---|
| 249 | unlock( cond->lock ); |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | // this casts the alarm node to our wrapped type since we used type erasure |
---|
| 253 | static void pthread_alarm_node_wrap_cast( alarm_node_t & a ) { timeout_handler( (pthread_alarm_node_wrap(L) &)a ); } |
---|
[c20533ea] | 254 | } |
---|
| 255 | |
---|
[ac5816d] | 256 | //----------------------------------------------------------------------------- |
---|
[7f958c4] | 257 | // Synchronization Locks |
---|
[fd54fef] | 258 | forall(L & | is_blocking_lock(L)) { |
---|
[c20533ea] | 259 | |
---|
[7f958c4] | 260 | //----------------------------------------------------------------------------- |
---|
| 261 | // condition variable |
---|
[848439f] | 262 | void ?{}( condition_variable(L) & this ){ |
---|
[eeb5023] | 263 | this.lock{}; |
---|
| 264 | this.blocked_threads{}; |
---|
| 265 | this.count = 0; |
---|
[848439f] | 266 | } |
---|
| 267 | |
---|
[cad1df1] | 268 | void ^?{}( condition_variable(L) & this ){ } |
---|
[848439f] | 269 | |
---|
[c18bf9e] | 270 | static void process_popped( condition_variable(L) & this, info_thread(L) & popped ) with( this ) { |
---|
[cad1df1] | 271 | if(&popped != 0p) { |
---|
[dff1fd1] | 272 | popped.signalled = true; |
---|
[eeb5023] | 273 | count--; |
---|
[cad1df1] | 274 | if (popped.lock) { |
---|
[ac5816d] | 275 | // if lock passed call on_notify |
---|
| 276 | on_notify(*popped.lock, popped.t); |
---|
[848439f] | 277 | } else { |
---|
[ac5816d] | 278 | // otherwise wake thread |
---|
| 279 | unpark(popped.t); |
---|
[848439f] | 280 | } |
---|
| 281 | } |
---|
[cad1df1] | 282 | } |
---|
| 283 | |
---|
| 284 | bool notify_one( condition_variable(L) & this ) with( this ) { |
---|
| 285 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
[82f4063] | 286 | bool ret = ! blocked_threads`isEmpty; |
---|
| 287 | process_popped(this, try_pop_front( blocked_threads )); |
---|
[848439f] | 288 | unlock( lock ); |
---|
| 289 | return ret; |
---|
| 290 | } |
---|
| 291 | |
---|
[eeb5023] | 292 | bool notify_all( condition_variable(L) & this ) with(this) { |
---|
[848439f] | 293 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
[82f4063] | 294 | bool ret = ! blocked_threads`isEmpty; |
---|
| 295 | while( ! blocked_threads`isEmpty ) { |
---|
| 296 | process_popped(this, try_pop_front( blocked_threads )); |
---|
[848439f] | 297 | } |
---|
| 298 | unlock( lock ); |
---|
| 299 | return ret; |
---|
| 300 | } |
---|
| 301 | |
---|
[eeb5023] | 302 | uintptr_t front( condition_variable(L) & this ) with(this) { |
---|
[82f4063] | 303 | return blocked_threads`isEmpty ? NULL : blocked_threads`first.info; |
---|
[848439f] | 304 | } |
---|
| 305 | |
---|
[22b7579] | 306 | bool empty( condition_variable(L) & this ) with(this) { |
---|
| 307 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
[82f4063] | 308 | bool ret = blocked_threads`isEmpty; |
---|
[22b7579] | 309 | unlock( lock ); |
---|
| 310 | return ret; |
---|
| 311 | } |
---|
[cad1df1] | 312 | |
---|
| 313 | int counter( condition_variable(L) & this ) with(this) { return count; } |
---|
[848439f] | 314 | |
---|
[c18bf9e] | 315 | static size_t queue_and_get_recursion( condition_variable(L) & this, info_thread(L) * i ) with(this) { |
---|
[ac5816d] | 316 | // add info_thread to waiting queue |
---|
[82f4063] | 317 | insert_last( blocked_threads, *i ); |
---|
[cad1df1] | 318 | count++; |
---|
| 319 | size_t recursion_count = 0; |
---|
[ac5816d] | 320 | if (i->lock) { |
---|
| 321 | // if lock was passed get recursion count to reset to after waking thread |
---|
[22b7579] | 322 | recursion_count = on_wait( *i->lock ); |
---|
[cad1df1] | 323 | } |
---|
| 324 | return recursion_count; |
---|
[848439f] | 325 | } |
---|
| 326 | |
---|
[cad1df1] | 327 | // helper for wait()'s' with no timeout |
---|
[c18bf9e] | 328 | static void queue_info_thread( condition_variable(L) & this, info_thread(L) & i ) with(this) { |
---|
[848439f] | 329 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
[cad1df1] | 330 | size_t recursion_count = queue_and_get_recursion(this, &i); |
---|
[848439f] | 331 | unlock( lock ); |
---|
[ac5816d] | 332 | |
---|
| 333 | // blocks here |
---|
| 334 | park( ); |
---|
| 335 | |
---|
| 336 | // resets recursion count here after waking |
---|
[22b7579] | 337 | if (i.lock) on_wakeup(*i.lock, recursion_count); |
---|
[848439f] | 338 | } |
---|
| 339 | |
---|
[ac5816d] | 340 | #define WAIT( u, l ) \ |
---|
| 341 | info_thread( L ) i = { active_thread(), u, l }; \ |
---|
| 342 | queue_info_thread( this, i ); |
---|
| 343 | |
---|
[eeb5023] | 344 | // helper for wait()'s' with a timeout |
---|
[c18bf9e] | 345 | static void queue_info_thread_timeout( condition_variable(L) & this, info_thread(L) & info, Duration t, Alarm_Callback callback ) with(this) { |
---|
[eeb5023] | 346 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
[cad1df1] | 347 | size_t recursion_count = queue_and_get_recursion(this, &info); |
---|
[afd7faf] | 348 | alarm_node_wrap(L) node_wrap = { t, 0`s, callback, &this, &info }; |
---|
[eeb5023] | 349 | register_self( &node_wrap.alarm_node ); |
---|
[848439f] | 350 | unlock( lock ); |
---|
| 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; |
---|
| 415 | else { assert(lock_used == &l); } |
---|
| 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 ); |
---|
| 439 | on_notify(*popped.lock, popped.t); |
---|
| 440 | } |
---|
| 441 | unlock( lock ); |
---|
| 442 | return ret; |
---|
| 443 | } |
---|
| 444 | |
---|
| 445 | bool notify_all( pthread_cond_var(L) & this ) with(this) { |
---|
| 446 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 447 | bool ret = ! blocked_threads`isEmpty; |
---|
| 448 | while( ! blocked_threads`isEmpty ) { |
---|
| 449 | info_thread(L) & popped = try_pop_front( blocked_threads ); |
---|
| 450 | on_notify(*popped.lock, popped.t); |
---|
| 451 | } |
---|
| 452 | unlock( lock ); |
---|
| 453 | return ret; |
---|
| 454 | } |
---|
| 455 | |
---|
| 456 | uintptr_t front( pthread_cond_var(L) & this ) with(this) { return blocked_threads`isEmpty ? NULL : blocked_threads`first.info; } |
---|
| 457 | bool empty ( pthread_cond_var(L) & this ) with(this) { return blocked_threads`isEmpty; } |
---|
| 458 | |
---|
| 459 | static size_t queue_and_get_recursion( pthread_cond_var(L) & this, info_thread(L) * i ) with(this) { |
---|
| 460 | // add info_thread to waiting queue |
---|
| 461 | insert_last( blocked_threads, *i ); |
---|
| 462 | size_t recursion_count = 0; |
---|
| 463 | recursion_count = on_wait( *i->lock ); |
---|
| 464 | return recursion_count; |
---|
| 465 | } |
---|
| 466 | |
---|
| 467 | static void queue_info_thread_timeout( pthread_cond_var(L) & this, info_thread(L) & info, Duration t, Alarm_Callback callback ) with(this) { |
---|
| 468 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 469 | size_t recursion_count = queue_and_get_recursion(this, &info); |
---|
| 470 | pthread_alarm_node_wrap(L) node_wrap = { t, 0`s, callback, &this, &info }; |
---|
| 471 | register_self( &node_wrap.alarm_node ); |
---|
| 472 | unlock( lock ); |
---|
| 473 | |
---|
| 474 | // blocks here |
---|
| 475 | park(); |
---|
| 476 | |
---|
| 477 | // unregisters alarm so it doesn't go off if this happens first |
---|
| 478 | unregister_self( &node_wrap.alarm_node ); |
---|
| 479 | |
---|
| 480 | // resets recursion count here after waking |
---|
| 481 | if (info.lock) on_wakeup(*info.lock, recursion_count); |
---|
| 482 | } |
---|
| 483 | |
---|
| 484 | void wait( pthread_cond_var(L) & this, L & l ) with(this) { |
---|
| 485 | wait( this, l, 0 ); |
---|
| 486 | } |
---|
| 487 | |
---|
| 488 | void wait( pthread_cond_var(L) & this, L & l, uintptr_t info ) with(this) { |
---|
| 489 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 490 | info_thread( L ) i = { active_thread(), info, &l }; |
---|
| 491 | size_t recursion_count = queue_and_get_recursion(this, &i); |
---|
| 492 | unlock( lock ); |
---|
| 493 | park( ); |
---|
| 494 | on_wakeup(*i.lock, recursion_count); |
---|
| 495 | } |
---|
| 496 | |
---|
| 497 | #define PTHREAD_WAIT_TIME( u, l, t ) \ |
---|
| 498 | info_thread( L ) i = { active_thread(), u, l }; \ |
---|
| 499 | queue_info_thread_timeout(this, i, t, pthread_alarm_node_wrap_cast ); \ |
---|
| 500 | return i.signalled; |
---|
| 501 | |
---|
| 502 | bool wait( pthread_cond_var(L) & this, L & l, timespec t ) { |
---|
| 503 | Duration d = { t }; |
---|
| 504 | WAIT_TIME( 0, &l , d ) |
---|
| 505 | } |
---|
| 506 | |
---|
| 507 | bool wait( pthread_cond_var(L) & this, L & l, uintptr_t info, timespec t ) { |
---|
| 508 | Duration d = { t }; |
---|
| 509 | WAIT_TIME( info, &l , d ) |
---|
| 510 | } |
---|
| 511 | } |
---|
[454f478] | 512 | //----------------------------------------------------------------------------- |
---|
| 513 | // Semaphore |
---|
| 514 | void ?{}( semaphore & this, int count = 1 ) { |
---|
| 515 | (this.lock){}; |
---|
| 516 | this.count = count; |
---|
| 517 | (this.waiting){}; |
---|
| 518 | } |
---|
| 519 | void ^?{}(semaphore & this) {} |
---|
| 520 | |
---|
| 521 | bool P(semaphore & this) with( this ){ |
---|
| 522 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 523 | count -= 1; |
---|
| 524 | if ( count < 0 ) { |
---|
| 525 | // queue current task |
---|
| 526 | append( waiting, active_thread() ); |
---|
| 527 | |
---|
| 528 | // atomically release spin lock and block |
---|
| 529 | unlock( lock ); |
---|
| 530 | park(); |
---|
| 531 | return true; |
---|
| 532 | } |
---|
| 533 | else { |
---|
| 534 | unlock( lock ); |
---|
| 535 | return false; |
---|
| 536 | } |
---|
| 537 | } |
---|
| 538 | |
---|
[e84ab3d] | 539 | thread$ * V (semaphore & this, const bool doUnpark ) with( this ) { |
---|
| 540 | thread$ * thrd = 0p; |
---|
[454f478] | 541 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 542 | count += 1; |
---|
| 543 | if ( count <= 0 ) { |
---|
| 544 | // remove task at head of waiting list |
---|
| 545 | thrd = pop_head( waiting ); |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | unlock( lock ); |
---|
| 549 | |
---|
| 550 | // make new owner |
---|
[22b7579] | 551 | if( doUnpark ) unpark( thrd ); |
---|
| 552 | |
---|
| 553 | return thrd; |
---|
| 554 | } |
---|
[454f478] | 555 | |
---|
[22b7579] | 556 | bool V(semaphore & this) with( this ) { |
---|
[e84ab3d] | 557 | thread$ * thrd = V(this, true); |
---|
[454f478] | 558 | return thrd != 0p; |
---|
| 559 | } |
---|
| 560 | |
---|
| 561 | bool V(semaphore & this, unsigned diff) with( this ) { |
---|
[e84ab3d] | 562 | thread$ * thrd = 0p; |
---|
[454f478] | 563 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 564 | int release = max(-count, (int)diff); |
---|
| 565 | count += diff; |
---|
| 566 | for(release) { |
---|
| 567 | unpark( pop_head( waiting ) ); |
---|
| 568 | } |
---|
| 569 | |
---|
| 570 | unlock( lock ); |
---|
| 571 | |
---|
| 572 | return thrd != 0p; |
---|
| 573 | } |
---|