summaryrefslogtreecommitdiff
path: root/src/mongo/util/tick_source.h
diff options
context:
space:
mode:
authorDaniel Gómez Ferro <daniel.gomezferro@mongodb.com>2022-02-15 14:44:11 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-02-15 16:26:43 +0000
commita5705b89f02a4cc1c3b1ce0eebc11e53fe668c5b (patch)
treeade8e8cf85852b669ddca7d7f87dfec85c0eb4b1 /src/mongo/util/tick_source.h
parenteeff4a62ad0702abfe3d599e16696baefc6c8cec (diff)
downloadmongo-a5705b89f02a4cc1c3b1ce0eebc11e53fe668c5b.tar.gz
SERVER-63651 Support non-monotonic clocks in PrepareConflictTracker
Diffstat (limited to 'src/mongo/util/tick_source.h')
-rw-r--r--src/mongo/util/tick_source.h14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/mongo/util/tick_source.h b/src/mongo/util/tick_source.h
index f77ea9d1d6f..5b0abc88aca 100644
--- a/src/mongo/util/tick_source.h
+++ b/src/mongo/util/tick_source.h
@@ -29,6 +29,7 @@
#pragma once
+#include <algorithm>
#include <cstdint>
namespace mongo {
@@ -64,5 +65,18 @@ public:
static_cast<double>(getTicksPerSecond()) * D::period::num / D::period::den;
return D(static_cast<int64_t>(ticks / ticksPerD));
}
+
+ /**
+ * Measures the length of the span from the start tick to the end tick and returns the result
+ * using duration type D.
+ * If the start tick is after (greater than) the end tick, returns a duration equivalent to 0
+ * ticks.
+ *
+ * e.g. tickSource->spanTo<Milliseconds>(start, end);
+ */
+ template <typename D>
+ D spanTo(Tick start, Tick end) {
+ return ticksTo<D>(std::max((end - start), Tick{0}));
+ }
};
} // namespace mongo