diff --git a/kernel/scheduler.cpp b/kernel/scheduler.cpp index 8b2e6b6..09541fa 100644 --- a/kernel/scheduler.cpp +++ b/kernel/scheduler.cpp @@ -235,6 +235,7 @@ struct Scheduler { KSpinlock lock; // The general lock. TODO Break this up! KMutex allThreadsMutex; // For accessing the allThreads list. + KSpinlock activeTimersSpinlock; // For accessing the activeTimers lists. KEvent killedEvent; // Set during shutdown when all processes have been terminated. uintptr_t blockShutdownProcessCount; @@ -1280,6 +1281,7 @@ void Scheduler::Yield(InterruptContext *context) { globalData->schedulerTimeMs = timeMs; // Notify the necessary timers. + KSpinlockAcquire(&activeTimersSpinlock); LinkedItem *_timer = activeTimers.firstItem; while (_timer) { @@ -1299,6 +1301,8 @@ void Scheduler::Yield(InterruptContext *context) { _timer = next; } + + KSpinlockRelease(&activeTimersSpinlock); } // Get the next thread to execute. diff --git a/kernel/synchronisation.cpp b/kernel/synchronisation.cpp index 1401e60..6209ffe 100644 --- a/kernel/synchronisation.cpp +++ b/kernel/synchronisation.cpp @@ -572,8 +572,7 @@ void TestWriterLocks() { #endif void KTimerSet(KTimer *timer, uint64_t triggerInMs, KAsyncTaskCallback _callback, EsGeneric _argument) { - KSpinlockAcquire(&scheduler.lock); - EsDefer(KSpinlockRelease(&scheduler.lock)); + KSpinlockAcquire(&scheduler.activeTimersSpinlock); // Reset the timer state. @@ -610,11 +609,12 @@ void KTimerSet(KTimer *timer, uint64_t triggerInMs, KAsyncTaskCallback _callback } else { scheduler.activeTimers.InsertEnd(&timer->item); } + + KSpinlockRelease(&scheduler.activeTimersSpinlock); } void KTimerRemove(KTimer *timer) { - KSpinlockAcquire(&scheduler.lock); - EsDefer(KSpinlockRelease(&scheduler.lock)); + KSpinlockAcquire(&scheduler.activeTimersSpinlock); if (timer->callback) { KernelPanic("KTimer::Remove - Timers with callbacks cannot be removed.\n"); @@ -623,6 +623,8 @@ void KTimerRemove(KTimer *timer) { if (timer->item.list) { scheduler.activeTimers.Remove(&timer->item); } + + KSpinlockRelease(&scheduler.activeTimersSpinlock); } void Scheduler::WaitMutex(KMutex *mutex) {