Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a less constraining memory order for all "update" operations on Counter/Gauge. #623

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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