summaryrefslogtreecommitdiffstats
path: root/staging_vespalib
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2019-12-03 12:58:34 +0000
committerHenning Baldersheim <balder@yahoo-inc.com>2019-12-04 15:59:11 +0000
commit3abd363e0bf07f0467fb9f66815833330f68b223 (patch)
treeedcdd7ca18b348606bc324b6046b6ef15be820ad /staging_vespalib
parent4136ec96afc319fa272970cd65acd633283a63d7 (diff)
Hide implementation to reduce FastOS_ visibility.
Diffstat (limited to 'staging_vespalib')
-rw-r--r--staging_vespalib/src/tests/clock/clock_benchmark.cpp5
-rw-r--r--staging_vespalib/src/tests/clock/clock_test.cpp3
-rw-r--r--staging_vespalib/src/vespa/vespalib/util/clock.cpp84
-rw-r--r--staging_vespalib/src/vespa/vespalib/util/clock.h33
4 files changed, 87 insertions, 38 deletions
diff --git a/staging_vespalib/src/tests/clock/clock_benchmark.cpp b/staging_vespalib/src/tests/clock/clock_benchmark.cpp
index c5229adea31..a9618d50682 100644
--- a/staging_vespalib/src/tests/clock/clock_benchmark.cpp
+++ b/staging_vespalib/src/tests/clock/clock_benchmark.cpp
@@ -1,10 +1,13 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/util/clock.h>
+#include <vespa/fastos/thread.h>
#include <cassert>
#include <vector>
#include <atomic>
#include <cstring>
+#include <condition_variable>
+#include <mutex>
using vespalib::Clock;
using fastos::TimeStamp;
@@ -134,7 +137,7 @@ main(int , char *argv[])
TestClock nsClock(nsValue, 1.0/frequency);
TestClock nsVolatileClock(nsVolatile, 1.0/frequency);
TestClock nsAtomicClock(nsAtomic, 1.0/frequency);
- assert(pool.NewThread(&clock, nullptr) != nullptr);
+ assert(pool.NewThread(clock.getRunnable(), nullptr) != nullptr);
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 c8c93272e85..bf7e3773055 100644
--- a/staging_vespalib/src/tests/clock/clock_test.cpp
+++ b/staging_vespalib/src/tests/clock/clock_test.cpp
@@ -2,6 +2,7 @@
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/vespalib/util/clock.h>
+#include <vespa/fastos/thread.h>
using vespalib::Clock;
using fastos::TimeStamp;
@@ -20,7 +21,7 @@ Test::Main()
Clock clock(0.050);
FastOS_ThreadPool pool(0x10000);
- ASSERT_TRUE(pool.NewThread(&clock, nullptr) != nullptr);
+ ASSERT_TRUE(pool.NewThread(clock.getRunnable(), nullptr) != nullptr);
fastos::SteadyTimeStamp start = clock.getTimeNS();
FastOS_Thread::Sleep(5000);
fastos::SteadyTimeStamp stop = clock.getTimeNS();
diff --git a/staging_vespalib/src/vespa/vespalib/util/clock.cpp b/staging_vespalib/src/vespa/vespalib/util/clock.cpp
index e935a80bd6b..0c7a9fe59ed 100644
--- a/staging_vespalib/src/vespa/vespalib/util/clock.cpp
+++ b/staging_vespalib/src/vespa/vespalib/util/clock.cpp
@@ -1,17 +1,71 @@
// Copyright 2017 Yahoo Holdings. 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>
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 *arguments)
+{
+ (void) arguments;
+ _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) :
_timeNS(0u),
- _timePeriodMS(static_cast<uint32_t>(timePeriod*1000)),
- _lock(),
- _cond(),
- _stop(false),
+ _updater(std::make_unique<clock::internal::Updater>(*this, timePeriod)),
_running(false)
{
setTime();
@@ -19,6 +73,7 @@ Clock::Clock(double timePeriod) :
Clock::~Clock()
{
+ _updater.reset();
assert(!_running);
}
@@ -27,24 +82,15 @@ void Clock::setTime() const
_timeNS.store(fastos::ClockSteady::now() - fastos::SteadyTimeStamp::ZERO, std::memory_order_relaxed);
}
-void Clock::Run(FastOS_ThreadInterface *thread, void *arguments)
-{
- (void) arguments;
- _running = true;
- std::unique_lock<std::mutex> guard(_lock);
- while ( ! thread->GetBreakFlag() && !_stop) {
- setTime();
- _cond.wait_for(guard, std::chrono::milliseconds(_timePeriodMS));
- }
- _running = false;
-}
-
void
Clock::stop()
{
- std::lock_guard<std::mutex> guard(_lock);
- _stop = true;
- _cond.notify_all();
+ _updater->stop();
+}
+
+FastOS_Runnable *
+Clock::getRunnable() {
+ return _updater.get();
}
}
diff --git a/staging_vespalib/src/vespa/vespalib/util/clock.h b/staging_vespalib/src/vespa/vespalib/util/clock.h
index fc8e9eaff23..61352cb6cdc 100644
--- a/staging_vespalib/src/vespa/vespalib/util/clock.h
+++ b/staging_vespalib/src/vespa/vespalib/util/clock.h
@@ -1,36 +1,32 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once
-#include <vespa/fastos/thread.h>
#include <vespa/fastos/timestamp.h>
-#include <mutex>
-#include <condition_variable>
+#include <atomic>
+#include <memory>
+
+class FastOS_Runnable;
namespace vespalib {
+namespace clock::internal { class Updater; }
+
/**
* Clock is a clock that updates the time at defined intervals.
* It is intended used where you want to check the time with low cost, but where
* resolution is not that important.
*/
-class Clock : public FastOS_Runnable
+class Clock
{
private:
- Clock(const Clock &);
- Clock & operator = (const Clock &);
-
- mutable std::atomic<int64_t> _timeNS;
- int _timePeriodMS;
- std::mutex _lock;
- std::condition_variable _cond;
- bool _stop;
- bool _running;
+ mutable std::atomic<int64_t> _timeNS;
+ std::unique_ptr<clock::internal::Updater> _updater;
+ bool _running;
void setTime() const;
-
- void Run(FastOS_ThreadInterface *thisThread, void *arguments) override;
-
+ void start();
+ friend clock::internal::Updater;
public:
Clock(double timePeriod=0.100);
~Clock();
@@ -41,9 +37,12 @@ public:
}
return getTimeNSAssumeRunning();
}
- fastos::SteadyTimeStamp getTimeNSAssumeRunning() const { return fastos::SteadyTimeStamp(_timeNS.load(std::memory_order_relaxed)); }
+ fastos::SteadyTimeStamp getTimeNSAssumeRunning() const {
+ return fastos::SteadyTimeStamp(_timeNS.load(std::memory_order_relaxed));
+ }
void stop();
+ FastOS_Runnable * getRunnable();
};
}