From c07181c1c4fcc204670c968c4b80867248752f5f Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Mon, 12 Sep 2022 21:40:54 +0200 Subject: [PATCH] core/mutex: fix priority inheritance on AVR This fixes https://github.com/RIOT-OS/RIOT/issues/18545 as the code previously relied on `sched_change_priority()` not directly scheduling a new thread while IRQs are disabled, but rather later when IRQs are restored. This is true for Cortex-M MCUs (where the PendSV IRQ is used to trigger the scheduler), but not e.g. for AVR. --- core/mutex.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/core/mutex.c b/core/mutex.c index 0f5e821367..8d7c283aea 100644 --- a/core/mutex.c +++ b/core/mutex.c @@ -157,16 +157,6 @@ void mutex_unlock(mutex_t *mutex) return; } -#ifdef MODULE_CORE_MUTEX_PRIORITY_INHERITANCE - thread_t *owner = thread_get(mutex->owner); - if ((owner) && (owner->priority != mutex->owner_original_priority)) { - DEBUG("PID[%" PRIkernel_pid "] prio %u --> %u\n", - owner->pid, - (unsigned)owner->priority, (unsigned)owner->priority); - sched_change_priority(owner, mutex->owner_original_priority); - } -#endif - if (mutex->queue.next == MUTEX_LOCKED) { mutex->queue.next = NULL; /* the mutex was locked and no thread was waiting for it */ @@ -188,6 +178,16 @@ void mutex_unlock(mutex_t *mutex) uint16_t process_priority = process->priority; +#ifdef MODULE_CORE_MUTEX_PRIORITY_INHERITANCE + thread_t *owner = thread_get(mutex->owner); + if ((owner) && (owner->priority != mutex->owner_original_priority)) { + DEBUG("PID[%" PRIkernel_pid "] prio %u --> %u\n", + owner->pid, + (unsigned)owner->priority, (unsigned)owner->priority); + sched_change_priority(owner, mutex->owner_original_priority); + } +#endif + irq_restore(irqstate); sched_switch(process_priority); }