Commit d994c429 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Remove race condition

parent 1026170f
......@@ -17,26 +17,16 @@ buffer_push(uint8_t x)
}
uint8_t
buffer_pop(void)
buffer_pop(uint8_t *x)
{
uint8_t x;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
x = _buf[_buf_head];
_buf_head = (_buf_head + 1) % BUFFER_SIZE;
}
return x;
}
if (_buf_head == _buf_tail) {
return 1; // error
}
uint8_t
buffer_empty(void)
{
uint8_t is_empty;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
is_empty = (_buf_head == _buf_tail);
*x = _buf[_buf_head];
_buf_head = (_buf_head + 1) % BUFFER_SIZE;
}
return is_empty;
return 0;
}
......@@ -4,7 +4,6 @@
#include <stdint.h>
void buffer_push(uint8_t x);
uint8_t buffer_pop(void);
uint8_t buffer_empty(void);
uint8_t buffer_pop(uint8_t *x);
#endif
......@@ -44,9 +44,11 @@ uart_init(void)
uint16_t
uart_read(void)
{
if (buffer_empty()) {
uint8_t data;
if (buffer_pop(&data)) {
return UART_ERROR;
}
return buffer_pop();
return data;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment