One thread waiting on multiple events?

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
lmamakos
Posts: 20
Joined: Sun May 13, 2012 3:59 pm

One thread waiting on multiple events?

Post by lmamakos »

I've been hacking along on an application, and I'm trying to reduce the number of threads to cut down on the per-thread stack RAM use. My application has a somewhat complex state machine at the core. At present, I have multiple tasks using the state machine protected with a mutex to ensure one trip through at a time. I may have timers fire, a message arriving on a serial interface or two.

What I'm wondering is if there's some idiom that I'm not aware of, or technique to have one thread asleep, waiting for some of these other things to occur. In particular, I'd like to start a read on a serial channel to read some number of bytes, and then notify me asynchronously. At present, I do a "blocking" read using chIOReadTime(). Likewise, I might have a timer expire or a message arrive from another thread using a mailbox that I'd want to process and then run though my state machine.

Is there an example I should look at to get a sense of mechanism that might work for me? Certainly the low level primitive are undoubtedly in there, but I'd like to make sure I'm using the primitives in the way intended.

Thanks for any pointers!
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: One thread waiting on multiple events?

Post by Giovanni »

Hi,

The mechanism you are looking for are Events, a thread can wait for up to 32 distinct event sources. Many device drivers generate events by default (CAN, MAC, Serial) and you can easily add event sources to those working with callbacks. They are meant for driving threads implementing state machines, so it is you exact use case.

Look at this excellent contribution for more info: viewtopic.php?f=3&t=803&start=10

Giovanni
lmamakos
Posts: 20
Joined: Sun May 13, 2012 3:59 pm

Re: One thread waiting on multiple events?

Post by lmamakos »

Thanks, this is just the sort of pointer I was looking for. I didn't know that the serial driver had an event source, I'll investigate that further.

I'm wondering if there would be use for a common capability through the I/O channel, Sequential Streams or I/O Queues abstraction to be able to have an event associated with that abstracted interface. I'd also like to hook, e.g., Mailboxes into the event flags construct so I could be notified to go look for new messages without either polling periodically or suspending. I don't know if that sort of capability is in there, or easily synthesized using the existing API.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: One thread waiting on multiple events?

Post by Giovanni »

There is also an abstract interface handling events too: BaseAsynchronousChannel. The serial and seria_usb drivers implement that inteface.

About mailboxes, you can atomically send a message and broadcast an event.

Giovanni
Post Reply