summaryrefslogtreecommitdiffstats
path: root/staging_vespalib
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2019-12-03 14:24:41 +0000
committerHenning Baldersheim <balder@yahoo-inc.com>2019-12-04 18:15:16 +0000
commit8946dbea418f760e4f0a5cbe2eb2bcbcddd25032 (patch)
treefc254ea5dea29ddf6a395b8bcdcb8c7540d22238 /staging_vespalib
parent7700f411ea6f4a3e7c0599fae239ec84c18c0038 (diff)
FastOS_THread::Sleep -> std::chrono::sleep_for
Renamed Timer -> ScheduledExecutor. Do not include thread.h when not needed in header files.
Diffstat (limited to 'staging_vespalib')
-rw-r--r--staging_vespalib/src/tests/clock/clock_test.cpp19
-rw-r--r--staging_vespalib/src/tests/shutdownguard/shutdownguard_test.cpp10
-rw-r--r--staging_vespalib/src/tests/timer/timer_test.cpp7
-rw-r--r--staging_vespalib/src/vespa/vespalib/util/CMakeLists.txt2
-rw-r--r--staging_vespalib/src/vespa/vespalib/util/scheduledexecutor.cpp (renamed from staging_vespalib/src/vespa/vespalib/util/timer.cpp)10
-rw-r--r--staging_vespalib/src/vespa/vespalib/util/scheduledexecutor.h (renamed from staging_vespalib/src/vespa/vespalib/util/timer.h)6
-rw-r--r--staging_vespalib/src/vespa/vespalib/util/shutdownguard.cpp22
-rw-r--r--staging_vespalib/src/vespa/vespalib/util/shutdownguard.h6
8 files changed, 33 insertions, 49 deletions
diff --git a/staging_vespalib/src/tests/clock/clock_test.cpp b/staging_vespalib/src/tests/clock/clock_test.cpp
index bf7e3773055..b5650244a45 100644
--- a/staging_vespalib/src/tests/clock/clock_test.cpp
+++ b/staging_vespalib/src/tests/clock/clock_test.cpp
@@ -2,36 +2,27 @@
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/vespalib/util/clock.h>
+#include <vespa/vespalib/util/time.h>
#include <vespa/fastos/thread.h>
using vespalib::Clock;
using fastos::TimeStamp;
-class Test : public vespalib::TestApp
-{
-public:
- int Main() override;
-};
-
-int
-Test::Main()
-{
- TEST_INIT("clock_test");
+TEST("Test that clock is ticking forward") {
Clock clock(0.050);
FastOS_ThreadPool pool(0x10000);
ASSERT_TRUE(pool.NewThread(clock.getRunnable(), nullptr) != nullptr);
fastos::SteadyTimeStamp start = clock.getTimeNS();
- FastOS_Thread::Sleep(5000);
+ std::this_thread::sleep_for(5s);
fastos::SteadyTimeStamp stop = clock.getTimeNS();
EXPECT_TRUE(stop > start);
- FastOS_Thread::Sleep(6000);
+ std::this_thread::sleep_for(6s);
clock.stop();
fastos::SteadyTimeStamp stop2 = clock.getTimeNS();
EXPECT_TRUE(stop2 > stop);
EXPECT_TRUE((stop2 - stop)/TimeStamp::MICRO > 1000);
- TEST_DONE();
}
-TEST_APPHOOK(Test)
+TEST_MAIN() { TEST_RUN_ALL(); } \ No newline at end of file
diff --git a/staging_vespalib/src/tests/shutdownguard/shutdownguard_test.cpp b/staging_vespalib/src/tests/shutdownguard/shutdownguard_test.cpp
index e6f7bd21750..fbaa5581173 100644
--- a/staging_vespalib/src/tests/shutdownguard/shutdownguard_test.cpp
+++ b/staging_vespalib/src/tests/shutdownguard/shutdownguard_test.cpp
@@ -13,20 +13,20 @@ Test::Main()
{
TEST_INIT("shutdownguard_test");
{
- ShutdownGuard farFuture(123456789);
- FastOS_Thread::Sleep(20);
+ ShutdownGuard farFuture(1000000s);
+ std::this_thread::sleep_for(20ms);
}
EXPECT_TRUE(true);
pid_t child = fork();
if (child == 0) {
- ShutdownGuard soon(30);
+ ShutdownGuard soon(30ms);
for (int i = 0; i < 1000; ++i) {
- FastOS_Thread::Sleep(20);
+ std::this_thread::sleep_for(20ms);
}
exit(0);
}
for (int i = 0; i < 1000; ++i) {
- FastOS_Thread::Sleep(20);
+ std::this_thread::sleep_for(20ms);
int stat = 0;
if (waitpid(child, &stat, WNOHANG) == child) {
EXPECT_TRUE(WIFEXITED(stat));
diff --git a/staging_vespalib/src/tests/timer/timer_test.cpp b/staging_vespalib/src/tests/timer/timer_test.cpp
index 309ee873b44..5472ad6e23f 100644
--- a/staging_vespalib/src/tests/timer/timer_test.cpp
+++ b/staging_vespalib/src/tests/timer/timer_test.cpp
@@ -1,8 +1,7 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/testkit/testapp.h>
-#include <vespa/vespalib/util/timer.h>
-#include <vespa/vespalib/util/executor.h>
+#include <vespa/vespalib/util/scheduledexecutor.h>
using namespace vespalib;
using vespalib::Executor;
@@ -37,7 +36,7 @@ void Test::testScheduling()
{
vespalib::CountDownLatch latch1(3);
vespalib::CountDownLatch latch2(2);
- Timer timer;
+ ScheduledExecutor timer;
timer.scheduleAtFixedRate(Task::UP(new TestTask(latch1)), 0.1, 0.2);
timer.scheduleAtFixedRate(Task::UP(new TestTask(latch2)), 0.5, 0.5);
EXPECT_TRUE(latch1.await(60000));
@@ -47,7 +46,7 @@ void Test::testScheduling()
void Test::testReset()
{
vespalib::CountDownLatch latch1(2);
- Timer timer;
+ ScheduledExecutor timer;
timer.scheduleAtFixedRate(Task::UP(new TestTask(latch1)), 2.0, 3.0);
timer.reset();
EXPECT_TRUE(!latch1.await(3000));
diff --git a/staging_vespalib/src/vespa/vespalib/util/CMakeLists.txt b/staging_vespalib/src/vespa/vespalib/util/CMakeLists.txt
index 20d47c90453..71364a813f6 100644
--- a/staging_vespalib/src/vespa/vespalib/util/CMakeLists.txt
+++ b/staging_vespalib/src/vespa/vespalib/util/CMakeLists.txt
@@ -16,7 +16,7 @@ vespa_add_library(staging_vespalib_vespalib_util OBJECT
document_runnable.cpp
rusage.cpp
shutdownguard.cpp
- timer.cpp
+ scheduledexecutor.cpp
xmlserializable.cpp
xmlstream.cpp
DEPENDS
diff --git a/staging_vespalib/src/vespa/vespalib/util/timer.cpp b/staging_vespalib/src/vespa/vespalib/util/scheduledexecutor.cpp
index a7acbe67965..61f9666114c 100644
--- a/staging_vespalib/src/vespa/vespalib/util/timer.cpp
+++ b/staging_vespalib/src/vespa/vespalib/util/scheduledexecutor.cpp
@@ -1,5 +1,5 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-#include "timer.h"
+#include "scheduledexecutor.h"
#include <vespa/fnet/scheduler.h>
#include <vespa/fnet/task.h>
#include <vespa/fnet/transport.h>
@@ -34,7 +34,7 @@ public:
}
};
-Timer::Timer()
+ScheduledExecutor::ScheduledExecutor()
: _threadPool(128 * 1024),
_transport(new FNET_Transport()),
_lock(),
@@ -43,7 +43,7 @@ Timer::Timer()
_transport->Start(&_threadPool);
}
-Timer::~Timer()
+ScheduledExecutor::~ScheduledExecutor()
{
vespalib::LockGuard guard(_lock);
_transport->ShutDown(true);
@@ -53,7 +53,7 @@ Timer::~Timer()
void
-Timer::scheduleAtFixedRate(vespalib::Executor::Task::UP task, double delay, double interval)
+ScheduledExecutor::scheduleAtFixedRate(vespalib::Executor::Task::UP task, double delay, double interval)
{
vespalib::LockGuard guard(_lock);
TimerTaskPtr tTask(new TimerTask(_transport->GetScheduler(), std::move(task), interval));
@@ -62,7 +62,7 @@ Timer::scheduleAtFixedRate(vespalib::Executor::Task::UP task, double delay, doub
}
void
-Timer::reset()
+ScheduledExecutor::reset()
{
vespalib::LockGuard guard(_lock);
_transport->ShutDown(true);
diff --git a/staging_vespalib/src/vespa/vespalib/util/timer.h b/staging_vespalib/src/vespa/vespalib/util/scheduledexecutor.h
index 0f7cde67ee4..dd12985aeda 100644
--- a/staging_vespalib/src/vespa/vespalib/util/timer.h
+++ b/staging_vespalib/src/vespa/vespalib/util/scheduledexecutor.h
@@ -17,7 +17,7 @@ class TimerTask;
* interval. The timer can be reset to clear all tasks currently being
* scheduled.
*/
-class Timer
+class ScheduledExecutor
{
private:
typedef std::unique_ptr<TimerTask> TimerTaskPtr;
@@ -31,13 +31,13 @@ public:
/**
* Create a new timer, capable of scheduling tasks at fixed intervals.
*/
- Timer();
+ ScheduledExecutor();
/**
* Destroys this timer, finishing the current task executing and then
* finishing.
*/
- ~Timer();
+ ~ScheduledExecutor();
/**
* Schedule new task to be executed at specified intervals.
diff --git a/staging_vespalib/src/vespa/vespalib/util/shutdownguard.cpp b/staging_vespalib/src/vespa/vespalib/util/shutdownguard.cpp
index 645ffea380d..cf8130f97c1 100644
--- a/staging_vespalib/src/vespa/vespalib/util/shutdownguard.cpp
+++ b/staging_vespalib/src/vespa/vespalib/util/shutdownguard.cpp
@@ -1,7 +1,7 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "shutdownguard.h"
#include <unistd.h>
-#include <sys/time.h>
+#include <thread>
#include <vespa/log/log.h>
LOG_SETUP(".vespalib.shutdownguard");
@@ -10,36 +10,30 @@ namespace vespalib {
namespace {
enum { STACK_SIZE = (1u << 16) };
-
-uint64_t getTimeInMillis() {
- struct timeval mytime;
- gettimeofday(&mytime, 0);
- uint64_t mult = 1000;
- return (mytime.tv_sec * mult) + (mytime.tv_usec / mult);
-}
}
void ShutdownGuard::Run(FastOS_ThreadInterface *, void *)
{
- while (_dieAtTime > getTimeInMillis()) {
- FastOS_Thread::Sleep(5);
+ while (_dieAtTime > steady_clock::now() && ! GetThread()->GetBreakFlag()) {
+ std::this_thread::sleep_for(5ms);
}
- if (_dieAtTime != 0) {
+ if (_dieAtTime < steady_clock::now()) {
LOG(warning, "ShutdownGuard is now forcing an exit of the process.");
_exit(EXIT_FAILURE);
}
}
-ShutdownGuard::ShutdownGuard(uint64_t millis) :
+ShutdownGuard::ShutdownGuard(duration millis) :
FastOS_Runnable(),
_pool(STACK_SIZE, 1),
- _dieAtTime(getTimeInMillis() + millis)
+ _dieAtTime(steady_clock::now() + millis)
{
_pool.NewThread(this);
}
ShutdownGuard::~ShutdownGuard()
{
- _dieAtTime = 0;
+ GetThread()->SetBreakFlag();
+ GetThread()->Join();
_pool.Close();
}
diff --git a/staging_vespalib/src/vespa/vespalib/util/shutdownguard.h b/staging_vespalib/src/vespa/vespalib/util/shutdownguard.h
index 5a9aad5d4d4..9de9df8bbad 100644
--- a/staging_vespalib/src/vespa/vespalib/util/shutdownguard.h
+++ b/staging_vespalib/src/vespa/vespalib/util/shutdownguard.h
@@ -1,8 +1,8 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once
+#include <vespa/vespalib/util/time.h>
#include <vespa/fastos/thread.h>
-#include <cstdint>
namespace vespalib {
@@ -16,7 +16,7 @@ namespace vespalib {
class ShutdownGuard : public FastOS_Runnable
{
FastOS_ThreadPool _pool;
- volatile uint64_t _dieAtTime;
+ steady_time _dieAtTime;
void Run(FastOS_ThreadInterface *, void *) override;
@@ -25,7 +25,7 @@ public:
* Construct a shutdown guard with a given lifetime.
* @arg millis the number of milliseconds before process automatically exits
**/
- ShutdownGuard(uint64_t millis);
+ ShutdownGuard(duration millis);
/**
* Destructor that dismisses the guard and collects the shutdown thread.