need advice to implement blocking ring buffer

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

Moderators: RoccoMarco, lbednarz, tfAteba

User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

need advice to implement blocking ring buffer

Post by alex31 »

Hello,

I want to implement a ring (circular) buffer of bytes, where permitted operation would be
write(const byte *from, size_t n), and read(byte *to, size_t n), in the same spirit than a mailbox, with the difference that basic
element is a byte, not a msg_t, and you can add or read a buffer of bytes instead of a single element. (workhorse for the copy will be memcpy because i want to maximize performance).
write(..., n) should block until the circular buffer can push all the «n» bytes, and read(..., n) should block until it can pop all the n bytes.

It's to be used in a context where there is only two endpoints, so that can make the problem easier than a generalised N writer / M reader.

Perhaps this kind of object already exists in chibios ?

If not, which synchronization object should i use ?, counting semaphore does not seems to be appropriate.

thanks for any idea
Alexandre
User avatar
FXCoder
Posts: 403
Joined: Sun Jun 12, 2016 4:10 am
Has thanked: 188 times
Been thanked: 134 times

Re: need advice to implement blocking ring buffer

Post by FXCoder »

Maybe os/hal/src/hal_buffers.c will help.
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

Re: need advice to implement blocking ring buffer

Post by alex31 »

Thanks for the link.

it's not what i search, i want a bytes stream ring buffer, and hal_buffers.c stuff is based on fixed length buffers.

I will implement it with a mutex and two conditional variables, but when it comes to using these beasts in concurrent algorithm, i always fear to be bitten by a subtle concurrent flaw ...

Alexandre
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: need advice to implement blocking ring buffer

Post by Giovanni »

There is a "pipe" mechanism in the work but not yet complete.

Giovanni
User avatar
FXCoder
Posts: 403
Joined: Sun Jun 12, 2016 4:10 am
Has thanked: 188 times
Been thanked: 134 times

Re: need advice to implement blocking ring buffer

Post by FXCoder »

While pipe is in the works an alternative could be implemented with hal_queues.c which implements byte Input and Output queues.
For example:

Code: Select all

size_t iqReadTimeout(input_queue_t *iqp, uint8_t *bp,
                     size_t n, sysinterval_t timeout)

If you specify TIME_INFINITE for timeout the read will wait for N bytes to be transferred from the queue to the buffer (or for the queue to be RESET).
Use...

Code: Select all

msg_t iqPutI(input_queue_t *iqp, uint8_t b)

...to write to the queue (non-blocking -e.g. used in ISR handlers)
See the header file for macros for checking queue space, etc. to craft your specific features.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: need advice to implement blocking ring buffer

Post by Giovanni »

Hi,

I completed the pipes subsystem in trunk but it is not tested and still subject to change. You may give it a try.

Giovanni
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

Re: need advice to implement blocking ring buffer

Post by alex31 »

Nice, thank you !!

I had just validated my own stuff using one mutex and two condvars, but i prefer to use standard chibi components.

My project is using 18.2.2, does pipes subsystem is dependent on new stuff on trunk, or can i backport it easily to 18.2.2 to try it in my project ?



Alexandre
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: need advice to implement blocking ring buffer

Post by Giovanni »

It should easy to back-port, it depends on existing mechanisms. Note that I haven't yet written test code for it but it is just an unholy hybrid between mailboxes and I/O queues, I don't expect huge problems.

Giovanni
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

Re: need advice to implement blocking ring buffer

Post by alex31 »

Again, thanks Giovanni for all your work.

It should easy to back-port, it depends on existing mechanisms. Note that I haven't yet written test code for it but it is just an unholy hybrid between mailboxes and I/O queues, I don't expect huge problems.


As you have said, backporting was not a problem, i can send the patch file for 18.2.2 if you want.

As i have wrote a test for my own implementation, it was easy for me to test yours since the api is the same.

i create a global pipe, then
i create two thread, one producer, one consumer,
producer write a sequence, consumer verify that it receives the sequence unaltered
in my original code, size of write and read is randomly generated.

unfortunately, even with fixed (and same) read/write size I have not been able to made it to work.
i have few write and read (with no respect of the sequence) then each thread are stuck (other thread, a shell continue to work)
producer is in state QUEUED
consumer is in state WTMTX,

here is my test code, it works with my impl, but i have perhaps made an obvious error using chpipes.

Code: Select all

#include <ch.h>
#include <hal.h>
#include <stdlib.h>

#define PIPE_SIZE 33
static uint8_t _pipeBuffer[PIPE_SIZE];
PIPE_DECL(pp, _pipeBuffer, PIPE_SIZE);

static THD_WORKING_AREA(waBlinker, 304);   
static void blinker (void *arg)         
{
  (void)arg;               
  chRegSetThreadName("blinker");      
  while (true) {            
    palToggleLine(LINE_C00_LED1);      
    chThdSleepMilliseconds(1000);      
  }
}

#define WLEN_MAX 32
#define RLEN_MAX 15
static THD_WORKING_AREA(waProducer, 304);   
static void producer (void *arg)      
{
  (void)arg;               
  chRegSetThreadName("producer");      
  uint8_t writeBuf[WLEN_MAX] = {0};
  uint8_t count=0;
  while (true) {
    //size_t wlen=1 + rand() % (WLEN_MAX);
    size_t wlen = 16;
    for (size_t i=0; i<wlen; i++) {
      writeBuf[i] = count++;
    }
    chPipeWriteTimeout(&pp, writeBuf, wlen, TIME_INFINITE);
    DebugTrace("write %d", wlen);
  }
}

static THD_WORKING_AREA(waConsumer, 304);   
static void consumer (void *arg)      
{
  (void)arg;               
  chRegSetThreadName("consumer");      
  uint8_t readBuf[RLEN_MAX] = {0};
 
  uint8_t count=0;
  while (true) {
    //size_t rlen=1 + rand() % (RLEN_MAX);
    size_t rlen=16;
    chPipeReadTimeout(&pp, readBuf, rlen, TIME_INFINITE);
    DebugTrace("read %d", rlen);
    for (size_t i=0; i<rlen; i++) {
      if (readBuf[i] != count++)  {
   DebugTrace("test error read %d instead %d", readBuf[i], count);
      }
    }
    chThdSleepMilliseconds(500);
  }
}


int main (void)
{

  halInit();
  chSysInit();
 
  srand(chSysGetRealtimeCounterX());
  // chPipeObjectInit(&pp, _pipeBuffer, PIPE_SIZE);  /* not needed when using PIPE_DECL*/
  consoleInit();   
  chThdCreateStatic(waBlinker, sizeof(waBlinker), NORMALPRIO, &blinker, NULL);
  chThdCreateStatic(waConsumer, sizeof(waConsumer), NORMALPRIO, &consumer, NULL);
  chThdCreateStatic(waProducer, sizeof(waProducer), NORMALPRIO, &producer, NULL);
 
  consoleLaunch();  // lancement du shell
 
  // main thread does nothing
  chThdSleep(TIME_INFINITE);
}


If the problem is on chpipes and not on my test code, I am always available to test chpipes when you 'll fix it.

Alexandre
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: need advice to implement blocking ring buffer

Post by Giovanni »

Hi,

Pipes should be OK now, I added a new test sequence to the RT suite.

Giovanni
Post Reply