Changeset 5663872 in mainline for kernel/generic/src/proc/scheduler.c


Ignore:
Timestamp:
2024-01-14T18:24:05Z (17 months ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
master
Children:
23f36a3
Parents:
4760793
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2023-02-18 13:37:30)
git-committer:
Jiří Zárevúcky <zarevucky.jiri@…> (2024-01-14 18:24:05)
Message:

Move stuff around for thread sleep

Only mark the thread as ready for wakeup after we switch to
another context. This way, soundness of the sychronization
does not depend on thread lock being held across the context
switch, which gives us more freedom.

File:
1 edited

Legend:

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

    r4760793 r5663872  
    443443                after_thread_ran();
    444444
     445                int expected;
     446
    445447                switch (THREAD->state) {
    446448                case Running:
     
    462464
    463465                case Sleeping:
    464                         /*
    465                          * Prefer the thread after it's woken up.
    466                          */
    467                         THREAD->priority = -1;
    468                         irq_spinlock_unlock(&THREAD->lock, false);
     466                        expected = SLEEP_INITIAL;
     467
     468                        /* Only set SLEEP_ASLEEP in sleep pad if it's still in initial state */
     469                        if (atomic_compare_exchange_strong_explicit(&THREAD->sleep_state,
     470                            &expected, SLEEP_ASLEEP,
     471                            memory_order_acq_rel, memory_order_acquire)) {
     472
     473                                /* Prefer the thread after it's woken up. */
     474                                THREAD->priority = -1;
     475                                irq_spinlock_unlock(&THREAD->lock, false);
     476                        } else {
     477                                assert(expected == SLEEP_WOKE);
     478                                /* The thread has already been woken up, requeue immediately. */
     479                                irq_spinlock_unlock(&THREAD->lock, false);
     480                                thread_ready(THREAD);
     481                        }
     482
    469483                        break;
    470484
Note: See TracChangeset for help on using the changeset viewer.