From fcdfd68d735144bd6735e6339f32589b2c5efa90 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Thu, 21 Jun 2018 13:40:01 -0400 Subject: [PATCH] Stricter memory ordering in RingBuffer --- src/RingBuffer.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/RingBuffer.cpp b/src/RingBuffer.cpp index a3e0983d4..d5a459b0c 100644 --- a/src/RingBuffer.cpp +++ b/src/RingBuffer.cpp @@ -54,9 +54,8 @@ size_t RingBuffer::Free( size_t start, size_t end ) // // For the writer only: // Only writer writes the end, so it can read it again relaxed -// And it reads the start written by reader, but reader sends no other -// information needing to synchronize with the start, so relaxed memory order -// is good there too +// And it reads the start written by reader, with acquire order, +// so that any reading done in Get() happens-before any reuse of the space. // size_t RingBuffer::AvailForPut() @@ -72,7 +71,7 @@ size_t RingBuffer::AvailForPut() size_t RingBuffer::Put(samplePtr buffer, sampleFormat format, size_t samplesToCopy) { - auto start = mStart.load( std::memory_order_relaxed ); + auto start = mStart.load( std::memory_order_acquire ); auto end = mEnd.load( std::memory_order_relaxed ); samplesToCopy = std::min( samplesToCopy, Free( start, end ) ); auto src = buffer; @@ -167,7 +166,7 @@ size_t RingBuffer::Get(samplePtr buffer, sampleFormat format, } // Communicate to writer that we have consumed some data, and that's all - mStart.store( start, std::memory_order_relaxed ); + mStart.store( start, std::memory_order_release ); return copied; }