2013-09-20 15:30:42 +02:00
|
|
|
/**
|
|
|
|
* Semaphore implemenation
|
|
|
|
*
|
2014-03-04 22:42:57 +01:00
|
|
|
* Copyright (C) 2013 Freie Universität Berlin
|
2013-09-20 15:30: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-09-20 15:30:42 +02:00
|
|
|
*
|
2014-03-04 22:42:57 +01:00
|
|
|
* @ingroup posix
|
|
|
|
*
|
2013-09-20 15:30:42 +02:00
|
|
|
* @{
|
2014-03-04 22:42:57 +01:00
|
|
|
*
|
2015-05-22 07:34:41 +02:00
|
|
|
* @file
|
2013-09-20 15:30:42 +02:00
|
|
|
* @brief Implementation of semaphores with priority queues
|
2014-03-04 22:42:57 +01:00
|
|
|
*
|
2013-09-20 15:30:42 +02:00
|
|
|
* @author Christian Mehlis <mehlis@inf.fu-berlin.de>
|
|
|
|
* @author René Kijewski <kijewski@inf.fu-berlin.de>
|
2014-03-04 22:42:57 +01:00
|
|
|
*
|
2013-09-20 15:30:42 +02:00
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
|
|
#include "irq.h"
|
|
|
|
#include "sched.h"
|
|
|
|
#include "tcb.h"
|
|
|
|
#include "thread.h"
|
|
|
|
|
|
|
|
#define ENABLE_DEBUG (0)
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
#include "semaphore.h"
|
|
|
|
|
|
|
|
int sem_init(sem_t *sem, int pshared, unsigned int value)
|
|
|
|
{
|
|
|
|
(void) pshared; /* nothing to do */
|
|
|
|
|
|
|
|
sem->value = value;
|
|
|
|
|
|
|
|
/* waiters for the mutex */
|
2014-05-07 00:41:21 +02:00
|
|
|
sem->queue.first = NULL;
|
2013-09-20 15:30:42 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sem_destroy(sem_t *sem)
|
|
|
|
{
|
2014-05-07 00:41:21 +02:00
|
|
|
if (sem->queue.first) {
|
2014-11-05 11:04:54 +01:00
|
|
|
DEBUG("sem_destroy: %" PRIkernel_pid ": tried to destroy active semaphore.\n",
|
|
|
|
sched_active_thread->pid);
|
2013-09-20 15:30:42 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sem_t *sem_open(const char *name, int oflag, ...)
|
|
|
|
{
|
|
|
|
(void) name; /* named semaphore currently not supported */
|
|
|
|
(void) oflag;
|
|
|
|
return SEM_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sem_close(sem_t *sem)
|
|
|
|
{
|
|
|
|
(void) sem; /* named semaphore currently not supported */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sem_unlink(const char *name)
|
|
|
|
{
|
|
|
|
(void) name; /* named semaphore currently not supported */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sem_wait(sem_t *sem)
|
|
|
|
{
|
2014-02-14 17:47:33 +01:00
|
|
|
while (1) {
|
2014-11-05 11:04:54 +01:00
|
|
|
unsigned old_state = disableIRQ();
|
|
|
|
|
2013-09-20 15:30:42 +02:00
|
|
|
unsigned value = sem->value;
|
2014-11-05 11:04:54 +01:00
|
|
|
if (value != 0) {
|
2013-09-20 15:30:42 +02:00
|
|
|
sem->value = value - 1;
|
2014-11-05 11:04:54 +01:00
|
|
|
restoreIRQ(old_state);
|
|
|
|
return 1;
|
2013-09-20 15:30:42 +02:00
|
|
|
}
|
2014-11-05 11:04:54 +01:00
|
|
|
|
|
|
|
/* I'm going blocked */
|
|
|
|
priority_queue_node_t n;
|
|
|
|
n.priority = (uint32_t) sched_active_thread->priority;
|
|
|
|
n.data = (uintptr_t) sched_active_thread;
|
|
|
|
n.next = NULL;
|
|
|
|
priority_queue_add(&sem->queue, &n);
|
|
|
|
|
|
|
|
DEBUG("sem_wait: %" PRIkernel_pid ": Adding node to mutex queue: prio: %" PRIu32 "\n",
|
|
|
|
sched_active_thread->pid, sched_active_thread->priority);
|
|
|
|
|
|
|
|
/* scheduler should schedule an other thread, that unlocks the
|
|
|
|
* mutex in the future, when this happens I get scheduled again
|
|
|
|
*/
|
|
|
|
sched_set_status((tcb_t*) sched_active_thread, STATUS_MUTEX_BLOCKED);
|
|
|
|
restoreIRQ(old_state);
|
|
|
|
thread_yield_higher();
|
2013-09-20 15:30:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int sem_timedwait(sem_t *sem, const struct timespec *abstime)
|
|
|
|
{
|
|
|
|
(void) sem; /* timedwait currently not supported */
|
|
|
|
(void) abstime;
|
|
|
|
return -1; /* signal problem */
|
|
|
|
}
|
|
|
|
|
|
|
|
int sem_trywait(sem_t *sem)
|
|
|
|
{
|
2014-10-27 16:18:40 +01:00
|
|
|
unsigned old_state = disableIRQ();
|
2013-09-20 15:30:42 +02:00
|
|
|
int result;
|
|
|
|
|
|
|
|
unsigned value = sem->value;
|
|
|
|
if (value == 0) {
|
|
|
|
result = -1;
|
2014-02-16 20:50:48 +01:00
|
|
|
}
|
|
|
|
else {
|
2013-09-20 15:30:42 +02:00
|
|
|
result = 0;
|
|
|
|
sem->value = value - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
restoreIRQ(old_state);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sem_post(sem_t *sem)
|
|
|
|
{
|
2014-10-27 16:18:40 +01:00
|
|
|
unsigned old_state = disableIRQ();
|
2013-09-20 15:30:42 +02:00
|
|
|
++sem->value;
|
|
|
|
|
2014-07-29 09:21:11 +02:00
|
|
|
priority_queue_node_t *next = priority_queue_remove_head(&sem->queue);
|
2013-09-20 15:30:42 +02:00
|
|
|
if (next) {
|
|
|
|
tcb_t *next_process = (tcb_t*) next->data;
|
2014-11-05 11:04:54 +01:00
|
|
|
DEBUG("sem_post: %" PRIkernel_pid ": waking up %" PRIkernel_pid "\n",
|
|
|
|
sched_active_thread->pid, next_process->pid);
|
2013-09-20 15:30:42 +02:00
|
|
|
sched_set_status(next_process, STATUS_PENDING);
|
2014-11-05 11:04:54 +01:00
|
|
|
|
|
|
|
uint16_t prio = next_process->priority;
|
|
|
|
restoreIRQ(old_state);
|
|
|
|
sched_switch(prio);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
restoreIRQ(old_state);
|
2013-09-20 15:30:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sem_getvalue(sem_t *sem, int *sval)
|
|
|
|
{
|
|
|
|
*sval = sem->value;
|
|
|
|
return 0;
|
|
|
|
}
|