diff --git a/dda_queue.c b/dda_queue.c index 098d985..4fda7e8 100644 --- a/dda_queue.c +++ b/dda_queue.c @@ -47,14 +47,11 @@ uint8_t queue_full() { /// check if the queue is completely empty uint8_t queue_empty() { - uint8_t save_reg = SREG; - cli(); - CLI_SEI_BUG_MEMORY_BARRIER(); - - uint8_t result = ((mb_tail == mb_head) && (movebuffer[mb_tail].live == 0))?255:0; + uint8_t result; - MEMORY_BARRIER(); - SREG = save_reg; + ATOMIC_START + result = ((mb_tail == mb_head) && (movebuffer[mb_tail].live == 0))?255:0; + ATOMIC_END return result; } diff --git a/memory_barrier.h b/memory_barrier.h index 8c55575..8aab3f6 100644 --- a/memory_barrier.h +++ b/memory_barrier.h @@ -28,4 +28,13 @@ #define CLI_SEI_BUG_MEMORY_BARRIER() #endif +#define ATOMIC_START { \ + uint8_t save_reg = SREG; \ + cli(); \ + CLI_SEI_BUG_MEMORY_BARRIER(); + +#define ATOMIC_END MEMORY_BARRIER(); \ + SREG = save_reg; \ + } + #endif