mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
add mutex_unlock_and_sleep()
This commit is contained in:
parent
8760ea8f73
commit
05f085d51a
@ -68,6 +68,13 @@ int mutex_lock(struct mutex_t *mutex);
|
||||
*/
|
||||
void mutex_unlock(struct mutex_t *mutex);
|
||||
|
||||
/**
|
||||
* @brief Unlocks the mutex and sends the current thread to sleep
|
||||
*
|
||||
* @param mutex Mutex-Object to unlock.
|
||||
*/
|
||||
void mutex_unlock_and_sleep(struct mutex_t *mutex);
|
||||
|
||||
#define MUTEX_YIELD 1
|
||||
#define MUTEX_INISR 2
|
||||
|
||||
|
22
core/mutex.c
22
core/mutex.c
@ -117,3 +117,25 @@ void mutex_unlock(struct mutex_t *mutex)
|
||||
|
||||
restoreIRQ(irqstate);
|
||||
}
|
||||
|
||||
void mutex_unlock_and_sleep(struct mutex_t *mutex)
|
||||
{
|
||||
DEBUG("%s: unlocking mutex. val: %u pid: %u, and take a nap\n", active_thread->name, mutex->val, thread_pid);
|
||||
int irqstate = disableIRQ();
|
||||
|
||||
if (mutex->val != 0) {
|
||||
if (mutex->queue.next) {
|
||||
queue_node_t *next = queue_remove_head(&(mutex->queue));
|
||||
tcb_t *process = (tcb_t*) next->data;
|
||||
DEBUG("%s: waking up waiter.\n", process->name);
|
||||
sched_set_status(process, STATUS_PENDING);
|
||||
}
|
||||
else {
|
||||
mutex->val = 0;
|
||||
}
|
||||
}
|
||||
DEBUG("%s: going to sleep.\n", active_thread->name);
|
||||
sched_set_status((tcb_t*) active_thread, STATUS_SLEEPING);
|
||||
restoreIRQ(irqstate);
|
||||
thread_yield();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user