2015-08-03 19:21:21 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 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
|
|
|
|
* 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 <errno.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
|
|
#include "irq.h"
|
|
|
|
#include "sched.h"
|
2015-10-19 16:15:37 +02:00
|
|
|
#include "sema.h"
|
2015-08-03 19:21:21 +02:00
|
|
|
#include "timex.h"
|
|
|
|
#include "thread.h"
|
2015-10-25 15:53:26 +01:00
|
|
|
#include "xtimer.h"
|
2015-08-03 19:21:21 +02:00
|
|
|
|
|
|
|
#define ENABLE_DEBUG (0)
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
#include "semaphore.h"
|
|
|
|
|
|
|
|
int sem_timedwait(sem_t *sem, const struct timespec *abstime)
|
|
|
|
{
|
2016-07-05 21:32:44 +02:00
|
|
|
uint64_t timeout = (((uint64_t)abstime->tv_sec) * SEC_IN_USEC) +
|
2015-10-25 15:53:26 +01:00
|
|
|
(abstime->tv_nsec / USEC_IN_NS);
|
2016-07-05 21:32:44 +02:00
|
|
|
uint64_t now = xtimer_now_usec64();
|
2015-10-25 15:53:26 +01:00
|
|
|
if (now > timeout) {
|
2015-08-03 19:21:21 +02:00
|
|
|
errno = ETIMEDOUT;
|
2015-10-19 16:43:27 +02:00
|
|
|
return -1;
|
2015-08-03 19:21:21 +02:00
|
|
|
}
|
2015-10-25 15:53:26 +01:00
|
|
|
timeout = timeout - now;
|
2016-07-05 21:32:44 +02:00
|
|
|
int res = sema_wait_timed((sema_t *)sem, timeout);
|
2015-08-03 19:21:21 +02:00
|
|
|
if (res < 0) {
|
|
|
|
errno = -res;
|
2015-10-19 16:43:27 +02:00
|
|
|
return -1;
|
2015-08-03 19:21:21 +02:00
|
|
|
}
|
2015-10-19 16:43:27 +02:00
|
|
|
return 0;
|
2015-08-03 19:21:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int sem_trywait(sem_t *sem)
|
|
|
|
{
|
|
|
|
unsigned int old_state, value;
|
|
|
|
int result;
|
|
|
|
if (sem == NULL) {
|
|
|
|
errno = EINVAL;
|
2015-10-19 16:43:27 +02:00
|
|
|
return -1;
|
2015-08-03 19:21:21 +02:00
|
|
|
}
|
2016-03-19 09:25:47 +01:00
|
|
|
old_state = irq_disable();
|
2015-08-03 19:21:21 +02:00
|
|
|
value = sem->value;
|
|
|
|
if (value == 0) {
|
|
|
|
errno = EAGAIN;
|
2015-10-19 16:43:27 +02:00
|
|
|
result = -1;
|
2015-08-03 19:21:21 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = 0;
|
|
|
|
sem->value = value - 1;
|
|
|
|
}
|
|
|
|
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(old_state);
|
2015-08-03 19:21:21 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|