Combine mailbox with events

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

Combine mailbox with events

Postby decimad » Sun Aug 09, 2015 2:33 pm

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: 10240
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 212 times
Been thanked: 198 times
Contact:

Re: Combine mailbox with events

Postby Giovanni » Sun Aug 09, 2015 3:49 pm

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

Postby decimad » Sun Aug 09, 2015 8:12 pm

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: 27
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 4 times
Been thanked: 2 times

Re: Combine mailbox with events

Postby faisal » Tue Sep 19, 2017 3:03 pm

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: 10240
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 212 times
Been thanked: 198 times
Contact:

Re: Combine mailbox with events

Postby Giovanni » Tue Sep 19, 2017 4:09 pm

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: 27
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 4 times
Been thanked: 2 times

Re: Combine mailbox with events

Postby faisal » Tue Sep 19, 2017 4:18 pm

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: 27
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 4 times
Been thanked: 2 times

Re: Combine mailbox with events

Postby faisal » Tue Sep 19, 2017 4:29 pm

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: 10240
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 212 times
Been thanked: 198 times
Contact:

Re: Combine mailbox with events

Postby Giovanni » Tue Sep 19, 2017 6:23 pm

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

"Articles" from the home page.

Giovanni

faisal
Posts: 27
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 4 times
Been thanked: 2 times

Re: Combine mailbox with events

Postby faisal » Wed Sep 20, 2017 4:40 pm

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

"Articles" from the home page.

Giovanni


Thanks Giovanni!


Return to “ChibiOS/RT”

Who is online

Users browsing this forum: No registered users and 1 guest