summaryrefslogtreecommitdiffstats
path: root/staging_vespalib
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2022-02-28 12:50:54 +0000
committerHenning Baldersheim <balder@yahoo-inc.com>2022-02-28 12:50:54 +0000
commit97d11fe1b41ea71291b0e027325224d28a963b07 (patch)
treeb31d16cca16f0019180c9e3465f8db75a71f198a /staging_vespalib
parent5a51cc51bf80f4d7c1afa1993e7df6c50a6e3d71 (diff)
Use the InvokeService to tick the clock instead of having a dedicated separate thread.
Diffstat (limited to 'staging_vespalib')
-rw-r--r--staging_vespalib/src/tests/clock/clock_benchmark.cpp6
-rw-r--r--staging_vespalib/src/tests/clock/clock_test.cpp9
-rw-r--r--staging_vespalib/src/vespa/vespalib/util/clock.cpp86
-rw-r--r--staging_vespalib/src/vespa/vespalib/util/clock.h13
4 files changed, 26 insertions, 88 deletions
diff --git a/staging_vespalib/src/tests/clock/clock_benchmark.cpp b/staging_vespalib/src/tests/clock/clock_benchmark.cpp
index 36fce181fdb..c8d3e2a5aff 100644
--- a/staging_vespalib/src/tests/clock/clock_benchmark.cpp
+++ b/staging_vespalib/src/tests/clock/clock_benchmark.cpp
@@ -1,6 +1,7 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/util/clock.h>
+#include <vespa/vespalib/util/invokeserviceimpl.h>
#include <vespa/fastos/thread.h>
#include <cassert>
#include <vector>
@@ -137,11 +138,12 @@ main(int , char *argv[])
NSValue nsValue;
NSVolatile nsVolatile;
NSAtomic nsAtomic;
- Clock clock(1.0/frequency);
+ vespalib::InvokeServiceImpl invoker(vespalib::from_s(1.0/frequency));
+ Clock clock;
TestClock nsClock(nsValue, 1.0/frequency);
TestClock nsVolatileClock(nsVolatile, 1.0/frequency);
TestClock nsAtomicClock(nsAtomic, 1.0/frequency);
- assert(pool.NewThread(clock.getRunnable(), nullptr) != nullptr);
+ clock.start(invoker);
assert(pool.NewThread(&nsClock, nullptr) != nullptr);
assert(pool.NewThread(&nsVolatileClock, nullptr) != nullptr);
assert(pool.NewThread(&nsAtomicClock, nullptr) != nullptr);
diff --git a/staging_vespalib/src/tests/clock/clock_test.cpp b/staging_vespalib/src/tests/clock/clock_test.cpp
index a7978610ed8..2c6cbc0c876 100644
--- a/staging_vespalib/src/tests/clock/clock_test.cpp
+++ b/staging_vespalib/src/tests/clock/clock_test.cpp
@@ -2,7 +2,7 @@
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/vespalib/util/clock.h>
-#include <vespa/fastos/thread.h>
+#include <vespa/vespalib/util/invokeserviceimpl.h>
#include <thread>
using vespalib::Clock;
@@ -18,10 +18,9 @@ void waitForMovement(steady_time start, Clock & clock, vespalib::duration timeou
}
TEST("Test that clock is ticking forward") {
-
- Clock clock(0.050);
- FastOS_ThreadPool pool(0x10000);
- ASSERT_TRUE(pool.NewThread(clock.getRunnable(), nullptr) != nullptr);
+ vespalib::InvokeServiceImpl invoker(50ms);
+ Clock clock;
+ clock.start(invoker);
steady_time start = clock.getTimeNS();
waitForMovement(start, clock, 10s);
steady_time stop = clock.getTimeNS();
diff --git a/staging_vespalib/src/vespa/vespalib/util/clock.cpp b/staging_vespalib/src/vespa/vespalib/util/clock.cpp
index 0c705a3ad94..a4b9ed0fd5d 100644
--- a/staging_vespalib/src/vespa/vespalib/util/clock.cpp
+++ b/staging_vespalib/src/vespa/vespalib/util/clock.cpp
@@ -1,82 +1,19 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "clock.h"
-#include <vespa/fastos/thread.h>
-#include <mutex>
-#include <condition_variable>
-#include <cassert>
-#include <chrono>
+#include <vespa/vespalib/util/invokeservice.h>
namespace vespalib {
-namespace clock::internal {
-
-class Updater : public FastOS_Runnable
-{
-private:
- Clock & _clock;
- int _timePeriodMS;
- std::mutex _lock;
- std::condition_variable _cond;
- bool _stop;
-
-
- void Run(FastOS_ThreadInterface *thisThread, void *arguments) override;
-
-public:
- Updater(Clock & clock, double timePeriod=0.100);
- ~Updater();
-
- void stop();
-};
-
-Updater::Updater(Clock & clock, double timePeriod)
- : _clock(clock),
- _timePeriodMS(static_cast<uint32_t>(timePeriod*1000)),
- _lock(),
- _cond(),
- _stop(false)
-{ }
-
-Updater::~Updater() = default;
-
-void
-Updater::Run(FastOS_ThreadInterface *thread, void *)
-{
- _clock._running = true;
- std::unique_lock<std::mutex> guard(_lock);
- while ( ! thread->GetBreakFlag() && !_stop) {
- _clock.setTime();
- _cond.wait_for(guard, std::chrono::milliseconds(_timePeriodMS));
- }
- _clock._running = false;
-}
-
-void
-Updater::stop()
-{
- std::lock_guard<std::mutex> guard(_lock);
- _stop = true;
- _cond.notify_all();
-}
-
-}
-
-Clock::Clock(double timePeriod) :
+Clock::Clock() :
_timeNS(0u),
- _updater(std::make_unique<clock::internal::Updater>(*this, timePeriod)),
- _running(false)
+ _running(false),
+ _invokeRegistration()
{
setTime();
}
-Clock::~Clock()
-{
- if (_running) {
- _updater->GetThread()->Join();
- }
- assert(!_running);
-}
+Clock::~Clock() = default;
void Clock::setTime() const
{
@@ -84,14 +21,17 @@ void Clock::setTime() const
}
void
-Clock::stop()
+Clock::start(InvokeService & invoker)
{
- _updater->stop();
+ _running.store(true, std::memory_order_relaxed);
+ _invokeRegistration = invoker.registerInvoke([this]() { setTime(); });
}
-FastOS_Runnable *
-Clock::getRunnable() {
- return _updater.get();
+void
+Clock::stop()
+{
+ _running.store(false, std::memory_order_relaxed);
+ _invokeRegistration.reset();
}
}
diff --git a/staging_vespalib/src/vespa/vespalib/util/clock.h b/staging_vespalib/src/vespa/vespalib/util/clock.h
index e0daf52dff1..d4cffc200fe 100644
--- a/staging_vespalib/src/vespa/vespalib/util/clock.h
+++ b/staging_vespalib/src/vespa/vespalib/util/clock.h
@@ -5,11 +5,10 @@
#include <atomic>
#include <memory>
-class FastOS_Runnable;
-
namespace vespalib {
-namespace clock::internal { class Updater; }
+class IDestructorCallback;
+class InvokeService;
/**
* Clock is a clock that updates the time at defined intervals.
@@ -21,14 +20,12 @@ class Clock
{
private:
mutable std::atomic<int64_t> _timeNS;
- std::unique_ptr<clock::internal::Updater> _updater;
std::atomic<bool> _running;
+ std::unique_ptr<IDestructorCallback> _invokeRegistration;
void setTime() const;
- void start();
- friend clock::internal::Updater;
public:
- Clock(double timePeriod=0.100);
+ Clock();
~Clock();
vespalib::steady_time getTimeNS() const {
@@ -41,8 +38,8 @@ public:
return vespalib::steady_time(std::chrono::nanoseconds(_timeNS.load(std::memory_order_relaxed)));
}
+ void start(InvokeService & invoker);
void stop();
- FastOS_Runnable * getRunnable();
};
}