Browse Source

sys/posix: adapt semaphore to new implementation

pr/spi.typo
Victor Arino 6 years ago
parent
commit
066db5857a
  1. 29
      sys/posix/include/semaphore.h
  2. 30
      sys/posix/semaphore/posix_semaphore.c

29
sys/posix/include/semaphore.h

@ -1,5 +1,6 @@
/*
* Copyright (C) 2013 Freie Universität Berlin
* Copyright (C) 2017 TriaGnoSys GmbH
* 2013 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
@ -18,6 +19,7 @@
* @author Christian Mehlis <mehlis@inf.fu-berlin.de>
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
* @author René Kijewski <kijewski@inf.fu-berlin.de>
* @author Víctor Ariño <victor.arino@zii.aero>
*/
#ifndef POSIX_SEMAPHORE_H_
@ -62,12 +64,8 @@ typedef sema_t sem_t;
*/
static inline int sem_init(sem_t *sem, int pshared, unsigned value)
{
int res = sema_create((sema_t *)sem, value);
sema_create((sema_t *)sem, value);
(void)pshared;
if (res < 0) {
errno = -res;
return -1;
}
return 0;
}
@ -94,11 +92,7 @@ static inline int sem_init(sem_t *sem, int pshared, unsigned value)
*/
static inline int sem_destroy(sem_t *sem)
{
int res = sema_destroy((sema_t *)sem);
if (res < 0) {
errno = -res;
return -1;
}
sema_destroy((sema_t *)sem);
return 0;
}
@ -127,6 +121,7 @@ static inline int sem_destroy(sem_t *sem)
static inline int sem_post(sem_t *sem)
{
int res = sema_post((sema_t *)sem);
if (res < 0) {
errno = -res;
return -1;
@ -154,6 +149,7 @@ static inline int sem_post(sem_t *sem)
static inline int sem_wait(sem_t *sem)
{
int res = sema_wait((sema_t *)sem);
if (res < 0) {
errno = -res;
return -1;
@ -257,7 +253,16 @@ int sem_timedwait(sem_t *sem, const struct timespec *abstime);
* @return 0 on success.
* @return -1, on error and errno set to indicate the error.
*/
int sem_trywait(sem_t *sem);
static inline int sem_trywait(sem_t *sem)
{
int res = sema_try_wait((sema_t *)sem);
if (res < 0) {
errno = -res;
return -1;
}
return 0;
}
/**
* @brief Get current value of @p sem and store it in @p sval.

30
sys/posix/semaphore/posix_semaphore.c

@ -1,5 +1,6 @@
/*
* Copyright (C) 2013 Freie Universität Berlin
* Copyright (C) 2017 TriaGnoSys GmbH
* 2013 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
@ -14,6 +15,7 @@
* @author Christian Mehlis <mehlis@inf.fu-berlin.de>
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
* @author René Kijewski <kijewski@inf.fu-berlin.de>
* @author Víctor Ariño <victor.arino@zii.aero>
*/
#include <errno.h>
@ -34,8 +36,9 @@
int sem_timedwait(sem_t *sem, const struct timespec *abstime)
{
uint64_t timeout = (((uint64_t)abstime->tv_sec) * SEC_IN_USEC) +
(abstime->tv_nsec / USEC_IN_NS);
(abstime->tv_nsec / USEC_IN_NS);
uint64_t now = xtimer_now_usec64();
if (now > timeout) {
errno = ETIMEDOUT;
return -1;
@ -49,27 +52,4 @@ int sem_timedwait(sem_t *sem, const struct timespec *abstime)
return 0;
}
int sem_trywait(sem_t *sem)
{
unsigned int old_state, value;
int result;
if (sem == NULL) {
errno = EINVAL;
return -1;
}
old_state = irq_disable();
value = sem->value;
if (value == 0) {
errno = EAGAIN;
result = -1;
}
else {
result = 0;
sem->value = value - 1;
}
irq_restore(old_state);
return result;
}
/** @} */

Loading…
Cancel
Save