From 40bf85e204eaeec700eb2211113de25c69a153d8 Mon Sep 17 00:00:00 2001 From: JulianHolzwarth Date: Fri, 24 May 2019 15:24:18 +0200 Subject: [PATCH] core/mutex.c _mutex_lock uses int pointer _mutex_lock uses pointer instead of int for blocking because of a possible interupt before the interupts are disabled The int pointer is volitile. Error in sys/xtimer/xtimer.c: _mutex_lock() will get fixed in next commit. --- core/include/mutex.h | 9 ++++++--- core/mutex.c | 4 ++-- sys/xtimer/xtimer.c | 3 ++- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/core/include/mutex.h b/core/include/mutex.h index f41a5ae9bc65a..5ed8c0624976d 100644 --- a/core/include/mutex.h +++ b/core/include/mutex.h @@ -23,6 +23,7 @@ #define MUTEX_H #include +#include #include "list.h" @@ -87,7 +88,7 @@ static inline void mutex_init(mutex_t *mutex) * @return 1 if mutex was unlocked, now it is locked. * @return 0 if the mutex was locked. */ -int _mutex_lock(mutex_t *mutex, int blocking); +int _mutex_lock(mutex_t *mutex, volatile uint8_t *blocking); /** * @brief Tries to get a mutex, non-blocking. @@ -100,7 +101,8 @@ int _mutex_lock(mutex_t *mutex, int blocking); */ static inline int mutex_trylock(mutex_t *mutex) { - return _mutex_lock(mutex, 0); + uint8_t blocking = 0; + return _mutex_lock(mutex, &blocking); } /** @@ -110,7 +112,8 @@ static inline int mutex_trylock(mutex_t *mutex) */ static inline void mutex_lock(mutex_t *mutex) { - _mutex_lock(mutex, 1); + uint8_t blocking = 1; + _mutex_lock(mutex, &blocking); } /** diff --git a/core/mutex.c b/core/mutex.c index cdc82d8b2cc80..1a24b4da6ad8f 100644 --- a/core/mutex.c +++ b/core/mutex.c @@ -32,7 +32,7 @@ #define ENABLE_DEBUG (0) #include "debug.h" -int _mutex_lock(mutex_t *mutex, int blocking) +int _mutex_lock(mutex_t *mutex, volatile uint8_t *blocking) { unsigned irqstate = irq_disable(); @@ -46,7 +46,7 @@ int _mutex_lock(mutex_t *mutex, int blocking) irq_restore(irqstate); return 1; } - else if (blocking) { + else if (*blocking) { thread_t *me = (thread_t*)sched_active_thread; DEBUG("PID[%" PRIkernel_pid "]: Adding node to mutex queue: prio: %" PRIu32 "\n", sched_active_pid, (uint32_t)me->priority); diff --git a/sys/xtimer/xtimer.c b/sys/xtimer/xtimer.c index 85762280848f7..ce09051018837 100644 --- a/sys/xtimer/xtimer.c +++ b/sys/xtimer/xtimer.c @@ -247,6 +247,7 @@ static void _mutex_timeout(void *arg) unsigned int irqstate = irq_disable(); mutex_thread_t *mt = (mutex_thread_t *)arg; + mt->blocking = 0; _mutex_remove_thread_from_waiting_queue(mt->mutex, mt->thread, &mt->dequeued); irq_restore(irqstate); @@ -262,7 +263,7 @@ int xtimer_mutex_lock_timeout(mutex_t *mutex, uint64_t timeout) t.arg = (void *)((mutex_thread_t *)&mt); xtimer_set64(&t, timeout); } - int ret = _mutex_lock(mutex, mt.blocking); + int ret = _mutex_lock(mutex, &mt.blocking); if (ret == 0) { return -1; }