Changes in src/libcfa/concurrency/kernel.c [4aa2fb2:0b33412]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/concurrency/kernel.c
r4aa2fb2 r0b33412 59 59 // Global state 60 60 61 thread_local processor * this_processor; 61 volatile thread_local processor * this_processor; 62 volatile thread_local unsigned short disable_preempt_count; 62 63 63 64 coroutine_desc * this_coroutine(void) { … … 142 143 this->preemption_alarm = NULL; 143 144 this->preemption = default_preemption(); 144 this->disable_preempt_count = 1; //Start with interrupts disabled145 145 this->pending_preemption = false; 146 146 … … 154 154 (&this->terminated){}; 155 155 this->is_terminated = false; 156 this->disable_preempt_count = 0; 156 this->preemption_alarm = NULL; 157 this->preemption = default_preemption(); 157 158 this->pending_preemption = false; 159 this->kernel_thread = pthread_self(); 158 160 159 161 this->runner = runner; 160 LIB_DEBUG_PRINT_SAFE("Kernel : constructing processor context %p\n", runner);162 LIB_DEBUG_PRINT_SAFE("Kernel : constructing system processor context %p\n", runner); 161 163 runner{ this }; 162 164 } 165 166 LIB_DEBUG_DO( bool validate( alarm_list_t * this ); ) 163 167 164 168 void ?{}(system_proc_t * this, cluster * cltr, processorCtx_t * runner) { … … 168 172 169 173 (&this->proc){ cltr, runner }; 174 175 verify( validate( &this->alarms ) ); 170 176 } 171 177 … … 209 215 if(readyThread) 210 216 { 217 verify( disable_preempt_count > 0 ); 218 211 219 runThread(this, readyThread); 220 221 verify( disable_preempt_count > 0 ); 212 222 213 223 //Some actions need to be taken from the kernel … … 289 299 processor * proc = (processor *) arg; 290 300 this_processor = proc; 301 disable_preempt_count = 1; 291 302 // SKULLDUGGERY: We want to create a context for the processor coroutine 292 303 // which is needed for the 2-step context switch. However, there is no reason … … 322 333 void start(processor * this) { 323 334 LIB_DEBUG_PRINT_SAFE("Kernel : Starting core %p\n", this); 324 335 336 // SIGALRM must only be caught by the system processor 337 sigset_t old_mask; 338 bool is_system_proc = this_processor == &systemProcessor->proc; 339 if ( is_system_proc ) { 340 // Child kernel-thread inherits the signal mask from the parent kernel-thread. So one special case for the 341 // system processor creating the user processor => toggle the blocking SIGALRM on system processor, create user 342 // processor, and toggle back (below) previous signal mask of the system processor. 343 344 sigset_t new_mask; 345 sigemptyset( &new_mask ); 346 sigemptyset( &old_mask ); 347 sigaddset( &new_mask, SIGALRM ); 348 349 if ( sigprocmask( SIG_BLOCK, &new_mask, &old_mask ) == -1 ) { 350 abortf( "internal error, sigprocmask" ); 351 } 352 353 assert( ! sigismember( &old_mask, SIGALRM ) ); 354 } 355 325 356 pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this ); 357 358 // Toggle back previous signal mask of system processor. 359 if ( is_system_proc ) { 360 if ( sigprocmask( SIG_SETMASK, &old_mask, NULL ) == -1 ) { 361 abortf( "internal error, sigprocmask" ); 362 } // if 363 } // if 326 364 327 365 LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this); … … 347 385 } 348 386 349 void ScheduleInternal() { 387 void BlockInternal() { 388 disable_interrupts(); 389 verify( disable_preempt_count > 0 ); 350 390 suspend(); 351 } 352 353 void ScheduleInternal( spinlock * lock ) { 391 verify( disable_preempt_count > 0 ); 392 enable_interrupts( __PRETTY_FUNCTION__ ); 393 } 394 395 void BlockInternal( spinlock * lock ) { 396 disable_interrupts(); 354 397 this_processor->finish.action_code = Release; 355 398 this_processor->finish.lock = lock; 399 400 verify( disable_preempt_count > 0 ); 356 401 suspend(); 357 } 358 359 void ScheduleInternal( thread_desc * thrd ) { 402 verify( disable_preempt_count > 0 ); 403 404 enable_interrupts( __PRETTY_FUNCTION__ ); 405 } 406 407 void BlockInternal( thread_desc * thrd ) { 408 disable_interrupts(); 360 409 this_processor->finish.action_code = Schedule; 361 410 this_processor->finish.thrd = thrd; 411 412 verify( disable_preempt_count > 0 ); 362 413 suspend(); 363 } 364 365 void ScheduleInternal( spinlock * lock, thread_desc * thrd ) { 414 verify( disable_preempt_count > 0 ); 415 416 enable_interrupts( __PRETTY_FUNCTION__ ); 417 } 418 419 void BlockInternal( spinlock * lock, thread_desc * thrd ) { 420 disable_interrupts(); 366 421 this_processor->finish.action_code = Release_Schedule; 367 422 this_processor->finish.lock = lock; 368 423 this_processor->finish.thrd = thrd; 424 425 verify( disable_preempt_count > 0 ); 369 426 suspend(); 370 } 371 372 void ScheduleInternal(spinlock ** locks, unsigned short count) { 427 verify( disable_preempt_count > 0 ); 428 429 enable_interrupts( __PRETTY_FUNCTION__ ); 430 } 431 432 void BlockInternal(spinlock ** locks, unsigned short count) { 433 disable_interrupts(); 373 434 this_processor->finish.action_code = Release_Multi; 374 435 this_processor->finish.locks = locks; 375 436 this_processor->finish.lock_count = count; 437 438 verify( disable_preempt_count > 0 ); 376 439 suspend(); 377 } 378 379 void ScheduleInternal(spinlock ** locks, unsigned short lock_count, thread_desc ** thrds, unsigned short thrd_count) { 440 verify( disable_preempt_count > 0 ); 441 442 enable_interrupts( __PRETTY_FUNCTION__ ); 443 } 444 445 void BlockInternal(spinlock ** locks, unsigned short lock_count, thread_desc ** thrds, unsigned short thrd_count) { 446 disable_interrupts(); 380 447 this_processor->finish.action_code = Release_Multi_Schedule; 381 448 this_processor->finish.locks = locks; … … 383 450 this_processor->finish.thrds = thrds; 384 451 this_processor->finish.thrd_count = thrd_count; 452 453 verify( disable_preempt_count > 0 ); 385 454 suspend(); 455 verify( disable_preempt_count > 0 ); 456 457 enable_interrupts( __PRETTY_FUNCTION__ ); 386 458 } 387 459 … … 403 475 LIB_DEBUG_PRINT_SAFE("Kernel : Main thread ready\n"); 404 476 405 // Enable preemption406 kernel_start_preemption();407 408 477 // Initialize the system cluster 409 478 systemCluster = (cluster *)&systemCluster_storage; … … 425 494 this_processor->current_thread = mainThread; 426 495 this_processor->current_coroutine = &mainThread->cor; 496 disable_preempt_count = 1; 497 498 // Enable preemption 499 kernel_start_preemption(); 427 500 428 501 // SKULLDUGGERY: Force a context switch to the system processor to set the main thread's context to the current UNIX … … 435 508 // THE SYSTEM IS NOW COMPLETELY RUNNING 436 509 LIB_DEBUG_PRINT_SAFE("Kernel : Started\n--------------------------------------------------\n\n"); 510 511 enable_interrupts( __PRETTY_FUNCTION__ ); 437 512 } 438 513 439 514 void kernel_shutdown(void) { 440 515 LIB_DEBUG_PRINT_SAFE("\n--------------------------------------------------\nKernel : Shutting down\n"); 516 517 disable_interrupts(); 441 518 442 519 // SKULLDUGGERY: Notify the systemProcessor it needs to terminates. … … 447 524 448 525 // THE SYSTEM IS NOW COMPLETELY STOPPED 526 527 // Disable preemption 528 kernel_stop_preemption(); 449 529 450 530 // Destroy the system processor and its context in reverse order of construction … … 550 630 if( !this->cond ) { 551 631 append( &this->blocked, this_thread() ); 552 ScheduleInternal( &this->lock ); 553 lock( &this->lock ); 554 } 555 unlock( &this->lock ); 632 BlockInternal( &this->lock ); 633 } 634 else { 635 unlock( &this->lock ); 636 } 556 637 } 557 638 … … 561 642 this->cond = true; 562 643 644 disable_interrupts(); 563 645 thread_desc * it; 564 646 while( it = pop_head( &this->blocked) ) { 565 647 ScheduleThread( it ); 566 648 } 649 enable_interrupts( __PRETTY_FUNCTION__ ); 567 650 } 568 651 unlock( &this->lock );
Note: See TracChangeset
for help on using the changeset viewer.