diff --git a/sys/include/sema.h b/sys/include/sema.h index 6d647e4dcd..4d5d724778 100644 --- a/sys/include/sema.h +++ b/sys/include/sema.h @@ -7,7 +7,7 @@ */ /** - * @defgroup sys_sem Semaphores + * @defgroup sys_sema Semaphores * @ingroup sys * @brief Lightweight semaphore implementation * @{ @@ -22,6 +22,7 @@ #ifndef SEM_H_ #define SEM_H_ +#include "msg.h" #include "priority_queue.h" #include "timex.h" @@ -35,7 +36,7 @@ extern "C" { typedef struct { volatile unsigned int value; /**< value of the semaphore */ priority_queue_t queue; /**< list of threads waiting for the semaphore */ -} sem_t; +} sema_t; /** * @brief Creates semaphore. @@ -44,13 +45,13 @@ typedef struct { * The Open Group Base Specifications Issue 7, sem_init() * (without `pshared` parameter) * - * @param[out] sem The created semaphore. + * @param[out] sema The created semaphore. * @param[in] value Initial value for the semaphore. * * @return 0 on success. * @return -EINVAL, if semaphore is invalid. */ -int sem_create(sem_t *sem, unsigned int value); +int sema_create(sema_t *sema, unsigned int value); /** * @brief Destroys a semaphore. @@ -59,19 +60,19 @@ int sem_create(sem_t *sem, unsigned int value); * The Open Group Base Specifications Issue 7, sem_destroy() * * - * @param[in] sem The semaphore to destroy. + * @param[in] sema The semaphore to destroy. * * @return 0 on success. * @return -EINVAL, if semaphore is invalid. */ -int sem_destroy(sem_t *sem); +int sema_destroy(sema_t *sema); /** * @brief Wait for a semaphore being posted. * * @pre Message queue of active thread is initialized (see @ref msg_init_queue()). * - * @param[in] sem A semaphore. + * @param[in] sema A semaphore. * @param[in] timeout Time until the semaphore times out. NULL for no timeout. * @param[out] msg Container for a spurious message during the timed wait (result == -EAGAIN). * @@ -81,12 +82,12 @@ int sem_destroy(sem_t *sem); * @return -ECANCELED, if the semaphore was destroyed. * @return -EAGAIN, if the thread received a message while waiting for the lock. */ -int sem_wait_timed_msg(sem_t *sem, timex_t *timeout, msg_t *msg); +int sema_wait_timed_msg(sema_t *sema, timex_t *timeout, msg_t *msg); /** * @brief Wait for a semaphore being posted (without timeout). * - * @param[in] sem A semaphore. + * @param[in] sema A semaphore. * @param[out] msg Container for a spurious message during the timed wait (result == -EAGAIN). * * @return 0 on success @@ -94,16 +95,16 @@ int sem_wait_timed_msg(sem_t *sem, timex_t *timeout, msg_t *msg); * @return -ECANCELED, if the semaphore was destroyed. * @return -EAGAIN, if the thread received a message while waiting for the lock. */ -static inline int sem_wait_msg(sem_t *sem, msg_t *msg) +static inline int sema_wait_msg(sema_t *sema, msg_t *msg) { - return sem_wait_timed_msg(sem, NULL, msg); + return sema_wait_timed_msg(sema, NULL, msg); } /** * @brief Wait for a semaphore being posted (dropping spurious messages). * @details Any spurious messages received while waiting for the semaphore are silently dropped. * - * @param[in] sem A semaphore. + * @param[in] sema A semaphore. * @param[in] timeout Time until the semaphore times out. NULL for no timeout. * * @return 0 on success @@ -111,31 +112,31 @@ static inline int sem_wait_msg(sem_t *sem, msg_t *msg) * @return -ETIMEDOUT, if the semaphore times out. * @return -ECANCELED, if the semaphore was destroyed. */ -int sem_wait_timed(sem_t *sem, timex_t *timeout); +int sema_wait_timed(sema_t *sema, timex_t *timeout); /** * @brief Wait for a semaphore being posted (without timeout, dropping spurious messages). * - * @param[in] sem A semaphore. + * @param[in] sema A semaphore. * * @return 0 on success * @return -EINVAL, if semaphore is invalid. * @return -ECANCELED, if the semaphore was destroyed. */ -static inline int sem_wait(sem_t *sem) +static inline int sema_wait(sema_t *sema) { - return sem_wait_timed(sem, NULL); + return sema_wait_timed(sema, NULL); } /** * @brief Signal semaphore. * - * @param[in] sem A semaphore. + * @param[in] sema A semaphore. * * @return -EINVAL, if semaphore is invalid. * @return -EOVERFLOW, if the semaphore's value would overflow. */ -int sem_post(sem_t *sem); +int sema_post(sema_t *sema); #ifdef __cplusplus } diff --git a/sys/posix/include/semaphore.h b/sys/posix/include/semaphore.h index 1ffe83c1e4..fabeb337e1 100644 --- a/sys/posix/include/semaphore.h +++ b/sys/posix/include/semaphore.h @@ -32,6 +32,8 @@ extern "C" { #endif +typedef sema_t sem_t; + /** * @brief Value returned if `sem_open' failed. */ @@ -58,7 +60,11 @@ extern "C" { * @return 0 on success. * @return -EINVAL, if semaphore is invalid. */ -#define sem_init(sem, pshared, value) sem_create(sem, value) +#define sem_init(sem, pshared, value) sema_create((sema_t *)(sem), (value)) + +#define sem_destroy(sem) sema_destroy((sema_t *)(sem)) +#define sem_post(sem) sema_post((sema_t *)(sem)) +#define sem_wait(sem) sema_wait((sema_t *)(sem)) /** * @brief Open a named semaphore @p name with open flags @p oflag. diff --git a/sys/posix/semaphore/posix_semaphore.c b/sys/posix/semaphore/posix_semaphore.c index 1076aae08a..4d91053de2 100644 --- a/sys/posix/semaphore/posix_semaphore.c +++ b/sys/posix/semaphore/posix_semaphore.c @@ -42,7 +42,7 @@ int sem_timedwait(sem_t *sem, const struct timespec *abstime) return -ETIMEDOUT; } timeout = timex_sub(timeout, now); - res = sem_wait_timed(sem, &timeout); + res = sema_wait_timed(sem, &timeout); if (res < 0) { errno = -res; } diff --git a/sys/sema/sema.c b/sys/sema/sema.c index 0cc1cff90e..e5c3b0ca5f 100644 --- a/sys/sema/sema.c +++ b/sys/sema/sema.c @@ -33,45 +33,39 @@ #define MSG_TIMEOUT (0x0502) #define MSG_DESTROYED (0x0503) -int sem_create(sem_t *sem, unsigned int value) +int sema_create(sema_t *sema, unsigned int value) { - if (sem == NULL) { -#ifdef MODULE_POSIX_SEMAPHORE - errno = EINVAL; -#endif + if (sema == NULL) { return -EINVAL; } - sem->value = value; + sema->value = value; /* waiters for the mutex */ - sem->queue.first = NULL; + sema->queue.first = NULL; return 0; } -int sem_destroy(sem_t *sem) +int sema_destroy(sema_t *sema) { unsigned int old_state; priority_queue_node_t *next; - if (sem == NULL) { -#ifdef MODULE_POSIX_SEMAPHORE - errno = EINVAL; -#endif + if (sema == NULL) { return -EINVAL; } old_state = disableIRQ(); - while ((next = priority_queue_remove_head(&sem->queue)) != NULL) { + while ((next = priority_queue_remove_head(&sema->queue)) != NULL) { msg_t msg; kernel_pid_t pid = (kernel_pid_t)next->data; msg.type = MSG_DESTROYED; - msg.content.ptr = (void *) sem; + msg.content.ptr = (void *) sema; msg_send_int(&msg, pid); } restoreIRQ(old_state); return 0; } -int sem_wait_timed_msg(sem_t *sem, timex_t *timeout, msg_t *msg) +int sema_wait_timed_msg(sema_t *sema, timex_t *timeout, msg_t *msg) { - if (sem == NULL) { + if (sema == NULL) { return -EINVAL; } while (1) { @@ -79,9 +73,9 @@ int sem_wait_timed_msg(sem_t *sem, timex_t *timeout, msg_t *msg) priority_queue_node_t n; vtimer_t timeout_timer; - unsigned value = sem->value; + unsigned value = sema->value; if (value != 0) { - sem->value = value - 1; + sema->value = value - 1; restoreIRQ(old_state); return 0; } @@ -90,14 +84,14 @@ int sem_wait_timed_msg(sem_t *sem, timex_t *timeout, msg_t *msg) n.priority = (uint32_t)sched_active_thread->priority; n.data = (unsigned int)sched_active_pid; n.next = NULL; - priority_queue_add(&sem->queue, &n); + priority_queue_add(&sema->queue, &n); - DEBUG("sem_wait: %" PRIkernel_pid ": Adding node to semaphore queue: prio: %" PRIu32 "\n", + DEBUG("sema_wait: %" PRIkernel_pid ": Adding node to semaphore queue: prio: %" PRIu32 "\n", sched_active_thread->pid, sched_active_thread->priority); if (timeout != NULL) { vtimer_set_msg(&timeout_timer, *timeout, sched_active_pid, - MSG_TIMEOUT, sem); + MSG_TIMEOUT, sema); } restoreIRQ(old_state); @@ -107,7 +101,7 @@ int sem_wait_timed_msg(sem_t *sem, timex_t *timeout, msg_t *msg) vtimer_remove(&timeout_timer); /* remove timer just to be sure */ } - if (msg->content.ptr != (void *) sem) { + if (msg->content.ptr != (void *) sema) { return -EAGAIN; } @@ -124,41 +118,42 @@ int sem_wait_timed_msg(sem_t *sem, timex_t *timeout, msg_t *msg) } } -int sem_wait_timed(sem_t *sem, timex_t *timeout) +int sema_wait_timed(sema_t *sema, timex_t *timeout) { int result; do { msg_t msg; - result = sem_wait_timed_msg(sem, timeout, &msg); - DEBUG("sem_wait: %" PRIkernel_pid ": Discarding message from %" PRIkernel_pid "\n", + result = sema_wait_timed_msg(sema, timeout, &msg); + DEBUG("sema_wait: %" PRIkernel_pid ": Discarding message from %" PRIkernel_pid "\n", sched_active_thread->pid, msg->sender_pid); - } while (result == -EAGAIN); + } + while (result == -EAGAIN); return result; } -int sem_post(sem_t *sem) +int sema_post(sema_t *sema) { unsigned int old_state, value; priority_queue_node_t *next; - if (sem == NULL) { + if (sema == NULL) { return -EINVAL; } old_state = disableIRQ(); - value = sem->value; + value = sema->value; if (value == UINT_MAX) { restoreIRQ(old_state); return -EOVERFLOW; } - ++sem->value; - next = priority_queue_remove_head(&sem->queue); + ++sema->value; + next = priority_queue_remove_head(&sema->queue); if (next) { uint16_t prio = (uint16_t)next->priority; kernel_pid_t pid = (kernel_pid_t) next->data; msg_t msg; - DEBUG("sem_post: %" PRIkernel_pid ": waking up %" PRIkernel_pid "\n", + DEBUG("sema_post: %" PRIkernel_pid ": waking up %" PRIkernel_pid "\n", sched_active_thread->pid, next_process->pid); msg.type = MSG_SIGNAL; - msg.content.ptr = (void *) sem; + msg.content.ptr = (void *) sema; msg_send_int(&msg, pid); restoreIRQ(old_state); sched_switch(prio);