From 3a1502c2e21eec47d34d73bb5ba641dfec51a5c2 Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Fri, 15 Dec 2023 15:44:55 +0000 Subject: Remove most of the now void clock indirection. --- vespalib/src/vespa/vespalib/util/clock.h | 1 + vespalib/src/vespa/vespalib/util/doom.cpp | 4 ++-- vespalib/src/vespa/vespalib/util/doom.h | 22 +++++++++++++--------- vespalib/src/vespa/vespalib/util/fake_doom.cpp | 3 +-- vespalib/src/vespa/vespalib/util/fake_doom.h | 1 - vespalib/src/vespa/vespalib/util/testclock.cpp | 8 ++++++-- vespalib/src/vespa/vespalib/util/testclock.h | 6 +++--- 7 files changed, 26 insertions(+), 19 deletions(-) (limited to 'vespalib/src') diff --git a/vespalib/src/vespa/vespalib/util/clock.h b/vespalib/src/vespa/vespalib/util/clock.h index 1e37e3fff8d..ab4671f7bfb 100644 --- a/vespalib/src/vespa/vespalib/util/clock.h +++ b/vespalib/src/vespa/vespalib/util/clock.h @@ -28,6 +28,7 @@ public: vespalib::steady_time getTimeNS() const noexcept { return vespalib::steady_time(_timeNS.load(std::memory_order_relaxed)); } + const std::atomic & nowRef() const { return _timeNS; } }; } diff --git a/vespalib/src/vespa/vespalib/util/doom.cpp b/vespalib/src/vespa/vespalib/util/doom.cpp index 8fc82031e79..66df17e8897 100644 --- a/vespalib/src/vespa/vespalib/util/doom.cpp +++ b/vespalib/src/vespa/vespalib/util/doom.cpp @@ -5,9 +5,9 @@ namespace vespalib { -Doom::Doom(const Clock &clock, steady_time softDoom, +Doom::Doom(const std::atomic & now_ref, steady_time softDoom, steady_time hardDoom, bool explicitSoftDoom) noexcept - : _clock(clock), + : _now(now_ref), _softDoom(softDoom), _hardDoom(hardDoom), _isExplicitSoftDoom(explicitSoftDoom) diff --git a/vespalib/src/vespa/vespalib/util/doom.h b/vespalib/src/vespa/vespalib/util/doom.h index 1d7070cedd0..c9b66725b0f 100644 --- a/vespalib/src/vespa/vespalib/util/doom.h +++ b/vespalib/src/vespa/vespalib/util/doom.h @@ -2,26 +2,30 @@ #pragma once -#include "clock.h" +#include "time.h" +#include namespace vespalib { class Doom { public: - Doom(const Clock &clock, steady_time doom) noexcept - : Doom(clock, doom, doom, false) + Doom(const std::atomic & now_ref, steady_time doom) noexcept + : Doom(now_ref, doom, doom, false) {} - Doom(const Clock &clock, steady_time softDoom, + Doom(const std::atomic & now_ref, steady_time softDoom, steady_time hardDoom, bool explicitSoftDoom) noexcept; - bool soft_doom() const noexcept { return (_clock.getTimeNS() > _softDoom); } - bool hard_doom() const noexcept { return (_clock.getTimeNS() > _hardDoom); } - duration soft_left() const noexcept { return _softDoom - _clock.getTimeNS(); } - duration hard_left() const noexcept { return _hardDoom - _clock.getTimeNS(); } + bool soft_doom() const noexcept { return (getTimeNS() > _softDoom); } + bool hard_doom() const noexcept { return (getTimeNS() > _hardDoom); } + duration soft_left() const noexcept { return _softDoom - getTimeNS(); } + duration hard_left() const noexcept { return _hardDoom - getTimeNS(); } bool isExplicitSoftDoom() const noexcept { return _isExplicitSoftDoom; } static const Doom & never() noexcept; private: - const Clock &_clock; + vespalib::steady_time getTimeNS() const noexcept { + return vespalib::steady_time(_now.load(std::memory_order_relaxed)); + } + const std::atomic & _now; steady_time _softDoom; steady_time _hardDoom; bool _isExplicitSoftDoom; diff --git a/vespalib/src/vespa/vespalib/util/fake_doom.cpp b/vespalib/src/vespa/vespalib/util/fake_doom.cpp index d9cbce0b152..eeedc555d21 100644 --- a/vespalib/src/vespa/vespalib/util/fake_doom.cpp +++ b/vespalib/src/vespa/vespalib/util/fake_doom.cpp @@ -6,8 +6,7 @@ namespace vespalib { FakeDoom::FakeDoom(steady_time::duration time_to_doom) noexcept : _time(steady_clock::now()), - _clock(_time), - _doom(_clock, _clock.getTimeNS() + time_to_doom) + _doom(_time, _time.load(std::memory_order_relaxed) + time_to_doom) { } diff --git a/vespalib/src/vespa/vespalib/util/fake_doom.h b/vespalib/src/vespa/vespalib/util/fake_doom.h index 1e6cfcb0c4d..a5c25ac4d9d 100644 --- a/vespalib/src/vespa/vespalib/util/fake_doom.h +++ b/vespalib/src/vespa/vespalib/util/fake_doom.h @@ -12,7 +12,6 @@ namespace vespalib { */ class FakeDoom { std::atomic _time; - Clock _clock; Doom _doom; public: FakeDoom() noexcept : FakeDoom(1s) { } diff --git a/vespalib/src/vespa/vespalib/util/testclock.cpp b/vespalib/src/vespa/vespalib/util/testclock.cpp index 9f63ce6c217..fe2da504150 100644 --- a/vespalib/src/vespa/vespalib/util/testclock.cpp +++ b/vespalib/src/vespa/vespalib/util/testclock.cpp @@ -6,11 +6,15 @@ namespace vespalib { TestClock::TestClock() - : _ticker(std::make_unique(10ms)), - _clock(_ticker->nowRef()) + : _ticker(std::make_unique(10ms)) { } TestClock::~TestClock() = default; +const std::atomic & +TestClock::nowRef() const { + return _ticker->nowRef(); +} + } diff --git a/vespalib/src/vespa/vespalib/util/testclock.h b/vespalib/src/vespa/vespalib/util/testclock.h index 748c239b309..117e19bedb8 100644 --- a/vespalib/src/vespa/vespalib/util/testclock.h +++ b/vespalib/src/vespa/vespalib/util/testclock.h @@ -1,7 +1,8 @@ // Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #pragma once -#include "clock.h" +#include "time.h" +#include namespace vespalib { @@ -15,7 +16,6 @@ class TestClock { private: std::unique_ptr _ticker; - Clock _clock; public: TestClock(); TestClock(const TestClock &) = delete; @@ -23,7 +23,7 @@ public: TestClock(TestClock &&) = delete; TestClock & operator =(TestClock &&) = delete; ~TestClock(); - const Clock & clock() { return _clock; } + const std::atomic & nowRef() const; }; } -- cgit v1.2.3