Sending single message to multiple mailboxes

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

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
phofman
Posts: 8
Joined: Sat Apr 29, 2017 3:26 pm

Sending single message to multiple mailboxes

Post by phofman »

Hi, I am trying to solve the following functionality of single serial input/multiple outputs message dispatcher. The dispatcher receives a message and based on its contents it resends the message to several serial outputs/other processing threads. This is my idea but perhaps it could be less complicated:

Thread A - receiver (ISR?) receives a message into its own static buffer. It e.g. decides to forward the message to senders B and C. The receiver takes a free item from a memory pool, fills it with the message contents (memory copy) and sends the pointer/msg_t to mailboxes of the thread B and C. Upon each sending it increments some atomic variable "usersCount" (semaphor?) of the message struct.

Thread B/C - process the message (e.g. resend) and decrement the variable "usersCount". If it reaches zero (no thread is using the message any more), it returns the message to its pool for the receiver to allocate.

Please a few questions:
1) If no message is available in the pool (sized e.g. 3 items), the receiver (ISR?) could wait a bit, until available, but at most some timeout. Upon the timemout it would ignore the incoming message and start listening for new message. Is it reasonable to wait a bit in ISR + what choice should I use for the wait? I found a discussion about this older mail example https://github.com/ChibiOS/ChibiOS/blob ... ous/mail.c - is perhaps this the way to go? Could it be used in ISR?

2) If any of the sender threads B, C dies (?), the message would never be returned to the pool. Please how to handle such situation? In java I would use the "finally" section to make sure the message gets returned/freed.

I very much appreciate any suggestion.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Sending single message to multiple mailboxes

Post by Giovanni »

Hi,

About your point 1:

In RT4 there are "guarded pools", pools with a semaphore, if an element is not available then the caller waits, timeout is possible. It is the final form of that mail module.

Point 2:

Thread should never unexpectedly die, why worried about this? if something wrong happens it is the whole application to go down, there is no isolation.

About the counter, you can use a critical section for increment/decrement, such a short operation does not require a semaphore/mutex.

Giovanni
phofman
Posts: 8
Joined: Sat Apr 29, 2017 3:26 pm

Re: Sending single message to multiple mailboxes

Post by phofman »

Hi Giovanni,

Thanks a lot for your help.

In RT4 there are "guarded pools", pools with a semaphore, if an element is not available then the caller waits, timeout is possible. It is the final form of that mail module.


Very nice, I will have a look. There is "Merged RT4" changelog in master branch of https://github.com/ChibiOS/ChibiOS. Should I use that one? Currently I am using the stable 16.1.8. Please how safe is using the master branch clone?

Thread should never unexpectedly die, why worried about this? if something wrong happens it is the whole application to go down, there is no isolation.


I am trained from java where exceptions break flow of the code :-) Sorry, I did not realize the exceptions flow does not exist in C.

About the counter, you can use a critical section for increment/decrement


OK, I will use that. It would be basically a simple garbage collection to memory pools by reference counter. Is there perhaps any recommended API for that? Just asking, your great project always surprises me with its completeness.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Sending single message to multiple mailboxes

Post by Giovanni »

Hi,

The git repository is just a periodic clone, the latest code is in the subversion trunk repository on sourceforge, you need to checkout that one.

Critical zones are handled using chSysLock()/chSysUnlock().

Giovanni
phofman
Posts: 8
Joined: Sat Apr 29, 2017 3:26 pm

Re: Sending single message to multiple mailboxes

Post by phofman »

Thanks a lot.

I think I will need to allocate from pool in the ISR of serial port receiver. Can I call the chGuardedPoolAllocTimeoutS with very short timeout? To inexperienced myself It seems to make sense to be able to try waiting a little bit for the freed message to the pool, instead of dropping the incoming received message right away. But it would be in ISR...
steved
Posts: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Re: Sending single message to multiple mailboxes

Post by steved »

phofman wrote:But it would be in ISR...

If you're waiting in the ISR not a lot else can happen unless you re-enable interrupts; with potential stack problems and maybe others.
Can you get the ISR to hand off the task to a thread, maybe by triggering an event flag or semaphore?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Sending single message to multiple mailboxes

Post by Giovanni »

From ISRs you can ONLY call functions with name terminated by "I" (I-class), anything else is rejected by the state checker.

Timeouts make no sense in an ISR context, as steved suggested, you may need a thread there, you could also have a design problem if you think to have to wait for a resource when IRQ requests are faster than availability.

See the serial driver for example, it reads from the UART when an interrupt occurs but if there is no space in queue then data is discarded. It happens if the application does not consume data fast enough, an application design problem not a driver problem.

Giovanni
phofman
Posts: 8
Joined: Sat Apr 29, 2017 3:26 pm

Re: Sending single message to multiple mailboxes

Post by phofman »

OK, I will make the memory pools a bit larger (more redundancy) and will drop the incoming message in case of temporary shortage of freed objects. I will use regular chPoolAllocI, without any waiting for the memory pool to recover. Guys, thanks a lot.
Post Reply