Skip to content

Commit

Permalink
core/mutex.c _mutex_lock uses int pointer
Browse files Browse the repository at this point in the history
_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.
  • Loading branch information
JulianHolzwarth committed Mar 26, 2020
1 parent 267433b commit 40bf85e
Show file tree
Hide file tree
Showing 3 changed files with 10 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);
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);
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
3 changes: 2 additions & 1 deletion sys/xtimer/xtimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down

0 comments on commit 40bf85e

Please sign in to comment.