From 432407450c2c17789f40e8e1b476de98c20d3494 Mon Sep 17 00:00:00 2001 From: Radoslaw Zarzynski Date: Sat, 12 Oct 2024 19:19:43 +0000 Subject: [PATCH] common/ceph_time: introduce time_guard for RAII-styled timediff calculation Signed-off-by: Radoslaw Zarzynski (cherry picked from commit d4901af641cf04c1c29ef698c8ecd5c8ee0d809f) --- src/common/ceph_time.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/common/ceph_time.h b/src/common/ceph_time.h index bae038862cf..2747183453d 100644 --- a/src/common/ceph_time.h +++ b/src/common/ceph_time.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 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. -- 2.39.5