From d104cff214cff2ba645748313806f9d1fc82f696 Mon Sep 17 00:00:00 2001
From: Martine Lenders <mlenders@inf.fu-berlin.de>
Date: Sun, 25 Oct 2015 15:44:42 +0100
Subject: [PATCH] sema: port to xtimer

---
 Makefile.dep       |  2 +-
 sys/include/sema.h | 12 ++++++------
 sys/sema/sema.c    | 25 ++++++++++++++-----------
 3 files changed, 21 insertions(+), 18 deletions(-)

diff --git a/Makefile.dep b/Makefile.dep
index c8f975e40f..29776529fb 100644
--- a/Makefile.dep
+++ b/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)))
diff --git a/sys/include/sema.h b/sys/include/sema.h
index 4d5d724778..360c4deb5b 100644
--- a/sys/include/sema.h
+++ b/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);
 }
 
 /**
diff --git a/sys/sema/sema.c b/sys/sema/sema.c
index e5c3b0ca5f..ef7f28b113 100644
--- a/sys/sema/sema.c
+++ b/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 {
-- 
GitLab