Changeset 6e49dab in mainline


Ignore:
Timestamp:
2024-01-14T18:24:05Z (12 months ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
master
Children:
8996582
Parents:
23f36a3
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2023-03-11 19:05:47)
git-committer:
Jiří Zárevúcky <zarevucky.jiri@…> (2024-01-14 18:24:05)
Message:

Extract post-thread cleanup into a separate function

File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/proc/scheduler.c

    r23f36a3 r6e49dab  
    9999}
    100100
    101 /** Take actions after THREAD had run.
    102  *
    103  * Perform actions that need to be
    104  * taken after the running thread
    105  * had been preempted by the scheduler.
    106  *
    107  * THREAD->lock is locked on entry
    108  *
    109  */
    110 static void after_thread_ran(void)
    111 {
    112         after_thread_ran_arch();
    113 }
    114 
    115101#ifdef CONFIG_FPU_LAZY
    116102void scheduler_fpu_lazy_request(void)
     
    374360
    375361        scheduler_locked(ipl);
     362}
     363
     364static void cleanup_after_thread(thread_t *thread, state_t out_state)
     365{
     366        assert(CURRENT->mutex_locks == 0);
     367        assert(interrupts_disabled());
     368
     369        int expected;
     370
     371        switch (out_state) {
     372        case Running:
     373                thread_ready(thread);
     374                break;
     375
     376        case Exiting:
     377                waitq_close(&thread->join_wq);
     378
     379                /*
     380                 * Release the reference CPU has for the thread.
     381                 * If there are no other references (e.g. threads calling join),
     382                 * the thread structure is deallocated.
     383                 */
     384                thread_put(thread);
     385                break;
     386
     387        case Sleeping:
     388                expected = SLEEP_INITIAL;
     389
     390                /* Only set SLEEP_ASLEEP in sleep pad if it's still in initial state */
     391                if (!atomic_compare_exchange_strong_explicit(&thread->sleep_state,
     392                    &expected, SLEEP_ASLEEP,
     393                    memory_order_acq_rel, memory_order_acquire)) {
     394
     395                        assert(expected == SLEEP_WOKE);
     396                        /* The thread has already been woken up, requeue immediately. */
     397                        thread_ready(thread);
     398                }
     399                break;
     400
     401        default:
     402                /*
     403                 * Entering state is unexpected.
     404                 */
     405                panic("tid%" PRIu64 ": unexpected state %s.",
     406                    thread->tid, thread_states[thread->state]);
     407                break;
     408        }
    376409}
    377410
     
    460493
    461494        if (THREAD) {
    462                 /* Must be run after the switch to scheduler stack */
    463                 after_thread_ran();
    464 
    465                 int expected;
    466 
    467                 switch (THREAD->state) {
    468                 case Running:
    469                         irq_spinlock_unlock(&THREAD->lock, false);
    470                         thread_ready(THREAD);
    471                         break;
    472 
    473                 case Exiting:
    474                         irq_spinlock_unlock(&THREAD->lock, false);
    475                         waitq_close(&THREAD->join_wq);
    476 
    477                         /*
    478                          * Release the reference CPU has for the thread.
    479                          * If there are no other references (e.g. threads calling join),
    480                          * the thread structure is deallocated.
    481                          */
    482                         thread_put(THREAD);
    483                         break;
    484 
    485                 case Sleeping:
    486                         expected = SLEEP_INITIAL;
    487 
    488                         /* Only set SLEEP_ASLEEP in sleep pad if it's still in initial state */
    489                         if (atomic_compare_exchange_strong_explicit(&THREAD->sleep_state,
    490                             &expected, SLEEP_ASLEEP,
    491                             memory_order_acq_rel, memory_order_acquire)) {
    492 
    493                                 /* Prefer the thread after it's woken up. */
    494                                 THREAD->priority = -1;
    495                                 irq_spinlock_unlock(&THREAD->lock, false);
    496                         } else {
    497                                 assert(expected == SLEEP_WOKE);
    498                                 /* The thread has already been woken up, requeue immediately. */
    499                                 irq_spinlock_unlock(&THREAD->lock, false);
    500                                 thread_ready(THREAD);
    501                         }
    502 
    503                         break;
    504 
    505                 default:
    506                         /*
    507                          * Entering state is unexpected.
    508                          */
    509                         panic("tid%" PRIu64 ": unexpected state %s.",
    510                             THREAD->tid, thread_states[THREAD->state]);
    511                         break;
     495                after_thread_ran_arch();
     496
     497                state_t state = THREAD->state;
     498
     499                if (state == Sleeping) {
     500                        /* Prefer the thread after it's woken up. */
     501                        THREAD->priority = -1;
    512502                }
     503
     504                irq_spinlock_unlock(&THREAD->lock, false);
     505
     506                cleanup_after_thread(THREAD, state);
    513507
    514508                THREAD = NULL;
Note: See TracChangeset for help on using the changeset viewer.