1
0
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:
Martin 2014-02-18 08:25:16 +01:00
parent 8760ea8f73
commit 05f085d51a
2 changed files with 29 additions and 0 deletions

View File

@ -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

View File

@ -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();
}