summaryrefslogtreecommitdiffstats
path: root/src/common/ceph_time.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/ceph_time.h')
-rw-r--r--src/common/ceph_time.h19
1 files changed, 18 insertions, 1 deletions
diff --git a/src/common/ceph_time.h b/src/common/ceph_time.h
index bae038862cf..0b05be5372e 100644
--- a/src/common/ceph_time.h
+++ b/src/common/ceph_time.h
@@ -16,7 +16,7 @@
#define COMMON_CEPH_TIME_H
#include <chrono>
-#include <iostream>
+#include <iosfwd>
#include <string>
#include <optional>
#include <fmt/chrono.h>
@@ -342,6 +342,23 @@ public:
}
};
+// Please note time_guard is not thread safety -- multiple threads
+// updating same diff_accumulator can corrupt it.
+template <class ClockT = mono_clock>
+class time_guard {
+ const typename ClockT::time_point start;
+ timespan& diff_accumulator;
+
+public:
+ time_guard(timespan& diff_accumulator)
+ : start(ClockT::now()),
+ diff_accumulator(diff_accumulator) {
+ }
+ ~time_guard() {
+ diff_accumulator += ClockT::now() - start;
+ }
+};
+
namespace time_detail {
// So that our subtractions produce negative spans rather than
// arithmetic underflow.