Initialize atomics in metrics, update style guide on atomics
This commit is contained in:
@@ -176,6 +176,7 @@ struct Metric {
|
||||
family->p->perThreadState[std::this_thread::get_id()].instances[key];
|
||||
if (!ptr) {
|
||||
ptr = std::make_unique<Counter::State>();
|
||||
ptr->value.store(0, std::memory_order_relaxed);
|
||||
}
|
||||
Counter result;
|
||||
result.p = ptr.get();
|
||||
@@ -190,6 +191,7 @@ struct Metric {
|
||||
auto &ptr = family->p->instances[key];
|
||||
if (!ptr) {
|
||||
ptr = std::make_unique<Gauge::State>();
|
||||
ptr->value = 0.0;
|
||||
}
|
||||
Gauge result;
|
||||
result.p = ptr.get();
|
||||
@@ -210,8 +212,12 @@ struct Metric {
|
||||
ptr->thresholds = family->p->buckets; // Already sorted and deduplicated
|
||||
|
||||
// DESIGN: std::atomic is not copy-constructible
|
||||
// Initialize vector with correct size, all atomics default to 0
|
||||
// Initialize vector with correct size, all atomics explicitly initialized
|
||||
// to 0
|
||||
ptr->counts = std::vector<AtomicWord>(ptr->thresholds.size());
|
||||
for (auto &count : ptr->counts) {
|
||||
count.store(0, std::memory_order_relaxed);
|
||||
}
|
||||
ptr->sum.store(0, std::memory_order_relaxed);
|
||||
ptr->observations.store(0, std::memory_order_relaxed);
|
||||
}
|
||||
@@ -232,8 +238,8 @@ void Counter::inc(double x) {
|
||||
std::memory_order_relaxed);
|
||||
}
|
||||
void Gauge::inc(double x) {
|
||||
// IMPLEMENTATION DETAIL: Gauges currently support multiple writers via mutex,
|
||||
// though the public API documents single-writer semantics for consistency
|
||||
// IMPLEMENTATION DETAIL: Mutex protection used internally for thread safety,
|
||||
// but API contract remains single-writer per instance
|
||||
std::unique_lock<std::mutex> _{p->mutex};
|
||||
p->value += x;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user