diff --git a/docs/spsc/bipartite_buf.md b/docs/spsc/bipartite_buf.md index 0ea2f89..a7aa4cf 100644 --- a/docs/spsc/bipartite_buf.md +++ b/docs/spsc/bipartite_buf.md @@ -71,6 +71,55 @@ if (!write_started) { } ``` +## How it works +The Bipartite Buffer uses the same base principle as the [ring buffer data structure](https://en.wikipedia.org/wiki/Circular_buffer), however its ability to provide contiguous space for writing and reading requires modifying the approach slightly. + +Let's consider a typical usage scenario, we want to acquire 4 slots for writing in the following buffer: + +
+ +
+ +We have 7 free slots, so this would work fine for a regular ring buffer. +However when we unroll the buffer we can notice the issue: + ++ +
+ +We cannot acquire 4 slots from the start of the free space, as there is not enough contiguous space until the end of the buffer, and the only solution is to acquire 4 slots from the beginning. + +After acquiring those slots, we have a gap in available data caused by the skip and we must somehow tell the buffer to avoid reading from that region: + ++ +
+ +This is where we introduce another index - the **invalidate index** ``i``. +We can set it to the start of the region we want to skip, and next time we are reading the data, we only consider data until the invalidate index. + ++ +
+ +Now the first time we acquire data for reading, we will get the region from `r` to `i`: + ++ +
+ +And the next time we will acquire the region from `0` to `w`: + ++ +
+ +Lastly, when writing, we can write over invalidated parts of the buffer as it doesn't contain anything useful, but also have to move the invalidate index: + ++ +
+ ## Dealing with caches on embedded systems When using the library with DMA or asymmetric multicore on embedded systems with cache it is necessary to perform manual cache synchronization in one of the following ways: * Using platform specific data synchronization barriers (```DSB``` on ARM) diff --git a/docs/spsc/images/bipartite_buf_unwrapped_after_invalidate.svg b/docs/spsc/images/bipartite_buf_unwrapped_after_invalidate.svg new file mode 100644 index 0000000..277660e --- /dev/null +++ b/docs/spsc/images/bipartite_buf_unwrapped_after_invalidate.svg @@ -0,0 +1,13 @@ + \ No newline at end of file diff --git a/docs/spsc/images/bipartite_buf_unwrapped_after_invalidate_read1.svg b/docs/spsc/images/bipartite_buf_unwrapped_after_invalidate_read1.svg new file mode 100644 index 0000000..9ba513d --- /dev/null +++ b/docs/spsc/images/bipartite_buf_unwrapped_after_invalidate_read1.svg @@ -0,0 +1,13 @@ + \ No newline at end of file diff --git a/docs/spsc/images/bipartite_buf_unwrapped_after_invalidate_read2.svg b/docs/spsc/images/bipartite_buf_unwrapped_after_invalidate_read2.svg new file mode 100644 index 0000000..8a974d5 --- /dev/null +++ b/docs/spsc/images/bipartite_buf_unwrapped_after_invalidate_read2.svg @@ -0,0 +1,13 @@ + \ No newline at end of file diff --git a/docs/spsc/images/bipartite_buf_unwrapped_after_invalidate_write.svg b/docs/spsc/images/bipartite_buf_unwrapped_after_invalidate_write.svg new file mode 100644 index 0000000..081a58c --- /dev/null +++ b/docs/spsc/images/bipartite_buf_unwrapped_after_invalidate_write.svg @@ -0,0 +1,13 @@ + \ No newline at end of file diff --git a/docs/spsc/images/bipartite_buf_unwrapped_after_wrapping_write.svg b/docs/spsc/images/bipartite_buf_unwrapped_after_wrapping_write.svg new file mode 100644 index 0000000..cb4852b --- /dev/null +++ b/docs/spsc/images/bipartite_buf_unwrapped_after_wrapping_write.svg @@ -0,0 +1,13 @@ + \ No newline at end of file diff --git a/docs/spsc/images/ring_buf_has_space.svg b/docs/spsc/images/ring_buf_has_space.svg new file mode 100644 index 0000000..d049cbd --- /dev/null +++ b/docs/spsc/images/ring_buf_has_space.svg @@ -0,0 +1,13 @@ + \ No newline at end of file diff --git a/docs/spsc/images/ring_buf_unwrapped_has_space.svg b/docs/spsc/images/ring_buf_unwrapped_has_space.svg new file mode 100644 index 0000000..7d9411c --- /dev/null +++ b/docs/spsc/images/ring_buf_unwrapped_has_space.svg @@ -0,0 +1,13 @@ + \ No newline at end of file