2013-11-27 16:28:31 +01:00
|
|
|
/*
|
2015-12-25 20:59:33 +01:00
|
|
|
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
* 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-09-20 13:47:39 +02:00
|
|
|
* @author Joakim Nohlgård <joakim.nohlgard@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"
|
2016-01-04 22:29:34 +01:00
|
|
|
#include "thread.h"
|
2010-10-28 11:22:57 +02:00
|
|
|
#include "sched.h"
|
2013-07-23 13:39:50 +02:00
|
|
|
#include "irq.h"
|
2015-12-25 20:59:33 +01:00
|
|
|
#include "list.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2020-10-22 11:32:06 +02:00
|
|
|
#define ENABLE_DEBUG 0
|
2013-07-23 13:39:50 +02:00
|
|
|
#include "debug.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2019-05-24 15:24:18 +02:00
|
|
|
int _mutex_lock(mutex_t *mutex, volatile uint8_t *blocking)
|
2013-06-20 18:18:29 +02:00
|
|
|
{
|
2016-03-19 09:25:47 +01:00
|
|
|
unsigned irqstate = irq_disable();
|
2016-04-17 13:38:38 +02:00
|
|
|
|
2020-08-06 10:46:17 +02:00
|
|
|
DEBUG("PID[%" PRIkernel_pid "]: Mutex in use.\n", thread_getpid());
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2015-12-25 20:59:33 +01:00
|
|
|
if (mutex->queue.next == NULL) {
|
|
|
|
/* mutex is unlocked. */
|
|
|
|
mutex->queue.next = MUTEX_LOCKED;
|
2016-04-17 13:38:38 +02:00
|
|
|
DEBUG("PID[%" PRIkernel_pid "]: mutex_wait early out.\n",
|
2020-08-06 10:46:17 +02:00
|
|
|
thread_getpid());
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(irqstate);
|
2015-12-25 20:59:33 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2019-05-24 15:24:18 +02:00
|
|
|
else if (*blocking) {
|
2020-08-06 10:46:17 +02:00
|
|
|
thread_t *me = thread_get_active();
|
2016-04-17 13:38:38 +02:00
|
|
|
DEBUG("PID[%" PRIkernel_pid "]: Adding node to mutex queue: prio: %"
|
2020-08-06 10:46:17 +02:00
|
|
|
PRIu32 "\n", thread_getpid(), (uint32_t)me->priority);
|
2015-12-25 20:59:33 +01:00
|
|
|
sched_set_status(me, STATUS_MUTEX_BLOCKED);
|
|
|
|
if (mutex->queue.next == MUTEX_LOCKED) {
|
2020-03-30 17:02:08 +02:00
|
|
|
mutex->queue.next = (list_node_t *)&me->rq_entry;
|
2015-12-25 20:59:33 +01:00
|
|
|
mutex->queue.next->next = NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
thread_add_to_list(&mutex->queue, me);
|
|
|
|
}
|
|
|
|
irq_restore(irqstate);
|
|
|
|
thread_yield_higher();
|
2016-04-17 13:38:38 +02:00
|
|
|
/* We were woken up by scheduler. Waker removed us from queue.
|
|
|
|
* We have the mutex now. */
|
2015-12-25 20:59:33 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
irq_restore(irqstate);
|
|
|
|
return 0;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-25 20:59:33 +01:00
|
|
|
void mutex_unlock(mutex_t *mutex)
|
2013-06-20 18:18:29 +02:00
|
|
|
{
|
2016-03-19 09:25:47 +01:00
|
|
|
unsigned irqstate = irq_disable();
|
2016-04-17 13:38:38 +02:00
|
|
|
|
2018-11-09 08:41:50 +01:00
|
|
|
DEBUG("mutex_unlock(): queue.next: %p pid: %" PRIkernel_pid "\n",
|
2020-08-06 10:46:17 +02:00
|
|
|
(void *)mutex->queue.next, thread_getpid());
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2015-12-25 20:59:33 +01:00
|
|
|
if (mutex->queue.next == NULL) {
|
2015-01-01 22:52:20 +01:00
|
|
|
/* the mutex was not locked */
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(irqstate);
|
2015-01-01 22:52:20 +01:00
|
|
|
return;
|
|
|
|
}
|
2010-11-11 09:55:08 +01:00
|
|
|
|
2015-12-25 20:59:33 +01:00
|
|
|
if (mutex->queue.next == MUTEX_LOCKED) {
|
|
|
|
mutex->queue.next = NULL;
|
2015-01-01 22:52:20 +01:00
|
|
|
/* the mutex was locked and no thread was waiting for it */
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(irqstate);
|
2015-01-01 22:52:20 +01:00
|
|
|
return;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2016-02-29 01:37:26 +01:00
|
|
|
list_node_t *next = list_remove_head(&mutex->queue);
|
2015-12-25 20:59:33 +01:00
|
|
|
|
2020-03-30 17:02:08 +02:00
|
|
|
thread_t *process = container_of((clist_node_t *)next, thread_t, rq_entry);
|
2015-12-25 20:59:33 +01:00
|
|
|
|
2016-04-17 13:38:38 +02:00
|
|
|
DEBUG("mutex_unlock: waking up waiting thread %" PRIkernel_pid "\n",
|
|
|
|
process->pid);
|
2015-01-01 22:52:20 +01:00
|
|
|
sched_set_status(process, STATUS_PENDING);
|
|
|
|
|
2015-12-25 20:59:33 +01:00
|
|
|
if (!mutex->queue.next) {
|
|
|
|
mutex->queue.next = MUTEX_LOCKED;
|
|
|
|
}
|
|
|
|
|
2015-01-01 22:52:20 +01:00
|
|
|
uint16_t process_priority = process->priority;
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(irqstate);
|
2015-01-01 22:52:20 +01:00
|
|
|
sched_switch(process_priority);
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
2014-02-18 08:25:16 +01:00
|
|
|
|
2015-12-25 20:59:33 +01:00
|
|
|
void mutex_unlock_and_sleep(mutex_t *mutex)
|
2014-02-18 08:25:16 +01:00
|
|
|
{
|
2018-11-09 08:41:50 +01:00
|
|
|
DEBUG("PID[%" PRIkernel_pid "]: unlocking mutex. queue.next: %p, and "
|
2020-08-06 10:46:17 +02:00
|
|
|
"taking a nap\n", thread_getpid(), (void *)mutex->queue.next);
|
2016-03-19 09:25:47 +01:00
|
|
|
unsigned irqstate = irq_disable();
|
2014-02-18 08:25:16 +01:00
|
|
|
|
2015-12-25 20:59:33 +01:00
|
|
|
if (mutex->queue.next) {
|
|
|
|
if (mutex->queue.next == MUTEX_LOCKED) {
|
|
|
|
mutex->queue.next = NULL;
|
2014-02-18 08:25:16 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-12-25 20:59:33 +01:00
|
|
|
list_node_t *next = list_remove_head(&mutex->queue);
|
2020-03-30 17:02:08 +02:00
|
|
|
thread_t *process = container_of((clist_node_t *)next, thread_t,
|
2016-04-17 13:38:38 +02:00
|
|
|
rq_entry);
|
2016-04-08 22:30:44 +02:00
|
|
|
DEBUG("PID[%" PRIkernel_pid "]: waking up waiter.\n", process->pid);
|
2015-12-25 20:59:33 +01:00
|
|
|
sched_set_status(process, STATUS_PENDING);
|
|
|
|
if (!mutex->queue.next) {
|
|
|
|
mutex->queue.next = MUTEX_LOCKED;
|
|
|
|
}
|
2014-02-18 08:25:16 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-25 20:59:33 +01:00
|
|
|
|
2020-08-06 10:46:17 +02:00
|
|
|
DEBUG("PID[%" PRIkernel_pid "]: going to sleep.\n", thread_getpid());
|
|
|
|
sched_set_status(thread_get_active(), STATUS_SLEEPING);
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(irqstate);
|
2014-10-18 01:24:49 +02:00
|
|
|
thread_yield_higher();
|
2014-02-18 08:25:16 +01:00
|
|
|
}
|