Changeset 21a5dde1 for src/libcfa/concurrency/kernel.c
- Timestamp:
- Jul 20, 2017, 11:33:59 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, resolv-new, with_gc
- Children:
- 6d54c3a
- Parents:
- dab7ac7 (diff), e1e4aa9 (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
-
src/libcfa/concurrency/kernel.c
rdab7ac7 r21a5dde1 42 42 //----------------------------------------------------------------------------- 43 43 // Kernel storage 44 #define KERNEL_STORAGE(T,X) static char X##Storage[sizeof(T)] 45 46 KERNEL_STORAGE(processorCtx_t, systemProcessorCtx); 47 KERNEL_STORAGE(cluster, systemCluster); 48 KERNEL_STORAGE(system_proc_t, systemProcessor); 49 KERNEL_STORAGE(thread_desc, mainThread); 44 KERNEL_STORAGE(cluster, mainCluster); 45 KERNEL_STORAGE(processor, mainProcessor); 46 KERNEL_STORAGE(processorCtx_t, mainProcessorCtx); 47 KERNEL_STORAGE(thread_desc, mainThread); 50 48 KERNEL_STORAGE(machine_context_t, mainThreadCtx); 51 49 52 cluster * systemCluster;53 system_proc_t * systemProcessor;50 cluster * mainCluster; 51 processor * mainProcessor; 54 52 thread_desc * mainThread; 55 53 … … 57 55 // Global state 58 56 59 volatile thread_local processor * this_processor; 60 volatile thread_local coroutine_desc * this_coroutine; 61 volatile thread_local thread_desc * this_thread; 57 thread_local coroutine_desc * volatile this_coroutine; 58 thread_local thread_desc * volatile this_thread; 59 thread_local processor * volatile this_processor; 60 62 61 volatile thread_local bool preemption_in_progress = 0; 63 62 volatile thread_local unsigned short disable_preempt_count = 1; … … 85 84 86 85 this->limit = (void *)(((intptr_t)this->base) - this->size); 87 this->context = & mainThreadCtxStorage;86 this->context = &storage_mainThreadCtx; 88 87 this->top = this->base; 89 88 } … … 125 124 126 125 void ?{}(processor * this) { 127 this{ systemCluster };126 this{ mainCluster }; 128 127 } 129 128 … … 131 130 this->cltr = cltr; 132 131 (&this->terminated){ 0 }; 133 this-> is_terminated= false;132 this->do_terminate = false; 134 133 this->preemption_alarm = NULL; 135 this->preemption = default_preemption();136 134 this->pending_preemption = false; 137 135 … … 142 140 this->cltr = cltr; 143 141 (&this->terminated){ 0 }; 144 this-> is_terminated= false;142 this->do_terminate = false; 145 143 this->preemption_alarm = NULL; 146 this->preemption = default_preemption();147 144 this->pending_preemption = false; 148 145 this->kernel_thread = pthread_self(); 149 146 150 147 this->runner = runner; 151 LIB_DEBUG_PRINT_SAFE("Kernel : constructing systemprocessor context %p\n", runner);148 LIB_DEBUG_PRINT_SAFE("Kernel : constructing main processor context %p\n", runner); 152 149 runner{ this }; 153 150 } 154 151 155 LIB_DEBUG_DO( bool validate( alarm_list_t * this ); )156 157 void ?{}(system_proc_t * this, cluster * cltr, processorCtx_t * runner) {158 (&this->alarms){};159 (&this->alarm_lock){};160 this->pending_alarm = false;161 162 (&this->proc){ cltr, runner };163 164 verify( validate( &this->alarms ) );165 }166 167 152 void ^?{}(processor * this) { 168 if( ! this-> is_terminated) {153 if( ! this->do_terminate ) { 169 154 LIB_DEBUG_PRINT_SAFE("Kernel : core %p signaling termination\n", this); 170 this-> is_terminated= true;155 this->do_terminate = true; 171 156 P( &this->terminated ); 172 157 pthread_join( this->kernel_thread, NULL ); … … 176 161 void ?{}(cluster * this) { 177 162 ( &this->ready_queue ){}; 178 ( &this->lock ){}; 163 ( &this->ready_queue_lock ){}; 164 165 this->preemption = default_preemption(); 179 166 } 180 167 … … 199 186 200 187 thread_desc * readyThread = NULL; 201 for( unsigned int spin_count = 0; ! this-> is_terminated; spin_count++ )188 for( unsigned int spin_count = 0; ! this->do_terminate; spin_count++ ) 202 189 { 203 190 readyThread = nextThread( this->cltr ); … … 343 330 verifyf( thrd->next == NULL, "Expected null got %p", thrd->next ); 344 331 345 lock( &systemProcessor->proc.cltr->lock DEBUG_CTX2 );346 append( & systemProcessor->proc.cltr->ready_queue, thrd );347 unlock( & systemProcessor->proc.cltr->lock );332 lock( &this_processor->cltr->ready_queue_lock DEBUG_CTX2 ); 333 append( &this_processor->cltr->ready_queue, thrd ); 334 unlock( &this_processor->cltr->ready_queue_lock ); 348 335 349 336 verify( disable_preempt_count > 0 ); … … 352 339 thread_desc * nextThread(cluster * this) { 353 340 verify( disable_preempt_count > 0 ); 354 lock( &this-> lock DEBUG_CTX2 );341 lock( &this->ready_queue_lock DEBUG_CTX2 ); 355 342 thread_desc * head = pop_head( &this->ready_queue ); 356 unlock( &this-> lock );343 unlock( &this->ready_queue_lock ); 357 344 verify( disable_preempt_count > 0 ); 358 345 return head; … … 452 439 // Start by initializing the main thread 453 440 // SKULLDUGGERY: the mainThread steals the process main thread 454 // which will then be scheduled by the systemProcessor normally455 mainThread = (thread_desc *)& mainThreadStorage;441 // which will then be scheduled by the mainProcessor normally 442 mainThread = (thread_desc *)&storage_mainThread; 456 443 current_stack_info_t info; 457 444 mainThread{ &info }; … … 459 446 LIB_DEBUG_PRINT_SAFE("Kernel : Main thread ready\n"); 460 447 461 // Initialize the systemcluster462 systemCluster = (cluster *)&systemClusterStorage;463 systemCluster{};464 465 LIB_DEBUG_PRINT_SAFE("Kernel : Systemcluster ready\n");466 467 // Initialize the system processor and the systemprocessor ctx448 // Initialize the main cluster 449 mainCluster = (cluster *)&storage_mainCluster; 450 mainCluster{}; 451 452 LIB_DEBUG_PRINT_SAFE("Kernel : main cluster ready\n"); 453 454 // Initialize the main processor and the main processor ctx 468 455 // (the coroutine that contains the processing control flow) 469 systemProcessor = (system_proc_t *)&systemProcessorStorage; 470 systemProcessor{ systemCluster, (processorCtx_t *)&systemProcessorCtxStorage }; 471 472 // Add the main thread to the ready queue 473 // once resume is called on systemProcessor->runner the mainThread needs to be scheduled like any normal thread 474 ScheduleThread(mainThread); 456 mainProcessor = (processor *)&storage_mainProcessor; 457 mainProcessor{ mainCluster, (processorCtx_t *)&storage_mainProcessorCtx }; 475 458 476 459 //initialize the global state variables 477 this_processor = &systemProcessor->proc;460 this_processor = mainProcessor; 478 461 this_thread = mainThread; 479 462 this_coroutine = &mainThread->cor; 480 disable_preempt_count = 1;481 463 482 464 // Enable preemption 483 465 kernel_start_preemption(); 484 466 485 // SKULLDUGGERY: Force a context switch to the system processor to set the main thread's context to the current UNIX 467 // Add the main thread to the ready queue 468 // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread 469 ScheduleThread(mainThread); 470 471 // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX 486 472 // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that 487 473 // mainThread is on the ready queue when this call is made. 488 resume( systemProcessor->proc.runner );474 resume( mainProcessor->runner ); 489 475 490 476 … … 501 487 disable_interrupts(); 502 488 503 // SKULLDUGGERY: Notify the systemProcessor it needs to terminates.489 // SKULLDUGGERY: Notify the mainProcessor it needs to terminates. 504 490 // When its coroutine terminates, it return control to the mainThread 505 491 // which is currently here 506 systemProcessor->proc.is_terminated= true;492 mainProcessor->do_terminate = true; 507 493 suspend(); 508 494 … … 512 498 kernel_stop_preemption(); 513 499 514 // Destroy the systemprocessor and its context in reverse order of construction500 // Destroy the main processor and its context in reverse order of construction 515 501 // These were manually constructed so we need manually destroy them 516 ^( systemProcessor->proc.runner){};517 ^( systemProcessor){};502 ^(mainProcessor->runner){}; 503 ^(mainProcessor){}; 518 504 519 505 // Final step, destroy the main thread since it is no longer needed
Note: See TracChangeset
for help on using the changeset viewer.