Combine mailbox with events

Discussions and support about ChibiOS/RT, the free embedded RTOS.
Post Reply
decimad
Posts: 2
Joined: Sun Aug 09, 2015 2:28 pm

Combine mailbox with events

Post by decimad »

Hi there,
I'm running a thread loop which uses mailboxes for multi purpose messages from other threads/clients, however this thread is also notified from a ISR. Currently the ISR uses the mailbox too, but I'm wondering if it's somehow possible to wait on a mailbox and thread events at the same time with a timeout? As these messages are multi purpose and handled asynchronously, I get the message buffer from a pool, so there's some overhead which I would like to avoid in the ISR.

Kind Regards,
Michael
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Combine mailbox with events

Post by Giovanni »

Hi,

You can wait for just events: send an event when posting into the mailbox, the two operations will be atomic.

On the thread side wait for the two events (with timeout), when the mailbox event it received then "fetch" messages using TIME_IMMEDIATE until the mailbox is empty, this makes sure there can't be messages stuck in the mailbox.

Giovanni
decimad
Posts: 2
Joined: Sun Aug 09, 2015 2:28 pm

Re: Combine mailbox with events

Post by decimad »

Ouch, Captain Obvious has striked once again! :) Thanks for getting me out of my misery... I could then also replace the mailbox with a c++ queue and have RAII on the messages... everything's great now!
faisal
Posts: 382
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 45 times
Been thanked: 62 times

Re: Combine mailbox with events

Post by faisal »

Giovanni wrote:Hi,

You can wait for just events: send an event when posting into the mailbox, the two operations will be atomic.

On the thread side wait for the two events (with timeout), when the mailbox event it received then "fetch" messages using TIME_IMMEDIATE until the mailbox is empty, this makes sure there can't be messages stuck in the mailbox.

Giovanni


I'm doing something similar with a thread that needs to wait on a mailbox, or on other events (timeout timers ..). I've modified the other thread that posts the mailbox to also send an event. You mentioned that the two functions, chEvtSignal and chMBPost are atomic. What do you mean by that? Does the ordering of chEvtSignal and chMBPost not matter? What if you have chEvtSignal before chMBPost, and there is a context switch to the (higher) priority thread waiting for the signal. It will fail in it's attempt to fetch the mailbox. No? Thanks in advance for the help!
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Combine mailbox with events

Post by Giovanni »

The operation are *not* atomic if written normally, you need to create an atomic construct this way:

chSysLock();
chEvtSignalI();
chMBPostS();
chSysUnlock();

Note the I and the S after function names. You can have several I-class functions and a single final S-class must be present which reschedules.

Giovanni
faisal
Posts: 382
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 45 times
Been thanked: 62 times

Re: Combine mailbox with events

Post by faisal »

Giovanni wrote:The operation are *not* atomic if written normally, you need to create an atomic construct this way:

chSysLock();
chEvtSignalI();
chMBPostS();
chSysUnlock();

Note the I and the S after function names. You can have several I-class functions and a single final S-class must be present which reschedules.

Giovanni


Yup, that's what I suspected. Thanks Giovanni!
faisal
Posts: 382
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 45 times
Been thanked: 62 times

Re: Combine mailbox with events

Post by faisal »

I found this: http://www.chibios.org/dokuwiki/doku.ph ... :kb:atomic , which describes it pretty well. What I can't find however, is where is that article linked from? I looked in the book (http://www.chibios.org/dokuwiki/doku.ph ... book:start), but couldn't find it there. From the link, it seems that there is a 'chibios knowledge base' somewhere, but I can't find the link. I've seen other articles in chibios:kb that are useful, I would like to read all of them.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Combine mailbox with events

Post by Giovanni »

From here: http://www.chibios.org/dokuwiki/doku.ph ... cles:start

"Articles" from the home page.

Giovanni
faisal
Posts: 382
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 45 times
Been thanked: 62 times

Re: Combine mailbox with events

Post by faisal »

Giovanni wrote:From here: http://www.chibios.org/dokuwiki/doku.ph ... cles:start

"Articles" from the home page.

Giovanni


Thanks Giovanni!
Post Reply