From 69da7c5b15ad8b87c80ee2a220fa975a0c2571a5 Mon Sep 17 00:00:00 2001 From: Markus Hitter Date: Sun, 21 Jul 2013 22:43:24 +0200 Subject: [PATCH] Introduce ATOMIC_START and ATOMIC_END. As a sample application, use it in queue_empty(). There's also ATOMIC_BLOCK() coming with avr-libc, but this requires a C99 compiler while Arduino IDE insists on running avr-gcc in C89 mode. --- dda_queue.c | 11 ++++------- memory_barrier.h | 9 +++++++++ 2 files changed, 13 insertions(+), 7 deletions(-) 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