Skip to content

Commit

Permalink
fix for #25 by using mutexes for hwtimer_wait
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegHahm committed Aug 8, 2013
1 parent da7e098 commit 3f95d72
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions core/hwtimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @author Heiko Will <hwill@inf.fu-berlin.de>
* @author Thomas Hillebrandt <hillebra@inf.fu-berlin.de>
* @author Kaspar Schleiser <kaspar.schleiser@fu-berlin.de>
* @author Oliver Hahm <oliver.hahm@fu-berlin.de>
* @}
*/

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/*---------------------------------------------------------------------------*/
Expand Down

0 comments on commit 3f95d72

Please # to comment.