A byte queue is a structure that enables tasks to post and receive data bytes. The byte queue has a buffer that enables a number of posts to be completed without receives occuring. The buffer keeps the posted bytes in a fifo order so the oldest byte is received first. When the buffer isn’t full a post will put the byte at the back of the queue and the calling task continues execution. When the buffer is full a post will block the calling task until there is room for the byte. When the buffer isn’t empty a receive will return the byte from the front of the queue and continue execution of the calling task. When the buffer is empty a receive will block the calling task until a byte is posted.

You allocate a byte queue by declaring it as a C variable

    CTL_BYTE_QUEUE_t m1;

A byte queue is initialised using the ctl_byte_queue_init function.

    unsigned char queue[20];
    …
    ctl_byte_queue_init(&m1, queue, 20);

This example uses an 20 element array for the byte queue.

You can post a byte to a byte queue with an optional timeout using the ctl_byte_queue_post function.

    ctl_byte_queue_post(&m1, 45, 0, 0);

This example posts the byte `45` onto the byte queue.

    if (ctl_byte_queue_post(&m1, 45, 1, ctl_get_current_time()+1000) == 0)
      {
        // timeout occured
      }

This example uses a timeout and tests the return result to see if the timeout occured.

If you want to post a byte and you don’t want to block (e.g from an interrupt service routine) you can use the ctl_byte_queue_post_nb function

    if (ctl_byte_queue_post_nb(&m1, 45)==0)
      {
        // queue is full
      }

This example tests the return result to see if the post failed.

You can receive a byte with an optional timeout using the ctl_byte_queue_receive function.

    void *msg;
      ctl_byte_queue_receive(&m1, &msg, 0, 0);

This example receives the oldest byte in the byte queue.

    if (ctl_byte_queue_receive(&m1, &msg, 1, ctl_get_current_time()+1000) == 0)
      {
        // timeout occured
      }

This example uses a timeout and tests the return result to see if the timeout occured.

If you want to receive a byte and you don’t want to block (e.g from an interrupt service routine) you can use the ctl_byte_queue_receive_nb function.

    if (ctl_byte_queue_receive_nb(&m1, &msg)==0)
      {
        // queue is empty
      }

##### Example

The following example illustrates usage of a byte queue to implement the producer-consumer problem.

    CTL_BYTE_QUEUE_t m1;
    void *queue[20];

    void task1(void)
    {
      …
      ctl_byte_queue_post(&m1, (void *)i, 0, 0);
      …
    }

    void task2(void)
    {
      void *msg;
      …
      ctl_byte_queue_receive(&m1, &msg, 0, 0);
      …
    }

    int
    main(void)
    {
      …
      ctl_byte_queue_init(&m1, queue, 20);
      …
    }