diff --git a/core/mutex.c b/core/mutex.c index 72b6bc5fcb..6c8db0385e 100644 --- a/core/mutex.c +++ b/core/mutex.c @@ -49,7 +49,9 @@ * therefore, beneficial for the majority of applications. */ static inline __attribute__((always_inline)) -void _block(mutex_t *mutex, uinttxtptr_t pc) +void _block(mutex_t *mutex, + unsigned irq_state, + uinttxtptr_t pc) { /* pc is only used when MODULE_CORE_MUTEX_DEBUG */ (void)pc; @@ -85,7 +87,7 @@ void _block(mutex_t *mutex, uinttxtptr_t pc) } #endif - irq_enable(); + irq_restore(irq_state); thread_yield_higher(); /* We were woken up by scheduler. Waker removed us from queue. */ #if IS_USED(MODULE_CORE_MUTEX_DEBUG) @@ -95,12 +97,11 @@ void _block(mutex_t *mutex, uinttxtptr_t pc) bool mutex_lock_internal(mutex_t *mutex, bool block) { - assert(irq_is_enabled()); uinttxtptr_t pc = 0; #if IS_USED(MODULE_CORE_MUTEX_DEBUG) pc = cpu_get_caller_pc(); #endif - irq_disable(); + unsigned irq_state = irq_disable(); DEBUG("PID[%" PRIkernel_pid "] mutex_lock_internal(block=%u).\n", thread_getpid(), (unsigned)block); @@ -121,14 +122,14 @@ bool mutex_lock_internal(mutex_t *mutex, bool block) #endif DEBUG("PID[%" PRIkernel_pid "] mutex_lock(): early out.\n", thread_getpid()); - irq_enable(); + irq_restore(irq_state); } else { if (!block) { - irq_enable(); + irq_restore(irq_state); return false; } - _block(mutex, pc); + _block(mutex, irq_state, pc); } return true; @@ -136,12 +137,11 @@ bool mutex_lock_internal(mutex_t *mutex, bool block) int mutex_lock_cancelable(mutex_cancel_t *mc) { - assert(irq_is_enabled()); uinttxtptr_t pc = 0; #if IS_USED(MODULE_CORE_MUTEX_DEBUG) pc = cpu_get_caller_pc(); #endif - irq_disable(); + unsigned irq_state = irq_disable(); DEBUG("PID[%" PRIkernel_pid "] mutex_lock_cancelable()\n", thread_getpid()); @@ -149,7 +149,7 @@ int mutex_lock_cancelable(mutex_cancel_t *mc) if (mc->cancelled) { DEBUG("PID[%" PRIkernel_pid "] mutex_lock_cancelable cancelled " "early.\n", thread_getpid()); - irq_enable(); + irq_restore(irq_state); return -ECANCELED; } @@ -171,11 +171,11 @@ int mutex_lock_cancelable(mutex_cancel_t *mc) #endif DEBUG("PID[%" PRIkernel_pid "] mutex_lock_cancelable() early out.\n", thread_getpid()); - irq_enable(); + irq_restore(irq_state); return 0; } else { - _block(mutex, pc); + _block(mutex, irq_state, pc); if (mc->cancelled) { DEBUG("PID[%" PRIkernel_pid "] mutex_lock_cancelable() " "cancelled.\n", thread_getpid()); @@ -237,10 +237,9 @@ void mutex_unlock(mutex_t *mutex) void mutex_unlock_and_sleep(mutex_t *mutex) { - assert(irq_is_enabled()); DEBUG("PID[%" PRIkernel_pid "] mutex_unlock_and_sleep(): queue.next: %p\n", thread_getpid(), (void *)mutex->queue.next); - irq_disable(); + unsigned irqstate = irq_disable(); if (mutex->queue.next) { if (mutex->queue.next == MUTEX_LOCKED) { @@ -262,7 +261,7 @@ void mutex_unlock_and_sleep(mutex_t *mutex) DEBUG("PID[%" PRIkernel_pid "] mutex_unlock_and_sleep(): going to sleep.\n", thread_getpid()); sched_set_status(thread_get_active(), STATUS_SLEEPING); - irq_enable(); + irq_restore(irqstate); thread_yield_higher(); } diff --git a/sys/evtimer/evtimer.c b/sys/evtimer/evtimer.c index 90a5f90f81..a8f8132446 100644 --- a/sys/evtimer/evtimer.c +++ b/sys/evtimer/evtimer.c @@ -125,7 +125,7 @@ static void _update_head_offset(evtimer_t *evtimer) void evtimer_add(evtimer_t *evtimer, evtimer_event_t *event) { - irq_disable(); + unsigned state = irq_disable(); DEBUG("evtimer_add(): adding event with offset %" PRIu32 "\n", event->offset); @@ -134,7 +134,7 @@ void evtimer_add(evtimer_t *evtimer, evtimer_event_t *event) if (evtimer->events == event) { _set_timer(evtimer); } - irq_enable(); + irq_restore(state); if (sched_context_switch_request) { thread_yield_higher(); }