Changeset ae7be7a
- Timestamp:
- Mar 27, 2020, 12:05:49 PM (5 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- f0ce5f4
- Parents:
- 68887f9
- Location:
- libcfa/src/concurrency
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/concurrency/coroutine.hfa
r68887f9 rae7be7a 70 70 static inline void $ctx_switch( $coroutine * src, $coroutine * dst ) __attribute__((nonnull (1, 2))) { 71 71 // set state of current coroutine to inactive 72 src->state = src->state == Halted ? Halted : Inactive;72 src->state = src->state == Halted ? Halted : Blocked; 73 73 74 74 // set new coroutine that task is executing -
libcfa/src/concurrency/invoke.h
r68887f9 rae7be7a 92 92 }; 93 93 94 enum coroutine_state { Halted, Start, Primed, Inactive, Active, Rerun };94 enum coroutine_state { Halted, Start, Primed, Blocked, Ready, Active, Rerun }; 95 95 enum __Preemption_Reason { __NO_PREEMPTION, __ALARM_PREEMPTION, __POLL_PREEMPTION, __MANUAL_PREEMPTION }; 96 96 … … 201 201 #ifdef __CFA_DEBUG__ 202 202 // previous function to park/unpark the thread 203 const char * prev_park; 203 const char * park_caller; 204 enum coroutine_state park_result; 204 205 bool park_stale; 205 const char * prev_unpark; 206 const char * unpark_caller; 207 enum coroutine_state unpark_result; 206 208 bool unpark_stale; 207 209 #endif -
libcfa/src/concurrency/kernel.cfa
r68887f9 rae7be7a 293 293 if(readyThread) { 294 294 /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); 295 /* paranoid */ verifyf( readyThread->state == Inactive|| readyThread->state == Start || readyThread->preempted != __NO_PREEMPTION, "state : %d, preempted %d\n", readyThread->state, readyThread->preempted);295 /* paranoid */ verifyf( readyThread->state == Blocked || readyThread->state == Start || readyThread->preempted != __NO_PREEMPTION, "state : %d, preempted %d\n", readyThread->state, readyThread->preempted); 296 296 /* paranoid */ verifyf( readyThread->next == 0p, "Expected null got %p", readyThread->next ); 297 297 … … 331 331 // set state of processor coroutine to inactive 332 332 verify(proc_cor->state == Active); 333 proc_cor->state = Inactive;333 proc_cor->state = Blocked; 334 334 335 335 // Actually run the thread … … 339 339 verify(thrd_dst->state == Active || thrd_dst->state == Rerun); 340 340 } else { 341 verify(thrd_dst->state == Start || thrd_dst->state == Primed || thrd_dst->state == Inactive);341 verify(thrd_dst->state == Start || thrd_dst->state == Primed || thrd_dst->state == Blocked); 342 342 thrd_dst->state = Active; 343 343 } … … 365 365 // 1 - Regular case : the thread has blocked and now one has scheduled it yet. 366 366 // 2 - Racy case : the thread has blocked but someone has already tried to schedule it. 367 // 3 - Polite Racy case : the thread has blocked, someone has already tried to schedule it, but the thread is nice and wants to go through the ready-queue any way368 367 // 4 - Preempted 369 368 // In case 1, we may have won a race so we can't write to the state again. 370 369 // In case 2, we lost the race so we now own the thread. 371 // In case 3, we lost the race but can just reschedule the thread.372 370 373 371 if(unlikely(thrd_dst->preempted != __NO_PREEMPTION)) { … … 379 377 // set state of processor coroutine to active and the thread to inactive 380 378 static_assert(sizeof(thrd_dst->state) == sizeof(int)); 381 enum coroutine_state old_state = __atomic_exchange_n(&thrd_dst->state, Inactive, __ATOMIC_SEQ_CST); 379 enum coroutine_state old_state = __atomic_exchange_n(&thrd_dst->state, Blocked, __ATOMIC_SEQ_CST); 380 __cfaabi_dbg_debug_do( thrd_dst->park_result = old_state; ) 382 381 switch(old_state) { 383 382 case Halted: … … 398 397 default: 399 398 // This makes no sense, something is wrong abort 400 abort("Finished running a thread that was Inactive/Start/Primed %d\n", old_state);399 abort("Finished running a thread that was Blocked/Start/Primed %d\n", old_state); 401 400 } 402 401 } … … 404 403 // Just before returning to the processor, set the processor coroutine to active 405 404 proc_cor->state = Active; 405 kernelTLS.this_thread = 0p; 406 406 } 407 407 … … 521 521 522 522 // set state of current coroutine to inactive 523 src->state = src->state == Halted ? Halted : Inactive;523 src->state = src->state == Halted ? Halted : Blocked; 524 524 525 525 // context switch to specified coroutine … … 555 555 /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); 556 556 /* paranoid */ #if defined( __CFA_WITH_VERIFY__ ) 557 /* paranoid */ if( thrd->state == Inactive|| thrd->state == Start ) assertf( thrd->preempted == __NO_PREEMPTION,557 /* paranoid */ if( thrd->state == Blocked || thrd->state == Start ) assertf( thrd->preempted == __NO_PREEMPTION, 558 558 "Error inactive thread marked as preempted, state %d, preemption %d\n", thrd->state, thrd->preempted ); 559 559 /* paranoid */ if( thrd->preempted != __NO_PREEMPTION ) assertf(thrd->state == Active || thrd->state == Rerun, … … 561 561 /* paranoid */ #endif 562 562 /* paranoid */ verifyf( thrd->next == 0p, "Expected null got %p", thrd->next ); 563 564 if (thrd->preempted == __NO_PREEMPTION) thrd->state = Ready; 563 565 564 566 lock ( ready_queue_lock __cfaabi_dbg_ctx2 ); … … 598 600 disable_interrupts(); 599 601 static_assert(sizeof(thrd->state) == sizeof(int)); 600 602 601 603 // record activity 602 604 __cfaabi_dbg_record_thrd( *thrd, false, caller ); 603 605 604 606 enum coroutine_state old_state = __atomic_exchange_n(&thrd->state, Rerun, __ATOMIC_SEQ_CST); 607 __cfaabi_dbg_debug_do( thrd->unpark_result = old_state; ) 605 608 switch(old_state) { 606 609 case Active: 607 610 // Wake won the race, the thread will reschedule/rerun itself 608 611 break; 609 case Inactive:612 case Blocked: 610 613 /* paranoid */ verify( ! thrd->preempted != __NO_PREEMPTION ); 611 614 612 615 // Wake lost the race, 613 thrd->state = Inactive;616 thrd->state = Blocked; 614 617 __schedule_thread( thrd ); 615 618 break;
Note: See TracChangeset
for help on using the changeset viewer.