anderskaloer / ring-buffer Goto Github PK
View Code? Open in Web Editor NEWA simple ring buffer (circular buffer) designed for embedded systems.
License: MIT License
A simple ring buffer (circular buffer) designed for embedded systems.
License: MIT License
The ring buffer size is 16, I add 16 elements , but the number of items is 15 .
ring_buffer_t ring_buffer;
char buf_arr[16];
ring_buffer_init(&ring_buffer, buf_arr, sizeof(buf_arr));
assert(ring_buffer_num_items(&ring_buffer) == 0);
/* Add elements to buffer; one at a time */
for(i = 0; i < 16; i++) {
ring_buffer_queue(&ring_buffer, i);
}
/* Verify size */
assert(ring_buffer_num_items(&ring_buffer) == 16);
Hello,
am I right, that the ring buffer is not interrupt-safe? I've had a problem at work, that it didn't work while receiving a lot of data (1 MB) and the ring buffer is configured for 2048 bytes. The main thing was, that data is received with UART non-blocking and while data is going to be written into the buffer in the callback function new data may be received already and read from the buffer in parallel.
bool ring_buffer_read(ring_buffer_t *buffer, uint8_t *data) {
ring_buffer_size_t tail_index = buffer->tail_index;
ring_buffer_size_t head_index = buffer->head_index;
if (tail_index == head_index) {
/* No items */
return false;
}
*data = buffer->buffer[tail_index];
buffer->tail_index = ((tail_index + 1) & RING_BUFFER_MASK(buffer));
return true;
}
void ring_buffer_write(ring_buffer_t *buffer, uint8_t data) {
ring_buffer_size_t tail_index = buffer->tail_index;
ring_buffer_size_t head_index = buffer->head_index;
/* Is buffer full? */
if (((head_index - tail_index) & RING_BUFFER_MASK(buffer)) == RING_BUFFER_MASK(buffer)) {
return;
}
/* Place data in buffer */
buffer->buffer[head_index] = data;
buffer->head_index = ((head_index + 1) & RING_BUFFER_MASK(buffer));
}
I've changed a few minor things, but regarding the interrup safety, I think storing first in a local variable is key (also seen in a talk in the bare metal series on YouTube).
If I have two thread , one is a very slow producer and another is a very fast consumer .
Can I use your ringbuffer without lock ? and without any memory barrier ?
lock free or not? does it support multi producer and multi consumer?
Hello,
Do you recommend using the ring buffer in other applications or is it not "used" enough yet ?
Thanks,
Ran
it is thread safe ?
I am very interested in this demo。I tried to go through "Readme"for more details,but nothing in it。
Enrich the content of the document so that more readers learn more。
A great demo.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.