2015-08-03 18:52:32 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013-15 Freie Universität Berlin
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*
|
|
|
|
* @author Christian Mehlis <mehlis@inf.fu-berlin.de>
|
|
|
|
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
|
|
|
|
* @author René Kijewski <kijewski@inf.fu-berlin.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#include "irq.h"
|
|
|
|
#include "msg.h"
|
2015-10-25 15:44:42 +01:00
|
|
|
#include "xtimer.h"
|
2015-08-03 18:52:32 +02:00
|
|
|
|
2015-10-19 16:15:37 +02:00
|
|
|
#include "sema.h"
|
2015-08-03 18:52:32 +02:00
|
|
|
|
|
|
|
#define ENABLE_DEBUG (0)
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
#define MSG_SIGNAL (0x0501)
|
|
|
|
#define MSG_TIMEOUT (0x0502)
|
|
|
|
#define MSG_DESTROYED (0x0503)
|
|
|
|
|
2015-10-19 16:25:06 +02:00
|
|
|
int sema_create(sema_t *sema, unsigned int value)
|
2015-08-03 18:52:32 +02:00
|
|
|
{
|
2015-10-19 16:25:06 +02:00
|
|
|
if (sema == NULL) {
|
2015-08-03 18:52:32 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2015-10-19 16:25:06 +02:00
|
|
|
sema->value = value;
|
2015-08-03 18:52:32 +02:00
|
|
|
/* waiters for the mutex */
|
2015-10-19 16:25:06 +02:00
|
|
|
sema->queue.first = NULL;
|
2015-08-03 18:52:32 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-19 16:25:06 +02:00
|
|
|
int sema_destroy(sema_t *sema)
|
2015-08-03 18:52:32 +02:00
|
|
|
{
|
|
|
|
unsigned int old_state;
|
|
|
|
priority_queue_node_t *next;
|
2015-12-01 19:11:45 +01:00
|
|
|
|
2015-10-19 16:25:06 +02:00
|
|
|
if (sema == NULL) {
|
2015-08-03 18:52:32 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2016-03-19 09:25:47 +01:00
|
|
|
old_state = irq_disable();
|
2015-10-19 16:25:06 +02:00
|
|
|
while ((next = priority_queue_remove_head(&sema->queue)) != NULL) {
|
2015-08-03 18:52:32 +02:00
|
|
|
msg_t msg;
|
|
|
|
kernel_pid_t pid = (kernel_pid_t)next->data;
|
|
|
|
msg.type = MSG_DESTROYED;
|
2015-10-19 16:25:06 +02:00
|
|
|
msg.content.ptr = (void *) sema;
|
2015-08-03 18:52:32 +02:00
|
|
|
msg_send_int(&msg, pid);
|
|
|
|
}
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(old_state);
|
2015-08-03 18:52:32 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-25 15:44:42 +01:00
|
|
|
int sema_wait_timed_msg(sema_t *sema, uint64_t timeout, msg_t *msg)
|
2015-08-03 18:52:32 +02:00
|
|
|
{
|
2015-11-11 11:30:12 +01:00
|
|
|
unsigned old_state;
|
|
|
|
msg_t timeout_msg;
|
|
|
|
xtimer_t timeout_timer;
|
2015-12-01 19:11:45 +01:00
|
|
|
|
2015-10-19 16:25:06 +02:00
|
|
|
if (sema == NULL) {
|
2015-08-03 18:52:32 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2015-11-11 11:30:12 +01:00
|
|
|
if (timeout != 0) {
|
2016-03-19 09:25:47 +01:00
|
|
|
old_state = irq_disable();
|
2015-12-01 19:09:36 +01:00
|
|
|
timeout_timer.target = 0, timeout_timer.long_target = 0;
|
2015-11-11 11:30:12 +01:00
|
|
|
timeout_msg.type = MSG_TIMEOUT;
|
|
|
|
timeout_msg.content.ptr = (char *)sema;
|
|
|
|
/* we will stay in the same stack context so we can use timeout_msg */
|
|
|
|
xtimer_set_msg64(&timeout_timer, timeout, &timeout_msg, sched_active_pid);
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(old_state);
|
2015-11-11 11:30:12 +01:00
|
|
|
}
|
2015-08-03 18:52:32 +02:00
|
|
|
while (1) {
|
|
|
|
priority_queue_node_t n;
|
2015-11-11 11:30:12 +01:00
|
|
|
unsigned value;
|
2015-08-03 18:52:32 +02:00
|
|
|
|
2016-03-19 09:25:47 +01:00
|
|
|
old_state = irq_disable();
|
2015-11-11 11:30:12 +01:00
|
|
|
value = sema->value;
|
2015-08-03 18:52:32 +02:00
|
|
|
if (value != 0) {
|
2015-10-19 16:25:06 +02:00
|
|
|
sema->value = value - 1;
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(old_state);
|
2015-08-03 18:52:32 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* I'm going blocked */
|
|
|
|
n.priority = (uint32_t)sched_active_thread->priority;
|
|
|
|
n.data = (unsigned int)sched_active_pid;
|
|
|
|
n.next = NULL;
|
2015-10-19 16:25:06 +02:00
|
|
|
priority_queue_add(&sema->queue, &n);
|
2015-08-03 18:52:32 +02:00
|
|
|
|
2015-10-19 16:25:06 +02:00
|
|
|
DEBUG("sema_wait: %" PRIkernel_pid ": Adding node to semaphore queue: prio: %" PRIu32 "\n",
|
2015-08-03 18:52:32 +02:00
|
|
|
sched_active_thread->pid, sched_active_thread->priority);
|
|
|
|
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(old_state);
|
2015-08-03 18:52:32 +02:00
|
|
|
msg_receive(msg);
|
2016-03-19 09:25:47 +01:00
|
|
|
old_state = irq_disable();
|
2015-10-25 15:44:42 +01:00
|
|
|
if (timeout != 0) {
|
|
|
|
xtimer_remove(&timeout_timer);
|
2015-08-03 18:52:32 +02:00
|
|
|
}
|
2015-10-25 15:44:42 +01:00
|
|
|
priority_queue_remove(&sema->queue, &n);
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(old_state);
|
2015-10-25 15:44:42 +01:00
|
|
|
if (msg->content.ptr != (void *)sema) {
|
2015-08-03 18:52:32 +02:00
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (msg->type) {
|
|
|
|
case MSG_SIGNAL:
|
|
|
|
continue;
|
|
|
|
case MSG_TIMEOUT:
|
|
|
|
return -ETIMEDOUT;
|
|
|
|
case MSG_DESTROYED:
|
|
|
|
return -ECANCELED;
|
|
|
|
default:
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-25 15:44:42 +01:00
|
|
|
int sema_wait_timed(sema_t *sema, uint64_t timeout)
|
2015-08-03 18:52:32 +02:00
|
|
|
{
|
|
|
|
int result;
|
2015-12-01 19:11:45 +01:00
|
|
|
|
2015-08-03 18:52:32 +02:00
|
|
|
do {
|
|
|
|
msg_t msg;
|
2015-10-19 16:25:06 +02:00
|
|
|
result = sema_wait_timed_msg(sema, timeout, &msg);
|
|
|
|
DEBUG("sema_wait: %" PRIkernel_pid ": Discarding message from %" PRIkernel_pid "\n",
|
2015-08-03 18:52:32 +02:00
|
|
|
sched_active_thread->pid, msg->sender_pid);
|
2015-12-01 19:11:45 +01:00
|
|
|
} while (result == -EAGAIN);
|
2015-08-03 18:52:32 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-10-19 16:25:06 +02:00
|
|
|
int sema_post(sema_t *sema)
|
2015-08-03 18:52:32 +02:00
|
|
|
{
|
|
|
|
unsigned int old_state, value;
|
|
|
|
priority_queue_node_t *next;
|
2015-12-01 19:11:45 +01:00
|
|
|
|
2015-10-19 16:25:06 +02:00
|
|
|
if (sema == NULL) {
|
2015-08-03 18:52:32 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2016-03-19 09:25:47 +01:00
|
|
|
old_state = irq_disable();
|
2015-10-19 16:25:06 +02:00
|
|
|
value = sema->value;
|
2015-08-03 18:52:32 +02:00
|
|
|
if (value == UINT_MAX) {
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(old_state);
|
2015-08-03 18:52:32 +02:00
|
|
|
return -EOVERFLOW;
|
|
|
|
}
|
2015-10-19 16:25:06 +02:00
|
|
|
++sema->value;
|
|
|
|
next = priority_queue_remove_head(&sema->queue);
|
2015-08-03 18:52:32 +02:00
|
|
|
if (next) {
|
|
|
|
uint16_t prio = (uint16_t)next->priority;
|
|
|
|
kernel_pid_t pid = (kernel_pid_t) next->data;
|
|
|
|
msg_t msg;
|
2015-10-19 16:25:06 +02:00
|
|
|
DEBUG("sema_post: %" PRIkernel_pid ": waking up %" PRIkernel_pid "\n",
|
2015-08-03 18:52:32 +02:00
|
|
|
sched_active_thread->pid, next_process->pid);
|
|
|
|
msg.type = MSG_SIGNAL;
|
2015-10-19 16:25:06 +02:00
|
|
|
msg.content.ptr = (void *) sema;
|
2015-08-03 18:52:32 +02:00
|
|
|
msg_send_int(&msg, pid);
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(old_state);
|
2015-08-03 18:52:32 +02:00
|
|
|
sched_switch(prio);
|
|
|
|
}
|
|
|
|
else {
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(old_state);
|
2015-08-03 18:52:32 +02:00
|
|
|
}
|
|
|
|
|
2015-12-26 17:30:35 +01:00
|
|
|
return 0;
|
2015-08-03 18:52:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|