"Lock-Free Ring Buffer" (LFRB) is a minimal, customizable implementation of a ring buffer (a.k.a. circular buffer) in C, specifically suitable for embedded systems, such as single-core microcontrollers with:
- ARM Cortex-M CPU
- MSP430 CPU
- PIC24/dsPIC CPU
- other similar 16/32-bit CPUs
NOTE The presented LFRB implementation is intended for deeply embedded systems, such as single-core ARM Cortex-M MCUs without cache memories. The presented implementation might not be appropriate for complex multi-core SoCs with cache memory, etc. Please refer to the literature, for example:
The ring buffer does not require any "locking" (mutual exclusion mechanism) as long as the following restrictions are met:
- Only one thread/interrupt can produce data into the ring buffer
- Only one thread/interrupt can consume data from the ring buffer
- Both the producer and consumer run on the same CPU
- The system has strong memory consistency
- Additional assumptions about the "atomicity" of the LFRB counters are spelled out in the ring_buf.h header file
In other words, LFRB is a single-producer, single-consumer FIFO. Fortunately, this is the most frequently encountered scenario in deeply embedded applications.
The ring buffer implementation consists of two files located in the src directory:
- ring_buf.h - contains the interface
- ring_buf.c - contains the implementation
The ring buffer holds elements of they type RingBufElement, which
can be customized (typically uint8_t
, uint16_t
, uint32_t
, float
,
void*
(pointers), etc.)
The directory ET
contains the
Embedded Test (ET)
super-simple testing framework. ET is used to test the LFRB.
Specifically, the ET tests and examples of use of LFRB are located in the file:
- test/test_ring_buf.c - example of use and tests the LFRB.
To build and run the tests on the host, open a terminal, change to the test
sub-directory and type make
. The generated output is shown below:
make
gcc -MM -MT build_host/et_host.o -c -g -O -fno-pie -std=c11 -pedantic -Wall -Wextra -W -I. -I../src -I../et -DQ_HOST ../et/et_host.c > build_host/et_host.d
gcc -MM -MT build_host/et.o -c -g -O -fno-pie -std=c11 -pedantic -Wall -Wextra -W -I. -I../src -I../et -DQ_HOST ../et/et.c > build_host/et.d
gcc -MM -MT build_host/test_ring_buf.o -c -g -O -fno-pie -std=c11 -pedantic -Wall -Wextra -W -I. -I../src -I../et -DQ_HOST test_ring_buf.c > build_host/test_ring_buf.d
gcc -MM -MT build_host/ring_buf.o -c -g -O -fno-pie -std=c11 -pedantic -Wall -Wextra -W -I. -I../src -I../et -DQ_HOST ../src/ring_buf.c > build_host/ring_buf.d
gcc -c -g -O -fno-pie -std=c11 -pedantic -Wall -Wextra -W -I. -I../src -I../et -DQ_HOST ../src/ring_buf.c -o build_host/ring_buf.o
gcc -c -g -O -fno-pie -std=c11 -pedantic -Wall -Wextra -W -I. -I../src -I../et -DQ_HOST test_ring_buf.c -o build_host/test_ring_buf.o
gcc -c -g -O -fno-pie -std=c11 -pedantic -Wall -Wextra -W -I. -I../src -I../et -DQ_HOST ../et/et.c -o build_host/et.o
gcc -c -g -O -fno-pie -std=c11 -pedantic -Wall -Wextra -W -I. -I../src -I../et -DQ_HOST ../et/et_host.c -o build_host/et_host.o
gcc -no-pie -o build_host/test_ring_buf.exe build_host/ring_buf.o build_host/test_ring_buf.o build_host/et.o build_host/et_host.o
build_host/test_ring_buf.exe
ET embedded test 2.0.0, https://github.com/QuantumLeaps/ET
---------------- group: lock-free ring buffer -----------------
[1] "RingBuf_num_free" .. PASSED
[2] "RingBuf_put 3" .. PASSED
[3] "RingBuf_get" .. PASSED
[4] "RingBuf_process_all test_data" .. PASSED
------------ 4 test(s), 0 skipped -------------
OK
The LFRB distribution provides a simple makefile (see [test/nucleo-c031c6.mak)) to build the tests for the STM32 NUCLEO-C031C6 shown below.
REMARK
The STM32 NUCLEO board has been selected because it can to be programmed by simply copying the binary image to the board enumerated as a USB drive.
The LFRB distribution contains all files requried to build the binary image for the NUCLEO-C031C6 board. However, you still need to provide the GCC-ARM compiler and the serial terminal utility to receive the output produced by the board. To run the test on the STM32 NUCLEO-C031C6, you open a terminal window and type:
cd lock-free-ring-buffer/test
make -f make_nucleo-c031c6 USB=g:
NOTE
The GCC-ARM cross-compiler for Windows as well as themake
utility are available in the QTools collection for Windows.
The follwing screen shot shows the build process, programming the board (by copying the binary image) and the test output on the Termite serial terminal.
The LFRB is licensed under the MIT open source license.
If you'd like to discuss LFRB or related subjects, plese use the "Issues" tab.
Please feel free to clone, fork, and make pull requests to improve LFRB. If you like this project, please give it a star (in the upper-right corner of your browser window):