2010-09-22 15:10:42 +02:00
|
|
|
/**
|
|
|
|
* kernel mutex implementation
|
|
|
|
*
|
2013-06-18 17:21:38 +02:00
|
|
|
* Copyright (C) 2013 Freie Universität Berlin
|
2010-09-22 15:10:42 +02:00
|
|
|
*
|
2013-11-22 20:47:05 +01:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser General
|
2013-06-18 17:21:38 +02:00
|
|
|
* Public License. See the file LICENSE in the top level directory for more
|
|
|
|
* details.
|
2010-09-22 15:10:42 +02:00
|
|
|
*
|
|
|
|
* @ingroup kernel
|
|
|
|
* @{
|
|
|
|
* @file
|
2013-12-04 15:07:56 +01:00
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
2010-09-22 15:10:42 +02:00
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2013-07-23 13:39:50 +02:00
|
|
|
#include <inttypes.h>
|
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
#include "mutex.h"
|
|
|
|
#include "atomic.h"
|
|
|
|
#include "queue.h"
|
|
|
|
#include "tcb.h"
|
|
|
|
#include "kernel.h"
|
2010-10-28 11:22:57 +02:00
|
|
|
#include "sched.h"
|
2013-07-23 13:39:50 +02:00
|
|
|
#include "irq.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-07-23 13:39:50 +02:00
|
|
|
#include "debug.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-06-20 18:18:29 +02:00
|
|
|
int mutex_init(struct mutex_t *mutex)
|
|
|
|
{
|
2010-09-22 15:10:42 +02:00
|
|
|
mutex->val = 0;
|
|
|
|
|
|
|
|
mutex->queue.priority = 0;
|
|
|
|
mutex->queue.data = 0;
|
|
|
|
mutex->queue.next = NULL;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-06-20 18:18:29 +02:00
|
|
|
int mutex_trylock(struct mutex_t *mutex)
|
|
|
|
{
|
2010-11-30 11:37:44 +01:00
|
|
|
DEBUG("%s: trylocking to get mutex. val: %u\n", active_thread->name, mutex->val);
|
2013-06-20 18:18:29 +02:00
|
|
|
return (atomic_set_return(&mutex->val, thread_pid) == 0);
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2013-06-20 18:18:29 +02:00
|
|
|
int prio(void)
|
|
|
|
{
|
2010-10-28 11:22:57 +02:00
|
|
|
return active_thread->priority;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2013-06-20 18:18:29 +02:00
|
|
|
int mutex_lock(struct mutex_t *mutex)
|
|
|
|
{
|
2010-11-01 15:53:33 +01:00
|
|
|
DEBUG("%s: trying to get mutex. val: %u\n", active_thread->name, mutex->val);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (atomic_set_return(&mutex->val, 1) != 0) {
|
2013-06-20 18:18:29 +02:00
|
|
|
/* mutex was locked. */
|
2010-09-22 15:10:42 +02:00
|
|
|
mutex_wait(mutex);
|
|
|
|
}
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-06-20 18:18:29 +02:00
|
|
|
void mutex_wait(struct mutex_t *mutex)
|
|
|
|
{
|
2010-11-11 09:55:08 +01:00
|
|
|
int irqstate = disableIRQ();
|
2010-10-28 11:22:57 +02:00
|
|
|
DEBUG("%s: Mutex in use. %u\n", active_thread->name, mutex->val);
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (mutex->val == 0) {
|
2013-06-20 18:18:29 +02:00
|
|
|
/* somebody released the mutex. return. */
|
2010-10-28 11:22:57 +02:00
|
|
|
mutex->val = thread_pid;
|
|
|
|
DEBUG("%s: mutex_wait early out. %u\n", active_thread->name, mutex->val);
|
2010-11-11 09:55:08 +01:00
|
|
|
restoreIRQ(irqstate);
|
2010-09-22 15:10:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-20 18:18:29 +02:00
|
|
|
sched_set_status((tcb_t*) active_thread, STATUS_MUTEX_BLOCKED);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
queue_node_t n;
|
2010-10-28 11:22:57 +02:00
|
|
|
n.priority = (unsigned int) active_thread->priority;
|
|
|
|
n.data = (unsigned int) active_thread;
|
2010-09-22 15:10:42 +02:00
|
|
|
n.next = NULL;
|
|
|
|
|
2013-07-23 13:39:50 +02:00
|
|
|
DEBUG("%s: Adding node to mutex queue: prio: %"PRIu32"\n", active_thread->name, n.priority);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
queue_priority_add(&(mutex->queue), &n);
|
|
|
|
|
2010-11-11 09:55:08 +01:00
|
|
|
restoreIRQ(irqstate);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2010-10-28 11:22:57 +02:00
|
|
|
thread_yield();
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
/* we were woken up by scheduler. waker removed us from queue. we have the mutex now. */
|
|
|
|
}
|
|
|
|
|
2013-08-04 04:06:31 +02:00
|
|
|
void mutex_unlock(struct mutex_t *mutex)
|
2013-06-20 18:18:29 +02:00
|
|
|
{
|
2010-11-11 09:55:08 +01:00
|
|
|
DEBUG("%s: unlocking mutex. val: %u pid: %u\n", active_thread->name, mutex->val, thread_pid);
|
|
|
|
int irqstate = disableIRQ();
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (mutex->val != 0) {
|
|
|
|
if (mutex->queue.next) {
|
2010-11-11 09:55:08 +01:00
|
|
|
queue_node_t *next = queue_remove_head(&(mutex->queue));
|
2013-06-20 18:18:29 +02:00
|
|
|
tcb_t *process = (tcb_t*) next->data;
|
2013-07-16 15:25:23 +02:00
|
|
|
DEBUG("%s: waking up waiter.\n", process->name);
|
2010-11-11 09:55:08 +01:00
|
|
|
sched_set_status(process, STATUS_PENDING);
|
|
|
|
|
2010-11-11 11:22:45 +01:00
|
|
|
sched_switch(active_thread->priority, process->priority, inISR());
|
2013-06-20 18:18:29 +02:00
|
|
|
}
|
|
|
|
else {
|
2010-11-11 09:55:08 +01:00
|
|
|
mutex->val = 0;
|
|
|
|
}
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2010-11-11 09:55:08 +01:00
|
|
|
restoreIRQ(irqstate);
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|