Page 1 of 1

Send and receive in thread

Posted: Wed Dec 05, 2012 4:45 am
by oattao
Hi all, I need help about MailBox. I create two thread. One post message, another fetch message, when it receive message, it with turn on or turn off LED3 (I'm using STM32l-Discovery). Here the code I used and it done well (LED3 on or off as I expected).

Code: Select all

#include "ch.h"
#include "hal.h"

// Thread fetch message
static WORKING_AREA(waLEDS, 128);
static msg_t LEDS(void * arg) {
    Mailbox* mbox = (Mailbox *)arg;
    msg_t msg, result;
    while(TRUE) {
        result = chMBFetch(mbox, &msg, TIME_INFINITE);
        if(result == RDY_OK) {
            if(msg == 1)
                palSetPad(GPIOB, GPIOB_LED3);
            if(msg == 2)
                palClearPad(GPIOB, GPIOB_LED3);
        }
    }
}

// Thread post message
static WORKING_AREA(waLEDC, 128);
static msg_t LEDC(void * arg) {
    Mailbox* mbox = (Mailbox *)arg;   
    while(TRUE) {
        chMBPost(mbox, 1, TIME_INFINITE);
        chThdSleepMilliseconds(1000);
        chMBPost(mbox, 2, TIME_INFINITE);
        chThdSleepMilliseconds(1000);
    }
}
int main(void) {
      Mailbox mbox;
    msg_t mbox_buffer[1];
   
      chMBInit(&mbox, mbox_buffer, 1);
    halInit();
    chSysInit();
    chThdCreateStatic(waLEDS, sizeof(waLEDS), NORMALPRIO, LEDS, (void *)&mbox);
    chThdCreateStatic(waLEDC, sizeof(waLEDC), NORMALPRIO, LEDC, (void *)&mbox);

    while (TRUE) {
        chThdSleepMilliseconds(500);
    }
}

Now I want threads post and fetch message, too. I means thread LEDS fetchs message, control LED3 and post message; thread LEDC fetchs message, control LED4 and post message. I edited code like that:

Code: Select all

#include "ch.h"
#include "hal.h"

// Thread fetch message
static WORKING_AREA(waLEDS, 128);
static msg_t LEDS(void * arg) {
    Mailbox* mbox = (Mailbox *)arg;
    msg_t msg, result;
    while(TRUE) {
        result = chMBFetch(mbox, &msg, TIME_INFINITE);
        if(result == RDY_OK) {
            if(msg == 1)
                palSetPad(GPIOB, GPIOB_LED3);
            if(msg == 2)
                palClearPad(GPIOB, GPIOB_LED3);
        }
            chMBPost(mbox, 3, TIME_INFINITE);
        chThdSleepMilliseconds(1000);
        chMBPost(mbox, 4, TIME_INFINITE);
        chThdSleepMilliseconds(1000);
    }
}

// Thread post message
static WORKING_AREA(waLEDC, 128);
static msg_t LEDC(void * arg) {
    Mailbox* mbox = (Mailbox *)arg;   
      msg_t msg, result;
    while(TRUE) {
            result = chMBFetch(mbox, &msg, TIME_INFINITE);
        if(result == RDY_OK) {
            if(msg == 3)
                palSetPad(GPIOB, GPIOB_LED3);
            if(msg == 4)
                palClearPad(GPIOB, GPIOB_LED3);
            }
        chMBPost(mbox, 1, TIME_INFINITE);
        chThdSleepMilliseconds(1000);
        chMBPost(mbox, 2, TIME_INFINITE);
        chThdSleepMilliseconds(1000);
    }
}
int main(void) {
      Mailbox mbox;
    msg_t mbox_buffer[1];
   
      chMBInit(&mbox, mbox_buffer, 1);
    halInit();
    chSysInit();
    chThdCreateStatic(waLEDS, sizeof(waLEDS), NORMALPRIO, LEDS, (void *)&mbox);
    chThdCreateStatic(waLEDC, sizeof(waLEDC), NORMALPRIO, LEDC, (void *)&mbox);

    while (TRUE) {
        chThdSleepMilliseconds(500);
    }
}

But this case it didn't work, both LED3 and LED4 didn't blink.
I don't know how to solve. Please help me. Thanks in advance.

Re: Send and receive in thread

Posted: Wed Dec 05, 2012 6:36 am
by Giovanni
Hi,

Could it be a deadlock? both thread waiting for a message at the same time?

Anyway you don't need to verify for RDY_OK, if you don't use timeouts nor reset functions it is guaranteed it is RDY_OK.

Giovanni

Re: Send and receive in thread

Posted: Thu Dec 06, 2012 3:43 am
by oattao
Thank you very much for your help. I don't understand much about deadlock but my code can work now (I can send and receive message with a thread).

Re: Send and receive in thread

Posted: Thu Dec 06, 2012 7:14 am
by Tectu
A deadlock is, as Giovanni said, when two processes wait for the other one doing something.

In your case, you have thread A and B. Thread A waits for thread B putting something into the queue/mailbox, but thread A will only do that when it received a message through the queue/mailbox from Thread B. Therefore nothing happens.

Here's a nice article about deadlocks: wikipedia

Hope that helps understanding it a bit :)


~ Tectu

Re: Send and receive in thread

Posted: Thu Dec 06, 2012 7:46 am
by oattao
Thank you very much. I understand now. This forum is very helpfull. I like it.