]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common/ceph_time: introduce time_guard for RAII-styled timediff calculation
authorRadoslaw Zarzynski <rzarzyns@redhat.com>
Sat, 12 Oct 2024 19:19:43 +0000 (19:19 +0000)
committerRadoslaw Zarzynski <rzarzyns@redhat.com>
Tue, 17 Dec 2024 18:10:28 +0000 (18:10 +0000)
Signed-off-by: Radoslaw Zarzynski <rzarzyns@redhat.com>
src/common/ceph_time.h

index bae038862cf2a27178dd3f3df726d6b2a33d9312..2747183453d0c6f601d733061f61339c5a9a273f 100644 (file)
@@ -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.