Skip to content

Commit 9293d09

Browse files
Charlie FraschCharlie Frasch
authored andcommitted
WIP
1 parent 43cb26f commit 9293d09

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

Fifo4.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <memory>
66
#include <new>
77

8+
#include <sanitizer/tsan_interface.h>
9+
810

911
/// Threadsafe, efficient circular FIFO with cached cursors
1012
template<typename T, typename Alloc = std::allocator<T>>
@@ -54,11 +56,14 @@ class Fifo4 : private Alloc
5456
auto pushCursor = pushCursor_.load(std::memory_order_relaxed);
5557
if (full(pushCursor, popCursorCached_)) {
5658
popCursorCached_ = popCursor_.load(std::memory_order_acquire);
59+
// popCursorCached_ = popCursor_.load(std::memory_order_relaxed);
5760
if (full(pushCursor, popCursorCached_)) {
5861
return false;
5962
}
6063
}
6164

65+
//__tsan_acquire(&popCursor_);
66+
// std::atomic_thread_fence(std::memory_order_acquire);
6267
new (element(pushCursor)) T(value);
6368
pushCursor_.store(pushCursor + 1, std::memory_order_release);
6469
return true;
@@ -70,11 +75,14 @@ class Fifo4 : private Alloc
7075
auto popCursor = popCursor_.load(std::memory_order_relaxed);
7176
if (empty(pushCursorCached_, popCursor)) {
7277
pushCursorCached_ = pushCursor_.load(std::memory_order_acquire);
78+
// pushCursorCached_ = pushCursor_.load(std::memory_order_relaxed);
7379
if (empty(pushCursorCached_, popCursor)) {
7480
return false;
7581
}
7682
}
7783

84+
// __tsan_acquire(&pushCursor_);
85+
// std::atomic_thread_fence(std::memory_order_acquire);
7886
value = *element(popCursor);
7987
element(popCursor)->~T();
8088
popCursor_.store(popCursor + 1, std::memory_order_release);

Fifo5a.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ class Fifo5a : private Alloc
133133
/// Push one object onto the fifo.
134134
/// @return `true` if the operation is successful; `false` if fifo is full.
135135
auto push(T const& value) noexcept {
136-
auto pusher = push();
137-
if (pusher) {
136+
if (auto pusher = push(); pusher) {
138137
pusher = value;
139138
return true;
140139
}
@@ -211,8 +210,7 @@ class Fifo5a : private Alloc
211210
/// Pop one object from the fifo.
212211
/// @return `true` if the pop operation is successful; `false` if fifo is empty.
213212
auto pop(T& value) noexcept {
214-
auto popper = pop();
215-
if (popper) {
213+
if (auto popper = pop(); popper) {
216214
value = *popper;
217215
return true;
218216
}

bench.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,18 @@ void BM_Fifo(benchmark::State& state) {
6868
pinThread(cpu2);
6969
for (auto _ : state) {
7070
if constexpr(isRigtorp<fifo_type>::value) {
71-
while (not fifo.try_push(value)) {
72-
;
71+
while (auto again = not fifo.try_push(value)) {
72+
benchmark::DoNotOptimize(again);
7373
}
7474
} else {
75-
while (not fifo.push(value)) {
76-
;
75+
while (auto again = not fifo.push(value)) {
76+
benchmark::DoNotOptimize(again);
7777
}
7878
}
7979
++value;
8080

81-
while (not fifo.empty()) {
82-
;
81+
while (auto again = not fifo.empty()) {
82+
benchmark::DoNotOptimize(again);
8383
}
8484
}
8585
state.counters["ops/sec"] = benchmark::Counter(double(value), benchmark::Counter::kIsRate);

0 commit comments

Comments
 (0)