lock-free-ring-buffer icon indicating copy to clipboard operation
lock-free-ring-buffer copied to clipboard

"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.

Brought to you by:

Quantum Leaps


GitHub release (latest by date) GitHub

What is it?

"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.

Lock-Free Restrictions

The ring buffer does not require any "locking" (mutual exclusion mechanism) as long as the following restrictions are met:

  1. Only one thread/interrupt can produce data into the ring buffer
  2. Only one thread/interrupt can consume data from the ring buffer

In other words, a given LFRB can be used only by one pair of producer/ consumer threads/interrupts. Fortunately, this is the most frequently encountered scenario.

There are also some additional assumptions about the "atomicity" of the LFRB counters, which are spelled out in the ring_buf.h header file.

Code Structure

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.)

Test/Example of Use

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_ring_buf.c - example of use and tests the LFRB.

Licensing

The LFRB is licensed under the MIT open source license.

Comments/Discussion

If you'd like to discuss LFRB or related subjects, plese use the "Issues" tab.

How to Help this Project?

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):