Thread sleeping, wakeup on Mailbox or Event

Discussions and support about ChibiOS/RT, the free embedded RTOS.
electronic_eel
Posts: 79
Joined: Sat Mar 19, 2016 8:07 pm
Been thanked: 17 times

Thread sleeping, wakeup on Mailbox or Event

Postby electronic_eel » Mon Aug 26, 2024 12:56 pm

Hi,

I have several ChibiOS/RT threads running. They each have their own Mailbox where they receive commands and status updates from other parts of the system. So they usually sleep until they get woken up by a new entry in their Mailbox. The code looks like this:

Code: Select all

    while (true)
    {
        msg_t msg;
        if (chMBFetchTimeout(&fan_handler_mb, &msg, TIME_INFINITE) == MSG_OK)
        {
            // handle message


Now I have certain global state changes that each thread needs to be aware of and must handle immediately. I thought about using Events for this and have each thread register to listen for the Event. So the threads would have to call "chEvtWaitAny(ALL_EVENTS)" to get the Events.

But is it possible to have a thread sleep until either a new message is in the Mailbox or a new is Event received?

To me it looks like I just can wait for a new entry in the Mailbox or an Event, but not both at the same time. Or would just registering for an Event already wake up the thread, even if it is currently sleeping in "chMBFetchTimeout()"?

Thanks.

User avatar
Giovanni
Site Admin
Posts: 14496
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1090 times
Been thanked: 932 times
Contact:

Re: Thread sleeping, wakeup on Mailbox or Event

Postby Giovanni » Mon Aug 26, 2024 1:38 pm

Hi,,

Just wait for events, and make a "message incoming" event, when you get the event then you fetch from the mailbox using TIME_IMMEDIATE (no wait).

On the sender side send both the event and the message atomically.

chSysLock()
chEvtBroadcastFlagsI()
chMBPostTimeoutS()
chSysUnlock()

Giovanni

electronic_eel
Posts: 79
Joined: Sat Mar 19, 2016 8:07 pm
Been thanked: 17 times

Re: Thread sleeping, wakeup on Mailbox or Event

Postby electronic_eel » Mon Aug 26, 2024 2:01 pm

Hi,

thank you for you quick reply.

Yes, using this combination of Mailbox and Events will most probably work for my usecase.

I gives me the feeling of a workaround though. Because you have to use two OS mechanisms when one would be enough to model my intent. Other environments offer a way to wait for multiple kinds of events, for example the poll() syscall in POSIX that waits for several file descriptors or sockets, a timeout and signals at the same time. Or the async/await pattern available in several environments.

Would a mechanism that allows a thread to sleep and wakeup on multiple kinds of events, like Mailbox and Event in my example, make the ChibiOS implementation considerably more complex?

But this is just a feeling I get from this, I haven't done things like benchmarks or code size comparisons to back this up.

User avatar
Giovanni
Site Admin
Posts: 14496
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1090 times
Been thanked: 932 times
Contact:

Re: Thread sleeping, wakeup on Mailbox or Event

Postby Giovanni » Mon Aug 26, 2024 2:03 pm

Events are meant to be the "or" mechanism.

Giovanni


Return to “ChibiOS/RT”

Who is online

Users browsing this forum: No registered users and 4 guests