2013-11-27 16:28:31 +01:00
|
|
|
/*
|
2013-06-18 17:21:38 +02:00
|
|
|
* Copyright (C) 2013 Freie Universität Berlin
|
2010-09-22 15:10:42 +02:00
|
|
|
*
|
2014-07-31 19:45:27 +02:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
2013-11-27 16:28:31 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup core_sync
|
2010-09-22 15:10:42 +02:00
|
|
|
* @{
|
2013-11-27 16:28:31 +01:00
|
|
|
*
|
2015-05-22 07:34:41 +02:00
|
|
|
* @file
|
2013-11-27 16:28:31 +01:00
|
|
|
* @brief Kernel mutex implementation
|
|
|
|
*
|
2014-01-28 11:50:12 +01:00
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
2015-04-26 09:21:41 +02:00
|
|
|
* @author Joakim Gebart <joakim.gebart@eistec.se>
|
2013-11-27 16:28:31 +01:00
|
|
|
*
|
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"
|
2014-02-18 12:50:30 +01:00
|
|
|
#include "tcb.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
#include "atomic.h"
|
|
|
|
#include "kernel.h"
|
2010-10-28 11:22:57 +02:00
|
|
|
#include "sched.h"
|
2013-12-19 12:16:35 +01:00
|
|
|
#include "thread.h"
|
2013-07-23 13:39:50 +02:00
|
|
|
#include "irq.h"
|
2014-03-17 17:59:06 +01:00
|
|
|
#include "thread.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2014-03-03 00:24:40 +01:00
|
|
|
#define ENABLE_DEBUG (0)
|
2013-07-23 13:39:50 +02:00
|
|
|
#include "debug.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2014-04-01 16:31:48 +02:00
|
|
|
static void mutex_wait(struct mutex_t *mutex);
|
|
|
|
|
2013-06-20 18:18:29 +02:00
|
|
|
int mutex_trylock(struct mutex_t *mutex)
|
|
|
|
{
|
2015-04-26 09:21:41 +02:00
|
|
|
DEBUG("%s: trylocking to get mutex. val: %u\n", sched_active_thread->name, ATOMIC_VALUE(mutex->val));
|
|
|
|
return atomic_set_to_one(&mutex->val);
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2014-07-18 13:44:11 +02:00
|
|
|
void mutex_lock(struct mutex_t *mutex)
|
2013-06-20 18:18:29 +02:00
|
|
|
{
|
2015-04-26 09:21:41 +02:00
|
|
|
DEBUG("%s: trying to get mutex. val: %u\n", sched_active_thread->name, ATOMIC_VALUE(mutex->val));
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2015-04-26 09:21:41 +02:00
|
|
|
if (atomic_set_to_one(&mutex->val) == 0) {
|
2013-06-20 18:18:29 +02:00
|
|
|
/* mutex was locked. */
|
2010-09-22 15:10:42 +02:00
|
|
|
mutex_wait(mutex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-01 16:31:48 +02:00
|
|
|
static void mutex_wait(struct mutex_t *mutex)
|
2013-06-20 18:18:29 +02:00
|
|
|
{
|
2014-10-27 16:18:40 +01:00
|
|
|
unsigned irqstate = disableIRQ();
|
2015-04-26 09:21:41 +02:00
|
|
|
DEBUG("%s: Mutex in use. %u\n", sched_active_thread->name, ATOMIC_VALUE(mutex->val));
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2015-04-26 09:21:41 +02:00
|
|
|
if (atomic_set_to_one(&mutex->val)) {
|
2013-06-20 18:18:29 +02:00
|
|
|
/* somebody released the mutex. return. */
|
2015-04-26 09:21:41 +02:00
|
|
|
DEBUG("%s: mutex_wait early out. %u\n", sched_active_thread->name, ATOMIC_VALUE(mutex->val));
|
2010-11-11 09:55:08 +01:00
|
|
|
restoreIRQ(irqstate);
|
2010-09-22 15:10:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
sched_set_status((tcb_t*) sched_active_thread, STATUS_MUTEX_BLOCKED);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2014-07-29 09:21:11 +02:00
|
|
|
priority_queue_node_t n;
|
2014-04-10 22:28:35 +02:00
|
|
|
n.priority = (unsigned int) sched_active_thread->priority;
|
|
|
|
n.data = (unsigned int) sched_active_thread;
|
2010-09-22 15:10:42 +02:00
|
|
|
n.next = NULL;
|
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
DEBUG("%s: Adding node to mutex queue: prio: %" PRIu32 "\n", sched_active_thread->name, n.priority);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2014-07-29 09:21:11 +02:00
|
|
|
priority_queue_add(&(mutex->queue), &n);
|
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
|
|
|
|
2014-10-18 01:24:49 +02:00
|
|
|
thread_yield_higher();
|
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
|
|
|
{
|
2015-04-26 09:21:41 +02:00
|
|
|
DEBUG("%s: unlocking mutex. val: %u pid: %" PRIkernel_pid "\n", sched_active_thread->name, ATOMIC_VALUE(mutex->val), sched_active_pid);
|
2014-10-27 16:18:40 +01:00
|
|
|
unsigned irqstate = disableIRQ();
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2015-04-26 09:21:41 +02:00
|
|
|
if (ATOMIC_VALUE(mutex->val) != 0) {
|
2014-07-29 09:21:11 +02:00
|
|
|
priority_queue_node_t *next = priority_queue_remove_head(&(mutex->queue));
|
2014-05-07 00:41:21 +02:00
|
|
|
if (next) {
|
2014-04-01 16:31:48 +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);
|
|
|
|
|
2014-04-25 18:20:42 +02:00
|
|
|
sched_switch(process->priority);
|
2013-06-20 18:18:29 +02:00
|
|
|
}
|
|
|
|
else {
|
2015-04-26 09:21:41 +02:00
|
|
|
ATOMIC_VALUE(mutex->val) = 0; /* This is safe, interrupts are disabled */
|
2010-11-11 09:55:08 +01:00
|
|
|
}
|
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
|
|
|
}
|
2014-02-18 08:25:16 +01:00
|
|
|
|
|
|
|
void mutex_unlock_and_sleep(struct mutex_t *mutex)
|
|
|
|
{
|
2015-04-26 09:21:41 +02:00
|
|
|
DEBUG("%s: unlocking mutex. val: %u pid: %" PRIkernel_pid ", and taking a nap\n", sched_active_thread->name, ATOMIC_VALUE(mutex->val), sched_active_pid);
|
2014-10-27 16:18:40 +01:00
|
|
|
unsigned irqstate = disableIRQ();
|
2014-02-18 08:25:16 +01:00
|
|
|
|
2015-04-26 09:21:41 +02:00
|
|
|
if (ATOMIC_VALUE(mutex->val) != 0) {
|
2014-07-29 09:21:11 +02:00
|
|
|
priority_queue_node_t *next = priority_queue_remove_head(&(mutex->queue));
|
2014-05-07 00:41:21 +02:00
|
|
|
if (next) {
|
2014-04-01 16:31:48 +02:00
|
|
|
tcb_t *process = (tcb_t *) next->data;
|
2014-02-18 08:25:16 +01:00
|
|
|
DEBUG("%s: waking up waiter.\n", process->name);
|
|
|
|
sched_set_status(process, STATUS_PENDING);
|
|
|
|
}
|
|
|
|
else {
|
2015-04-26 09:21:41 +02:00
|
|
|
ATOMIC_VALUE(mutex->val) = 0; /* This is safe, interrupts are disabled */
|
2014-02-18 08:25:16 +01:00
|
|
|
}
|
|
|
|
}
|
2014-04-10 22:28:35 +02:00
|
|
|
DEBUG("%s: going to sleep.\n", sched_active_thread->name);
|
|
|
|
sched_set_status((tcb_t*) sched_active_thread, STATUS_SLEEPING);
|
2014-02-18 08:25:16 +01:00
|
|
|
restoreIRQ(irqstate);
|
2014-10-18 01:24:49 +02:00
|
|
|
thread_yield_higher();
|
2014-02-18 08:25:16 +01:00
|
|
|
}
|