Skip to content

Commit d4901af

Browse files
committed
common/ceph_time: introduce time_guard for RAII-styled timediff calculation
Signed-off-by: Radoslaw Zarzynski <[email protected]>
1 parent 87d6447 commit d4901af

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/common/ceph_time.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,23 @@ class coarse_mono_clock {
342342
}
343343
};
344344

345+
// Please note time_guard is not thread safety -- multiple threads
346+
// updating same diff_accumulator can corrupt it.
347+
template <class ClockT = mono_clock>
348+
class time_guard {
349+
const typename ClockT::time_point start;
350+
timespan& diff_accumulator;
351+
352+
public:
353+
time_guard(timespan& diff_accumulator)
354+
: start(ClockT::now()),
355+
diff_accumulator(diff_accumulator) {
356+
}
357+
~time_guard() {
358+
diff_accumulator += ClockT::now() - start;
359+
}
360+
};
361+
345362
namespace time_detail {
346363
// So that our subtractions produce negative spans rather than
347364
// arithmetic underflow.

0 commit comments

Comments
 (0)