From 903575815458e4e3c12195418f0582f5a9350f81 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Mon, 3 Dec 2007 14:54:05 +0000 Subject: git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@124 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- src/chevents.c | 6 ++---- src/chmsg.c | 15 +++------------ src/chsem.c | 8 ++++---- src/chthreads.c | 20 ++++++++++++++++---- src/include/debug.h | 19 +++++++++++++++++++ 5 files changed, 44 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/chevents.c b/src/chevents.c index f301fd106..13a858acd 100644 --- a/src/chevents.c +++ b/src/chevents.c @@ -143,10 +143,8 @@ t_eventid chEvtWait(t_eventmask ewmask, } static void wakeup(void *p) { -#ifdef CH_USE_DEBUG - if (((Thread *)p)->p_state != PRWTEVENT) - chDbgPanic("chevents.c, wakeup()"); -#endif + + chDbgAssert(((Thread *)p)->p_state == PRWTEVENT, "chevents.c, wakeup()"); chSchReadyI(p, RDY_TIMEOUT); } diff --git a/src/chmsg.c b/src/chmsg.c index 844628d75..61546d7c1 100644 --- a/src/chmsg.c +++ b/src/chmsg.c @@ -66,11 +66,8 @@ t_msg chMsgSendWithEvent(Thread *tp, t_msg msg, EventSource *esp) { chSysLock(); + chDbgAssert(tp->p_state != PRWTMSG, "chmsg.c, chMsgSendWithEvent()"); fifo_insert(currp, &tp->p_msgqueue); -#ifdef CH_USE_DEBUG - if (tp->p_state == PRWTMSG) - chDbgPanic("chmsg.c, chMsgSendWithEvent()"); -#endif chEvtSendI(esp); currp->p_msg = msg; chSchGoSleepS(PRSNDMSG); @@ -84,10 +81,7 @@ t_msg chMsgSendWithEvent(Thread *tp, t_msg msg, EventSource *esp) { #ifdef CH_USE_MESSAGES_TIMEOUT static void wakeup(void *p) { -#ifdef CH_USE_DEBUG - if (((Thread *)p)->p_state != PRSNDMSG) - chDbgPanic("chmsg.c, wakeup()"); -#endif + chDbgAssert(((Thread *)p)->p_state == PRSNDMSG, "chmsg.c, wakeup()"); chSchReadyI(dequeue(p), RDY_TIMEOUT); } @@ -186,10 +180,7 @@ void chMsgRelease(t_msg msg) { chSysLock(); -#ifdef CH_USE_DEBUG - if (!chMsgIsPendingI(currp)) - chDbgPanic("chmsg.c, chMsgRelease()"); -#endif + chDbgAssert(chMsgIsPendingI(currp), "chmsg.c, chMsgRelease()"); chSchWakeupS(fifo_remove(&currp->p_msgqueue), msg); chSysUnlock(); diff --git a/src/chsem.c b/src/chsem.c index 01ec80649..083e25412 100644 --- a/src/chsem.c +++ b/src/chsem.c @@ -33,6 +33,7 @@ */ void chSemInit(Semaphore *sp, t_cnt n) { + chDbgAssert(n >= 0, "chsem.c, chSemInit()"); fifo_init(&sp->s_queue); sp->s_cnt = n; } @@ -48,6 +49,7 @@ void chSemInit(Semaphore *sp, t_cnt n) { void chSemReset(Semaphore *sp, t_cnt n) { t_cnt cnt; + chDbgAssert(n >= 0, "chsem.c, chSemReset()"); chSysLock(); cnt = sp->s_cnt; @@ -73,6 +75,7 @@ void chSemReset(Semaphore *sp, t_cnt n) { void chSemResetI(Semaphore *sp, t_cnt n) { t_cnt cnt; + chDbgAssert(n >= 0, "chsem.c, chSemResetI()"); cnt = sp->s_cnt; sp->s_cnt = n; while (cnt++ < 0) @@ -116,10 +119,7 @@ t_msg chSemWaitS(Semaphore *sp) { #ifdef CH_USE_SEMAPHORES_TIMEOUT static void wakeup(void *p) { -#ifdef CH_USE_DEBUG - if (((Thread *)p)->p_state != PRWTSEM) - chDbgPanic("chsem.c, wakeup()"); -#endif + chDbgAssert(((Thread *)p)->p_state == PRWTSEM, "chsem.c, wakeup()"); chSemFastSignalI(((Thread *)p)->p_semp); chSchReadyI(dequeue(p), RDY_TIMEOUT); } diff --git a/src/chthreads.c b/src/chthreads.c index 041690af9..1f37bb808 100644 --- a/src/chthreads.c +++ b/src/chthreads.c @@ -51,6 +51,14 @@ void _InitThread(t_prio prio, t_tmode mode, Thread *tp) { #endif } +#ifdef CH_USE_DEBUG +static void memfill(BYTE8 *p, ULONG32 n, BYTE8 v) { + + while (n) + *p++ = v, n--; +} +#endif + /** * Creates a new thread. * @param prio the priority level for the new thread. Usually the threads are @@ -84,6 +92,12 @@ Thread *chThdCreate(t_prio prio, t_tmode mode, void *workspace, t_size wsize, t_tfunc pf, void *arg) { Thread *tp = workspace; + chDbgAssert((wsize > UserStackSize(0)) && (prio <= HIGHPRIO) && + (workspace != NULL) && (pf != NULL), + "chthreads.c, chThdCreate()"); +#ifdef CH_USE_DEBUG + memfill(workspace, wsize, MEM_FILL_PATTERN); +#endif _InitThread(prio, mode, tp); SETUP_CONTEXT(workspace, wsize, pf, arg); #ifdef CH_USE_RESUME @@ -108,6 +122,7 @@ Thread *chThdCreate(t_prio prio, t_tmode mode, void *workspace, */ void chThdSetPriority(t_prio newprio) { + chDbgAssert(newprio <= HIGHPRIO, "chthreads.c, chThdSetPriority()") chSysLock(); #ifdef CH_USE_RT_SEMAPHORES @@ -139,10 +154,7 @@ void chThdSuspend(Thread **tpp) { chSysLock(); -#ifdef CH_USE_DEBUG - if (*tpp) - chDbgPanic("chthreads.c, chThdSuspend()"); -#endif + chDbgAssert(*tpp == NULL, "chthreads.c, chThdSuspend()"); *tpp = currp; chSchGoSleepS(PRSUSPENDED); *tpp = NULL; diff --git a/src/include/debug.h b/src/include/debug.h index f5aed7ffe..3d763d84d 100644 --- a/src/include/debug.h +++ b/src/include/debug.h @@ -31,6 +31,11 @@ #define TRACE_BUFFER_SIZE 64 #endif +/** + * Fill value for threads working area in debug mode. + */ +#define MEM_FILL_PATTERN 0x55 + typedef struct { void *cse_slpdata; t_time cse_time; @@ -58,10 +63,24 @@ extern "C" { } #endif +/** + * Condition assertion, if the condition check fails then the kernel panics + * with the specified message. + * @param c the condition to be verified to be true + * @param m the text message + * @note The condition is tested only if the \p CH_USE_DEBUG switch is + * specified in \p chconf.h else the macro does nothing. + */ +#define chDbgAssert(c, m) { \ + if (!(c)) \ + chDbgPanic(m); \ +} + #else /* CH_USE_DEBUG */ #define chDbgInit() #define chDbgPanic(msg) {} +#define chDbgAssert(c, m) {} #endif /* CH_USE_DEBUG */ -- cgit v1.2.3