Changeset ae7be7a


Ignore:
Timestamp:
Mar 27, 2020, 12:05:49 PM (5 years ago)
Author:
Thierry Delisle <tdelisle@…>
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
Message:

Fixed incorrect setting of ready state and added result to previous park information

Location:
libcfa/src/concurrency
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/concurrency/coroutine.hfa

    r68887f9 rae7be7a  
    7070static inline void $ctx_switch( $coroutine * src, $coroutine * dst ) __attribute__((nonnull (1, 2))) {
    7171        // set state of current coroutine to inactive
    72         src->state = src->state == Halted ? Halted : Inactive;
     72        src->state = src->state == Halted ? Halted : Blocked;
    7373
    7474        // set new coroutine that task is executing
  • libcfa/src/concurrency/invoke.h

    r68887f9 rae7be7a  
    9292        };
    9393
    94         enum coroutine_state { Halted, Start, Primed, Inactive, Active, Rerun };
     94        enum coroutine_state { Halted, Start, Primed, Blocked, Ready, Active, Rerun };
    9595        enum __Preemption_Reason { __NO_PREEMPTION, __ALARM_PREEMPTION, __POLL_PREEMPTION, __MANUAL_PREEMPTION };
    9696
     
    201201                #ifdef __CFA_DEBUG__
    202202                        // previous function to park/unpark the thread
    203                         const char * prev_park;
     203                        const char * park_caller;
     204                        enum coroutine_state park_result;
    204205                        bool park_stale;
    205                         const char * prev_unpark;
     206                        const char * unpark_caller;
     207                        enum coroutine_state unpark_result;
    206208                        bool unpark_stale;
    207209                #endif
  • libcfa/src/concurrency/kernel.cfa

    r68887f9 rae7be7a  
    293293                        if(readyThread) {
    294294                                /* 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);
    296296                                /* paranoid */ verifyf( readyThread->next == 0p, "Expected null got %p", readyThread->next );
    297297
     
    331331        // set state of processor coroutine to inactive
    332332        verify(proc_cor->state == Active);
    333         proc_cor->state = Inactive;
     333        proc_cor->state = Blocked;
    334334
    335335        // Actually run the thread
     
    339339                        verify(thrd_dst->state == Active || thrd_dst->state == Rerun);
    340340                } 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);
    342342                        thrd_dst->state = Active;
    343343                }
     
    365365                // 1 - Regular case : the thread has blocked and now one has scheduled it yet.
    366366                // 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 way
    368367                // 4 - Preempted
    369368                // In case 1, we may have won a race so we can't write to the state again.
    370369                // 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.
    372370
    373371                if(unlikely(thrd_dst->preempted != __NO_PREEMPTION)) {
     
    379377                // set state of processor coroutine to active and the thread to inactive
    380378                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; )
    382381                switch(old_state) {
    383382                        case Halted:
     
    398397                        default:
    399398                                // 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);
    401400                }
    402401        }
     
    404403        // Just before returning to the processor, set the processor coroutine to active
    405404        proc_cor->state = Active;
     405        kernelTLS.this_thread = 0p;
    406406}
    407407
     
    521521
    522522        // set state of current coroutine to inactive
    523         src->state = src->state == Halted ? Halted : Inactive;
     523        src->state = src->state == Halted ? Halted : Blocked;
    524524
    525525        // context switch to specified coroutine
     
    555555        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
    556556        /* 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,
    558558                          "Error inactive thread marked as preempted, state %d, preemption %d\n", thrd->state, thrd->preempted );
    559559        /* paranoid */ if( thrd->preempted != __NO_PREEMPTION ) assertf(thrd->state == Active || thrd->state == Rerun,
     
    561561        /* paranoid */ #endif
    562562        /* paranoid */ verifyf( thrd->next == 0p, "Expected null got %p", thrd->next );
     563
     564        if (thrd->preempted == __NO_PREEMPTION) thrd->state = Ready;
    563565
    564566        lock  ( ready_queue_lock __cfaabi_dbg_ctx2 );
     
    598600        disable_interrupts();
    599601        static_assert(sizeof(thrd->state) == sizeof(int));
    600        
     602
    601603        // record activity
    602604        __cfaabi_dbg_record_thrd( *thrd, false, caller );
    603605
    604606        enum coroutine_state old_state = __atomic_exchange_n(&thrd->state, Rerun, __ATOMIC_SEQ_CST);
     607        __cfaabi_dbg_debug_do( thrd->unpark_result = old_state; )
    605608        switch(old_state) {
    606609                case Active:
    607610                        // Wake won the race, the thread will reschedule/rerun itself
    608611                        break;
    609                 case Inactive:
     612                case Blocked:
    610613                        /* paranoid */ verify( ! thrd->preempted != __NO_PREEMPTION );
    611614
    612615                        // Wake lost the race,
    613                         thrd->state = Inactive;
     616                        thrd->state = Blocked;
    614617                        __schedule_thread( thrd );
    615618                        break;
Note: See TracChangeset for help on using the changeset viewer.