From 70c86d43ec79032c7172507fc12bf7d78d44a3de Mon Sep 17 00:00:00 2001 From: gdisirio Date: Mon, 15 Oct 2007 14:52:56 +0000 Subject: git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@53 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- src/chdelta.c | 8 +++++--- src/chevents.c | 6 ++---- src/chmsg.c | 2 +- src/chsem.c | 4 ++-- src/chthreads.c | 31 ++++++++++++++++++++++++++++--- src/include/delta.h | 3 +++ src/include/threads.h | 11 ++++++++++- src/lib/evtimer.c | 4 ++-- src/templates/chconf.h | 4 ++++ 9 files changed, 57 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/chdelta.c b/src/chdelta.c index 8a8100e5f..16dfac5bc 100644 --- a/src/chdelta.c +++ b/src/chdelta.c @@ -40,7 +40,9 @@ 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". + * meaning "infinite". In this case the structure is initialized + * but not inserted in the delta list, the timer will never be + * triggered. * @param vtfunc the timer callback function. After invoking the callback * the timer is disabled and the structure can be disposed or * reused. @@ -51,8 +53,8 @@ void chVTInit(void) { void chVTSetI(VirtualTimer *vtp, t_time time, t_vtfunc vtfunc, void *par) { vtp->vt_par = par; + vtp->vt_func = vtfunc; if (time) { - vtp->vt_func = vtfunc; VirtualTimer *p = dlist.dl_next; while (p->vt_dtime < time) { time -= p->vt_dtime; @@ -66,7 +68,7 @@ void chVTSetI(VirtualTimer *vtp, t_time time, t_vtfunc vtfunc, void *par) { p->vt_dtime -= time; } else - vtp->vt_func = NULL; + vtp->vt_next = vtp->vt_prev = vtp; // Allows a chVTResetI() on the fake timer. } /** diff --git a/src/chevents.c b/src/chevents.c index f88412e11..a617e3edf 100644 --- a/src/chevents.c +++ b/src/chevents.c @@ -205,7 +205,6 @@ t_eventid chEvtWaitTimeout(t_eventmask ewmask, t_time time) { t_eventid i; t_eventmask m; - t_msg msg; chSysLock(); @@ -221,11 +220,10 @@ t_eventid chEvtWaitTimeout(t_eventmask ewmask, chVTSetI(&vt, time, unwait, currp); currp->p_ewmask = ewmask; chSchGoSleepS(PRWTEVENT); - if (!vt.vt_func) { - t_msg msg = currp->p_rdymsg; + if (!chVTIsArmedI(&vt)) { chSysUnlock(); - return msg; + return RDY_TIMEOUT; } chVTResetI(&vt); } diff --git a/src/chmsg.c b/src/chmsg.c index 4140e3165..010da86e5 100644 --- a/src/chmsg.c +++ b/src/chmsg.c @@ -115,7 +115,7 @@ t_msg chMsgSendTimeout(Thread *tp, t_msg msg, t_time time) { currp->p_msg = msg; chSchGoSleepS(PRSNDMSG); msg = currp->p_rdymsg; - if (vt.vt_func) + if (chVTIsArmedI(&vt)) chVTResetI(&vt); chSysUnlock(); diff --git a/src/chsem.c b/src/chsem.c index a58ee71b1..091a045b5 100644 --- a/src/chsem.c +++ b/src/chsem.c @@ -139,7 +139,7 @@ t_msg chSemWaitTimeout(Semaphore *sp, t_time time) { currp->p_semp = sp; chSchGoSleepS(PRWTSEM); msg = currp->p_rdymsg; - if (vt.vt_func) + if (chVTIsArmedI(&vt)) chVTResetI(&vt); chSysUnlock(); @@ -169,7 +169,7 @@ t_msg chSemWaitTimeoutS(Semaphore *sp, t_time time) { fifo_insert(currp, &sp->s_queue); currp->p_semp = sp; chSchGoSleepS(PRWTSEM); - if (vt.vt_func) + if (chVTIsArmedI(&vt)) chVTResetI(&vt); return currp->p_rdymsg; } diff --git a/src/chthreads.c b/src/chthreads.c index 00ae04778..a63f5721b 100644 --- a/src/chthreads.c +++ b/src/chthreads.c @@ -121,9 +121,34 @@ void chThdSetPriority(t_prio newprio) { chSysUnlock(); } +#ifdef CH_USE_SUSPEND +/** + * Suspends the invoking thread. + * + * @param tpp pointer to a \p Thread pointer, the \p Thread pointer is set + * to point to the suspended process before it enters the + * \p PRSUSPENDED state, it is set to \p NULL after it is resumed. + * This allows to implement a "test and resume" on the variable + * into interrupt handlers. + * @note The function is available only if the \p CH_USE_SUSPEND + * option is enabled in \p chconf.h. + */ +void chThdSuspend(Thread **tpp) { + + chSysLock(); + + *tpp = currp; + chSchGoSleepS(PRSUSPENDED); + *tpp = NULL; + + chSysUnlock(); +} +#endif /* CH_USE_SUSPEND */ + #ifdef CH_USE_RESUME /** - * Resumes a thread created with the \p P_SUSPENDED option. + * Resumes a thread created with the \p P_SUSPENDED option or suspended with + * \p chThdSuspend(). * @param tp the pointer to the thread * @note The function has no effect on threads in any other state than * \p PRSUSPENDED. @@ -134,8 +159,8 @@ void chThdResume(Thread *tp) { chSysLock(); - if (tp->p_state == PRSUSPENDED) - chSchWakeupS(tp, RDY_OK); + if ((tp)->p_state == PRSUSPENDED) + chSchWakeupS((tp), RDY_OK); chSysUnlock(); } diff --git a/src/include/delta.h b/src/include/delta.h index 1af774959..4365eb482 100644 --- a/src/include/delta.h +++ b/src/include/delta.h @@ -97,6 +97,9 @@ extern "C" { } #endif +/** Returns TRUE if the speciified timer is armed.*/ +#define chVTIsArmedI(vtp) ((vtp)->vt_func != NULL) + #endif /* CH_USE_VIRTUAL_TIMER */ #endif /* _DELTA_H_ */ diff --git a/src/include/threads.h b/src/include/threads.h index 0440b6007..3e1713d31 100644 --- a/src/include/threads.h +++ b/src/include/threads.h @@ -192,8 +192,10 @@ extern "C" { Thread *chThdCreate(t_prio prio, t_tmode mode, void *workspace, t_size wsize, t_tfunc pf, void *arg); void chThdSetPriority(t_prio newprio); - void chThdResume(Thread *tp); void chThdExit(t_msg msg); +#ifdef CH_USE_RESUME + void chThdResume(Thread *tp); +#endif #ifdef CH_USE_TERMINATE void chThdTerminate(Thread *tp); #endif @@ -240,6 +242,13 @@ extern "C" { */ #define chThdGetExitEventSource(tp) (&(tp)->p_exitesource) +/** + * Resumes a thread created with the \p P_SUSPENDED option or suspended with + * \p chThdSuspend(). + * @param tp the pointer to the thread + */ +#define chThdResumeI(tp) chSchReadyI(tp) + #endif /* _THREADS_H_ */ /** @} */ diff --git a/src/lib/evtimer.c b/src/lib/evtimer.c index 1ab384086..b273e1fd3 100644 --- a/src/lib/evtimer.c +++ b/src/lib/evtimer.c @@ -45,7 +45,7 @@ void evtStart(EvTimer *etp) { chSysLock(); - if (!etp->et_vt.vt_func) + if (!chVTIsArmedI(&etp->et_vt)) chVTSetI(&etp->et_vt, etp->et_interval, tmrcb, etp); chSysUnlock(); @@ -60,7 +60,7 @@ void evtStop(EvTimer *etp) { chSysLock(); - if (etp->et_vt.vt_func) + if (chVTIsArmedI(&etp->et_vt)) chVTResetI(&etp->et_vt); chSysUnlock(); diff --git a/src/templates/chconf.h b/src/templates/chconf.h index 478abd8f9..9c5d303bd 100644 --- a/src/templates/chconf.h +++ b/src/templates/chconf.h @@ -52,6 +52,10 @@ * function is included in the kernel.*/ #define CH_USE_RESUME +/** Configuration option: if specified then the \p chThdSuspend() + * function is included in the kernel.*/ +#define CH_USE_SUSPEND + /** Configuration option: if specified then the \p chThdTerminate() * and \p chThdShouldTerminate() functions are included in the kernel.*/ #define CH_USE_TERMINATE -- cgit v1.2.3