Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions core/src/gauge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@ void Gauge::Decrement() { Decrement(1.0); }

void Gauge::Decrement(const double value) { Change(-1.0 * value); }

void Gauge::Set(const double value) { value_.store(value); }
void Gauge::Set(const double value) {
value_.store(value, std::memory_order_relaxed);
}

void Gauge::Change(const double value) {
#if __cpp_lib_atomic_float >= 201711L
value_.fetch_add(value);
value_.fetch_add(value, std::memory_order_relaxed);
#else
// Pre-C++ 20 fallback: busy loop (which might be more expansive than using
// fetch_add).
auto current = value_.load();
while (!value_.compare_exchange_weak(current, current + value)) {
auto current = value_.load(std::memory_order_relaxed);
while (!value_.compare_exchange_weak(current, current + value,
std::memory_order_relaxed,
std::memory_order_relaxed)) {
// intentionally empty block
}
#endif
Expand Down