From 3c4cadc596f201c3377de40a62685b3b9d7b9de1 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Sun, 26 Oct 2008 10:45:42 +0000 Subject: git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@484 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- readme.txt | 4 ++++ src/chschd.c | 18 ++++++++++++------ src/chthreads.c | 15 ++++++++++++++- src/chvt.c | 34 +++++++++++++++------------------- src/include/scheduler.h | 4 ++++ src/include/threads.h | 30 ++++++++++++++++++++++-------- src/include/vt.h | 13 +++++-------- test/test.c | 2 +- test/testmtx.c | 14 +++++++------- 9 files changed, 84 insertions(+), 50 deletions(-) diff --git a/readme.txt b/readme.txt index fe51b009e..9a0d9b7bd 100644 --- a/readme.txt +++ b/readme.txt @@ -74,6 +74,10 @@ Win32-MinGW - ChibiOS/RT simulator and demo into a WIN32 process, ***************************************************************************** *** 0.7.3 *** +- FIX: Fixed a bug in chThdSleepUntil(), this API is no more a macro now. +- NEW: New chThdSleepSeconds(), chThdSleepMilliseconds() and + chThdSleepMicroseconds() utility macros. +- CHANGE: Zero is no more a valid time specification for the chVTSetI() API. - CHANGE: Removed the files chsleep.c and sleep.h. - CHANGE: Renamed the files chdelta.c and delta.h to chvt.c and vt.h. All the system time related functions and macros are now moved here. diff --git a/src/chschd.c b/src/chschd.c index b6fd624e0..573b311ec 100644 --- a/src/chschd.c +++ b/src/chschd.c @@ -111,19 +111,25 @@ static void wakeup(void *p) { * the specified time has elapsed. * * @param newstate the new thread state - * @param time the number of ticks before the operation timeouts + * @param time the number of ticks before the operation timeouts. the value + * zero (\p TIME_INFINITE) is allowed. * @return The wakeup message. * @retval RDY_TIMEOUT if a timeout occurs. * @note The function must be called in the system mutex zone. * @note The function is not meant to be used in the user code directly. */ msg_t chSchGoSleepTimeoutS(tstate_t newstate, systime_t time) { - VirtualTimer vt; - chVTSetI(&vt, time, wakeup, currp); - chSchGoSleepS(newstate); - if (chVTIsArmedI(&vt)) - chVTResetI(&vt); + if (TIME_INFINITE != time) { + VirtualTimer vt; + + chVTSetI(&vt, time, wakeup, currp); + chSchGoSleepS(newstate); + if (chVTIsArmedI(&vt)) + chVTResetI(&vt); + } + else + chSchGoSleepS(newstate); return currp->p_rdymsg; } diff --git a/src/chthreads.c b/src/chthreads.c index 11d2442ad..3f55c4de5 100644 --- a/src/chthreads.c +++ b/src/chthreads.c @@ -289,7 +289,7 @@ void chThdTerminate(Thread *tp) { /** * Suspends the invoking thread for the specified time. - * @param time the system ticks number + * @param time the delay in system ticks */ void chThdSleep(systime_t time) { @@ -298,6 +298,19 @@ void chThdSleep(systime_t time) { chSysUnlock(); } +/** + * Suspends the invoking thread until the system time arrives to the specified + * value. + * @param time the absolute system time + */ +void chThdSleepUntil(systime_t time) { + + chSysLock(); + if ((time -= chSysGetTime()) > 0) + chSchGoSleepTimeoutS(PRSLEEP, time); + chSysUnlock(); +} + /** * Terminates the current thread by specifying an exit status code. * diff --git a/src/chvt.c b/src/chvt.c index 77978e16c..03847c44e 100644 --- a/src/chvt.c +++ b/src/chvt.c @@ -40,36 +40,32 @@ void chVTInit(void) { /** * Enables a virtual timer. * @param vtp the \p VirtualTimer structure pointer - * @param time the number of time ticks, the value zero is allowed with - * meaning "infinite". In this case the structure is initialized - * but not inserted in the delta list, the timer will never be - * triggered. + * @param time the number of time ticks, the value zero is not allowed * @param vtfunc the timer callback function. After invoking the callback * the timer is disabled and the structure can be disposed or * reused. * @param par a parameter that will be passed to the callback function * @note Must be called with the interrupts disabled. - * @note The associated function is invoked by an interrupt handler. + * @note The associated function is invoked from an interrupt handler. */ void chVTSetI(VirtualTimer *vtp, systime_t time, vtfunc_t vtfunc, void *par) { + VirtualTimer *p; + + chDbgAssert(time != 0, "chvt.c, chVTSetI()"); vtp->vt_par = par; vtp->vt_func = vtfunc; - if (time) { - VirtualTimer *p = vtlist.vt_next; - while (p->vt_time < time) { - time -= p->vt_time; - p = p->vt_next; - } - - vtp->vt_prev = (vtp->vt_next = p)->vt_prev; - vtp->vt_prev->vt_next = p->vt_prev = vtp; - vtp->vt_time = time; - if (p != (void *)&vtlist) - p->vt_time -= time; + p = vtlist.vt_next; + while (p->vt_time < time) { + time -= p->vt_time; + p = p->vt_next; } - else - vtp->vt_next = vtp->vt_prev = vtp; // Allows a chVTResetI() on the fake timer. + + vtp->vt_prev = (vtp->vt_next = p)->vt_prev; + vtp->vt_prev->vt_next = p->vt_prev = vtp; + vtp->vt_time = time; + if (p != (void *)&vtlist) + p->vt_time -= time; } /** diff --git a/src/include/scheduler.h b/src/include/scheduler.h index 3f9eb7a39..b05db16a2 100644 --- a/src/include/scheduler.h +++ b/src/include/scheduler.h @@ -32,6 +32,10 @@ /** Returned when the thread was made ready because of a reset. */ #define RDY_RESET -2 +/** Infinite time specification for all the syscalls with a timeout + specification.*/ +#define TIME_INFINITE 0 + /** The priority of the first thread on the given ready list. */ #define firstprio(rlp) ((rlp)->p_next->p_prio) diff --git a/src/include/threads.h b/src/include/threads.h index e9894650d..a3d37a7db 100644 --- a/src/include/threads.h +++ b/src/include/threads.h @@ -193,6 +193,7 @@ extern "C" { void chThdSuspend(Thread **tpp); void chThdTerminate(Thread *tp); void chThdSleep(systime_t time); + void chThdSleepUntil(systime_t time); void chThdExit(msg_t msg); #ifdef CH_USE_WAITEXIT msg_t chThdWait(Thread *tp); @@ -268,15 +269,28 @@ extern "C" { chThdCreateStatic(workspace, wsize, prio, pf, NULL) /** - * Suspends the invoking thread until the system time arrives to the specified - * value. + * Delays the invoking thread for the specified number of seconds. + * @note The specified time is rounded up to a value allowed by the real + * system clock. + * @note The maximum specified value is implementation dependent. */ -#define chThdSleepUntil(t) { \ - chSysLock(); \ - chSchGoSleepTimeoutS(PRSLEEP, \ - (systime_t)((t) - chSysGetTime())); \ - chSysUnlock(); \ -} +#define chThdSleepSeconds(sec) chThdSleep(S2ST(sec)) + +/** + * Delays the invoking thread for the specified number of milliseconds. + * @note The specified time is rounded up to a value allowed by the real + * system clock. + * @note The maximum specified value is implementation dependent. + */ +#define chThdSleepMilliseconds(msec) chThdSleep(MS2ST(msec)) + +/** + * Delays the invoking thread for the specified number of microseconds. + * @note The specified time is rounded up to a value allowed by the real + * system clock. + * @note The maximum specified value is implementation dependent. + */ +#define chThdSleepMicroseconds(usec) chThdSleep(US2ST(usec)) #endif /* _THREADS_H_ */ diff --git a/src/include/vt.h b/src/include/vt.h index 954c87da5..72385715b 100644 --- a/src/include/vt.h +++ b/src/include/vt.h @@ -34,13 +34,13 @@ * Time conversion utility. Converts from milliseconds to system ticks number. * @note The result is rounded upward to the next tick boundary. */ -#define MS2ST(msec) ((systime_t)(((((msec) - 1L) * CH_FREQUENCY) / 1000) + 1)) +#define MS2ST(msec) ((systime_t)(((((msec) - 1L) * CH_FREQUENCY) / 1000L) + 1L)) /** * Time conversion utility. Converts from microseconds to system ticks number. * @note The result is rounded upward to the next tick boundary. */ -#define US2ST(usec) ((systime_t)(((((usec) - 1L) * CH_FREQUENCY) / 1000000) + 1)) +#define US2ST(usec) ((systime_t)(((((usec) - 1L) * CH_FREQUENCY) / 1000000L) + 1L)) /** Virtual Timer callback function.*/ typedef void (*vtfunc_t)(void *); @@ -85,12 +85,12 @@ typedef struct { extern VTList vtlist; #define chVTDoTickI() { \ - vtlist.vt_systime++; \ + vtlist.vt_systime++; \ if (&vtlist != (VTList *)vtlist.vt_next) { \ VirtualTimer *vtp; \ \ - --vtlist.vt_next->vt_time; \ - while (!(vtp = vtlist.vt_next)->vt_time) { \ + --vtlist.vt_next->vt_time; \ + while (!(vtp = vtlist.vt_next)->vt_time) { \ vtfunc_t fn = vtp->vt_func; \ vtp->vt_func = NULL; \ (vtp->vt_next->vt_prev = (void *)&vtlist)->vt_next = vtp->vt_next;\ @@ -99,9 +99,6 @@ extern VTList vtlist; } \ } -/** Infinite time specification.*/ -#define TIME_INFINITE 0 - /* * Virtual Timers APIs. */ diff --git a/test/test.c b/test/test.c index eacb2a9e3..c56a23552 100644 --- a/test/test.c +++ b/test/test.c @@ -250,7 +250,7 @@ msg_t TestThread(void *p) { i = 0; while (tests[i]) { #if DELAY_BETWEEN_TESTS > 0 - chThdSleep(MS2ST(DELAY_BETWEEN_TESTS)); + chThdSleepMilliseconds(DELAY_BETWEEN_TESTS); #endif test_println("---------------------------------------------------------------------------"); test_print("--- Test Case "); diff --git a/test/testmtx.c b/test/testmtx.c index f389ebe6f..6c1beea28 100644 --- a/test/testmtx.c +++ b/test/testmtx.c @@ -85,7 +85,7 @@ static void mtx2_teardown(void) { static msg_t thread2(void *p) { - chThdSleep(MS2ST(10)); + chThdSleepMilliseconds(10); chMtxLock(&m1); chMtxUnlock(); test_emit_token(*(char *)p); @@ -95,7 +95,7 @@ static msg_t thread2(void *p) { static msg_t thread3(void *p) { chMtxLock(&m1); - chThdSleep(MS2ST(40)); + chThdSleepMilliseconds(40); chMtxUnlock(); test_emit_token(*(char *)p); return 0; @@ -103,7 +103,7 @@ static msg_t thread3(void *p) { static msg_t thread4(void *p) { - chThdSleep(MS2ST(20)); + chThdSleepMilliseconds(20); test_cpu_pulse(50); test_emit_token(*(char *)p); return 0; @@ -156,7 +156,7 @@ static msg_t thread5(void *p) { static msg_t thread6(void *p) { - chThdSleep(MS2ST(10)); + chThdSleepMilliseconds(10); chMtxLock(&m2); test_cpu_pulse(20); chMtxLock(&m1); @@ -170,7 +170,7 @@ static msg_t thread6(void *p) { static msg_t thread7(void *p) { - chThdSleep(MS2ST(20)); + chThdSleepMilliseconds(20); chMtxLock(&m2); test_cpu_pulse(50); chMtxUnlock(); @@ -180,7 +180,7 @@ static msg_t thread7(void *p) { static msg_t thread8(void *p) { - chThdSleep(MS2ST(40)); + chThdSleepMilliseconds(40); test_cpu_pulse(200); test_emit_token(*(char *)p); return 0; @@ -188,7 +188,7 @@ static msg_t thread8(void *p) { static msg_t thread9(void *p) { - chThdSleep(MS2ST(50)); + chThdSleepMilliseconds(50); chMtxLock(&m2); test_cpu_pulse(50); chMtxUnlock(); -- cgit v1.2.3