Thread to thread communication - Mailboxes - Mempools and dereferencing

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

Moderators: RoccoMarco, lbednarz, utzig, tfAteba, barthess

Xela
Posts: 15
Joined: Thu Sep 07, 2017 12:05 pm
Has thanked: 3 times

Thread to thread communication - Mailboxes - Mempools and dereferencing

Postby Xela » Tue Dec 05, 2017 5:07 pm

I am currently trying to implement a producer/consumer data flow between two threads. At some point in the future these threads will be converted to ISRs. I am assuming it is possible that I can implement this in a way that works in both contexts (thread/ISR).

I have one thread that gets samples from an inertial unit (imu).
I have another thread that streams these values over USB.

I have been looking at examples that send a pointer to the structure of the latest samples (x,y,z) (ADC example) via a mailbox to the USB thread.

I am assuming here that if I pass by reference all the way, I will have read/write problems with the threads stomping on the same statically defined data structure in my imu driver.

I therefore want to implement a memory pool to avoid these read/write collisions.

I was going to implement my own memory pool but have discovered this is already implemented in ChibiOS.

I had initially thought to put the memory pool inside my imu driver. It therefore would not be accessible on the application layer, only pointers to the top of the pool would be retrieved through my imu drivers API. I saw though that the USB thread then would not be able to free the chunk in the pool if it did not have access to it directly.

Obviously then i need to implement the memory pool in my application and have my imu thread/ISR populate it.

That made me think.. if the memory pool is globally accessible in my application so that both threads can create_new/free, what do I need a mailbox for?

I have been doing some reading in the Chibi docs and other forum questions. From what I have read today I understand that the mailbox itself is an implementation of a memory pool? What I read may be out of date however and still trying to learn some of this stuff.

Am I making this more difficult than it needs to be as everything I need is already there for me to use?

I have read some code examples that have data types cast a message types and sent as a pointer to a mailbox. On the consumer side, this inverse operation is performed and the data is used as intended.

I can't see this working if it is 'pass by reference' from end to end. Under the hood, does the mailbox make an actual copy of the data pointed to, implementing a buffer of values instead of a buffer of pointers to the original data source? And if so, is it FIFO?

I think the most concise way of asking my question is: Does the mailbox Dereference the structures that are passed to it?

Cheers
Alex

User avatar
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: Thread to thread communication - Mailboxes - Mempools and dereferencing

Postby Giovanni » Tue Dec 05, 2017 6:55 pm

Hi,

It is a common use case. Pools are implemented as linked lists, the link is placed at beginning of the objects and overwrites the object data, this is why pools can only used to store disposed objects not "live" ones.

In ChibiOS trunk there is a new object called "Objects FIFO" implemented as a pool+mailbox, it does exactly what you need, you may give it a try.

Trunk code is stable enough for development usually.

Giovanni

Xela
Posts: 15
Joined: Thu Sep 07, 2017 12:05 pm
Has thanked: 3 times

Re: Thread to thread communication - Mailboxes - Mempools and dereferencing

Postby Xela » Tue Dec 05, 2017 7:03 pm

Thank you for the quick response Giovanni

I will look into the Objects FIFO.

Thank you

faisal
Posts: 374
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 44 times
Been thanked: 60 times

Re: Thread to thread communication - Mailboxes - Mempools and dereferencing

Postby faisal » Thu Dec 14, 2017 6:05 am

Giovanni wrote:Hi,

It is a common use case. Pools are implemented as linked lists, the link is placed at beginning of the objects and overwrites the object data, this is why pools can only used to store disposed objects not "live" ones.

In ChibiOS trunk there is a new object called "Objects FIFO" implemented as a pool+mailbox, it does exactly what you need, you may give it a try.

Trunk code is stable enough for development usually.

Giovanni


I didn't follow you, what do you mean by pools can only be used to store disposed objects and not "live" ones?

I use memory pools along with a mailbox to pass messages and structures between ISRs/threads (single producer/consumer). What's the difference between that and using an i/o buffer queue from hal_buffers? If I use both in manner where the consumer is waiting either on a new mailbox object or i/o buffer queue waiting for the next full buffer - which one would be more suitable (goal is *speed*)?

User avatar
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: Thread to thread communication - Mailboxes - Mempools and dereferencing

Postby Giovanni » Thu Dec 14, 2017 8:18 am

Putting an object into a pool overwrites the first 4 bytes of the object.

Giovanni


Return to “General Support”

Who is online

Users browsing this forum: No registered users and 44 guests