From 3f95d72828c1721a91a41dad8fdbac0363cb8650 Mon Sep 17 00:00:00 2001 From: Oleg Hahm Date: Thu, 8 Aug 2013 20:11:57 +0200 Subject: [PATCH] fix for #25 by using mutexes for hwtimer_wait --- core/hwtimer.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/core/hwtimer.c b/core/hwtimer.c index 76ab082a52..d14b3dd361 100644 --- a/core/hwtimer.c +++ b/core/hwtimer.c @@ -13,6 +13,7 @@ * @author Heiko Will * @author Thomas Hillebrandt * @author Kaspar Schleiser + * @author Oliver Hahm * @} */ @@ -45,6 +46,10 @@ static void multiplexer(int source) timer[source].callback(timer[source].data); } +static void hwtimer_releasemutex(void* mutex) { + mutex_unlock((mutex_t*) mutex); +} + static void hwtimer_wakeup(void *ptr) { int pid = (int)ptr; @@ -98,20 +103,25 @@ unsigned long hwtimer_now(void) void hwtimer_wait(unsigned long ticks) { + mutex_t mutex; + if (ticks <= 6 || inISR()) { hwtimer_spin(ticks); return; } - + mutex_init(&mutex); + mutex_lock(&mutex); /* -2 is to adjust the real value */ - int res = hwtimer_set(ticks - 2, hwtimer_wakeup, (void*)(unsigned int)(active_thread->pid)); - + int res = hwtimer_set(ticks - 2, hwtimer_releasemutex, &mutex); if (res == -1) { + mutex_unlock(&mutex); hwtimer_spin(ticks); return; } - - thread_sleep(); + + /* try to lock mutex again will cause the thread to go into + * STATUS_MUTEX_BLOCKED until hwtimer fires the releasemutex */ + mutex_lock(&mutex); } /*---------------------------------------------------------------------------*/