diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2021-11-29 18:56:50 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2021-11-29 18:56:50 +0000 |
commit | 0d00752580ffb3fdc06279acf60f94f634a84b6f (patch) | |
tree | a679c3ebc4c9edb114c62a6b027a60e7dd03eaf2 /vespalib | |
parent | 9abebd5437e505b945ee3e6e569a315f55b8fb8e (diff) |
generalize from wakeup to invoke.
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/src/tests/wakeupservice/wakeupservice_test.cpp | 24 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/CMakeLists.txt | 2 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/invokeservice.h (renamed from vespalib/src/vespa/vespalib/util/iwakeupservice.h) | 7 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/invokeserviceimpl.cpp (renamed from vespalib/src/vespa/vespalib/util/wakeupservice.cpp) | 22 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/invokeserviceimpl.h | 36 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/wakeupservice.h | 42 |
6 files changed, 63 insertions, 70 deletions
diff --git a/vespalib/src/tests/wakeupservice/wakeupservice_test.cpp b/vespalib/src/tests/wakeupservice/wakeupservice_test.cpp index ba642958fd6..88a7969e153 100644 --- a/vespalib/src/tests/wakeupservice/wakeupservice_test.cpp +++ b/vespalib/src/tests/wakeupservice/wakeupservice_test.cpp @@ -1,20 +1,20 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include <vespa/vespalib/testkit/test_kit.h> -#include <vespa/vespalib/util/wakeupservice.h> +#include <vespa/vespalib/util/invokeserviceimpl.h> using namespace vespalib; -struct WakeupCounter : public IWakeup { - WakeupCounter() : _count(0) {} - void wakeup() override { _count++; } +struct InvokeCounter { + InvokeCounter() : _count(0) {} + void inc() noexcept { _count++; } std::atomic<uint64_t> _count; }; TEST("require that wakeup is called") { - WakeupCounter a; - WakeupService service(1ms); + InvokeCounter a; + InvokeServiceImpl service(1ms); EXPECT_EQUAL(0u, a._count); - auto ra = service.registerForInvoke([&a](){ a.wakeup(); }); + auto ra = service.registerInvoke([&a]() noexcept { a.inc(); }); EXPECT_TRUE(ra); while (a._count == 0) { std::this_thread::sleep_for(1ms); @@ -26,18 +26,18 @@ TEST("require that wakeup is called") { } TEST("require that same wakeup can be registered multiple times.") { - WakeupCounter a; - WakeupService service(1ms); + InvokeCounter a; + InvokeServiceImpl service(1ms); EXPECT_EQUAL(0u, a._count); - auto ra1 = service.registerForInvoke([&a](){ a.wakeup(); }); + auto ra1 = service.registerInvoke([&a]() noexcept { a.inc(); }); EXPECT_TRUE(ra1); - auto ra2 = service.registerForInvoke([&a](){ a.wakeup(); }); + auto ra2 = service.registerInvoke([&a]() noexcept { a.inc(); }); while (a._count == 0) { std::this_thread::sleep_for(1ms); } ra1.reset(); uint64_t countAtStop = a._count; - ra2 = service.registerForInvoke([&a](){ a.wakeup(); }); + ra2 = service.registerInvoke([&a]() noexcept { a.inc(); }); EXPECT_TRUE(ra2); std::this_thread::sleep_for(1s); EXPECT_LESS(countAtStop, a._count); diff --git a/vespalib/src/vespa/vespalib/util/CMakeLists.txt b/vespalib/src/vespa/vespalib/util/CMakeLists.txt index 92371418c8a..562eb876f27 100644 --- a/vespalib/src/vespa/vespalib/util/CMakeLists.txt +++ b/vespalib/src/vespa/vespalib/util/CMakeLists.txt @@ -69,7 +69,7 @@ vespa_add_library(vespalib_vespalib_util OBJECT time.cpp unwind_message.cpp valgrind.cpp - wakeupservice.cpp + invokeserviceimpl.cpp zstdcompressor.cpp DEPENDS ) diff --git a/vespalib/src/vespa/vespalib/util/iwakeupservice.h b/vespalib/src/vespa/vespalib/util/invokeservice.h index 81a29fb4be5..3e3973234d1 100644 --- a/vespalib/src/vespa/vespalib/util/iwakeupservice.h +++ b/vespalib/src/vespa/vespalib/util/invokeservice.h @@ -2,7 +2,6 @@ #pragma once -#include "executor.h" #include "idestructorcallback.h" #include <functional> @@ -12,10 +11,10 @@ namespace vespalib { * Interface to register for receiving regular invoke calls. * The registration will last as long as the returned object is kept alive. **/ -class IWakeupService { +class InvokeService { public: - virtual ~IWakeupService() = default; - virtual std::unique_ptr<IDestructorCallback> registerForInvoke(std::function<void()> func) = 0; + virtual ~InvokeService() = default; + virtual std::unique_ptr<IDestructorCallback> registerInvoke(std::function<void()> func) = 0; }; } diff --git a/vespalib/src/vespa/vespalib/util/wakeupservice.cpp b/vespalib/src/vespa/vespalib/util/invokeserviceimpl.cpp index dd8c7cd3ed1..0d83f50b8d7 100644 --- a/vespalib/src/vespa/vespalib/util/wakeupservice.cpp +++ b/vespalib/src/vespa/vespalib/util/invokeserviceimpl.cpp @@ -1,11 +1,11 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -#include "wakeupservice.h" +#include "invokeserviceimpl.h" #include <cassert> namespace vespalib { -WakeupService::WakeupService(duration napTime) +InvokeServiceImpl::InvokeServiceImpl(duration napTime) : _naptime(napTime), _lock(), _closed(false), @@ -14,7 +14,7 @@ WakeupService::WakeupService(duration napTime) { } -WakeupService::~WakeupService() +InvokeServiceImpl::~InvokeServiceImpl() { { std::lock_guard guard(_lock); @@ -26,9 +26,9 @@ WakeupService::~WakeupService() } } -class WakeupService::Registration : public IDestructorCallback { +class InvokeServiceImpl::Registration : public IDestructorCallback { public: - Registration(WakeupService * service, VoidFunc func) noexcept + Registration(InvokeServiceImpl * service, VoidFunc func) noexcept : _service(service), _func(func) { } @@ -38,22 +38,22 @@ public: _service->unregister(_func); } private: - WakeupService * _service; + InvokeServiceImpl * _service; VoidFunc _func; }; std::unique_ptr<IDestructorCallback> -WakeupService::registerForInvoke(VoidFunc func) { +InvokeServiceImpl::registerInvoke(VoidFunc func) { std::lock_guard guard(_lock); _toWakeup.push_back(func); if ( ! _thread) { - _thread = std::make_unique<std::thread>(WakeupService::run, this); + _thread = std::make_unique<std::thread>(InvokeServiceImpl::run, this); } return std::make_unique<Registration>(this, func); } void -WakeupService::unregister(VoidFunc func) { +InvokeServiceImpl::unregister(VoidFunc func) { std::lock_guard guard(_lock); auto found = std::find_if(_toWakeup.begin(), _toWakeup.end(), [&func](const VoidFunc & a) { return func.target<VoidFunc>() == a.target<VoidFunc>(); @@ -63,7 +63,7 @@ WakeupService::unregister(VoidFunc func) { } void -WakeupService::runLoop() { +InvokeServiceImpl::runLoop() { bool done = false; while ( ! done ) { { @@ -81,7 +81,7 @@ WakeupService::runLoop() { } void -WakeupService::run(WakeupService * service) { +InvokeServiceImpl::run(InvokeServiceImpl * service) { service->runLoop(); } diff --git a/vespalib/src/vespa/vespalib/util/invokeserviceimpl.h b/vespalib/src/vespa/vespalib/util/invokeserviceimpl.h new file mode 100644 index 00000000000..7dfadfd9973 --- /dev/null +++ b/vespalib/src/vespa/vespalib/util/invokeserviceimpl.h @@ -0,0 +1,36 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +#include "invokeservice.h" +#include "time.h" +#include <mutex> +#include <vector> +#include <thread> + +namespace vespalib { + +/** + * An invoke service what will invoke the given function with at specified frequency. + */ +class InvokeServiceImpl : public InvokeService { + using VoidFunc = std::function<void()>; +public: + InvokeServiceImpl(duration napTime); + InvokeServiceImpl(const InvokeServiceImpl &) = delete; + InvokeServiceImpl & operator=(const InvokeServiceImpl &) = delete; + ~InvokeServiceImpl() override; + std::unique_ptr<IDestructorCallback> registerInvoke(VoidFunc func) override; +private: + class Registration; + void unregister(VoidFunc func); + void runLoop(); + static void run(InvokeServiceImpl *); + duration _naptime; + std::mutex _lock; + bool _closed; + std::vector<VoidFunc> _toWakeup; + std::unique_ptr<std::thread> _thread; +}; + +} diff --git a/vespalib/src/vespa/vespalib/util/wakeupservice.h b/vespalib/src/vespa/vespalib/util/wakeupservice.h deleted file mode 100644 index c2f46a766a8..00000000000 --- a/vespalib/src/vespa/vespalib/util/wakeupservice.h +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. - -#pragma once - -#include "iwakeupservice.h" -#include "time.h" -#include <mutex> -#include <vector> -#include <thread> - -namespace vespalib { - -/** - * A wakeup service what will do a wakeup call with the frequency specified. - * Purpose is to assist thread executors which has lazy threads. - * 1 thread doing wakeup is better than many threads waking up regularly by them selves. - * Then it can be done with higher frequency with less impact. - */ -class WakeupService : public IWakeupService { - using VoidFunc = std::function<void()>; -public: - WakeupService(duration napTime); - WakeupService(const WakeupService &) = delete; - WakeupService & operator=(const WakeupService &) = delete; - ~WakeupService() override; - /** - * Register the one to be woken up - */ - std::unique_ptr<IDestructorCallback> registerForInvoke(VoidFunc func) override; -private: - class Registration; - void unregister(VoidFunc func); - void runLoop(); - static void run(WakeupService *); - duration _naptime; - std::mutex _lock; - bool _closed; - std::vector<VoidFunc> _toWakeup; - std::unique_ptr<std::thread> _thread; -}; - -} |