diff --git a/src/common/pico_sync/include/pico/mutex.h b/src/common/pico_sync/include/pico/mutex.h index c9b4c1b..4b5d175 100644 --- a/src/common/pico_sync/include/pico/mutex.h +++ b/src/common/pico_sync/include/pico/mutex.h @@ -31,8 +31,7 @@ extern "C" { typedef struct __packed_aligned mutex { lock_core_t core; - bool owned; - int8_t owner; + int8_t owner; //! core number or -1 for unowned } mutex_t; /*! \brief Initialise a mutex structure diff --git a/src/common/pico_sync/mutex.c b/src/common/pico_sync/mutex.c index e2771d9..762a54d 100644 --- a/src/common/pico_sync/mutex.c +++ b/src/common/pico_sync/mutex.c @@ -13,6 +13,7 @@ static_assert(sizeof(mutex_t) == 8, ""); void mutex_init(mutex_t *mtx) { lock_init(&mtx->core, next_striped_spin_lock_num()); + mtx->owner = -1; __mem_fence_release(); } @@ -21,8 +22,7 @@ void __time_critical_func(mutex_enter_blocking)(mutex_t *mtx) { bool block = true; do { uint32_t save = spin_lock_blocking(mtx->core.spin_lock); - if (!mtx->owned) { - mtx->owned = true; + if (mtx->owner < 0) { mtx->owner = get_core_num(); block = false; } @@ -36,8 +36,7 @@ void __time_critical_func(mutex_enter_blocking)(mutex_t *mtx) { bool __time_critical_func(mutex_try_enter)(mutex_t *mtx, uint32_t *owner_out) { bool entered; uint32_t save = spin_lock_blocking(mtx->core.spin_lock); - if (!mtx->owned) { - mtx->owned = true; + if (mtx->owner < 0) { mtx->owner = get_core_num(); entered = true; } else { @@ -57,8 +56,7 @@ bool __time_critical_func(mutex_enter_block_until)(mutex_t *mtx, absolute_time_t bool block = true; do { uint32_t save = spin_lock_blocking(mtx->core.spin_lock); - if (!mtx->owned) { - mtx->owned = true; + if (mtx->owner < 0) { mtx->owner = get_core_num(); block = false; } @@ -75,10 +73,7 @@ bool __time_critical_func(mutex_enter_block_until)(mutex_t *mtx, absolute_time_t void __time_critical_func(mutex_exit)(mutex_t *mtx) { uint32_t save = spin_lock_blocking(mtx->core.spin_lock); assert(mtx->owned); - mtx->owned = 0; -#ifndef NDEBUG mtx->owner = -1; -#endif __sev(); spin_unlock(mtx->core.spin_lock, save); }