From 8c82de6a32bcbc8df0f6dadb7aacd7c41bcf77d8 Mon Sep 17 00:00:00 2001 From: Patrick Donnelly Date: Thu, 27 Jul 2017 12:03:15 -0700 Subject: [PATCH] common: use atomic uin64_t for counter Making this interface thread-safe... Signed-off-by: Patrick Donnelly (cherry picked from commit 59b5931a2f47f90fced779fb89e06868c739c688) --- src/include/counter.h | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/include/counter.h b/src/include/counter.h index d161c0b7dac61..61ed7409c7e4f 100644 --- a/src/include/counter.h +++ b/src/include/counter.h @@ -15,6 +15,8 @@ #ifndef CEPH_COUNTER_H #define CEPH_COUNTER_H +#include + template class Counter { public: @@ -30,23 +32,23 @@ public: ~Counter() { _count()--; } - static unsigned long count() { + static uint64_t count() { return _count(); } - static unsigned long increments() { + static uint64_t increments() { return _increments(); } - static unsigned long decrements() { + static uint64_t decrements() { return increments()-count(); } private: - static unsigned long &_count() { - static unsigned long c; + static std::atomic &_count() { + static std::atomic c; return c; } - static unsigned long &_increments() { - static unsigned long i; + static std::atomic &_increments() { + static std::atomic i; return i; } }; -- 2.39.5