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
Combine mailbox with events
- Giovanni
- Site Admin
- Posts: 14457
- Joined: Wed May 27, 2009 8:48 am
- Location: Salerno, Italy
- Has thanked: 1076 times
- Been thanked: 922 times
- Contact:
Re: Combine mailbox with events
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
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
Re: Combine mailbox with events
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!
Re: Combine mailbox with events
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!
- Giovanni
- Site Admin
- Posts: 14457
- Joined: Wed May 27, 2009 8:48 am
- Location: Salerno, Italy
- Has thanked: 1076 times
- Been thanked: 922 times
- Contact:
Re: Combine mailbox with events
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
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
Re: Combine mailbox with events
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!
Re: Combine mailbox with events
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.
- Giovanni
- Site Admin
- Posts: 14457
- Joined: Wed May 27, 2009 8:48 am
- Location: Salerno, Italy
- Has thanked: 1076 times
- Been thanked: 922 times
- Contact:
Re: Combine mailbox with events
From here: http://www.chibios.org/dokuwiki/doku.ph ... cles:start
"Articles" from the home page.
Giovanni
"Articles" from the home page.
Giovanni
Re: Combine mailbox with events
Giovanni wrote:From here: http://www.chibios.org/dokuwiki/doku.ph ... cles:start
"Articles" from the home page.
Giovanni
Thanks Giovanni!
Who is online
Users browsing this forum: No registered users and 21 guests