Page 1 of 1

Mutex/Condvar problem

Posted: Wed Nov 28, 2012 4:06 pm
by Prof. Dr. YoMan
Hi,

I guess the problem is on my side, but you may enlight me. :)

Code: Select all

main { // priority default + 0
  ..
  chMtxLock(&mutex);
  chCondWait(&condVar);
  doSomething();
  chMtxUnlock();
  ..
}

Code: Select all

thread { // priority default+1
  ..
  if (chMtxTryLock(&mutex) == FALSE) {
    // a) never gets here <====
    doBeforeSomething();
    chCondSignal(&condVar);
    chMtxLock(&mutex);
    doAfterSomething();
  }
  else {
    // b)
    doSomethingElse();
  }
  chMtxUnlock();
..
}


What I expect:
If mutex not locked in main: case b) is run in thread.
If mutex is locked in main and waiting for condVar: case a) is run in thread.

What I get:
b) is always run in thread.

Why?

I want doBeforeSomething(), then doSomething(), then doAfterSomething() OR doSomethingElse(). nThanks for help.

Re: Mutex/Condvar problem

Posted: Wed Nov 28, 2012 4:42 pm
by Giovanni
Have the mutex and the condvar been initialized?

Better post the full code and make sure to enable the debug options.

Giovanni

Re: Mutex/Condvar problem

Posted: Wed Nov 28, 2012 5:10 pm
by Prof. Dr. YoMan
Yes they where initialized:

Code: Select all

static MUTEX_DECL(mutex);
static CONDVAR_DECL(condVar);

The rest of the code does nothing special. No other mutexes, convars, ... involved.
All chibi-debug options are enabled (which helped me to understand a lot).
The thread is created as static-thread in main.

Since it seems not to be that obvious I will post a stripped down code (but on thursday or friday).

Re: Mutex/Condvar problem

Posted: Thu Nov 29, 2012 9:24 pm
by Prof. Dr. YoMan
Ok, retried. Failed.

Code: Select all

MUTEX_DECL(wifi_powerMutex);
CONDVAR_DECL(wifi_powerCondVar);

static WORKING_AREA(wifi_irqThreadWA, 256);
static msg_t wifi_iqrThread(void *arg) { // gets mbox message from real IRQ which is build with EXT-driver
..
    spiAcquireBus(WIFI_SPI_DRIVER);
    spiSelect(WIFI_SPI_DRIVER);
...
    if (chMtxTryLock(&wifi_powerMutex) == TRUE) {
      doA();
      chMtxUnlock();
      spiUnselect(WIFI_SPI_DRIVER);
      spiReleaseBus(WIFI_SPI_DRIVER);
    }
    else {
      doB();
      spiUnselect(WIFI_SPI_DRIVER);
      spiReleaseBus(WIFI_SPI_DRIVER);
      chCondSignal(&wifi_powerCondVar);
    }
..
}

main {
..
  chThdCreateStatic(&wifi_irqThreadWA, sizeof(wifi_irqThreadWA), NORMALPRIO + 1, wifi_iqrThread, NULL);
..
  chMtxLock(&wifi_powerMutex);
  chCondWait(&wifi_powerCondVar); // XXX
  chMtxUnlock();
..
}


When i debug with a breakpoint at XXX (wifi_powerMutex claimed) is see the code use the "doA()" path in the irqThread.

I added some SPI stuff, but i think the problem is not with it.

Re: Mutex/Condvar problem

Posted: Thu Nov 29, 2012 10:17 pm
by Giovanni
Hi,

Did you see the test cases 6 and 7 in testmtx.c where condvars are used? you could try that. I will try the code you posted tomorrow.

Giovanni

Re: Mutex/Condvar problem

Posted: Thu Nov 29, 2012 10:25 pm
by Prof. Dr. YoMan
Debugging I see that
chCondWait(&wifi_powerCondVar);
releases the wifi_powerMutex.

Re: Mutex/Condvar problem

Posted: Thu Nov 29, 2012 10:29 pm
by Giovanni
That is normal, waiting on a condvar releases the last mutex.

Giovanni

Re: Mutex/Condvar problem

Posted: Thu Nov 29, 2012 10:38 pm
by Prof. Dr. YoMan
Then it is clear that i always get to doA();

I will have to do this with binary semaphores for signaling. Seems i have to learn these condition variables and their use case a bit more.

Re: Mutex/Condvar problem

Posted: Thu Nov 29, 2012 10:56 pm
by Giovanni
Mutexes and Condition Variables should be used to form "monitor" constructs, not the easiest thing to do, this article should explain it a bit: http://en.wikipedia.org/wiki/Condition_variable

Giovanni