# Behavior of xStreamBufferSend / xStreamBufferReceive

**URL:** <https://forums.freertos.org/t/behavior-of-xstreambuffersend-xstreambufferreceive/7205>\
**Category:** Kernel\
**Created:** [May 19, 2018, 11:27am UTC](https://forums.freertos.org/t/behavior-of-xstreambuffersend-xstreambufferreceive/7205 "2018-05-19T11:27:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 19, 2018, 11:27am UTC](https://forums.freertos.org/t/behavior-of-xstreambuffersend-xstreambufferreceive/7205/1 "2018-05-19T11:27:44Z")

</div>

**[hubpav](https://sourceforge.net/u/hubpav/)** wrote on [Saturday, May 19, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/822f23f5/#3b84):

Hi,

I am not 100% sure how **xStreamBufferSend** / **xStreamBufferReceive** works. Is it safe to assume that **xDataLengthBytes** are guaranteed to be sent / received when **portMAX\_DELAY** is specified when the call is finished?

If that is the case, then the following wrapper is not necessary. Is that corret?

```auto
size_t os_stream_write(os_stream_t *stream, const void *buffer, size_t length)
{
    size_t bytes_written = 0;

    while (true)
    {
        size_t chunk_length = xStreamBufferSend(stream->_handle, buffer, length, portMAX_DELAY);

        bytes_written += chunk_length;

        length -= chunk_length;

        if (length == 0)
        {
            break;
        }

        buffer = (uint8_t *) buffer + chunk_length;
    }

    return bytes_written;
}

```

Thank you. Cheers, Pavel

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [May 20, 2018, 1:27am UTC](https://forums.freertos.org/t/behavior-of-xstreambuffersend-xstreambufferreceive/7205/2 "2018-05-20T01:27:11Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Sunday, May 20, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/822f23f5/#3b84/39bc):

> I am not 100% sure how _xStreamBufferSend_ / _xStreamBufferReceive_  
> works. Is it safe to assume that _xDataLengthBytes_ are guaranteed to be  
> sent / received when _portMAX\_DELAY_ is specified when the call is finished?

You have all the source code, and xStreamBufferSend() is a small  
function to read. Lines 520 and 549 (at the time of writing) show a  
loop with the task waiting under the required amount of space is  
available:  
[https://sourceforge.net/p/freertos/code/HEAD/tree/trunk/FreeRTOS/Source/stream\_buffer.c](https://sourceforge.net/p/freertos/code/HEAD/tree/trunk/FreeRTOS/Source/stream_buffer.c)

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 20, 2018, 5:49pm UTC](https://forums.freertos.org/t/behavior-of-xstreambuffersend-xstreambufferreceive/7205/3 "2018-05-20T17:49:04Z")

</div>

**[hubpav](https://sourceforge.net/u/hubpav/)** wrote on [Sunday, May 20, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/822f23f5/#8859):

Hi Richard, thank you!

Apparently **xStreamBufferSend** handles that for you inside the **do { } while** loop, so no wrapper is necessary there.

With **xStreamBufferReceive** , the situation is different though… the call can return without having the desired length available under normal circumstances (as task can get notified from various reasons) - even if **portMAX\_DELAY** has been specified.

This is the wrapper (with timeout support) I have been using for reference (feedback is welcome):

```auto
size_t os_stream_timed_read(os_stream_t *stream, void *buffer, size_t length, uint32_t milliseconds)
{
    if (milliseconds == 0)
    {
        return xStreamBufferReceive(stream->_handle, buffer, length, 0);
    }

    size_t bytes_read = 0;

    uint64_t now = os_time_get();

    uint64_t timeout = now + milliseconds;

    while (now < timeout)
    {
        size_t chunk_length = xStreamBufferReceive(stream->_handle, buffer, length, pdMS_TO_TICKS(timeout - now));

        bytes_read += chunk_length;

        length -= chunk_length;

        if (length == 0)
        {
            break;
        }

        buffer = (uint8_t *) buffer + chunk_length;

        now = os_time_get();
    }

    return bytes_read;
}

```

Thank you. Cheers, Pavel

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [May 20, 2018, 7:23pm UTC](https://forums.freertos.org/t/behavior-of-xstreambuffersend-xstreambufferreceive/7205/4 "2018-05-20T19:23:35Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Sunday, May 20, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/822f23f5/#8859/8642):

Correct, in xStreamBufferReceive() the second parameter is the size of  
the buffer into which data is to be copied, not the number of bytes to  
receive, so xSteamBufferRecevie() will return as soon as it has ‘some’  
data, the length of the data it receives in one go being capped to the  
buffer length. See the xBufferLengthBytes description here:  
[https://www.freertos.org/xStreamBufferReceive.html](https://www.freertos.org/xStreamBufferReceive.html)

Ref your implementation of os\_stream\_timed\_read() - from a brief look it  
seems you have have an issue if the tick count overflows, the likelihood  
of which you have avoided by using a uint64\_t - access to which will  
probably not be atomic. It may be easier to use code similar to that  
shown on this page: [https://www.freertos.org/xTaskCheckForTimeOut.html](https://www.freertos.org/xTaskCheckForTimeOut.html)  
which uses FreeRTOS API calls to take possible overflows into account  
for you.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 20, 2018, 10:22pm UTC](https://forums.freertos.org/t/behavior-of-xstreambuffersend-xstreambufferreceive/7205/5 "2018-05-20T22:22:56Z")

</div>

**[hubpav](https://sourceforge.net/u/hubpav/)** wrote on [Sunday, May 20, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/822f23f5/#0254):

Thank you, Richard. I was not aware of the **xTaskCheckForTimeout** function and I will look into it.

You are correct - I use **uint64\_t** for my application ticks as it will “never” overflow… 🙂

However, I have been trying to make the access to this variable safe. Here is the snippet including the **vApplicationTickHook** callback implementation:

```auto
static uint64_t _os_time = 0;

uint64_t os_time_get(void)
{
    taskENTER_CRITICAL();

    uint64_t ret = _os_time;

    taskEXIT_CRITICAL();

    return ret;
}

void vApplicationTickHook(void)
{
    static TickType_t xTickLast = 0;

    TickType_t xTickNow = xTaskGetTickCountFromISR();

    _os_time += xTickNow - xTickLast;

    xTickLast = xTickNow;
}

```

I hope this is sufficient for safe tick counter operation.

Thanks! Regards, Pavel
