Changes in src/libcfa/concurrency/kernel.c [c81ebf9:0c78741]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/concurrency/kernel.c
rc81ebf9 r0c78741 36 36 //CFA Includes 37 37 #include "libhdr.h" 38 #include "preemption.h"39 38 40 39 //Private includes … … 48 47 KERNEL_STORAGE(processorCtx_t, systemProcessorCtx); 49 48 KERNEL_STORAGE(cluster, systemCluster); 50 KERNEL_STORAGE( system_proc_t, systemProcessor);49 KERNEL_STORAGE(processor, systemProcessor); 51 50 KERNEL_STORAGE(thread_desc, mainThread); 52 51 KERNEL_STORAGE(machine_context_t, mainThread_context); 53 52 54 53 cluster * systemCluster; 55 system_proc_t* systemProcessor;54 processor * systemProcessor; 56 55 thread_desc * mainThread; 57 56 … … 119 118 // Processor coroutine 120 119 void ?{}(processorCtx_t * this, processor * proc) { 121 (&this->__cor){ "Processor"};120 (&this->__cor){}; 122 121 this->proc = proc; 123 122 proc->runner = this; … … 140 139 (&this->terminated){}; 141 140 this->is_terminated = false; 142 this->preemption_alarm = NULL;143 this->preemption = default_preemption();144 this->disable_preempt_count = 1; //Start with interrupts disabled145 this->pending_preemption = false;146 141 147 142 start( this ); … … 154 149 (&this->terminated){}; 155 150 this->is_terminated = false; 156 this->disable_preempt_count = 0;157 this->pending_preemption = false;158 151 159 152 this->runner = runner; 160 153 LIB_DEBUG_PRINT_SAFE("Kernel : constructing processor context %p\n", runner); 161 154 runner{ this }; 162 }163 164 void ?{}(system_proc_t * this, cluster * cltr, processorCtx_t * runner) {165 (&this->alarms){};166 (&this->alarm_lock){};167 this->pending_alarm = false;168 169 (&this->proc){ cltr, runner };170 155 } 171 156 … … 193 178 void main(processorCtx_t * runner) { 194 179 processor * this = runner->proc; 195 196 180 LIB_DEBUG_PRINT_SAFE("Kernel : core %p starting\n", this); 197 181 182 thread_desc * readyThread = NULL; 183 for( unsigned int spin_count = 0; ! this->is_terminated; spin_count++ ) 198 184 { 199 // Setup preemption data 200 preemption_scope scope = { this }; 201 202 LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this); 203 204 thread_desc * readyThread = NULL; 205 for( unsigned int spin_count = 0; ! this->is_terminated; spin_count++ ) 185 readyThread = nextThread( this->cltr ); 186 187 if(readyThread) 206 188 { 207 readyThread = nextThread( this->cltr ); 208 209 if(readyThread) 210 { 211 runThread(this, readyThread); 212 213 //Some actions need to be taken from the kernel 214 finishRunning(this); 215 216 spin_count = 0; 217 } 218 else 219 { 220 spin(this, &spin_count); 221 } 222 } 223 224 LIB_DEBUG_PRINT_SAFE("Kernel : core %p stopping\n", this); 225 } 226 189 runThread(this, readyThread); 190 191 //Some actions need to be taken from the kernel 192 finishRunning(this); 193 194 spin_count = 0; 195 } 196 else 197 { 198 spin(this, &spin_count); 199 } 200 } 201 202 LIB_DEBUG_PRINT_SAFE("Kernel : core %p unlocking thread\n", this); 227 203 signal( &this->terminated ); 228 204 LIB_DEBUG_PRINT_SAFE("Kernel : core %p terminated\n", this); … … 323 299 LIB_DEBUG_PRINT_SAFE("Kernel : Starting core %p\n", this); 324 300 301 // pthread_attr_t attributes; 302 // pthread_attr_init( &attributes ); 303 325 304 pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this ); 305 306 // pthread_attr_destroy( &attributes ); 326 307 327 308 LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this); … … 335 316 assertf( thrd->next == NULL, "Expected null got %p", thrd->next ); 336 317 337 lock( &systemProcessor-> proc.cltr->lock );338 append( &systemProcessor-> proc.cltr->ready_queue, thrd );339 unlock( &systemProcessor-> proc.cltr->lock );318 lock( &systemProcessor->cltr->lock ); 319 append( &systemProcessor->cltr->ready_queue, thrd ); 320 unlock( &systemProcessor->cltr->lock ); 340 321 } 341 322 … … 386 367 } 387 368 388 //=============================================================================================389 // Kernel Setup logic390 //=============================================================================================391 369 //----------------------------------------------------------------------------- 392 370 // Kernel boot procedures … … 401 379 mainThread{ &info }; 402 380 403 LIB_DEBUG_PRINT_SAFE("Kernel : Main thread ready\n");404 405 // Enable preemption406 kernel_start_preemption();407 408 381 // Initialize the system cluster 409 382 systemCluster = (cluster *)&systemCluster_storage; 410 383 systemCluster{}; 411 384 412 LIB_DEBUG_PRINT_SAFE("Kernel : System cluster ready\n");413 414 385 // Initialize the system processor and the system processor ctx 415 386 // (the coroutine that contains the processing control flow) 416 systemProcessor = ( system_proc_t*)&systemProcessor_storage;387 systemProcessor = (processor *)&systemProcessor_storage; 417 388 systemProcessor{ systemCluster, (processorCtx_t *)&systemProcessorCtx_storage }; 418 389 419 390 // Add the main thread to the ready queue 420 // once resume is called on systemProcessor-> runnerthe mainThread needs to be scheduled like any normal thread391 // once resume is called on systemProcessor->ctx the mainThread needs to be scheduled like any normal thread 421 392 ScheduleThread(mainThread); 422 393 423 394 //initialize the global state variables 424 this_processor = &systemProcessor->proc;395 this_processor = systemProcessor; 425 396 this_processor->current_thread = mainThread; 426 397 this_processor->current_coroutine = &mainThread->cor; … … 429 400 // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that 430 401 // mainThread is on the ready queue when this call is made. 431 resume( systemProcessor->proc.runner);402 resume(systemProcessor->runner); 432 403 433 404 … … 443 414 // When its coroutine terminates, it return control to the mainThread 444 415 // which is currently here 445 systemProcessor-> proc.is_terminated = true;416 systemProcessor->is_terminated = true; 446 417 suspend(); 447 418 … … 450 421 // Destroy the system processor and its context in reverse order of construction 451 422 // These were manually constructed so we need manually destroy them 452 ^(systemProcessor-> proc.runner){};423 ^(systemProcessor->runner){}; 453 424 ^(systemProcessor){}; 454 425 … … 513 484 } 514 485 515 //=============================================================================================516 // Kernel Utilities517 //=============================================================================================518 486 //----------------------------------------------------------------------------- 519 487 // Locks … … 523 491 void ^?{}( spinlock * this ) { 524 492 525 }526 527 bool try_lock( spinlock * this ) {528 return this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0;529 493 } 530 494
Note:
See TracChangeset
for help on using the changeset viewer.