mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-01-18 12:52:44 +01:00
commit
0ad9d82d8b
@ -85,6 +85,9 @@ endif
|
||||
ifneq (,$(findstring hashes,$(USEMODULE)))
|
||||
DIRS += hashes
|
||||
endif
|
||||
ifneq (,$(findstring semaphore,$(USEMODULE)))
|
||||
DIRS += semaphore
|
||||
endif
|
||||
|
||||
all: $(BINDIR)$(MODULE).a
|
||||
@for i in $(DIRS) ; do "$(MAKE)" -C $$i ; done ;
|
||||
|
102
sys/include/semaphore.h
Normal file
102
sys/include/semaphore.h
Normal file
@ -0,0 +1,102 @@
|
||||
#ifndef _SEMAPHORE_H
|
||||
#define _SEMAPHORE_H 1
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <time.h>
|
||||
|
||||
/** Value returned if `sem_open' failed. */
|
||||
#define SEM_FAILED ((sem_t *) 0)
|
||||
|
||||
#include "queue.h"
|
||||
|
||||
typedef struct sem {
|
||||
volatile unsigned int value;
|
||||
queue_node_t queue;
|
||||
} sem_t;
|
||||
|
||||
/**
|
||||
* @brief Initialize semaphore object SEM to VALUE.
|
||||
*
|
||||
* @param sem Semaphore to initialize
|
||||
* @param pshared unused
|
||||
* @param value Value to set
|
||||
*/
|
||||
int sem_init(sem_t *sem, int pshared, unsigned int value);
|
||||
|
||||
/**
|
||||
* @brief Free resources associated with semaphore object SEM.
|
||||
*
|
||||
* @param sem Semaphore to destroy
|
||||
*/
|
||||
int sem_destroy(sem_t *sem);
|
||||
|
||||
/*
|
||||
* @brief Open a named semaphore NAME with open flags OFLAG.
|
||||
*
|
||||
* @brief WARNING: named semaphore are currently not supported
|
||||
*
|
||||
* @param name Name to set
|
||||
* @param oflag Flags to set
|
||||
*/
|
||||
sem_t *sem_open(const char *name, int oflag, ...);
|
||||
|
||||
/**
|
||||
* @brief Close descriptor for named semaphore SEM.
|
||||
*
|
||||
* @brief WARNING: named semaphore are currently not supported
|
||||
*
|
||||
* @param sem Semaphore to close
|
||||
*/
|
||||
int sem_close(sem_t *sem);
|
||||
|
||||
/**
|
||||
* @brief Remove named semaphore NAME.
|
||||
*
|
||||
* @brief WARNING: named semaphore are currently not supported
|
||||
*
|
||||
* @param name Name to unlink
|
||||
*/
|
||||
int sem_unlink(const char *name);
|
||||
|
||||
/**
|
||||
* @brief Wait for SEM being posted.
|
||||
*
|
||||
* @param sem Semaphore to wait
|
||||
*/
|
||||
int sem_wait(sem_t *sem);
|
||||
|
||||
/**
|
||||
* @brief Similar to `sem_wait' but wait only until ABSTIME.
|
||||
*
|
||||
* @brief WARNING: currently not supported
|
||||
*
|
||||
* @param sem Semaphore to wait on
|
||||
* @param abstime Max time to wait for a post
|
||||
*
|
||||
*/
|
||||
int sem_timedwait(sem_t *sem, const struct timespec *abstime);
|
||||
|
||||
/**
|
||||
* @brief Test whether SEM is posted.
|
||||
*
|
||||
* @param sem Semaphore to trywait on
|
||||
*
|
||||
*/
|
||||
int sem_trywait(sem_t *sem);
|
||||
|
||||
/**
|
||||
* @brief Post SEM.
|
||||
*
|
||||
* @param sem Semaphore to post on
|
||||
*/
|
||||
int sem_post(sem_t *sem);
|
||||
|
||||
/**
|
||||
* @brief Get current value of SEM and store it in *SVAL.
|
||||
*
|
||||
* @param sem Semaphore to get value from
|
||||
* @param sval place whre value goes to
|
||||
*/
|
||||
int sem_getvalue(sem_t *sem, int *sval);
|
||||
|
||||
#endif /* semaphore.h */
|
@ -71,7 +71,7 @@ ipv6_addr_t flowcontrol_init(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
sem_init(&slwin_stat.send_win_not_full, BORDER_SWS);
|
||||
sem_init(&slwin_stat.send_win_not_full, 0, BORDER_SWS);
|
||||
|
||||
for (i = 0; i < BORDER_SWS; i++) {
|
||||
slwin_stat.send_win[i].frame_len = 0;
|
||||
@ -162,7 +162,7 @@ void flowcontrol_deliver_from_uart(border_packet_t *packet, int len)
|
||||
if (in_window(packet->seq_num, slwin_stat.last_ack + 1, slwin_stat.last_frame)) {
|
||||
if (synack_seqnum == packet->seq_num) {
|
||||
synack_seqnum = -1;
|
||||
sem_signal(&connection_established);
|
||||
sem_post(&connection_established);
|
||||
}
|
||||
|
||||
do {
|
||||
@ -170,7 +170,7 @@ void flowcontrol_deliver_from_uart(border_packet_t *packet, int len)
|
||||
slot = &(slwin_stat.send_win[++slwin_stat.last_ack % BORDER_SWS]);
|
||||
vtimer_remove(&slot->timeout);
|
||||
memset(&slot->frame, 0, BORDER_BUFFER_SIZE);
|
||||
sem_signal(&slwin_stat.send_win_not_full);
|
||||
sem_post(&slwin_stat.send_win_not_full);
|
||||
}
|
||||
while (slwin_stat.last_ack != packet->seq_num);
|
||||
}
|
||||
|
@ -1,52 +0,0 @@
|
||||
/**
|
||||
* Semaphore implemenation
|
||||
*
|
||||
* Copyright (C) 2013 INRIA.
|
||||
*
|
||||
* This file subject to the terms and conditions of the GNU Lesser General
|
||||
* Public License. See the file LICENSE in the top level directory for more
|
||||
* details.
|
||||
*
|
||||
* @ingroup sixlowpan
|
||||
* @{
|
||||
* @file semaphore.c
|
||||
* @brief Implemntation of semaphores using mutexes
|
||||
* @author Martin Lenders <mlenders@inf.fu-berlin.de>
|
||||
* @author Oliver Hahm <oliver.hahm@inria.fr>
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include "semaphore.h"
|
||||
|
||||
void sem_init(sem_t *sem, int8_t value)
|
||||
{
|
||||
sem->value = value;
|
||||
mutex_init(&sem->mutex);
|
||||
sem->locked = 0;
|
||||
}
|
||||
|
||||
int sem_wait(sem_t *sem)
|
||||
{
|
||||
int res;
|
||||
|
||||
if (--(sem->value) <= 0 && !sem->locked) {
|
||||
sem->locked = !(sem->locked);
|
||||
res = mutex_lock(&(sem->mutex));
|
||||
|
||||
if (res < 0) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sem_signal(sem_t *sem)
|
||||
{
|
||||
if (++(sem->value) > 0 && sem->locked) {
|
||||
sem->locked = !(sem->locked);
|
||||
mutex_unlock(&(sem->mutex));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
/**
|
||||
* Semaphore data struct and prototypes
|
||||
*
|
||||
* Copyright (C) 2013 INRIA.
|
||||
*
|
||||
* This file subject to the terms and conditions of the GNU Lesser General
|
||||
* Public License. See the file LICENSE in the top level directory for more
|
||||
* details.
|
||||
*
|
||||
* @ingroup sixlowpan
|
||||
* @{
|
||||
* @file semaphore.h
|
||||
* @brief data struct and prototypes for semaphores
|
||||
* @author Martin Lenders <mlenders@inf.fu-berlin.de>
|
||||
* @author Oliver Hahm <oliver.hahm@inria.fr>
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifndef _SIXLOWPAN_SEMAPHORE_H
|
||||
#define _SIXLOWPAN_SEMAPHORE_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "mutex.h"
|
||||
|
||||
typedef struct sem_t {
|
||||
int8_t value;
|
||||
int8_t locked;
|
||||
mutex_t mutex;
|
||||
} sem_t;
|
||||
|
||||
void sem_init(sem_t *sem, int8_t value);
|
||||
int sem_wait(sem_t *sem);
|
||||
int sem_signal(sem_t *sem);
|
||||
|
||||
#endif /* _SIXLOWPAN_SEMAPHORE_H*/
|
5
sys/semaphore/Makefile
Normal file
5
sys/semaphore/Makefile
Normal file
@ -0,0 +1,5 @@
|
||||
INCLUDES = -I../include -I$(RIOTBASE)/core/include
|
||||
MODULE = semaphore
|
||||
|
||||
include $(RIOTBASE)/Makefile.base
|
||||
|
157
sys/semaphore/semaphore.c
Normal file
157
sys/semaphore/semaphore.c
Normal file
@ -0,0 +1,157 @@
|
||||
/**
|
||||
* Semaphore implemenation
|
||||
*
|
||||
* Copyright (C) 2013 INRIA.
|
||||
*
|
||||
* This file subject to the terms and conditions of the GNU Lesser General
|
||||
* Public License. See the file LICENSE in the top level directory for more
|
||||
* details.
|
||||
*
|
||||
* @ingroup sixlowpan
|
||||
* @{
|
||||
* @file semaphore.c
|
||||
* @brief Implementation of semaphores with priority queues
|
||||
* @author Christian Mehlis <mehlis@inf.fu-berlin.de>
|
||||
* @author René Kijewski <kijewski@inf.fu-berlin.de>
|
||||
* @}
|
||||
*/
|
||||
|
||||
#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 */
|
||||
sem->queue.priority = 0;
|
||||
sem->queue.data = 0;
|
||||
sem->queue.next = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sem_destroy(sem_t *sem)
|
||||
{
|
||||
if (sem->queue.next) {
|
||||
DEBUG("%s: tried to destroy active semaphore.\n", active_thread->name);
|
||||
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;
|
||||
}
|
||||
|
||||
static void sem_thread_blocked(sem_t *sem)
|
||||
{
|
||||
/* I'm going blocked */
|
||||
sched_set_status((tcb_t*) active_thread, STATUS_MUTEX_BLOCKED);
|
||||
|
||||
queue_node_t n;
|
||||
n.priority = (uint32_t) active_thread->priority;
|
||||
n.data = (size_t) active_thread;
|
||||
n.next = NULL;
|
||||
|
||||
DEBUG("%s: Adding node to mutex queue: prio: %"PRIu32"\n",
|
||||
active_thread->name, n.priority);
|
||||
|
||||
/* add myself to the waiters queue */
|
||||
queue_priority_add(&sem->queue, &n);
|
||||
|
||||
/* scheduler should schedule an other thread, that unlocks the
|
||||
* mutex in the future, when this happens I get scheduled again
|
||||
*/
|
||||
thread_yield();
|
||||
}
|
||||
|
||||
int sem_wait(sem_t *sem)
|
||||
{
|
||||
int old_state = disableIRQ();
|
||||
for (;;) {
|
||||
unsigned value = sem->value;
|
||||
if (value == 0) {
|
||||
sem_thread_blocked(sem);
|
||||
continue;
|
||||
} else {
|
||||
sem->value = value - 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
restoreIRQ(old_state);
|
||||
return 1;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
int old_state = disableIRQ();
|
||||
int result;
|
||||
|
||||
unsigned value = sem->value;
|
||||
if (value == 0) {
|
||||
result = -1;
|
||||
} else {
|
||||
result = 0;
|
||||
sem->value = value - 1;
|
||||
}
|
||||
|
||||
restoreIRQ(old_state);
|
||||
return result;
|
||||
}
|
||||
|
||||
int sem_post(sem_t *sem)
|
||||
{
|
||||
int old_state = disableIRQ();
|
||||
++sem->value;
|
||||
|
||||
queue_node_t *next = queue_remove_head(&sem->queue);
|
||||
if (next) {
|
||||
tcb_t *next_process = (tcb_t*) next->data;
|
||||
DEBUG("%s: waking up %s\n", active_thread->name, next_process->name);
|
||||
sched_set_status(next_process, STATUS_PENDING);
|
||||
sched_switch(active_thread->priority, next_process->priority, inISR());
|
||||
}
|
||||
|
||||
restoreIRQ(old_state);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sem_getvalue(sem_t *sem, int *sval)
|
||||
{
|
||||
*sval = sem->value;
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user