diff options
author | Sven Gothel <[email protected]> | 2021-11-16 06:20:24 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2021-11-16 06:20:24 +0100 |
commit | 075af3920c7e187655041fccd41dbd9f2c739adb (patch) | |
tree | bfd63e6c0bf500603c6e3f5556e340c13e66f3b1 /include | |
parent | 522a73962a1fba9f245fff03a1dc2f5cdf4ef23b (diff) |
ringbuffer: Remove locking mutex before notify_all, leading to pessimistic re-block of notified wait() thread.
Holding the same lock @ notify_all as the waiting thread, would lead to waking up the waiting thread,
which only would be blocked again as it attempts to re-acquire the lock.
Hence removing it.
A lock is also not required for the readPos or writePos field,
as it is atomic of memory_order_seq_cst.
Diffstat (limited to 'include')
-rw-r--r-- | include/jau/ringbuffer.hpp | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/include/jau/ringbuffer.hpp b/include/jau/ringbuffer.hpp index 3302b45..893dc37 100644 --- a/include/jau/ringbuffer.hpp +++ b/include/jau/ringbuffer.hpp @@ -481,7 +481,8 @@ class ringbuffer { dtor_one( localReadPos ); } { - std::unique_lock<std::mutex> lockRead(syncRead); // SC-DRF w/ putImpl via same lock + // Pessimization (would re-block notified wait() thread): std::unique_lock<std::mutex> lockRead(syncRead); // SC-DRF w/ putImpl via same lock + // Also not required as readPos is atomic of memory_order_seq_cst readPos = localReadPos; // SC-DRF release atomic readPos cvRead.notify_all(); // notify waiting putter } @@ -576,7 +577,8 @@ class ringbuffer { localReadPos = ( localReadPos + togo_count - 1 ) % capacityPlusOne; // last read-pos } { - std::unique_lock<std::mutex> locRead(syncRead); // SC-DRF w/ putImpl via same lock + // Pessimization (would re-block notified wait() thread): std::unique_lock<std::mutex> lockRead(syncRead); // SC-DRF w/ putImpl via same lock + // Also not required as readPos is atomic of memory_order_seq_cst readPos = localReadPos; // SC-DRF release atomic readPos cvRead.notify_all(); // notify waiting putter } @@ -658,7 +660,8 @@ class ringbuffer { localReadPos = ( localReadPos + togo_count - 1 ) % capacityPlusOne; // last read-pos } { - std::unique_lock<std::mutex> lockRead(syncRead); // SC-DRF w/ putImpl via same lock + // Pessimization (would re-block notified wait() thread): std::unique_lock<std::mutex> lockRead(syncRead); // SC-DRF w/ putImpl via same lock + // Also not required as readPos is atomic of memory_order_seq_cst readPos = localReadPos; // SC-DRF release atomic readPos cvRead.notify_all(); // notify waiting putter } @@ -696,7 +699,8 @@ class ringbuffer { new (const_cast<pointer_mutable>(array + localWritePos)) value_type( std::move(e) ); // placement new } { - std::unique_lock<std::mutex> lockWrite(syncWrite); // SC-DRF w/ getImpl via same lock + // Pessimization (would re-block notified wait() thread): std::unique_lock<std::mutex> lockWrite(syncWrite); // SC-DRF w/ getImpl via same lock + // Also not required as writePos is atomic of memory_order_seq_cst writePos = localWritePos; // SC-DRF release atomic writePos cvWrite.notify_all(); // notify waiting getter } @@ -738,7 +742,8 @@ class ringbuffer { new (const_cast<pointer_mutable>(array + localWritePos)) value_type( e ); // placement new } { - std::unique_lock<std::mutex> lockWrite(syncWrite); // SC-DRF w/ getImpl via same lock + // Pessimization (would re-block notified wait() thread): std::unique_lock<std::mutex> lockWrite(syncWrite); // SC-DRF w/ getImpl via same lock + // Also not required as writePos is atomic of memory_order_seq_cst writePos = localWritePos; // SC-DRF release atomic writePos cvWrite.notify_all(); // notify waiting getter } @@ -824,7 +829,8 @@ class ringbuffer { localWritePos = ( localWritePos + togo_count - 1 ) % capacityPlusOne; // last write-pos } { - std::unique_lock<std::mutex> lockRead(syncWrite); // SC-DRF w/ getImpl via same lock + // Pessimization (would re-block notified wait() thread): std::unique_lock<std::mutex> lockWrite(syncWrite); // SC-DRF w/ getImpl via same lock + // Also not required as writePos is atomic of memory_order_seq_cst writePos = localWritePos; // SC-DRF release atomic writePos cvWrite.notify_all(); // notify waiting getter } |