From 8e6bc53956f12a9d2f14ea288b7835f26a5d725a Mon Sep 17 00:00:00 2001 From: Pawel Dziepak Date: Thu, 20 Feb 2014 02:31:30 +0100 Subject: [PATCH] spmc_queue: Fix order of block::{next, consumer, producer} reads Signed-off-by: Pawel Dziepak --- src/spmc_queue.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/spmc_queue.c b/src/spmc_queue.c index 6561ee0..b75c856 100644 --- a/src/spmc_queue.c +++ b/src/spmc_queue.c @@ -15,7 +15,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#undef NDEBUG + #include #include #include @@ -190,17 +190,19 @@ void* spmc_queue_dequeue(struct spmc_queue* queue) do { block = queue->consumer_consumer; + consumer = block->consumer_consumer; + read_barrier(); next = block->next; read_barrier(); producer = block->producer; - consumer = block->consumer_consumer; - if (producer == block->consumer_consumer && !next) { - unlock_blocks(queue); - return NULL; - } - + assert(producer >= consumer); if (producer == consumer) { + if (!next) { + unlock_blocks(queue); + return NULL; + } + if (compare_and_swap(&queue->consumer_consumer, block, next)) free_block(queue, block); } else -- 2.11.4.GIT