Changeset 3959595 for libcfa/src/concurrency/locks.cfa
- Timestamp:
- Nov 5, 2020, 9:52:11 PM (2 years ago)
- Branches:
- arm-eh, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 231b18f, b5d51b0
- Parents:
- be73f30 (diff), 3febb2d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/concurrency/locks.cfa
rbe73f30 r3959595 15 15 this.t = t; 16 16 this.lock = 0p; 17 this.listed = false; 17 18 } 18 19 … … 21 22 this.info = info; 22 23 this.lock = 0p; 24 this.listed = false; 23 25 } 24 26 … … 78 80 if ( owner == thrd && !multi_acquisition) { 79 81 fprintf(stderr, "A single acquisition lock holder attempted to reacquire the lock resulting in a deadlock."); // Possibly throw instead 80 82 exit(EXIT_FAILURE); 81 83 } else if ( owner != 0p && owner != thrd ) { 82 84 append( blocked_threads, thrd ); 83 85 wait_count++; 84 86 unlock( lock ); 85 park( __cfaabi_dbg_ctx);87 park( ); 86 88 } else if ( owner == thrd && multi_acquisition ) { 87 89 recursion_count++; … … 115 117 fprintf( stderr, "There was an attempt to release a lock that isn't held" ); 116 118 return; 117 } else if ( strict_owner && owner !=active_thread() ) {119 } else if ( strict_owner && active_thread() ) { 118 120 fprintf( stderr, "A thread other than the owner attempted to release an owner lock" ); 119 121 return; … … 125 127 recursion_count = ( thrd && multi_acquisition ? 1 : 0 ); 126 128 wait_count--; 127 unpark( thrd __cfaabi_dbg_ctx2);129 unpark( thrd ); 128 130 } 129 131 unlock( lock ); … … 152 154 owner = t; 153 155 if ( multi_acquisition ) recursion_count = 1; 154 unpark( t __cfaabi_dbg_ctx2 ); 156 #if !defined( __CFA_NO_STATISTICS__ ) 157 kernelTLS.this_stats = t->curr_cluster->stats; 158 #endif 159 unpark( t ); 155 160 unlock( lock ); 156 161 } … … 161 166 if ( owner == 0p ){ // no owner implies lock isn't held 162 167 fprintf( stderr, "A lock that is not held was passed to a synchronization lock" ); 163 } else if ( strict_owner && owner !=active_thread() ) {168 } else if ( strict_owner && active_thread() ) { 164 169 fprintf( stderr, "A thread other than the owner of a lock passed it to a synchronization lock" ); 165 170 } else { … … 168 173 recursion_count = ( thrd && multi_acquisition ? 1 : 0 ); 169 174 wait_count--; 170 unpark( thrd __cfaabi_dbg_ctx2);175 unpark( thrd ); 171 176 } 172 177 unlock( lock ); … … 177 182 /////////////////////////////////////////////////////////////////// 178 183 179 // In an ideal world this may not be necessary 180 // Is it possible for nominal inheritance to inherit traits?? 181 // If that occurs we would avoid all this extra code 184 // This is temporary until an inheritance bug is fixed 182 185 183 186 void lock( mutex_lock & this ){ … … 230 233 231 234 /////////////////////////////////////////////////////////////////// 232 //// Synchronization Locks235 //// condition variable 233 236 /////////////////////////////////////////////////////////////////// 234 237 235 238 forall(dtype L | is_blocking_lock(L)) { 236 void ?{}( synchronization_lock(L) & this, bool reacquire_after_signal ){ 239 240 void timeout_handler ( alarm_node_wrap(L) & this ) with( this ) { 241 // This condition_variable member is called from the kernel, and therefore, cannot block, but it can spin. 242 lock( cond->lock __cfaabi_dbg_ctx2 ); 243 if ( (*i)->listed ) { // is thread on queue 244 info_thread(L) * copy = *i; 245 remove( cond->blocked_threads, i ); //remove this thread O(1) 246 cond->wait_count--; 247 if( !copy->lock ) { 248 unlock( cond->lock ); 249 #if !defined( __CFA_NO_STATISTICS__ ) 250 #warning unprotected access to tls TODO discuss this 251 kernelTLS.this_stats = copy->t->curr_cluster->stats; 252 #endif 253 unpark( copy->t ); 254 } else { 255 add_(*copy->lock, copy->t); // call lock's add_ 256 } 257 } 258 unlock( cond->lock ); 259 } 260 261 void alarm_node_wrap_cast( alarm_node_t & a ) { 262 timeout_handler( (alarm_node_wrap(L) &)a ); 263 } 264 265 void ?{}( condition_variable(L) & this ){ 237 266 this.lock{}; 238 267 this.blocked_threads{}; 239 268 this.count = 0; 240 this.reacquire_after_signal = reacquire_after_signal;241 }242 243 void ^?{}( synchronization_lock(L) & this ){244 // default245 }246 247 void ?{}( condition_variable(L) & this ){248 ((synchronization_lock(L) &)this){ true };249 269 } 250 270 … … 253 273 } 254 274 255 void ?{}( thread_queue(L) & this ){256 ((synchronization_lock(L) &)this){ false};257 } 258 259 void ^?{}( thread_queue(L) & this ){275 void ?{}( alarm_node_wrap(L) & this, $thread * thrd, Time alarm, Duration period, Alarm_Callback callback ) { 276 this.alarm_node{ thrd, alarm, period, callback }; 277 } 278 279 void ^?{}( alarm_node_wrap(L) & this ) { 260 280 // default 261 281 } 262 282 263 bool notify_one( synchronization_lock(L) & this ) with( this ) {283 bool notify_one( condition_variable(L) & this ) with( this ) { 264 284 lock( lock __cfaabi_dbg_ctx2 ); 265 285 bool ret = !!blocked_threads; 266 286 info_thread(L) * popped = pop_head( blocked_threads ); 287 popped->listed = false; 267 288 if(popped != 0p) { 268 if( reacquire_after_signal ){ 289 count--; 290 if (popped->lock) { 269 291 add_(*popped->lock, popped->t); 270 292 } else { 271 unpark( 272 popped->t __cfaabi_dbg_ctx2 273 ); 293 unpark(popped->t); 274 294 } 275 295 } … … 278 298 } 279 299 280 bool notify_all( synchronization_lock(L) & this ) with(this) {300 bool notify_all( condition_variable(L) & this ) with(this) { 281 301 lock( lock __cfaabi_dbg_ctx2 ); 282 302 bool ret = blocked_threads ? true : false; 283 303 while( blocked_threads ) { 284 304 info_thread(L) * popped = pop_head( blocked_threads ); 305 popped->listed = false; 285 306 if(popped != 0p){ 286 if( reacquire_after_signal ){ 307 count--; 308 if (popped->lock) { 287 309 add_(*popped->lock, popped->t); 288 310 } else { 289 unpark( 290 popped->t __cfaabi_dbg_ctx2 291 ); 311 unpark(popped->t); 292 312 } 293 313 } … … 297 317 } 298 318 299 uintptr_t front( synchronization_lock(L) & this ) with(this) { 300 return (*peek(blocked_threads)).info; 301 } 302 303 bool empty( synchronization_lock(L) & this ) with(this) { 319 uintptr_t front( condition_variable(L) & this ) with(this) { 320 if(!blocked_threads) return NULL; 321 return peek(blocked_threads)->info; 322 } 323 324 bool empty( condition_variable(L) & this ) with(this) { 304 325 return blocked_threads ? false : true; 305 326 } 306 327 307 int counter( synchronization_lock(L) & this ) with(this) {328 int counter( condition_variable(L) & this ) with(this) { 308 329 return count; 309 330 } 310 331 311 void queue_info_thread( synchronization_lock(L) & this, info_thread(L) & i ) with(this) { 312 lock( lock __cfaabi_dbg_ctx2 ); 313 append( blocked_threads, &i ); 314 count++; 315 unlock( lock ); 316 park( __cfaabi_dbg_ctx ); 317 } 318 319 320 void wait( synchronization_lock(L) & this ) with(this) { 321 info_thread( L ) i = { active_thread() }; 322 queue_info_thread( this, i ); 323 } 324 325 void wait( synchronization_lock(L) & this, uintptr_t info ) with(this) { 326 info_thread( L ) i = { active_thread(), info }; 327 queue_info_thread( this, i ); 328 } 329 // I still need to implement the time delay wait routines 330 bool wait( synchronization_lock(L) & this, Duration duration ) with(this) { 331 timeval tv = { time(0) }; 332 Time t = { tv }; 333 return wait( this, t + duration ); 334 } 335 336 bool wait( synchronization_lock(L) & this, uintptr_t info, Duration duration ) with(this) { 337 // TODO: ADD INFO 338 return wait( this, duration ); 339 } 340 341 bool wait( synchronization_lock(L) & this, Time time ) with(this) { 342 return false; //default 343 } 344 345 bool wait( synchronization_lock(L) & this, uintptr_t info, Time time ) with(this) { 346 // TODO: ADD INFO 347 return wait( this, time ); 348 } 349 350 void queue_info_thread_unlock( synchronization_lock(L) & this, L & l, info_thread(L) & i ) with(this) { 332 // helper for wait()'s' without a timeout 333 void queue_info_thread( condition_variable(L) & this, info_thread(L) & i ) with(this) { 351 334 lock( lock __cfaabi_dbg_ctx2 ); 352 335 append( this.blocked_threads, &i ); 353 336 count++; 354 i.lock = &l; 355 size_t recursion_count = get_recursion_count(l); 356 remove_( l ); 357 unlock( lock ); 358 park( __cfaabi_dbg_ctx ); // blocks here 359 360 set_recursion_count(l, recursion_count); // resets recursion count here after waking 361 } 362 363 void wait( synchronization_lock(L) & this, L & l ) with(this) { 337 i.listed = true; 338 size_t recursion_count; 339 if (i.lock) { 340 recursion_count = get_recursion_count(*i.lock); 341 remove_( *i.lock ); 342 } 343 344 unlock( lock ); 345 park( ); // blocks here 346 347 if (i.lock) set_recursion_count(*i.lock, recursion_count); // resets recursion count here after waking 348 } 349 350 // helper for wait()'s' with a timeout 351 void queue_info_thread_timeout( condition_variable(L) & this, info_thread(L) & info, Time t ) with(this) { 352 lock( lock __cfaabi_dbg_ctx2 ); 353 354 info_thread(L) * queue_ptr = &info; 355 356 alarm_node_wrap(L) node_wrap = { info.t, t, 0`s, alarm_node_wrap_cast }; 357 node_wrap.cond = &this; 358 node_wrap.i = &queue_ptr; 359 360 register_self( &node_wrap.alarm_node ); 361 362 append( blocked_threads, queue_ptr ); 363 info.listed = true; 364 count++; 365 366 size_t recursion_count; 367 if (info.lock) { 368 recursion_count = get_recursion_count(*info.lock); 369 remove_( *info.lock ); 370 } 371 372 unlock( lock ); 373 park(); 374 375 if (info.lock) set_recursion_count(*info.lock, recursion_count); 376 } 377 378 void wait( condition_variable(L) & this ) with(this) { 379 info_thread( L ) i = { active_thread() }; 380 queue_info_thread( this, i ); 381 } 382 383 void wait( condition_variable(L) & this, uintptr_t info ) with(this) { 384 info_thread( L ) i = { active_thread(), info }; 385 queue_info_thread( this, i ); 386 } 387 388 void wait( condition_variable(L) & this, Duration duration ) with(this) { 389 info_thread( L ) i = { active_thread() }; 390 queue_info_thread_timeout(this, i, __kernel_get_time() + duration ); 391 } 392 393 void wait( condition_variable(L) & this, uintptr_t info, Duration duration ) with(this) { 394 info_thread( L ) i = { active_thread(), info }; 395 queue_info_thread_timeout(this, i, __kernel_get_time() + duration ); 396 } 397 398 void wait( condition_variable(L) & this, Time time ) with(this) { 399 info_thread( L ) i = { active_thread() }; 400 queue_info_thread_timeout(this, i, time); 401 } 402 403 void wait( condition_variable(L) & this, uintptr_t info, Time time ) with(this) { 404 info_thread( L ) i = { active_thread(), info }; 405 queue_info_thread_timeout(this, i, time); 406 } 407 408 void wait( condition_variable(L) & this, L & l ) with(this) { 364 409 info_thread(L) i = { active_thread() }; 365 queue_info_thread_unlock( this, l, i ); 366 } 367 368 void wait( synchronization_lock(L) & this, L & l, uintptr_t info ) with(this) { 410 i.lock = &l; 411 queue_info_thread( this, i ); 412 } 413 414 void wait( condition_variable(L) & this, L & l, uintptr_t info ) with(this) { 369 415 info_thread(L) i = { active_thread(), info }; 370 queue_info_thread_unlock( this, l, i ); 371 } 372 373 bool wait( synchronization_lock(L) & this, L & l, Duration duration ) with(this) { 374 timeval tv = { time(0) }; 375 Time t = { tv }; 376 return wait( this, l, t + duration ); 377 } 378 379 bool wait( synchronization_lock(L) & this, L & l, uintptr_t info, Duration duration ) with(this) { 380 // TODO: ADD INFO 381 return wait( this, l, duration ); 382 } 383 384 bool wait( synchronization_lock(L) & this, L & l, Time time ) with(this) { 385 return false; //default 386 } 387 388 bool wait( synchronization_lock(L) & this, L & l, uintptr_t info, Time time ) with(this) { 389 // TODO: ADD INFO 390 return wait( this, l, time ); 391 } 392 } 393 394 /////////////////////////////////////////////////////////////////// 395 //// condition lock alternative approach 396 /////////////////////////////////////////////////////////////////// 397 398 // the solution below is less efficient but does not require the lock to have a specific add/remove routine 399 400 /////////////////////////////////////////////////////////////////// 401 //// is_simple_lock 402 /////////////////////////////////////////////////////////////////// 403 404 forall(dtype L | is_simple_lock(L)) { 405 void ?{}( condition_lock(L) & this ){ 406 // default 407 } 408 409 void ^?{}( condition_lock(L) & this ){ 410 // default 411 } 412 413 bool notify_one( condition_lock(L) & this ) with(this) { 414 return notify_one( c_var ); 415 } 416 417 bool notify_all( condition_lock(L) & this ) with(this) { 418 return notify_all( c_var ); 419 } 420 421 void wait( condition_lock(L) & this, L & l ) with(this) { 422 lock( m_lock ); 423 size_t recursion = get_recursion_count( l ); 424 unlock( l ); 425 wait( c_var, m_lock ); 426 lock( l ); 427 set_recursion_count( l , recursion ); 428 unlock( m_lock ); 429 } 430 } 416 i.lock = &l; 417 queue_info_thread( this, i ); 418 } 419 420 void wait( condition_variable(L) & this, L & l, Duration duration ) with(this) { 421 info_thread(L) i = { active_thread() }; 422 i.lock = &l; 423 queue_info_thread_timeout(this, i, __kernel_get_time() + duration ); 424 } 425 426 void wait( condition_variable(L) & this, L & l, uintptr_t info, Duration duration ) with(this) { 427 info_thread(L) i = { active_thread(), info }; 428 i.lock = &l; 429 queue_info_thread_timeout(this, i, __kernel_get_time() + duration ); 430 } 431 432 void wait( condition_variable(L) & this, L & l, Time time ) with(this) { 433 info_thread(L) i = { active_thread() }; 434 i.lock = &l; 435 queue_info_thread_timeout(this, i, time ); 436 } 437 438 void wait( condition_variable(L) & this, L & l, uintptr_t info, Time time ) with(this) { 439 info_thread(L) i = { active_thread(), info }; 440 i.lock = &l; 441 queue_info_thread_timeout(this, i, time ); 442 } 443 }
Note: See TracChangeset
for help on using the changeset viewer.