Changeset 9236060 for src/libcfa/concurrency/kernel.c
- Timestamp:
- Aug 14, 2017, 2:03:39 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:
- 74b007ba
- Parents:
- fd344aa (diff), 54cd58b (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
rfd344aa r9236060 1 // -*- Mode: CFA -*-2 1 // 3 2 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo … … 10 9 // Author : Thierry Delisle 11 10 // Created On : Tue Jan 17 12:27:26 2017 12 // Last Modified By : Thierry Delisle13 // Last Modified On : --14 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Jul 21 22:33:18 2017 13 // Update Count : 2 15 14 // 16 15 … … 42 41 //----------------------------------------------------------------------------- 43 42 // 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); 43 KERNEL_STORAGE(cluster, mainCluster); 44 KERNEL_STORAGE(processor, mainProcessor); 45 KERNEL_STORAGE(processorCtx_t, mainProcessorCtx); 46 KERNEL_STORAGE(thread_desc, mainThread); 50 47 KERNEL_STORAGE(machine_context_t, mainThreadCtx); 51 48 52 cluster * systemCluster;53 system_proc_t * systemProcessor;49 cluster * mainCluster; 50 processor * mainProcessor; 54 51 thread_desc * mainThread; 55 52 … … 57 54 // Global state 58 55 59 volatile thread_local processor * this_processor; 60 volatile thread_local coroutine_desc * this_coroutine; 61 volatile thread_local thread_desc * this_thread; 56 thread_local coroutine_desc * volatile this_coroutine; 57 thread_local thread_desc * volatile this_thread; 58 thread_local processor * volatile this_processor; 59 62 60 volatile thread_local bool preemption_in_progress = 0; 63 61 volatile thread_local unsigned short disable_preempt_count = 1; … … 85 83 86 84 this.limit = (void *)(((intptr_t)this.base) - this.size); 87 this.context = & mainThreadCtxStorage;85 this.context = &storage_mainThreadCtx; 88 86 this.top = this.base; 89 87 } … … 125 123 126 124 void ?{}(processor & this) { 127 this{ systemCluster };125 this{ mainCluster }; 128 126 } 129 127 … … 131 129 this.cltr = cltr; 132 130 (this.terminated){ 0 }; 133 this. is_terminated= false;131 this.do_terminate = false; 134 132 this.preemption_alarm = NULL; 135 this.preemption = default_preemption();136 133 this.pending_preemption = false; 137 134 … … 142 139 this.cltr = cltr; 143 140 (this.terminated){ 0 }; 144 this. is_terminated= false;141 this.do_terminate = false; 145 142 this.preemption_alarm = NULL; 146 this.preemption = default_preemption();147 143 this.pending_preemption = false; 148 144 this.kernel_thread = pthread_self(); 149 145 150 146 this.runner = &runner; 151 LIB_DEBUG_PRINT_SAFE("Kernel : constructing systemprocessor context %p\n", &runner);147 LIB_DEBUG_PRINT_SAFE("Kernel : constructing main processor context %p\n", &runner); 152 148 runner{ &this }; 153 149 } 154 150 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 151 void ^?{}(processor & this) { 168 if( ! this. is_terminated) {152 if( ! this.do_terminate ) { 169 153 LIB_DEBUG_PRINT_SAFE("Kernel : core %p signaling termination\n", &this); 170 this. is_terminated= true;154 this.do_terminate = true; 171 155 P( &this.terminated ); 172 156 pthread_join( this.kernel_thread, NULL ); … … 176 160 void ?{}(cluster & this) { 177 161 ( this.ready_queue ){}; 178 ( this.lock ){}; 162 ( this.ready_queue_lock ){}; 163 164 this.preemption = default_preemption(); 179 165 } 180 166 … … 199 185 200 186 thread_desc * readyThread = NULL; 201 for( unsigned int spin_count = 0; ! this-> is_terminated; spin_count++ )187 for( unsigned int spin_count = 0; ! this->do_terminate; spin_count++ ) 202 188 { 203 189 readyThread = nextThread( this->cltr ); … … 343 329 verifyf( thrd->next == NULL, "Expected null got %p", thrd->next ); 344 330 345 lock( &systemProcessor->proc.cltr->lock DEBUG_CTX2 );346 append( & systemProcessor->proc.cltr->ready_queue, thrd );347 unlock( & systemProcessor->proc.cltr->lock );331 lock( &this_processor->cltr->ready_queue_lock DEBUG_CTX2 ); 332 append( &this_processor->cltr->ready_queue, thrd ); 333 unlock( &this_processor->cltr->ready_queue_lock ); 348 334 349 335 verify( disable_preempt_count > 0 ); … … 352 338 thread_desc * nextThread(cluster * this) { 353 339 verify( disable_preempt_count > 0 ); 354 lock( &this-> lock DEBUG_CTX2 );340 lock( &this->ready_queue_lock DEBUG_CTX2 ); 355 341 thread_desc * head = pop_head( &this->ready_queue ); 356 unlock( &this-> lock );342 unlock( &this->ready_queue_lock ); 357 343 verify( disable_preempt_count > 0 ); 358 344 return head; … … 452 438 // Start by initializing the main thread 453 439 // SKULLDUGGERY: the mainThread steals the process main thread 454 // which will then be scheduled by the systemProcessor normally455 mainThread = (thread_desc *)& mainThreadStorage;440 // which will then be scheduled by the mainProcessor normally 441 mainThread = (thread_desc *)&storage_mainThread; 456 442 current_stack_info_t info; 457 443 (*mainThread){ &info }; … … 459 445 LIB_DEBUG_PRINT_SAFE("Kernel : Main thread ready\n"); 460 446 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 ctx447 // Initialize the main cluster 448 mainCluster = (cluster *)&storage_mainCluster; 449 (*mainCluster){}; 450 451 LIB_DEBUG_PRINT_SAFE("Kernel : main cluster ready\n"); 452 453 // Initialize the main processor and the main processor ctx 468 454 // (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); 455 mainProcessor = (processor *)&storage_mainProcessor; 456 (*mainProcessor){ mainCluster, *(processorCtx_t *)&storage_mainProcessorCtx }; 475 457 476 458 //initialize the global state variables 477 this_processor = &systemProcessor->proc;459 this_processor = mainProcessor; 478 460 this_thread = mainThread; 479 461 this_coroutine = &mainThread->cor; 480 disable_preempt_count = 1;481 462 482 463 // Enable preemption 483 464 kernel_start_preemption(); 484 465 485 // SKULLDUGGERY: Force a context switch to the system processor to set the main thread's context to the current UNIX 466 // Add the main thread to the ready queue 467 // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread 468 ScheduleThread(mainThread); 469 470 // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX 486 471 // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that 487 472 // mainThread is on the ready queue when this call is made. 488 resume( * systemProcessor->proc.runner );473 resume( *mainProcessor->runner ); 489 474 490 475 … … 501 486 disable_interrupts(); 502 487 503 // SKULLDUGGERY: Notify the systemProcessor it needs to terminates.488 // SKULLDUGGERY: Notify the mainProcessor it needs to terminates. 504 489 // When its coroutine terminates, it return control to the mainThread 505 490 // which is currently here 506 systemProcessor->proc.is_terminated= true;491 mainProcessor->do_terminate = true; 507 492 suspend(); 508 493 … … 512 497 kernel_stop_preemption(); 513 498 514 // Destroy the systemprocessor and its context in reverse order of construction499 // Destroy the main processor and its context in reverse order of construction 515 500 // These were manually constructed so we need manually destroy them 516 ^(* systemProcessor->proc.runner){};517 ^( systemProcessor){};501 ^(*mainProcessor->runner){}; 502 ^(mainProcessor){}; 518 503 519 504 // Final step, destroy the main thread since it is no longer needed … … 699 684 return top; 700 685 } 686 701 687 // Local Variables: // 702 688 // mode: c //
Note: See TracChangeset
for help on using the changeset viewer.