2010-09-22 15:10:42 +02:00
|
|
|
/**
|
|
|
|
* kernel mutex implementation
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Freie Universität Berlin
|
|
|
|
*
|
|
|
|
* This file subject to the terms and conditions of the GNU General Public
|
|
|
|
* License. See the file LICENSE in the top level directory for more details.
|
|
|
|
*
|
|
|
|
* @ingroup kernel
|
|
|
|
* @{
|
|
|
|
* @file
|
|
|
|
* @author Kaspar Schleiser <kaspar.schleiser@fu-berlin.de>
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#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"
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
//#define ENABLE_DEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
int mutex_init(struct mutex_t* mutex) {
|
|
|
|
mutex->val = 0;
|
|
|
|
|
|
|
|
mutex->queue.priority = 0;
|
|
|
|
mutex->queue.data = 0;
|
|
|
|
mutex->queue.next = NULL;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mutex_trylock(struct mutex_t* mutex) {
|
2010-11-01 15:53:33 +01:00
|
|
|
DEBUG("%s: trylocking to get mutex. val: %u\n", active_thread->name, mutex->val);
|
2010-10-28 11:22:57 +02:00
|
|
|
return (atomic_set_return(&mutex->val, thread_pid ) == 0);
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int prio() {
|
2010-10-28 11:22:57 +02:00
|
|
|
return active_thread->priority;
|
2010-09-22 15:10:42 +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
|
|
|
|
2010-10-28 11:22:57 +02:00
|
|
|
if (atomic_set_return(&mutex->val,thread_pid) != 0) {
|
2010-09-22 15:10:42 +02:00
|
|
|
// mutex was locked.
|
|
|
|
mutex_wait(mutex);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mutex_unlock(struct mutex_t* mutex, int yield) {
|
2010-10-28 11:22:57 +02:00
|
|
|
DEBUG("%s: unlocking mutex. val: %u pid: %u\n", active_thread->name, mutex->val, thread_pid);
|
2010-09-22 15:10:42 +02:00
|
|
|
int me_value;
|
|
|
|
|
|
|
|
if (inISR()) {
|
|
|
|
me_value = 0;
|
|
|
|
yield = MUTEX_INISR;
|
|
|
|
} else {
|
2010-10-28 11:22:57 +02:00
|
|
|
me_value = thread_pid;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (atomic_set_return(&mutex->val,0) != me_value ) {
|
|
|
|
// there were waiters.
|
|
|
|
mutex_wake_waiters(mutex, yield);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mutex_wait(struct mutex_t *mutex) {
|
|
|
|
dINT();
|
2010-10-28 11:22:57 +02:00
|
|
|
DEBUG("%s: Mutex in use. %u\n", active_thread->name, mutex->val);
|
2010-09-22 15:10:42 +02:00
|
|
|
if (mutex->val == 0) {
|
|
|
|
// 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-09-22 15:10:42 +02:00
|
|
|
eINT();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-10-28 11:22:57 +02:00
|
|
|
sched_set_status((tcb*)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;
|
|
|
|
|
2010-10-28 11:22:57 +02:00
|
|
|
DEBUG("%s: Adding node to mutex queue: prio: %u data: %u\n", active_thread->name, n.priority, n.data);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
queue_priority_add(&(mutex->queue), &n);
|
|
|
|
|
|
|
|
eINT();
|
|
|
|
|
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. */
|
|
|
|
}
|
|
|
|
|
|
|
|
void mutex_wake_waiters(struct mutex_t *mutex, int flags) {
|
|
|
|
if ( ! (flags & MUTEX_INISR)) dINT();
|
2010-10-28 11:22:57 +02:00
|
|
|
DEBUG("%s: waking up waiters.\n", active_thread->name);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
queue_node_t *next = queue_remove_head(&(mutex->queue));
|
2010-11-01 13:29:40 +01:00
|
|
|
|
|
|
|
/* queue is empty */
|
|
|
|
if (!next) {
|
2010-11-01 15:53:33 +01:00
|
|
|
DEBUG("%s: no waiters?\n", active_thread->name);
|
2010-11-01 13:29:40 +01:00
|
|
|
mutex->val = 0;
|
2010-11-01 13:45:30 +01:00
|
|
|
if ( ! (flags & MUTEX_INISR)) eINT();
|
2010-11-01 13:29:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
tcb* process = (tcb*)next->data;
|
|
|
|
|
|
|
|
sched_set_status(process, STATUS_PENDING);
|
|
|
|
|
|
|
|
if ( mutex->queue.next != NULL) {
|
|
|
|
mutex->val = -1;
|
|
|
|
} else {
|
|
|
|
mutex->val = process->pid;
|
|
|
|
}
|
|
|
|
|
2010-10-28 11:22:57 +02:00
|
|
|
DEBUG("%s: waiters woken up.\n", active_thread->name);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
/* If called from process, reenable interrupts, yield if requested */
|
|
|
|
if (! (flags & MUTEX_INISR)) {
|
|
|
|
eINT();
|
2010-10-28 11:22:57 +02:00
|
|
|
if (flags & MUTEX_YIELD) thread_yield();
|
2010-09-22 15:10:42 +02:00
|
|
|
} else {
|
2010-10-28 11:22:57 +02:00
|
|
|
sched_context_switch_request = 1;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|