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