data-pipe between threads.

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

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
rew
Posts: 383
Joined: Sat Jul 19, 2014 12:59 pm
Has thanked: 2 times
Been thanked: 14 times

data-pipe between threads.

Post by rew »

Hi,
I'm writing something with an ESP8266, so I occasionally get data from the other side. So far I simply dump it on the console, but on short notice, I will have to start doing something with it.

Is there a chibios way of passing "byte stream" oriented data between threads?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: data-pipe between threads.

Post by Giovanni »

Hi,

You can use mailboxes for this.

Giovanni
rew
Posts: 383
Joined: Sat Jul 19, 2014 12:59 pm
Has thanked: 2 times
Been thanked: 14 times

Re: data-pipe between threads.

Post by rew »

Ok. But if I then "post" data of just one byte, I'll have to allocate 4 times "spare" memory for the buffer, right?

I could improve on memory efficiency by implementing something similar with a "byte" datatype.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: data-pipe between threads.

Post by Giovanni »

You may use I/O queues that are byte oriented but those are non-blocking on one side.

Giovanni
rew
Posts: 383
Joined: Sat Jul 19, 2014 12:59 pm
Has thanked: 2 times
Been thanked: 14 times

Re: data-pipe between threads.

Post by rew »

Now he tells me, just when I have the message queue implementation working... :-)

Thanks! It seems that the IO queue would be a better fit. I currently just have a thread that reads a byte at a time from the ESP8266, and now, when appropriate it puts it on the message queue. But eventually, I'll move to that other kind of serial driver where my callback gets called each time a character comes in. Then, when appropriate, putting things into that IO queue would be ideal.

For now it works, and when I get performance or memory problems I know where I can shave off some bytes.. :-)
rew
Posts: 383
Joined: Sat Jul 19, 2014 12:59 pm
Has thanked: 2 times
Been thanked: 14 times

Re: data-pipe between threads.

Post by rew »

Very similar problem. Currently working on the input thread is waiting for serial input, but should/would switch to USB input the next time it reads a character.

The input thread has a big call-stack. Duplicating it for both the USB-serial input and physical-serial input is not possible (chip is out of memory).

Waiting with a timeout would work acceptable I'd say. But the BaseSequentialStream does not support timeouts.

Then I found memory streams. I thought they would allow me to pass data from one process to the other, but looking at the implementation, memory is never reused. So if I declare my buffer to be 100 bytes, I can transfer 100 bytes and never more. Correct?

What's the use of such a stream? I appreciate the use case: put data in flash, and process it with a function that is used to BaseSequentialStream as input.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: data-pipe between threads.

Post by Giovanni »

Hi,

MemoryStreams are the equivalent of Java string buffers, those are for handling a memory buffer using a stream interface.

Passing data among threads is done using mailboxes or messages.

Giovanni
Post Reply