Sending single message to multiple mailboxes
Posted: Sun May 14, 2017 9:16 pm
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.
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.