Changes in src/libcfa/concurrency/kernel.c [85b1deb:5510027]
- File:
-
- 1 edited
-
src/libcfa/concurrency/kernel.c (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/concurrency/kernel.c
r85b1deb r5510027 16 16 //C Includes 17 17 #include <stddef.h> 18 #include <errno.h>19 #include <string.h>20 18 extern "C" { 21 19 #include <stdio.h> … … 51 49 thread_desc * mainThread; 52 50 53 extern "C" { 54 struct { __dllist_t(cluster) list; __spinlock_t lock; } __cfa_dbg_global_clusters; 55 } 51 struct { __dllist_t(cluster) list; __spinlock_t lock; } global_clusters; 56 52 57 53 //----------------------------------------------------------------------------- … … 147 143 runner.proc = &this; 148 144 149 idleLock{};145 sem_init(&idleLock, 0, 0); 150 146 151 147 start( &this ); … … 153 149 154 150 void ^?{}(processor & this) with( this ){ 155 if( ! __atomic_load_n(&do_terminate, __ATOMIC_ACQUIRE)) {151 if( ! do_terminate ) { 156 152 __cfaabi_dbg_print_safe("Kernel : core %p signaling termination\n", &this); 157 158 __atomic_store_n(&do_terminate, true, __ATOMIC_RELAXED); 159 wake( &this ); 160 153 terminate(&this); 154 verify(this.do_terminate); 155 verify( kernelTLS.this_processor != &this); 161 156 P( terminated ); 162 157 verify( kernelTLS.this_processor != &this); 163 } 164 165 pthread_join( kernel_thread, NULL ); 158 pthread_join( kernel_thread, NULL ); 159 } 160 161 sem_destroy(&idleLock); 166 162 } 167 163 … … 202 198 203 199 thread_desc * readyThread = NULL; 204 for( unsigned int spin_count = 0; ! __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST); spin_count++ )200 for( unsigned int spin_count = 0; ! this->do_terminate; spin_count++ ) 205 201 { 206 202 readyThread = nextThread( this->cltr ); … … 221 217 else 222 218 { 223 // spin(this, &spin_count); 224 halt(this); 219 spin(this, &spin_count); 225 220 } 226 221 } … … 295 290 } 296 291 292 // Handles spinning logic 293 // TODO : find some strategy to put cores to sleep after some time 294 void spin(processor * this, unsigned int * spin_count) { 295 // (*spin_count)++; 296 halt(this); 297 } 298 297 299 // KERNEL_ONLY 298 300 // Context invoker for processors … … 401 403 unlock( ready_queue_lock ); 402 404 403 if( was_empty) {405 if( was_empty ) { 404 406 lock (proc_list_lock __cfaabi_dbg_ctx2); 405 407 if(idles) { 406 wake _fast(idles.head);408 wake(idles.head); 407 409 } 408 410 unlock (proc_list_lock); 409 411 } 410 else if( struct processor * idle = idles.head ) {411 wake_fast(idle);412 }413 414 412 } 415 413 … … 546 544 __cfaabi_dbg_print_safe("Kernel : Starting\n"); 547 545 548 __cfa_dbg_global_clusters.list{ __get };549 __cfa_dbg_global_clusters.lock{};546 global_clusters.list{ __get }; 547 global_clusters.lock{}; 550 548 551 549 // Initialize the main cluster … … 628 626 // When its coroutine terminates, it return control to the mainThread 629 627 // which is currently here 630 __atomic_store_n(&mainProcessor->do_terminate, true, __ATOMIC_RELEASE);628 mainProcessor->do_terminate = true; 631 629 returnToKernel(); 632 mainThread->self_cor.state = Halted;633 630 634 631 // THE SYSTEM IS NOW COMPLETELY STOPPED … … 646 643 ^(mainThread){}; 647 644 648 ^( __cfa_dbg_global_clusters.list){};649 ^( __cfa_dbg_global_clusters.lock){};645 ^(global_clusters.list){}; 646 ^(global_clusters.lock){}; 650 647 651 648 __cfaabi_dbg_print_safe("Kernel : Shutdown complete\n"); … … 657 654 658 655 void halt(processor * this) with( *this ) { 659 // verify( ! __atomic_load_n(&do_terminate, __ATOMIC_SEQ_CST) );660 661 656 with( *cltr ) { 662 657 lock (proc_list_lock __cfaabi_dbg_ctx2); … … 668 663 __cfaabi_dbg_print_safe("Kernel : Processor %p ready to sleep\n", this); 669 664 670 wait( idleLock ); 665 verify( ({int sval = 0; sem_getvalue(&this->idleLock, &sval); sval; }) < 200); 666 int __attribute__((unused)) ret = sem_wait(&idleLock); 667 verify(ret > 0 || errno == EINTR); 671 668 672 669 __cfaabi_dbg_print_safe("Kernel : Processor %p woke up and ready to run\n", this); … … 678 675 unlock (proc_list_lock); 679 676 } 677 } 678 679 void wake(processor * this) { 680 __cfaabi_dbg_print_safe("Kernel : Waking up processor %p\n", this); 681 int __attribute__((unused)) ret = sem_post(&this->idleLock); 682 verify(ret > 0 || errno == EINTR); 683 verify( ({int sval = 0; sem_getvalue(&this->idleLock, &sval); sval; }) < 200); 680 684 } 681 685 … … 793 797 // Global Queues 794 798 void doregister( cluster & cltr ) { 795 lock ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2);796 push_front( __cfa_dbg_global_clusters.list, cltr );797 unlock ( __cfa_dbg_global_clusters.lock );799 lock ( global_clusters.lock __cfaabi_dbg_ctx2); 800 push_front( global_clusters.list, cltr ); 801 unlock ( global_clusters.lock ); 798 802 } 799 803 800 804 void unregister( cluster & cltr ) { 801 lock ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2);802 remove( __cfa_dbg_global_clusters.list, cltr );803 unlock( __cfa_dbg_global_clusters.lock );805 lock ( global_clusters.lock __cfaabi_dbg_ctx2); 806 remove( global_clusters.list, cltr ); 807 unlock( global_clusters.lock ); 804 808 } 805 809
Note:
See TracChangeset
for help on using the changeset viewer.