- File:
-
- 1 edited
-
libcfa/src/concurrency/kernel.cfa (modified) (26 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/concurrency/kernel.cfa
r1f45c7d r24e321c 22 22 #include <errno.h> 23 23 #include <stdio.h> 24 #include <string.h> 24 25 #include <signal.h> 25 26 #include <unistd.h> … … 31 32 #include "kernel_private.hfa" 32 33 #include "preemption.hfa" 34 #include "strstream.hfa" 35 #include "device/cpu.hfa" 33 36 34 37 //Private includes … … 110 113 #endif 111 114 112 extern $thread* mainThread;115 extern thread$ * mainThread; 113 116 extern processor * mainProcessor; 114 117 115 118 //----------------------------------------------------------------------------- 116 119 // Kernel Scheduling logic 117 static $thread* __next_thread(cluster * this);118 static $thread* __next_thread_slow(cluster * this);119 static inline bool __must_unpark( $thread* thrd ) __attribute((nonnull(1)));120 static void __run_thread(processor * this, $thread* dst);120 static thread$ * __next_thread(cluster * this); 121 static thread$ * __next_thread_slow(cluster * this); 122 static inline bool __must_unpark( thread$ * thrd ) __attribute((nonnull(1))); 123 static void __run_thread(processor * this, thread$ * dst); 121 124 static void __wake_one(cluster * cltr); 122 125 … … 181 184 __cfadbg_print_safe(runtime_core, "Kernel : core %p started\n", this); 182 185 183 $thread* readyThread = 0p;186 thread$ * readyThread = 0p; 184 187 MAIN_LOOP: 185 188 for() { … … 231 234 __cfadbg_print_safe(runtime_core, "Kernel : core %p waiting on eventfd %d\n", this, this->idle); 232 235 233 __disable_interrupts_hard(); 234 eventfd_t val; 235 eventfd_read( this->idle, &val ); 236 __enable_interrupts_hard(); 236 { 237 eventfd_t val; 238 ssize_t ret = read( this->idle, &val, sizeof(val) ); 239 if(ret < 0) { 240 switch((int)errno) { 241 case EAGAIN: 242 #if EAGAIN != EWOULDBLOCK 243 case EWOULDBLOCK: 244 #endif 245 case EINTR: 246 // No need to do anything special here, just assume it's a legitimate wake-up 247 break; 248 default: 249 abort( "KERNEL : internal error, read failure on idle eventfd, error(%d) %s.", (int)errno, strerror( (int)errno ) ); 250 } 251 } 252 } 237 253 238 254 #if !defined(__CFA_NO_STATISTICS__) … … 325 341 } 326 342 327 __STATS( if(this->print_halts) __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 0\n", this->unique_id, rdtscl()); )343 __STATS( if(this->print_halts) __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 0\n", this->unique_id, rdtscl()); ) 328 344 __cfadbg_print_safe(runtime_core, "Kernel : core %p waiting on eventfd %d\n", this, this->idle); 329 345 330 // __disable_interrupts_hard(); 331 eventfd_t val; 332 eventfd_read( this->idle, &val ); 333 // __enable_interrupts_hard(); 346 { 347 eventfd_t val; 348 ssize_t ret = read( this->idle, &val, sizeof(val) ); 349 if(ret < 0) { 350 switch((int)errno) { 351 case EAGAIN: 352 #if EAGAIN != EWOULDBLOCK 353 case EWOULDBLOCK: 354 #endif 355 case EINTR: 356 // No need to do anything special here, just assume it's a legitimate wake-up 357 break; 358 default: 359 abort( "KERNEL : internal error, read failure on idle eventfd, error(%d) %s.", (int)errno, strerror( (int)errno ) ); 360 } 361 } 362 } 334 363 335 364 __STATS( if(this->print_halts) __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 1\n", this->unique_id, rdtscl()); ) … … 388 417 // runThread runs a thread by context switching 389 418 // from the processor coroutine to the target thread 390 static void __run_thread(processor * this, $thread* thrd_dst) {419 static void __run_thread(processor * this, thread$ * thrd_dst) { 391 420 /* paranoid */ verify( ! __preemption_enabled() ); 392 421 /* paranoid */ verifyf( thrd_dst->state == Ready || thrd_dst->preempted != __NO_PREEMPTION, "state : %d, preempted %d\n", thrd_dst->state, thrd_dst->preempted); … … 394 423 __builtin_prefetch( thrd_dst->context.SP ); 395 424 396 int curr = __kernel_getcpu();397 if(thrd_dst->last_cpu != curr) {398 int64_t l = thrd_dst->last_cpu;399 int64_t c = curr;400 int64_t v = (l << 32) | c;401 __push_stat( __tls_stats(), v, false, "Processor", this );402 }403 404 thrd_dst->last_cpu = curr;405 406 425 __cfadbg_print_safe(runtime_core, "Kernel : core %p running thread %p (%s)\n", this, thrd_dst, thrd_dst->self_cor.name); 407 426 408 $coroutine* proc_cor = get_coroutine(this->runner);427 coroutine$ * proc_cor = get_coroutine(this->runner); 409 428 410 429 // set state of processor coroutine to inactive … … 425 444 /* paranoid */ verify( thrd_dst->context.SP ); 426 445 /* paranoid */ verify( thrd_dst->state != Halted ); 427 /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ) || thrd_dst->curr_cor == proc_cor || thrd_dst->corctx_flag, "ERROR : Destination $thread%p has been corrupted.\n StackPointer too small.\n", thrd_dst ); // add escape condition if we are setting up the processor428 /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit) || thrd_dst->curr_cor == proc_cor || thrd_dst->corctx_flag, "ERROR : Destination $thread%p has been corrupted.\n StackPointer too large.\n", thrd_dst ); // add escape condition if we are setting up the processor446 /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ) || thrd_dst->curr_cor == proc_cor || thrd_dst->corctx_flag, "ERROR : Destination thread$ %p has been corrupted.\n StackPointer too small.\n", thrd_dst ); // add escape condition if we are setting up the processor 447 /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit) || thrd_dst->curr_cor == proc_cor || thrd_dst->corctx_flag, "ERROR : Destination thread$ %p has been corrupted.\n StackPointer too large.\n", thrd_dst ); // add escape condition if we are setting up the processor 429 448 /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_dst->canary ); 430 449 … … 438 457 439 458 /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_dst->canary ); 440 /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit) || thrd_dst->corctx_flag, "ERROR : Destination $thread%p has been corrupted.\n StackPointer too large.\n", thrd_dst );441 /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ) || thrd_dst->corctx_flag, "ERROR : Destination $thread%p has been corrupted.\n StackPointer too small.\n", thrd_dst );459 /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit) || thrd_dst->corctx_flag, "ERROR : Destination thread$ %p has been corrupted.\n StackPointer too large.\n", thrd_dst ); 460 /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ) || thrd_dst->corctx_flag, "ERROR : Destination thread$ %p has been corrupted.\n StackPointer too small.\n", thrd_dst ); 442 461 /* paranoid */ verify( thrd_dst->context.SP ); 443 462 /* paranoid */ verify( thrd_dst->curr_cluster == this->cltr ); … … 457 476 if(unlikely(thrd_dst->preempted != __NO_PREEMPTION)) { 458 477 // The thread was preempted, reschedule it and reset the flag 459 schedule_thread$( thrd_dst );478 schedule_thread$( thrd_dst, UNPARK_LOCAL ); 460 479 break RUNNING; 461 480 } … … 505 524 void returnToKernel() { 506 525 /* paranoid */ verify( ! __preemption_enabled() ); 507 $coroutine* proc_cor = get_coroutine(kernelTLS().this_processor->runner);508 $thread* thrd_src = kernelTLS().this_thread;526 coroutine$ * proc_cor = get_coroutine(kernelTLS().this_processor->runner); 527 thread$ * thrd_src = kernelTLS().this_thread; 509 528 510 529 __STATS( thrd_src->last_proc = kernelTLS().this_processor; ) … … 534 553 535 554 /* paranoid */ verify( ! __preemption_enabled() ); 536 /* paranoid */ verifyf( ((uintptr_t)thrd_src->context.SP) < ((uintptr_t)__get_stack(thrd_src->curr_cor)->base ) || thrd_src->corctx_flag, "ERROR : Returning $thread%p has been corrupted.\n StackPointer too small.\n", thrd_src );537 /* paranoid */ verifyf( ((uintptr_t)thrd_src->context.SP) > ((uintptr_t)__get_stack(thrd_src->curr_cor)->limit) || thrd_src->corctx_flag, "ERROR : Returning $thread%p has been corrupted.\n StackPointer too large.\n", thrd_src );555 /* paranoid */ verifyf( ((uintptr_t)thrd_src->context.SP) < ((uintptr_t)__get_stack(thrd_src->curr_cor)->base ) || thrd_src->corctx_flag, "ERROR : Returning thread$ %p has been corrupted.\n StackPointer too small.\n", thrd_src ); 556 /* paranoid */ verifyf( ((uintptr_t)thrd_src->context.SP) > ((uintptr_t)__get_stack(thrd_src->curr_cor)->limit) || thrd_src->corctx_flag, "ERROR : Returning thread$ %p has been corrupted.\n StackPointer too large.\n", thrd_src ); 538 557 } 539 558 … … 541 560 // Scheduler routines 542 561 // KERNEL ONLY 543 static void __schedule_thread( $thread * thrd) {562 static void __schedule_thread( thread$ * thrd, unpark_hint hint ) { 544 563 /* paranoid */ verify( ! __preemption_enabled() ); 545 564 /* paranoid */ verify( ready_schedule_islocked()); … … 561 580 // Dereference the thread now because once we push it, there is not guaranteed it's still valid. 562 581 struct cluster * cl = thrd->curr_cluster; 563 __STATS(bool outside = thrd->last_proc && thrd->last_proc != kernelTLS().this_processor; )582 __STATS(bool outside = hint == UNPARK_LOCAL && thrd->last_proc && thrd->last_proc != kernelTLS().this_processor; ) 564 583 565 584 // push the thread to the cluster ready-queue 566 push( cl, thrd, local);585 push( cl, thrd, hint ); 567 586 568 587 // variable thrd is no longer safe to use … … 589 608 } 590 609 591 void schedule_thread$( $thread * thrd) {610 void schedule_thread$( thread$ * thrd, unpark_hint hint ) { 592 611 ready_schedule_lock(); 593 __schedule_thread( thrd );612 __schedule_thread( thrd, hint ); 594 613 ready_schedule_unlock(); 595 614 } 596 615 597 616 // KERNEL ONLY 598 static inline $thread* __next_thread(cluster * this) with( *this ) {617 static inline thread$ * __next_thread(cluster * this) with( *this ) { 599 618 /* paranoid */ verify( ! __preemption_enabled() ); 600 619 601 620 ready_schedule_lock(); 602 $thread* thrd = pop_fast( this );621 thread$ * thrd = pop_fast( this ); 603 622 ready_schedule_unlock(); 604 623 … … 608 627 609 628 // KERNEL ONLY 610 static inline $thread* __next_thread_slow(cluster * this) with( *this ) {629 static inline thread$ * __next_thread_slow(cluster * this) with( *this ) { 611 630 /* paranoid */ verify( ! __preemption_enabled() ); 612 631 613 632 ready_schedule_lock(); 614 $thread* thrd;633 thread$ * thrd; 615 634 for(25) { 616 635 thrd = pop_slow( this ); … … 626 645 } 627 646 628 static inline bool __must_unpark( $thread* thrd ) {647 static inline bool __must_unpark( thread$ * thrd ) { 629 648 int old_ticket = __atomic_fetch_add(&thrd->ticket, 1, __ATOMIC_SEQ_CST); 630 649 switch(old_ticket) { … … 642 661 } 643 662 644 void __kernel_unpark( $thread * thrd) {663 void __kernel_unpark( thread$ * thrd, unpark_hint hint ) { 645 664 /* paranoid */ verify( ! __preemption_enabled() ); 646 665 /* paranoid */ verify( ready_schedule_islocked()); … … 650 669 if(__must_unpark(thrd)) { 651 670 // Wake lost the race, 652 __schedule_thread( thrd );671 __schedule_thread( thrd, hint ); 653 672 } 654 673 … … 657 676 } 658 677 659 void unpark( $thread * thrd) {678 void unpark( thread$ * thrd, unpark_hint hint ) { 660 679 if( !thrd ) return; 661 680 … … 663 682 disable_interrupts(); 664 683 // Wake lost the race, 665 schedule_thread$( thrd );684 schedule_thread$( thrd, hint ); 666 685 enable_interrupts(false); 667 686 } … … 681 700 // Should never return 682 701 void __cfactx_thrd_leave() { 683 $thread* thrd = active_thread();684 $monitor* this = &thrd->self_mon;702 thread$ * thrd = active_thread(); 703 monitor$ * this = &thrd->self_mon; 685 704 686 705 // Lock the monitor now … … 694 713 /* paranoid */ verify( kernelTLS().this_thread == thrd ); 695 714 /* paranoid */ verify( thrd->context.SP ); 696 /* paranoid */ verifyf( ((uintptr_t)thrd->context.SP) > ((uintptr_t)__get_stack(thrd->curr_cor)->limit), "ERROR : $thread%p has been corrupted.\n StackPointer too large.\n", thrd );697 /* paranoid */ verifyf( ((uintptr_t)thrd->context.SP) < ((uintptr_t)__get_stack(thrd->curr_cor)->base ), "ERROR : $thread%p has been corrupted.\n StackPointer too small.\n", thrd );715 /* paranoid */ verifyf( ((uintptr_t)thrd->context.SP) > ((uintptr_t)__get_stack(thrd->curr_cor)->limit), "ERROR : thread$ %p has been corrupted.\n StackPointer too large.\n", thrd ); 716 /* paranoid */ verifyf( ((uintptr_t)thrd->context.SP) < ((uintptr_t)__get_stack(thrd->curr_cor)->base ), "ERROR : thread$ %p has been corrupted.\n StackPointer too small.\n", thrd ); 698 717 699 718 thrd->state = Halting; … … 713 732 bool force_yield( __Preemption_Reason reason ) { 714 733 __disable_interrupts_checked(); 715 $thread* thrd = kernelTLS().this_thread;734 thread$ * thrd = kernelTLS().this_thread; 716 735 /* paranoid */ verify(thrd->state == Active); 717 736 … … 825 844 //============================================================================================= 826 845 void __kernel_abort_msg( char * abort_text, int abort_text_size ) { 827 $thread* thrd = __cfaabi_tls.this_thread;846 thread$ * thrd = __cfaabi_tls.this_thread; 828 847 829 848 if(thrd) {
Note:
See TracChangeset
for help on using the changeset viewer.