From e335ccfb433ae1de73dc7d3abe86a4f980c4556d Mon Sep 17 00:00:00 2001 From: Oliver Hahm Date: Thu, 4 Nov 2010 16:21:39 +0100 Subject: [PATCH] * fixed wrong return value for thread_wakeup * changed hwtimer_wait to use thread_sleep instead of mutexes --- core/hwtimer.c | 71 ++++++++++++++++++++++++++---------------- core/include/hwtimer.h | 2 +- core/include/tcb.h | 2 +- core/thread.c | 2 +- 4 files changed, 48 insertions(+), 29 deletions(-) diff --git a/core/hwtimer.c b/core/hwtimer.c index e056a3dd6..e19c8a2a8 100644 --- a/core/hwtimer.c +++ b/core/hwtimer.c @@ -23,7 +23,7 @@ #include #include -#include +#include /*---------------------------------------------------------------------------*/ @@ -33,8 +33,14 @@ typedef struct hwtimer_t { uint8_t checksum; } hwtimer_t; +typedef struct hwtimer_wait_t { + unsigned int pid; /**< pid of waiting thread */ + uint8_t state; /**state = 0; + while (!(thread_wakeup((*((hwtimer_wait_t*)hwt)).pid))) { + hwtimer_set(HWTIMER_WAIT_BACKOFF, hwtimer_wakeup, (void*) &hwt); + } } void hwtimer_spin(unsigned long ticks) @@ -109,7 +121,8 @@ void hwtimer_init_comp(uint32_t fcpu) { available_timers = 0; hwtimer_arch_init(multiplexer, fcpu); for (i = 0; i < HWTIMER_QUEUESIZE; i++) { - queue[i] = 0xff; // init queue as empty + /* init queue as empty */ + queue[i] = 0xff; } for (i = 0; i < HWTIMER_QUEUESIZE; i++) { enqueue(i); @@ -119,7 +132,7 @@ void hwtimer_init_comp(uint32_t fcpu) { /*---------------------------------------------------------------------------*/ int hwtimer_active(void) { - return queue_items != HWTIMER_QUEUESIZE; + return (queue_items != HWTIMER_QUEUESIZE); } /*---------------------------------------------------------------------------*/ @@ -133,34 +146,37 @@ unsigned long hwtimer_now(void) void hwtimer_wait(unsigned long ticks) { - mutex_t mutex; - if (ticks <= 4 || inISR()) { + 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_releasemutex, &mutex); + hwtimer_wait_t hwt; + hwt.pid = active_thread->pid; + hwt.state = 1; + /* -2 is to adjust the real value */ + int res = hwtimer_set(ticks-2, hwtimer_wakeup, (void*) &hwt); if (res == -1) { - mutex_unlock(&mutex, true); hwtimer_spin(ticks); return; } - mutex_lock(&mutex); + while (hwt.state) { + thread_sleep(); + } } /*---------------------------------------------------------------------------*/ static int _hwtimer_set(unsigned long offset, void (*callback)(void*), void *ptr, bool absolute) { - if (! inISR() ) dINT(); -// hwtimer_arch_disable_interrupt(); + if (!inISR()) { + dINT(); + } int x = dequeue(); if (x == Q_FULL) { + if (! inISR()) { + eINT(); + } printf("[KT] no timers left\n"); -// hwtimer_arch_enable_interrupt(); - if (! inISR()) eINT(); return -1; } @@ -168,13 +184,16 @@ static int _hwtimer_set(unsigned long offset, void (*callback)(void*), void *ptr timer[x].data = ptr; timer[x].checksum = ++timer_id; - if (absolute) + if (absolute) { hwtimer_arch_set_absolute(offset, x); - else + } + else { hwtimer_arch_set(offset, x); + } - //hwtimer_arch_enable_interrupt(); - if (! inISR()) eINT(); + if (!inISR()) { + eINT(); + } return (timer[x].checksum << 8) + x; } diff --git a/core/include/hwtimer.h b/core/include/hwtimer.h index 39f2c0f18..b2d8d2666 100644 --- a/core/include/hwtimer.h +++ b/core/include/hwtimer.h @@ -30,7 +30,7 @@ #define __HWTIMER_H #include -#include "hwtimer_cpu.h" +#include /** * @def HWTIMER_SPEED diff --git a/core/include/tcb.h b/core/include/tcb.h index 66ef085e3..34d7c1447 100644 --- a/core/include/tcb.h +++ b/core/include/tcb.h @@ -34,7 +34,7 @@ typedef struct tcb { char* sp; - unsigned int status; + uint8_t status; uint16_t pid; uint16_t priority; diff --git a/core/thread.c b/core/thread.c index 8afbcb844..59aaabcce 100644 --- a/core/thread.c +++ b/core/thread.c @@ -62,7 +62,7 @@ int thread_wakeup(int pid) { } else { sched_context_switch_request = 1; } - return 0; + return 1; } else { DEBUG("thread_wakeup: Thread is not sleeping!\n"); if (!isr) eINT();