Skip to content

Commit

Permalink
Merge pull request #11660 from JulianHolzwarth/pr/xtimer_mutex_lock_t…
Browse files Browse the repository at this point in the history
…imeout/_mutex_remove_thread_from_waiting_queue

core/mutex: use volatile pointer for blocking flag of _mutex_lock
  • Loading branch information
MichelRottleuthner authored Mar 27, 2020
2 parents b535c66 + dd6e51b commit 80ef241
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
9 changes: 6 additions & 3 deletions core/include/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#define MUTEX_H

#include <stddef.h>
#include <stdint.h>

#include "list.h"

Expand Down Expand Up @@ -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.
Expand All @@ -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);
volatile uint8_t blocking = 0;
return _mutex_lock(mutex, &blocking);
}

/**
Expand All @@ -110,7 +112,8 @@ static inline int mutex_trylock(mutex_t *mutex)
*/
static inline void mutex_lock(mutex_t *mutex)
{
_mutex_lock(mutex, 1);
volatile uint8_t blocking = 1;
_mutex_lock(mutex, &blocking);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions core/mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion sys/xtimer/xtimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,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;
}
Expand Down

0 comments on commit 80ef241

Please sign in to comment.