From ece6ef6bf70d1b3033687dedd23a9ce4cfc4f59b Mon Sep 17 00:00:00 2001 From: gdisirio Date: Sat, 28 Aug 2010 14:32:56 +0000 Subject: Added assertions to the semaphores subsystem. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@2138 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/kernel/src/chsem.c | 35 +++++++++++++++++++++++++++++++++++ readme.txt | 1 + 2 files changed, 36 insertions(+) diff --git a/os/kernel/src/chsem.c b/os/kernel/src/chsem.c index 2959b4d63..e5ec9afde 100644 --- a/os/kernel/src/chsem.c +++ b/os/kernel/src/chsem.c @@ -115,6 +115,11 @@ void chSemResetI(Semaphore *sp, cnt_t n) { chDbgCheck((sp != NULL) && (n >= 0), "chSemResetI"); + chDbgAssert(((sp->s_cnt >= 0) && isempty(&sp->s_queue)) || + ((sp->s_cnt < 0) && notempty(&sp->s_queue)), + "chSemResetI(), #1", + "inconsistent semaphore"); + cnt = sp->s_cnt; sp->s_cnt = n; while (++cnt <= 0) @@ -148,6 +153,11 @@ msg_t chSemWaitS(Semaphore *sp) { chDbgCheck(sp != NULL, "chSemWaitS"); + chDbgAssert(((sp->s_cnt >= 0) && isempty(&sp->s_queue)) || + ((sp->s_cnt < 0) && notempty(&sp->s_queue)), + "chSemWaitS(), #1", + "inconsistent semaphore"); + if (--sp->s_cnt < 0) { currp->p_u.wtobjp = sp; sem_insert(currp, &sp->s_queue); @@ -198,6 +208,11 @@ msg_t chSemWaitTimeoutS(Semaphore *sp, systime_t time) { chDbgCheck(sp != NULL, "chSemWaitTimeoutS"); + chDbgAssert(((sp->s_cnt >= 0) && isempty(&sp->s_queue)) || + ((sp->s_cnt < 0) && notempty(&sp->s_queue)), + "chSemWaitTimeoutS(), #1", + "inconsistent semaphore"); + if (--sp->s_cnt < 0) { if (TIME_IMMEDIATE == time) { sp->s_cnt++; @@ -219,6 +234,11 @@ void chSemSignal(Semaphore *sp) { chDbgCheck(sp != NULL, "chSemSignal"); + chDbgAssert(((sp->s_cnt >= 0) && isempty(&sp->s_queue)) || + ((sp->s_cnt < 0) && notempty(&sp->s_queue)), + "chSemSignal(), #1", + "inconsistent semaphore"); + chSysLock(); if (++sp->s_cnt <= 0) chSchWakeupS(fifo_remove(&sp->s_queue), RDY_OK); @@ -235,6 +255,11 @@ void chSemSignalI(Semaphore *sp) { chDbgCheck(sp != NULL, "chSemSignalI"); + chDbgAssert(((sp->s_cnt >= 0) && isempty(&sp->s_queue)) || + ((sp->s_cnt < 0) && notempty(&sp->s_queue)), + "chSemSignalI(), #1", + "inconsistent semaphore"); + if (++sp->s_cnt <= 0) { /* note, it is done this way in order to allow a tail call on chSchReadyI().*/ @@ -260,6 +285,16 @@ msg_t chSemSignalWait(Semaphore *sps, Semaphore *spw) { chDbgCheck((sps != NULL) && (spw != NULL), "chSemSignalWait"); + chDbgAssert(((sps->s_cnt >= 0) && isempty(&sps->s_queue)) || + ((sps->s_cnt < 0) && notempty(&sps->s_queue)), + "chSemSignalWait(), #1", + "inconsistent semaphore"); + + chDbgAssert(((spw->s_cnt >= 0) && isempty(&spw->s_queue)) || + ((spw->s_cnt < 0) && notempty(&spw->s_queue)), + "chSemSignalWait(), #2", + "inconsistent semaphore"); + chSysLock(); if (++sps->s_cnt <= 0) chSchReadyI(fifo_remove(&sps->s_queue))->p_u.rdymsg = RDY_OK; diff --git a/readme.txt b/readme.txt index c2589db51..b95f1b611 100644 --- a/readme.txt +++ b/readme.txt @@ -71,6 +71,7 @@ 2.0.3). - FIX: Fixed a documentation error regarding the ADC driver function adcStartConversion() (bug 3039890)(backported to 2.0.3). +- NEW: More assertions added to the semaphores subsystem. - NEW: New kernel hooks: SYSTEM_TICK_EVENT_HOOK(), SYSTEM_HALT_HOOK(). - NEW: Added board files for the Olimex STM32-H103. - NEW: New kernel APIs chSysGetIdleThread() and chThdGetTicks(), the new -- cgit v1.2.3