mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
core/mutex: clean up mutex_lock()
- Split out handling of the blocking code path of mutex_lock() into a static `_block()` function. This improves readability a bit and will ease review of a follow up PR. - Return `void` instead of `int`.
This commit is contained in:
parent
90103f2a6a
commit
a06a7978d3
@ -187,15 +187,13 @@ static inline int mutex_trylock(mutex_t *mutex)
|
|||||||
*
|
*
|
||||||
* @param[in,out] mutex Mutex object to lock.
|
* @param[in,out] mutex Mutex object to lock.
|
||||||
*
|
*
|
||||||
* @retval 0 The mutex was locked by the caller
|
|
||||||
*
|
|
||||||
* @pre @p mutex is not `NULL`
|
* @pre @p mutex is not `NULL`
|
||||||
* @pre Mutex at @p mutex has been initialized
|
* @pre Mutex at @p mutex has been initialized
|
||||||
* @pre Must be called in thread context
|
* @pre Must be called in thread context
|
||||||
*
|
*
|
||||||
* @post The mutex @p is locked and held by the calling thread.
|
* @post The mutex @p is locked and held by the calling thread.
|
||||||
*/
|
*/
|
||||||
int mutex_lock(mutex_t *mutex);
|
void mutex_lock(mutex_t *mutex);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Unlocks the mutex.
|
* @brief Unlocks the mutex.
|
||||||
|
47
core/mutex.c
47
core/mutex.c
@ -33,21 +33,20 @@
|
|||||||
#define ENABLE_DEBUG 0
|
#define ENABLE_DEBUG 0
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
|
|
||||||
int mutex_lock(mutex_t *mutex)
|
/**
|
||||||
|
* @brief Block waiting for a locked mutex
|
||||||
|
* @pre IRQs are disabled
|
||||||
|
* @post IRQs are restored to @p irq_state
|
||||||
|
* @post The calling thread is no longer waiting for the mutex, either
|
||||||
|
* because it got the mutex, or because the operation was cancelled
|
||||||
|
* (only possible for @ref mutex_lock_cancelable)
|
||||||
|
*
|
||||||
|
* Most applications don't use @ref mutex_lock_cancelable. Inlining this
|
||||||
|
* function into both @ref mutex_lock and @ref mutex_lock_cancelable is,
|
||||||
|
* therefore, beneficial for the majority of applications.
|
||||||
|
*/
|
||||||
|
static inline __attribute__((always_inline)) void _block(mutex_t *mutex, unsigned irq_state)
|
||||||
{
|
{
|
||||||
unsigned irq_state = irq_disable();
|
|
||||||
|
|
||||||
DEBUG("PID[%" PRIkernel_pid "] mutex_lock().\n", thread_getpid());
|
|
||||||
|
|
||||||
if (mutex->queue.next == NULL) {
|
|
||||||
/* mutex is unlocked. */
|
|
||||||
mutex->queue.next = MUTEX_LOCKED;
|
|
||||||
DEBUG("PID[%" PRIkernel_pid "] mutex_lock(): early out.\n",
|
|
||||||
thread_getpid());
|
|
||||||
irq_restore(irq_state);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
thread_t *me = thread_get_active();
|
thread_t *me = thread_get_active();
|
||||||
DEBUG("PID[%" PRIkernel_pid "] mutex_lock() Adding node to mutex queue: "
|
DEBUG("PID[%" PRIkernel_pid "] mutex_lock() Adding node to mutex queue: "
|
||||||
"prio: %" PRIu32 "\n", thread_getpid(), (uint32_t)me->priority);
|
"prio: %" PRIu32 "\n", thread_getpid(), (uint32_t)me->priority);
|
||||||
@ -60,11 +59,27 @@ int mutex_lock(mutex_t *mutex)
|
|||||||
thread_add_to_list(&mutex->queue, me);
|
thread_add_to_list(&mutex->queue, me);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
irq_restore(irq_state);
|
irq_restore(irq_state);
|
||||||
thread_yield_higher();
|
thread_yield_higher();
|
||||||
/* We were woken up by scheduler. Waker removed us from queue. */
|
/* We were woken up by scheduler. Waker removed us from queue. */
|
||||||
return 0;
|
}
|
||||||
|
|
||||||
|
void mutex_lock(mutex_t *mutex)
|
||||||
|
{
|
||||||
|
unsigned irq_state = irq_disable();
|
||||||
|
|
||||||
|
DEBUG("PID[%" PRIkernel_pid "] mutex_lock().\n", thread_getpid());
|
||||||
|
|
||||||
|
if (mutex->queue.next == NULL) {
|
||||||
|
/* mutex is unlocked. */
|
||||||
|
mutex->queue.next = MUTEX_LOCKED;
|
||||||
|
DEBUG("PID[%" PRIkernel_pid "] mutex_lock(): early out.\n",
|
||||||
|
thread_getpid());
|
||||||
|
irq_restore(irq_state);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
_block(mutex, irq_state);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void mutex_unlock(mutex_t *mutex)
|
void mutex_unlock(mutex_t *mutex)
|
||||||
|
Loading…
Reference in New Issue
Block a user