Browse Source

sema: port to xtimer

cc430
Martine Lenders 7 years ago
parent
commit
d104cff214
  1. 2
      Makefile.dep
  2. 12
      sys/include/sema.h
  3. 25
      sys/sema/sema.c

2
Makefile.dep

@ -288,7 +288,7 @@ ifneq (,$(filter posix_semaphore,$(USEMODULE)))
endif
ifneq (,$(filter sema,$(USEMODULE)))
USEMODULE += vtimer
USEMODULE += xtimer
endif
ifneq (,$(filter vtimer,$(USEMODULE)))

12
sys/include/sema.h

@ -73,7 +73,7 @@ int sema_destroy(sema_t *sema);
* @pre Message queue of active thread is initialized (see @ref msg_init_queue()).
*
* @param[in] sema A semaphore.
* @param[in] timeout Time until the semaphore times out. NULL for no timeout.
* @param[in] timeout Time in microseconds until the semaphore times out. 0 for no timeout.
* @param[out] msg Container for a spurious message during the timed wait (result == -EAGAIN).
*
* @return 0 on success
@ -82,7 +82,7 @@ int sema_destroy(sema_t *sema);
* @return -ECANCELED, if the semaphore was destroyed.
* @return -EAGAIN, if the thread received a message while waiting for the lock.
*/
int sema_wait_timed_msg(sema_t *sema, timex_t *timeout, msg_t *msg);
int sema_wait_timed_msg(sema_t *sema, uint64_t timeout, msg_t *msg);
/**
* @brief Wait for a semaphore being posted (without timeout).
@ -97,7 +97,7 @@ int sema_wait_timed_msg(sema_t *sema, timex_t *timeout, msg_t *msg);
*/
static inline int sema_wait_msg(sema_t *sema, msg_t *msg)
{
return sema_wait_timed_msg(sema, NULL, msg);
return sema_wait_timed_msg(sema, 0, msg);
}
/**
@ -105,14 +105,14 @@ static inline int sema_wait_msg(sema_t *sema, msg_t *msg)
* @details Any spurious messages received while waiting for the semaphore are silently dropped.
*
* @param[in] sema A semaphore.
* @param[in] timeout Time until the semaphore times out. NULL for no timeout.
* @param[in] timeout Time in microseconds until the semaphore times out. 0 for no timeout.
*
* @return 0 on success
* @return -EINVAL, if semaphore is invalid.
* @return -ETIMEDOUT, if the semaphore times out.
* @return -ECANCELED, if the semaphore was destroyed.
*/
int sema_wait_timed(sema_t *sema, timex_t *timeout);
int sema_wait_timed(sema_t *sema, uint64_t timeout);
/**
* @brief Wait for a semaphore being posted (without timeout, dropping spurious messages).
@ -125,7 +125,7 @@ int sema_wait_timed(sema_t *sema, timex_t *timeout);
*/
static inline int sema_wait(sema_t *sema)
{
return sema_wait_timed(sema, NULL);
return sema_wait_timed(sema, 0);
}
/**

25
sys/sema/sema.c

@ -22,7 +22,7 @@
#include "irq.h"
#include "msg.h"
#include "vtimer.h"
#include "xtimer.h"
#include "sema.h"
@ -63,7 +63,7 @@ int sema_destroy(sema_t *sema)
return 0;
}
int sema_wait_timed_msg(sema_t *sema, timex_t *timeout, msg_t *msg)
int sema_wait_timed_msg(sema_t *sema, uint64_t timeout, msg_t *msg)
{
if (sema == NULL) {
return -EINVAL;
@ -71,7 +71,8 @@ int sema_wait_timed_msg(sema_t *sema, timex_t *timeout, msg_t *msg)
while (1) {
unsigned old_state = disableIRQ();
priority_queue_node_t n;
vtimer_t timeout_timer;
xtimer_t timeout_timer;
msg_t timeout_msg;
unsigned value = sema->value;
if (value != 0) {
@ -89,19 +90,21 @@ int sema_wait_timed_msg(sema_t *sema, timex_t *timeout, msg_t *msg)
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, sema);
if (timeout != 0) {
timeout_msg.type = MSG_TIMEOUT;
timeout_msg.content.ptr = (char *)sema;
/* we will stay in the same stack context so we can use timeout_msg */
xtimer_set_msg64(&timeout_timer, timeout, &timeout_msg, sched_active_pid);
}
restoreIRQ(old_state);
msg_receive(msg);
if (timeout != NULL) {
vtimer_remove(&timeout_timer); /* remove timer just to be sure */
if (timeout != 0) {
xtimer_remove(&timeout_timer);
}
if (msg->content.ptr != (void *) sema) {
priority_queue_remove(&sema->queue, &n);
if (msg->content.ptr != (void *)sema) {
return -EAGAIN;
}
@ -118,7 +121,7 @@ int sema_wait_timed_msg(sema_t *sema, timex_t *timeout, msg_t *msg)
}
}
int sema_wait_timed(sema_t *sema, timex_t *timeout)
int sema_wait_timed(sema_t *sema, uint64_t timeout)
{
int result;
do {

Loading…
Cancel
Save