Changeset 358cba0 for src/libcfa/concurrency
- Timestamp:
- May 15, 2018, 4:17:15 PM (7 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, with_gc
- Children:
- 2e5fa345
- Parents:
- 7d0a3ba (diff), a61fa0bb (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. - Location:
- src/libcfa/concurrency
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/concurrency/coroutine
r7d0a3ba r358cba0 72 72 // Suspend implementation inlined for performance 73 73 static inline void suspend() { 74 coroutine_desc * src = TL_GET( this_coroutine ); // optimization 74 // optimization : read TLS once and reuse it 75 // Safety note: this is preemption safe since if 76 // preemption occurs after this line, the pointer 77 // will also migrate which means this value will 78 // stay in syn with the TLS 79 coroutine_desc * src = TL_GET( this_coroutine ); 75 80 76 81 assertf( src->last != 0, … … 89 94 forall(dtype T | is_coroutine(T)) 90 95 static inline void resume(T & cor) { 91 coroutine_desc * src = TL_GET( this_coroutine ); // optimization 96 // optimization : read TLS once and reuse it 97 // Safety note: this is preemption safe since if 98 // preemption occurs after this line, the pointer 99 // will also migrate which means this value will 100 // stay in syn with the TLS 101 coroutine_desc * src = TL_GET( this_coroutine ); 92 102 coroutine_desc * dst = get_coroutine(cor); 93 103 … … 107 117 dst->last = src; 108 118 dst->starter = dst->starter ? dst->starter : src; 109 } // if119 } 110 120 111 121 // always done for performance testing … … 114 124 115 125 static inline void resume(coroutine_desc * dst) { 116 coroutine_desc * src = TL_GET( this_coroutine ); // optimization 126 // optimization : read TLS once and reuse it 127 // Safety note: this is preemption safe since if 128 // preemption occurs after this line, the pointer 129 // will also migrate which means this value will 130 // stay in syn with the TLS 131 coroutine_desc * src = TL_GET( this_coroutine ); 117 132 118 133 // not resuming self ? … … 125 140 // set last resumer 126 141 dst->last = src; 127 } // if142 } 128 143 129 144 // always done for performance testing -
src/libcfa/concurrency/coroutine.c
r7d0a3ba r358cba0 84 84 // Wrapper for co 85 85 void CoroutineCtxSwitch(coroutine_desc* src, coroutine_desc* dst) { 86 verify( TL_GET( preemption_state ).enabled || TL_GET( this_processor )->do_terminate ); 86 // Safety note : This could cause some false positives due to preemption 87 verify( TL_GET( preemption_state.enabled ) || TL_GET( this_processor )->do_terminate ); 87 88 disable_interrupts(); 88 89 … … 91 92 92 93 // set new coroutine that task is executing 93 TL_SET( this_coroutine, dst );94 kernelTLS.this_coroutine = dst; 94 95 95 96 // context switch to specified coroutine … … 102 103 103 104 enable_interrupts( __cfaabi_dbg_ctx ); 104 verify( TL_GET( preemption_state ).enabled || TL_GET( this_processor )->do_terminate ); 105 // Safety note : This could cause some false positives due to preemption 106 verify( TL_GET( preemption_state.enabled ) || TL_GET( this_processor )->do_terminate ); 105 107 } //ctxSwitchDirect 106 108 -
src/libcfa/concurrency/invoke.c
r7d0a3ba r358cba0 69 69 // Fetch the thread handle from the user defined thread structure 70 70 struct thread_desc* thrd = get_thread( this ); 71 thrd->self_cor.last = NULL; 71 72 72 73 // Officially start the thread by enabling preemption -
src/libcfa/concurrency/invoke.h
r7d0a3ba r358cba0 18 18 #include "bits/locks.h" 19 19 20 #define TL_GET( member ) kernelT hreadData.member21 #define TL_SET( member, value ) kernelT hreadData.member = value;20 #define TL_GET( member ) kernelTLS.member 21 #define TL_SET( member, value ) kernelTLS.member = value; 22 22 23 23 #ifdef __cforall … … 44 44 volatile bool in_progress; 45 45 } preemption_state; 46 } kernelT hreadData;46 } kernelTLS; 47 47 } 48 48 49 49 static inline struct coroutine_desc * volatile active_coroutine() { return TL_GET( this_coroutine ); } 50 static inline struct thread_desc * volatile active_thread() { return TL_GET( this_thread); }51 static inline struct processor * volatile active_processor() { return TL_GET( this_processor ); }50 static inline struct thread_desc * volatile active_thread () { return TL_GET( this_thread ); } 51 static inline struct processor * volatile active_processor() { return TL_GET( this_processor ); } // UNSAFE 52 52 #endif 53 53 … … 136 136 struct thread_desc * next; 137 137 138 __cfaabi_dbg_debug_do( 139 // instrusive link field for debugging 140 struct thread_desc * dbg_next; 141 struct thread_desc * dbg_prev; 142 ) 138 struct { 139 struct thread_desc * next; 140 struct thread_desc * prev; 141 } node; 143 142 }; 144 143 … … 147 146 static inline thread_desc * & get_next( thread_desc & this ) { 148 147 return this.next; 148 } 149 150 static inline [thread_desc *&, thread_desc *& ] __get( thread_desc & this ) { 151 return this.node.[next, prev]; 149 152 } 150 153 -
src/libcfa/concurrency/kernel
r7d0a3ba r358cba0 40 40 41 41 //----------------------------------------------------------------------------- 42 // Cluster 43 struct cluster { 44 // Ready queue locks 45 __spinlock_t ready_queue_lock; 42 // Processor 43 extern struct cluster * mainCluster; 46 44 47 // Ready queue for threads48 __queue_t(thread_desc) ready_queue;49 50 // Name of the cluster51 const char * name;52 53 // Preemption rate on this cluster54 Duration preemption_rate;55 };56 57 extern struct cluster * mainCluster;58 extern Duration default_preemption();59 60 void ?{} (cluster & this, const char * name, Duration preemption_rate);61 void ^?{}(cluster & this);62 63 static inline void ?{} (cluster & this) { this{"Anonymous Cluster", default_preemption()}; }64 static inline void ?{} (cluster & this, Duration preemption_rate) { this{"Anonymous Cluster", preemption_rate}; }65 static inline void ?{} (cluster & this, const char * name) { this{name, default_preemption()}; }66 67 //-----------------------------------------------------------------------------68 // Processor69 45 enum FinishOpCode { No_Action, Release, Schedule, Release_Schedule, Release_Multi, Release_Multi_Schedule }; 70 46 … … 98 74 99 75 // Cluster from which to get threads 100 cluster * cltr;76 struct cluster * cltr; 101 77 102 78 // Name of the processor … … 124 100 bool pending_preemption; 125 101 102 // Idle lock 103 104 // Link lists fields 105 struct { 106 struct processor * next; 107 struct processor * prev; 108 } node; 109 126 110 #ifdef __CFA_DEBUG__ 127 111 // Last function to enable preemption on this processor … … 130 114 }; 131 115 132 void ?{}(processor & this, const char * name, cluster & cltr);116 void ?{}(processor & this, const char * name, struct cluster & cltr); 133 117 void ^?{}(processor & this); 134 118 135 119 static inline void ?{}(processor & this) { this{ "Anonymous Processor", *mainCluster}; } 136 static inline void ?{}(processor & this, cluster & cltr) { this{ "Anonymous Processor", cltr}; }120 static inline void ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; } 137 121 static inline void ?{}(processor & this, const char * name) { this{name, *mainCluster }; } 122 123 static inline [processor *&, processor *& ] __get( processor & this ) { 124 return this.node.[next, prev]; 125 } 126 127 //----------------------------------------------------------------------------- 128 // Cluster 129 struct cluster { 130 // Ready queue locks 131 __spinlock_t ready_queue_lock; 132 133 // Ready queue for threads 134 __queue_t(thread_desc) ready_queue; 135 136 // Name of the cluster 137 const char * name; 138 139 // Preemption rate on this cluster 140 Duration preemption_rate; 141 142 // List of processors 143 __spinlock_t proc_list_lock; 144 __dllist_t(struct processor) procs; 145 __dllist_t(struct processor) idles; 146 147 // Link lists fields 148 struct { 149 cluster * next; 150 cluster * prev; 151 } node; 152 }; 153 extern Duration default_preemption(); 154 155 void ?{} (cluster & this, const char * name, Duration preemption_rate); 156 void ^?{}(cluster & this); 157 158 static inline void ?{} (cluster & this) { this{"Anonymous Cluster", default_preemption()}; } 159 static inline void ?{} (cluster & this, Duration preemption_rate) { this{"Anonymous Cluster", preemption_rate}; } 160 static inline void ?{} (cluster & this, const char * name) { this{name, default_preemption()}; } 161 162 static inline [cluster *&, cluster *& ] __get( cluster & this ) { 163 return this.node.[next, prev]; 164 } 138 165 139 166 // Local Variables: // -
src/libcfa/concurrency/kernel.c
r7d0a3ba r358cba0 49 49 thread_desc * mainThread; 50 50 51 struct { __dllist_t(thread_desc) list; __spinlock_t lock; } global_threads ; 52 struct { __dllist_t(cluster ) list; __spinlock_t lock; } global_clusters; 53 51 54 //----------------------------------------------------------------------------- 52 55 // Global state … … 56 59 // volatile thread_local unsigned short disable_preempt_count = 1; 57 60 58 thread_local struct KernelThreadData kernelT hreadData= {61 thread_local struct KernelThreadData kernelTLS = { 59 62 NULL, 60 63 NULL, … … 117 120 self_mon_p = &self_mon; 118 121 next = NULL; 119 __cfaabi_dbg_debug_do( 120 dbg_next = NULL; 121 dbg_prev = NULL; 122 __cfaabi_dbg_thread_register(&this); 123 ) 122 123 node.next = NULL; 124 node.prev = NULL; 125 doregister(this); 124 126 125 127 monitors{ &self_mon_p, 1, (fptr_t)0 }; … … 155 157 terminate(&this); 156 158 verify(this.do_terminate); 157 verify( TL_GET( this_processor )!= &this);159 verify( kernelTLS.this_processor != &this); 158 160 P( terminated ); 159 verify( TL_GET( this_processor )!= &this);161 verify( kernelTLS.this_processor != &this); 160 162 pthread_join( kernel_thread, NULL ); 161 163 } … … 167 169 ready_queue{}; 168 170 ready_queue_lock{}; 171 172 procs{ __get }; 173 idles{ __get }; 174 175 doregister(this); 169 176 } 170 177 171 178 void ^?{}(cluster & this) { 172 179 unregister(this); 173 180 } 174 181 … … 183 190 __cfaabi_dbg_print_safe("Kernel : core %p starting\n", this); 184 191 192 doregister(this->cltr, this); 193 185 194 { 186 195 // Setup preemption data … … 196 205 if(readyThread) 197 206 { 198 verify( ! TL_GET( preemption_state ).enabled );207 verify( ! kernelTLS.preemption_state.enabled ); 199 208 200 209 runThread(this, readyThread); 201 210 202 verify( ! TL_GET( preemption_state ).enabled );211 verify( ! kernelTLS.preemption_state.enabled ); 203 212 204 213 //Some actions need to be taken from the kernel … … 216 225 } 217 226 227 unregister(this->cltr, this); 228 218 229 V( this->terminated ); 219 230 … … 221 232 } 222 233 234 // KERNEL ONLY 223 235 // runThread runs a thread by context switching 224 236 // from the processor coroutine to the target thread … … 228 240 coroutine_desc * thrd_cor = dst->curr_cor; 229 241 230 // Reset the terminating actions here242 // Reset the terminating actions here 231 243 this->finish.action_code = No_Action; 232 244 233 // Update global state234 TL_SET( this_thread, dst );245 // Update global state 246 kernelTLS.this_thread = dst; 235 247 236 248 // Context Switch to the thread … … 239 251 } 240 252 253 // KERNEL_ONLY 241 254 void returnToKernel() { 242 coroutine_desc * proc_cor = get_coroutine( TL_GET( this_processor )->runner);243 coroutine_desc * thrd_cor = TL_GET( this_thread )->curr_cor = TL_GET( this_coroutine );255 coroutine_desc * proc_cor = get_coroutine(kernelTLS.this_processor->runner); 256 coroutine_desc * thrd_cor = kernelTLS.this_thread->curr_cor = kernelTLS.this_coroutine; 244 257 ThreadCtxSwitch(thrd_cor, proc_cor); 245 258 } 246 259 260 // KERNEL_ONLY 247 261 // Once a thread has finished running, some of 248 262 // its final actions must be executed from the kernel 249 263 void finishRunning(processor * this) with( this->finish ) { 250 264 if( action_code == Release ) { 251 verify( ! TL_GET( preemption_state ).enabled );265 verify( ! kernelTLS.preemption_state.enabled ); 252 266 unlock( *lock ); 253 267 } … … 256 270 } 257 271 else if( action_code == Release_Schedule ) { 258 verify( ! TL_GET( preemption_state ).enabled );272 verify( ! kernelTLS.preemption_state.enabled ); 259 273 unlock( *lock ); 260 274 ScheduleThread( thrd ); 261 275 } 262 276 else if( action_code == Release_Multi ) { 263 verify( ! TL_GET( preemption_state ).enabled );277 verify( ! kernelTLS.preemption_state.enabled ); 264 278 for(int i = 0; i < lock_count; i++) { 265 279 unlock( *locks[i] ); … … 285 299 } 286 300 301 // KERNEL_ONLY 287 302 // Context invoker for processors 288 303 // This is the entry point for processors (kernel threads) … … 290 305 void * CtxInvokeProcessor(void * arg) { 291 306 processor * proc = (processor *) arg; 292 TL_SET( this_processor, proc );293 TL_SET( this_coroutine, NULL );294 TL_SET( this_thread, NULL );295 TL_GET( preemption_state ).[enabled, disable_count] = [false, 1];307 kernelTLS.this_processor = proc; 308 kernelTLS.this_coroutine = NULL; 309 kernelTLS.this_thread = NULL; 310 kernelTLS.preemption_state.[enabled, disable_count] = [false, 1]; 296 311 // SKULLDUGGERY: We want to create a context for the processor coroutine 297 312 // which is needed for the 2-step context switch. However, there is no reason … … 305 320 306 321 //Set global state 307 TL_SET( this_coroutine, get_coroutine(proc->runner));308 TL_SET( this_thread, NULL );322 kernelTLS.this_coroutine = get_coroutine(proc->runner); 323 kernelTLS.this_thread = NULL; 309 324 310 325 //We now have a proper context from which to schedule threads … … 333 348 } 334 349 350 // KERNEL_ONLY 335 351 void kernel_first_resume(processor * this) { 336 coroutine_desc * src = TL_GET( this_coroutine );352 coroutine_desc * src = kernelTLS.this_coroutine; 337 353 coroutine_desc * dst = get_coroutine(this->runner); 338 354 339 verify( ! TL_GET( preemption_state ).enabled );355 verify( ! kernelTLS.preemption_state.enabled ); 340 356 341 357 create_stack(&dst->stack, dst->stack.size); 342 358 CtxStart(&this->runner, CtxInvokeCoroutine); 343 359 344 verify( ! TL_GET( preemption_state ).enabled );360 verify( ! kernelTLS.preemption_state.enabled ); 345 361 346 362 dst->last = src; … … 351 367 352 368 // set new coroutine that task is executing 353 TL_SET( this_coroutine, dst );369 kernelTLS.this_coroutine = dst; 354 370 355 371 // SKULLDUGGERY normally interrupts are enable before leaving a coroutine ctxswitch. … … 368 384 src->state = Active; 369 385 370 verify( ! TL_GET( preemption_state ).enabled );386 verify( ! kernelTLS.preemption_state.enabled ); 371 387 } 372 388 373 389 //----------------------------------------------------------------------------- 374 390 // Scheduler routines 391 392 // KERNEL ONLY 375 393 void ScheduleThread( thread_desc * thrd ) { 376 // if( ! thrd ) return;377 394 verify( thrd ); 378 395 verify( thrd->self_cor.state != Halted ); 379 396 380 verify( ! TL_GET( preemption_state ).enabled );397 verify( ! kernelTLS.preemption_state.enabled ); 381 398 382 399 verifyf( thrd->next == NULL, "Expected null got %p", thrd->next ); … … 388 405 } 389 406 390 verify( ! TL_GET( preemption_state ).enabled ); 391 } 392 407 verify( ! kernelTLS.preemption_state.enabled ); 408 } 409 410 // KERNEL ONLY 393 411 thread_desc * nextThread(cluster * this) with( *this ) { 394 verify( ! TL_GET( preemption_state ).enabled );412 verify( ! kernelTLS.preemption_state.enabled ); 395 413 lock( ready_queue_lock __cfaabi_dbg_ctx2 ); 396 414 thread_desc * head = pop_head( ready_queue ); 397 415 unlock( ready_queue_lock ); 398 verify( ! TL_GET( preemption_state ).enabled );416 verify( ! kernelTLS.preemption_state.enabled ); 399 417 return head; 400 418 } … … 402 420 void BlockInternal() { 403 421 disable_interrupts(); 404 verify( ! TL_GET( preemption_state ).enabled );422 verify( ! kernelTLS.preemption_state.enabled ); 405 423 returnToKernel(); 406 verify( ! TL_GET( preemption_state ).enabled );424 verify( ! kernelTLS.preemption_state.enabled ); 407 425 enable_interrupts( __cfaabi_dbg_ctx ); 408 426 } … … 410 428 void BlockInternal( __spinlock_t * lock ) { 411 429 disable_interrupts(); 412 with( * TL_GET( this_processor )) {430 with( *kernelTLS.this_processor ) { 413 431 finish.action_code = Release; 414 432 finish.lock = lock; 415 433 } 416 434 417 verify( ! TL_GET( preemption_state ).enabled );435 verify( ! kernelTLS.preemption_state.enabled ); 418 436 returnToKernel(); 419 verify( ! TL_GET( preemption_state ).enabled );437 verify( ! kernelTLS.preemption_state.enabled ); 420 438 421 439 enable_interrupts( __cfaabi_dbg_ctx ); … … 424 442 void BlockInternal( thread_desc * thrd ) { 425 443 disable_interrupts(); 426 with( * TL_GET( this_processor )) {444 with( * kernelTLS.this_processor ) { 427 445 finish.action_code = Schedule; 428 446 finish.thrd = thrd; 429 447 } 430 448 431 verify( ! TL_GET( preemption_state ).enabled );449 verify( ! kernelTLS.preemption_state.enabled ); 432 450 returnToKernel(); 433 verify( ! TL_GET( preemption_state ).enabled );451 verify( ! kernelTLS.preemption_state.enabled ); 434 452 435 453 enable_interrupts( __cfaabi_dbg_ctx ); … … 439 457 assert(thrd); 440 458 disable_interrupts(); 441 with( * TL_GET( this_processor )) {459 with( * kernelTLS.this_processor ) { 442 460 finish.action_code = Release_Schedule; 443 461 finish.lock = lock; … … 445 463 } 446 464 447 verify( ! TL_GET( preemption_state ).enabled );465 verify( ! kernelTLS.preemption_state.enabled ); 448 466 returnToKernel(); 449 verify( ! TL_GET( preemption_state ).enabled );467 verify( ! kernelTLS.preemption_state.enabled ); 450 468 451 469 enable_interrupts( __cfaabi_dbg_ctx ); … … 454 472 void BlockInternal(__spinlock_t * locks [], unsigned short count) { 455 473 disable_interrupts(); 456 with( * TL_GET( this_processor )) {474 with( * kernelTLS.this_processor ) { 457 475 finish.action_code = Release_Multi; 458 476 finish.locks = locks; … … 460 478 } 461 479 462 verify( ! TL_GET( preemption_state ).enabled );480 verify( ! kernelTLS.preemption_state.enabled ); 463 481 returnToKernel(); 464 verify( ! TL_GET( preemption_state ).enabled );482 verify( ! kernelTLS.preemption_state.enabled ); 465 483 466 484 enable_interrupts( __cfaabi_dbg_ctx ); … … 469 487 void BlockInternal(__spinlock_t * locks [], unsigned short lock_count, thread_desc * thrds [], unsigned short thrd_count) { 470 488 disable_interrupts(); 471 with( * TL_GET( this_processor )) {489 with( *kernelTLS.this_processor ) { 472 490 finish.action_code = Release_Multi_Schedule; 473 491 finish.locks = locks; … … 477 495 } 478 496 479 verify( ! TL_GET( preemption_state ).enabled );497 verify( ! kernelTLS.preemption_state.enabled ); 480 498 returnToKernel(); 481 verify( ! TL_GET( preemption_state ).enabled );499 verify( ! kernelTLS.preemption_state.enabled ); 482 500 483 501 enable_interrupts( __cfaabi_dbg_ctx ); 484 502 } 485 503 504 // KERNEL ONLY 486 505 void LeaveThread(__spinlock_t * lock, thread_desc * thrd) { 487 verify( ! TL_GET( preemption_state ).enabled );488 with( * TL_GET( this_processor )) {506 verify( ! kernelTLS.preemption_state.enabled ); 507 with( * kernelTLS.this_processor ) { 489 508 finish.action_code = thrd ? Release_Schedule : Release; 490 509 finish.lock = lock; … … 501 520 // Kernel boot procedures 502 521 void kernel_startup(void) { 503 verify( ! TL_GET( preemption_state ).enabled );522 verify( ! kernelTLS.preemption_state.enabled ); 504 523 __cfaabi_dbg_print_safe("Kernel : Starting\n"); 524 525 global_threads. list{ __get }; 526 global_threads. lock{}; 527 global_clusters.list{ __get }; 528 global_clusters.lock{}; 505 529 506 530 // Initialize the main cluster … … 547 571 548 572 //initialize the global state variables 549 TL_SET( this_processor, mainProcessor );550 TL_SET( this_thread, mainThread );551 TL_SET( this_coroutine, &mainThread->self_cor );573 kernelTLS.this_processor = mainProcessor; 574 kernelTLS.this_thread = mainThread; 575 kernelTLS.this_coroutine = &mainThread->self_cor; 552 576 553 577 // Enable preemption … … 561 585 // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that 562 586 // mainThread is on the ready queue when this call is made. 563 kernel_first_resume( TL_GET( this_processor ));587 kernel_first_resume( kernelTLS.this_processor ); 564 588 565 589 … … 568 592 __cfaabi_dbg_print_safe("Kernel : Started\n--------------------------------------------------\n\n"); 569 593 570 verify( ! TL_GET( preemption_state ).enabled );594 verify( ! kernelTLS.preemption_state.enabled ); 571 595 enable_interrupts( __cfaabi_dbg_ctx ); 572 verify( TL_GET( preemption_state ).enabled);596 verify( TL_GET( preemption_state.enabled ) ); 573 597 } 574 598 … … 576 600 __cfaabi_dbg_print_safe("\n--------------------------------------------------\nKernel : Shutting down\n"); 577 601 578 verify( TL_GET( preemption_state ).enabled);602 verify( TL_GET( preemption_state.enabled ) ); 579 603 disable_interrupts(); 580 verify( ! TL_GET( preemption_state ).enabled );604 verify( ! kernelTLS.preemption_state.enabled ); 581 605 582 606 // SKULLDUGGERY: Notify the mainProcessor it needs to terminates. … … 604 628 605 629 //============================================================================================= 630 // Kernel Quiescing 631 //============================================================================================= 632 633 // void halt(processor * this) with( this ) { 634 // pthread_mutex_lock( &idle.lock ); 635 636 637 638 // // SKULLDUGGERY: Even if spurious wake-up is a thing 639 // // spuriously waking up a kernel thread is not a big deal 640 // // if it is very rare. 641 // pthread_cond_wait( &idle.cond, &idle.lock); 642 // pthread_mutex_unlock( &idle.lock ); 643 // } 644 645 // void wake(processor * this) with( this ) { 646 // pthread_mutex_lock (&idle.lock); 647 // pthread_cond_signal (&idle.cond); 648 // pthread_mutex_unlock(&idle.lock); 649 // } 650 651 //============================================================================================= 606 652 // Unexpected Terminating logic 607 653 //============================================================================================= … … 609 655 610 656 static __spinlock_t kernel_abort_lock; 611 static __spinlock_t kernel_debug_lock;612 657 static bool kernel_abort_called = false; 613 658 614 void * kernel_abort 659 void * kernel_abort(void) __attribute__ ((__nothrow__)) { 615 660 // abort cannot be recursively entered by the same or different processors because all signal handlers return when 616 661 // the globalAbort flag is true. … … 618 663 619 664 // first task to abort ? 620 if ( ! kernel_abort_called ) { // not first task to abort ? 665 if ( kernel_abort_called ) { // not first task to abort ? 666 unlock( kernel_abort_lock ); 667 668 sigset_t mask; 669 sigemptyset( &mask ); 670 sigaddset( &mask, SIGALRM ); // block SIGALRM signals 671 sigsuspend( &mask ); // block the processor to prevent further damage during abort 672 _exit( EXIT_FAILURE ); // if processor unblocks before it is killed, terminate it 673 } 674 else { 621 675 kernel_abort_called = true; 622 676 unlock( kernel_abort_lock ); 623 677 } 624 else { 625 unlock( kernel_abort_lock ); 626 627 sigset_t mask; 628 sigemptyset( &mask ); 629 sigaddset( &mask, SIGALRM ); // block SIGALRM signals 630 sigaddset( &mask, SIGUSR1 ); // block SIGUSR1 signals 631 sigsuspend( &mask ); // block the processor to prevent further damage during abort 632 _exit( EXIT_FAILURE ); // if processor unblocks before it is killed, terminate it 633 } 634 635 return TL_GET( this_thread ); 678 679 return kernelTLS.this_thread; 636 680 } 637 681 … … 639 683 thread_desc * thrd = kernel_data; 640 684 641 int len = snprintf( abort_text, abort_text_size, "Error occurred while executing task %.256s (%p)", thrd->self_cor.name, thrd ); 642 __cfaabi_dbg_bits_write( abort_text, len ); 643 644 if ( get_coroutine(thrd) != TL_GET( this_coroutine ) ) { 645 len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", TL_GET( this_coroutine )->name, TL_GET( this_coroutine ) ); 685 if(thrd) { 686 int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd ); 646 687 __cfaabi_dbg_bits_write( abort_text, len ); 688 689 if ( get_coroutine(thrd) != kernelTLS.this_coroutine ) { 690 len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", kernelTLS.this_coroutine->name, kernelTLS.this_coroutine ); 691 __cfaabi_dbg_bits_write( abort_text, len ); 692 } 693 else { 694 __cfaabi_dbg_bits_write( ".\n", 2 ); 695 } 647 696 } 648 697 else { 649 __cfaabi_dbg_bits_write( ".\n", 2);698 int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" ); 650 699 } 651 700 } 652 701 653 702 int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) { 654 return get_coroutine(TL_GET( this_thread )) == get_coroutine(mainThread) ? 4 : 2; 655 } 703 return get_coroutine(kernelTLS.this_thread) == get_coroutine(mainThread) ? 4 : 2; 704 } 705 706 static __spinlock_t kernel_debug_lock; 656 707 657 708 extern "C" { … … 682 733 if ( count < 0 ) { 683 734 // queue current task 684 append( waiting, (thread_desc *)TL_GET( this_thread ));735 append( waiting, kernelTLS.this_thread ); 685 736 686 737 // atomically release spin lock and block … … 708 759 709 760 //----------------------------------------------------------------------------- 761 // Global Queues 762 void doregister( thread_desc & thrd ) { 763 // lock ( global_thread.lock ); 764 // push_front( global_thread.list, thrd ); 765 // unlock ( global_thread.lock ); 766 } 767 768 void unregister( thread_desc & thrd ) { 769 // lock ( global_thread.lock ); 770 // remove( global_thread.list, thrd ); 771 // unlock( global_thread.lock ); 772 } 773 774 void doregister( cluster & cltr ) { 775 // lock ( global_cluster.lock ); 776 // push_front( global_cluster.list, cltr ); 777 // unlock ( global_cluster.lock ); 778 } 779 780 void unregister( cluster & cltr ) { 781 // lock ( global_cluster.lock ); 782 // remove( global_cluster.list, cltr ); 783 // unlock( global_cluster.lock ); 784 } 785 786 787 void doregister( cluster * cltr, processor * proc ) { 788 // lock (cltr->proc_list_lock __cfaabi_dbg_ctx2); 789 // push_front(cltr->procs, *proc); 790 // unlock (cltr->proc_list_lock); 791 } 792 793 void unregister( cluster * cltr, processor * proc ) { 794 // lock (cltr->proc_list_lock __cfaabi_dbg_ctx2); 795 // remove(cltr->procs, *proc ); 796 // unlock(cltr->proc_list_lock); 797 } 798 799 //----------------------------------------------------------------------------- 710 800 // Debug 711 801 __cfaabi_dbg_debug_do( 712 struct {713 thread_desc * tail;714 } __cfaabi_dbg_thread_list = { NULL };715 716 void __cfaabi_dbg_thread_register( thread_desc * thrd ) {717 if( !__cfaabi_dbg_thread_list.tail ) {718 __cfaabi_dbg_thread_list.tail = thrd;719 return;720 }721 __cfaabi_dbg_thread_list.tail->dbg_next = thrd;722 thrd->dbg_prev = __cfaabi_dbg_thread_list.tail;723 __cfaabi_dbg_thread_list.tail = thrd;724 }725 726 void __cfaabi_dbg_thread_unregister( thread_desc * thrd ) {727 thread_desc * prev = thrd->dbg_prev;728 thread_desc * next = thrd->dbg_next;729 730 if( next ) { next->dbg_prev = prev; }731 else {732 assert( __cfaabi_dbg_thread_list.tail == thrd );733 __cfaabi_dbg_thread_list.tail = prev;734 }735 736 if( prev ) { prev->dbg_next = next; }737 738 thrd->dbg_prev = NULL;739 thrd->dbg_next = NULL;740 }741 742 802 void __cfaabi_dbg_record(__spinlock_t & this, const char * prev_name) { 743 803 this.prev_name = prev_name; 744 this.prev_thrd = TL_GET( this_thread );804 this.prev_thrd = kernelTLS.this_thread; 745 805 } 746 806 ) -
src/libcfa/concurrency/kernel_private.h
r7d0a3ba r358cba0 100 100 #define KERNEL_STORAGE(T,X) static char storage_##X[sizeof(T)] 101 101 102 103 void doregister( struct thread_desc & thrd ); 104 void unregister( struct thread_desc & thrd ); 105 106 void doregister( struct cluster & cltr ); 107 void unregister( struct cluster & cltr ); 108 109 void doregister( struct cluster * cltr, struct processor * proc ); 110 void unregister( struct cluster * cltr, struct processor * proc ); 111 102 112 // Local Variables: // 103 113 // mode: c // -
src/libcfa/concurrency/monitor.c
r7d0a3ba r358cba0 85 85 // Lock the monitor spinlock 86 86 lock( this->lock __cfaabi_dbg_ctx2 ); 87 thread_desc * thrd = TL_GET( this_thread ); 87 // Interrupts disable inside critical section 88 thread_desc * thrd = kernelTLS.this_thread; 88 89 89 90 __cfaabi_dbg_print_safe( "Kernel : %10p Entering mon %p (%p)\n", thrd, this, this->owner); … … 134 135 // Lock the monitor spinlock 135 136 lock( this->lock __cfaabi_dbg_ctx2 ); 136 thread_desc * thrd = TL_GET( this_thread ); 137 // Interrupts disable inside critical section 138 thread_desc * thrd = kernelTLS.this_thread; 137 139 138 140 __cfaabi_dbg_print_safe( "Kernel : %10p Entering dtor for mon %p (%p)\n", thrd, this, this->owner); … … 168 170 169 171 // Create the node specific to this wait operation 170 wait_ctx_primed( TL_GET( this_thread ), 0 )172 wait_ctx_primed( thrd, 0 ) 171 173 172 174 // Some one else has the monitor, wait for him to finish and then run … … 179 181 __cfaabi_dbg_print_safe( "Kernel : blocking \n" ); 180 182 181 wait_ctx( TL_GET( this_thread ), 0 )183 wait_ctx( thrd, 0 ) 182 184 this->dtor_node = &waiter; 183 185 … … 199 201 lock( this->lock __cfaabi_dbg_ctx2 ); 200 202 201 __cfaabi_dbg_print_safe( "Kernel : %10p Leaving mon %p (%p)\n", TL_GET( this_thread ), this, this->owner);202 203 verifyf( TL_GET( this_thread ) == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", TL_GET( this_thread ), this->owner, this->recursion, this );203 __cfaabi_dbg_print_safe( "Kernel : %10p Leaving mon %p (%p)\n", kernelTLS.this_thread, this, this->owner); 204 205 verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this ); 204 206 205 207 // Leaving a recursion level, decrement the counter … … 289 291 // Sorts monitors before entering 290 292 void ?{}( monitor_guard_t & this, monitor_desc * m [], __lock_size_t count, fptr_t func ) { 293 thread_desc * thrd = TL_GET( this_thread ); 294 291 295 // Store current array 292 296 this.m = m; … … 297 301 298 302 // Save previous thread context 299 this.prev = TL_GET( this_thread )->monitors;303 this.prev = thrd->monitors; 300 304 301 305 // Update thread context (needed for conditions) 302 ( TL_GET( this_thread )->monitors){m, count, func};306 (thrd->monitors){m, count, func}; 303 307 304 308 // __cfaabi_dbg_print_safe( "MGUARD : enter %d\n", count); … … 328 332 // Sorts monitors before entering 329 333 void ?{}( monitor_dtor_guard_t & this, monitor_desc * m [], fptr_t func ) { 334 // optimization 335 thread_desc * thrd = TL_GET( this_thread ); 336 330 337 // Store current array 331 338 this.m = *m; 332 339 333 340 // Save previous thread context 334 this.prev = TL_GET( this_thread )->monitors;341 this.prev = thrd->monitors; 335 342 336 343 // Update thread context (needed for conditions) 337 ( TL_GET( this_thread )->monitors){m, 1, func};344 (thrd->monitors){m, 1, func}; 338 345 339 346 __enter_monitor_dtor( this.m, func ); … … 473 480 474 481 // Create the node specific to this wait operation 475 wait_ctx_primed( TL_GET( this_thread ), 0 )482 wait_ctx_primed( kernelTLS.this_thread, 0 ) 476 483 477 484 //save contexts … … 566 573 567 574 // Create the node specific to this wait operation 568 wait_ctx_primed( TL_GET( this_thread ), 0 );575 wait_ctx_primed( kernelTLS.this_thread, 0 ); 569 576 570 577 // Save monitor states … … 612 619 613 620 // Create the node specific to this wait operation 614 wait_ctx_primed( TL_GET( this_thread ), 0 );621 wait_ctx_primed( kernelTLS.this_thread, 0 ); 615 622 616 623 monitor_save; … … 618 625 619 626 for( __lock_size_t i = 0; i < count; i++) { 620 verify( monitors[i]->owner == TL_GET( this_thread ));627 verify( monitors[i]->owner == kernelTLS.this_thread ); 621 628 } 622 629 -
src/libcfa/concurrency/preemption.c
r7d0a3ba r358cba0 149 149 // Disable interrupts by incrementing the counter 150 150 void disable_interrupts() { 151 with( TL_GET( preemption_state )) {151 with( kernelTLS.preemption_state ) { 152 152 enabled = false; 153 153 __attribute__((unused)) unsigned short new_val = disable_count + 1; … … 160 160 // If counter reaches 0, execute any pending CtxSwitch 161 161 void enable_interrupts( __cfaabi_dbg_ctx_param ) { 162 processor * proc = TL_GET( this_processor ); // Cache the processor now since interrupts can start happening after the atomic add163 thread_desc * thrd = TL_GET( this_thread ); // Cache the thread now since interrupts can start happening after the atomic add164 165 with( TL_GET( preemption_state )){162 processor * proc = kernelTLS.this_processor; // Cache the processor now since interrupts can start happening after the atomic add 163 thread_desc * thrd = kernelTLS.this_thread; // Cache the thread now since interrupts can start happening after the atomic add 164 165 with( kernelTLS.preemption_state ){ 166 166 unsigned short prev = disable_count; 167 167 disable_count -= 1; … … 185 185 // Don't execute any pending CtxSwitch even if counter reaches 0 186 186 void enable_interrupts_noPoll() { 187 unsigned short prev = TL_GET( preemption_state ).disable_count;188 TL_GET( preemption_state ).disable_count -= 1;187 unsigned short prev = kernelTLS.preemption_state.disable_count; 188 kernelTLS.preemption_state.disable_count -= 1; 189 189 verifyf( prev != 0u, "Incremented from %u\n", prev ); // If this triggers someone is enabled already enabled interrupts 190 190 if( prev == 1 ) { 191 TL_GET( preemption_state ).enabled = true;191 kernelTLS.preemption_state.enabled = true; 192 192 } 193 193 } … … 234 234 } 235 235 236 236 // KERNEL ONLY 237 237 // Check if a CtxSwitch signal handler shoud defer 238 238 // If true : preemption is safe 239 239 // If false : preemption is unsafe and marked as pending 240 240 static inline bool preemption_ready() { 241 bool ready = TL_GET( preemption_state ).enabled && !TL_GET( preemption_state ).in_progress; // Check if preemption is safe 242 TL_GET( this_processor )->pending_preemption = !ready; // Adjust the pending flag accordingly 241 // Check if preemption is safe 242 bool ready = kernelTLS.preemption_state.enabled && ! kernelTLS.preemption_state.in_progress; 243 244 // Adjust the pending flag accordingly 245 kernelTLS.this_processor->pending_preemption = !ready; 243 246 return ready; 244 247 } … … 254 257 255 258 // Start with preemption disabled until ready 256 TL_GET( preemption_state ).enabled = false;257 TL_GET( preemption_state ).disable_count = 1;259 kernelTLS.preemption_state.enabled = false; 260 kernelTLS.preemption_state.disable_count = 1; 258 261 259 262 // Initialize the event kernel … … 320 323 // before the kernel thread has even started running. When that happens an iterrupt 321 324 // we a null 'this_processor' will be caught, just ignore it. 322 if(! TL_GET( this_processor )) return;325 if(! kernelTLS.this_processor ) return; 323 326 324 327 choose(sfp->si_value.sival_int) { 325 328 case PREEMPT_NORMAL : ;// Normal case, nothing to do here 326 case PREEMPT_TERMINATE: verify( TL_GET( this_processor )->do_terminate);329 case PREEMPT_TERMINATE: verify( kernelTLS.this_processor->do_terminate); 327 330 default: 328 331 abort( "internal error, signal value is %d", sfp->si_value.sival_int ); … … 332 335 if( !preemption_ready() ) { return; } 333 336 334 __cfaabi_dbg_print_buffer_decl( " KERNEL: preempting core %p (%p).\n", TL_GET( this_processor ), TL_GET( this_thread ) ); 335 336 TL_GET( preemption_state ).in_progress = true; // Sync flag : prevent recursive calls to the signal handler 337 signal_unblock( SIGUSR1 ); // We are about to CtxSwitch out of the signal handler, let other handlers in 338 TL_GET( preemption_state ).in_progress = false; // Clear the in progress flag 337 __cfaabi_dbg_print_buffer_decl( " KERNEL: preempting core %p (%p).\n", kernelTLS.this_processor, kernelTLS.this_thread ); 338 339 // Sync flag : prevent recursive calls to the signal handler 340 kernelTLS.preemption_state.in_progress = true; 341 342 // We are about to CtxSwitch out of the signal handler, let other handlers in 343 signal_unblock( SIGUSR1 ); 344 345 // TODO: this should go in finish action 346 // Clear the in progress flag 347 kernelTLS.preemption_state.in_progress = false; 339 348 340 349 // Preemption can occur here 341 350 342 BlockInternal( (thread_desc*)TL_GET( this_thread )); // Do the actual CtxSwitch351 BlockInternal( kernelTLS.this_thread ); // Do the actual CtxSwitch 343 352 } 344 353 … … 348 357 // Block sigalrms to control when they arrive 349 358 sigset_t mask; 359 sigfillset(&mask); 360 if ( pthread_sigmask( SIG_BLOCK, &mask, NULL ) == -1 ) { 361 abort( "internal error, pthread_sigmask" ); 362 } 363 350 364 sigemptyset( &mask ); 351 365 sigaddset( &mask, SIGALRM ); 352 353 if ( pthread_sigmask( SIG_BLOCK, &mask, NULL ) == -1 ) {354 abort( "internal error, pthread_sigmask" );355 }356 366 357 367 // Main loop … … 409 419 410 420 void __cfaabi_check_preemption() { 411 bool ready = TL_GET( preemption_state ).enabled;421 bool ready = kernelTLS.preemption_state.enabled; 412 422 if(!ready) { abort("Preemption should be ready"); } 413 423 -
src/libcfa/concurrency/thread.c
r7d0a3ba r358cba0 39 39 curr_cluster = &cl; 40 40 next = NULL; 41 __cfaabi_dbg_debug_do( 42 dbg_next = NULL; 43 dbg_prev = NULL; 44 __cfaabi_dbg_thread_register(&this); 45 ) 41 42 node.next = NULL; 43 node.prev = NULL; 44 doregister(this); 46 45 47 46 monitors{ &self_mon_p, 1, (fptr_t)0 }; … … 49 48 50 49 void ^?{}(thread_desc& this) with( this ) { 50 unregister(this); 51 51 ^self_cor{}; 52 52 } … … 81 81 disable_interrupts(); 82 82 create_stack(&thrd_c->stack, thrd_c->stack.size); 83 TL_SET( this_coroutine, thrd_c );83 kernelTLS.this_coroutine = thrd_c; 84 84 CtxStart(&this, CtxInvokeThread); 85 85 assert( thrd_c->last->stack.context ); … … 91 91 92 92 extern "C" { 93 // KERNEL ONLY 93 94 void __finish_creation(void) { 94 coroutine_desc* thrd_c = TL_GET( this_coroutine );95 coroutine_desc* thrd_c = kernelTLS.this_coroutine; 95 96 ThreadCtxSwitch( thrd_c, thrd_c->last ); 96 97 } … … 98 99 99 100 void yield( void ) { 100 verify( TL_GET( preemption_state ).enabled ); 101 // Safety note : This could cause some false positives due to preemption 102 verify( TL_GET( preemption_state.enabled ) ); 101 103 BlockInternal( TL_GET( this_thread ) ); 102 verify( TL_GET( preemption_state ).enabled ); 104 // Safety note : This could cause some false positives due to preemption 105 verify( TL_GET( preemption_state.enabled ) ); 103 106 } 104 107 … … 109 112 } 110 113 114 // KERNEL ONLY 111 115 void ThreadCtxSwitch(coroutine_desc* src, coroutine_desc* dst) { 112 116 // set state of current coroutine to inactive … … 116 120 // set new coroutine that the processor is executing 117 121 // and context switch to it 118 TL_SET( this_coroutine, dst );122 kernelTLS.this_coroutine = dst; 119 123 assert( src->stack.context ); 120 124 CtxSwitch( src->stack.context, dst->stack.context ); 121 TL_SET( this_coroutine, src );125 kernelTLS.this_coroutine = src; 122 126 123 127 // set state of new coroutine to active
Note:
See TracChangeset
for help on using the changeset viewer.