aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2021-11-29 17:23:35 +0000
committerHenning Baldersheim <balder@yahoo-inc.com>2021-11-29 17:23:35 +0000
commit9abebd5437e505b945ee3e6e569a315f55b8fb8e (patch)
tree983f9c70c81d6c83bd69d291ef35faaccb5920d5 /vespalib
parent5ba67ce7e1ee4eca6253cf6918e0c80452f86cda (diff)
Use std::function and std::unique_ptr
Diffstat (limited to 'vespalib')
-rw-r--r--vespalib/CMakeLists.txt2
-rw-r--r--vespalib/src/tests/wakeupservice/CMakeLists.txt1
-rw-r--r--vespalib/src/tests/wakeupservice/wakeupservice_test.cpp11
-rw-r--r--vespalib/src/vespa/vespalib/util/iwakeupservice.h5
-rw-r--r--vespalib/src/vespa/vespalib/util/wakeupservice.cpp29
-rw-r--r--vespalib/src/vespa/vespalib/util/wakeupservice.h15
6 files changed, 32 insertions, 31 deletions
diff --git a/vespalib/CMakeLists.txt b/vespalib/CMakeLists.txt
index ff4e62cc67c..2bc339fe31c 100644
--- a/vespalib/CMakeLists.txt
+++ b/vespalib/CMakeLists.txt
@@ -150,8 +150,8 @@ vespa_define_module(
src/tests/util/size_literals
src/tests/valgrind
src/tests/visit_ranges
- src/tests/wakeup
src/tests/wakeupservice
+ src/tests/wakeup
src/tests/websocket
src/tests/zcurve
diff --git a/vespalib/src/tests/wakeupservice/CMakeLists.txt b/vespalib/src/tests/wakeupservice/CMakeLists.txt
index b00fe475c82..b9c80f9aab0 100644
--- a/vespalib/src/tests/wakeupservice/CMakeLists.txt
+++ b/vespalib/src/tests/wakeupservice/CMakeLists.txt
@@ -6,3 +6,4 @@ vespa_add_executable(vespalib_wakeupservice_test_app TEST
vespalib
)
vespa_add_test(NAME vespalib_wakeupservice_test_app COMMAND vespalib_wakeupservice_test_app)
+
diff --git a/vespalib/src/tests/wakeupservice/wakeupservice_test.cpp b/vespalib/src/tests/wakeupservice/wakeupservice_test.cpp
index 7b24d47d678..ba642958fd6 100644
--- a/vespalib/src/tests/wakeupservice/wakeupservice_test.cpp
+++ b/vespalib/src/tests/wakeupservice/wakeupservice_test.cpp
@@ -14,7 +14,7 @@ TEST("require that wakeup is called") {
WakeupCounter a;
WakeupService service(1ms);
EXPECT_EQUAL(0u, a._count);
- auto ra = service.registerForWakeup(&a);
+ auto ra = service.registerForInvoke([&a](){ a.wakeup(); });
EXPECT_TRUE(ra);
while (a._count == 0) {
std::this_thread::sleep_for(1ms);
@@ -25,20 +25,19 @@ TEST("require that wakeup is called") {
EXPECT_EQUAL(countAtStop, a._count);
}
-TEST("require that same wakeup can only be registered once, but reregisterd after unregistered.") {
+TEST("require that same wakeup can be registered multiple times.") {
WakeupCounter a;
WakeupService service(1ms);
EXPECT_EQUAL(0u, a._count);
- auto ra1 = service.registerForWakeup(&a);
+ auto ra1 = service.registerForInvoke([&a](){ a.wakeup(); });
EXPECT_TRUE(ra1);
- auto ra2 = service.registerForWakeup(&a);
- EXPECT_FALSE(ra2);
+ auto ra2 = service.registerForInvoke([&a](){ a.wakeup(); });
while (a._count == 0) {
std::this_thread::sleep_for(1ms);
}
ra1.reset();
uint64_t countAtStop = a._count;
- ra2 = service.registerForWakeup(&a);
+ ra2 = service.registerForInvoke([&a](){ a.wakeup(); });
EXPECT_TRUE(ra2);
std::this_thread::sleep_for(1s);
EXPECT_LESS(countAtStop, a._count);
diff --git a/vespalib/src/vespa/vespalib/util/iwakeupservice.h b/vespalib/src/vespa/vespalib/util/iwakeupservice.h
index fbd8a299fe7..81a29fb4be5 100644
--- a/vespalib/src/vespa/vespalib/util/iwakeupservice.h
+++ b/vespalib/src/vespa/vespalib/util/iwakeupservice.h
@@ -4,17 +4,18 @@
#include "executor.h"
#include "idestructorcallback.h"
+#include <functional>
namespace vespalib {
/**
- * Interface to register for receiving wakeup calls.
+ * Interface to register for receiving regular invoke calls.
* The registration will last as long as the returned object is kept alive.
**/
class IWakeupService {
public:
virtual ~IWakeupService() = default;
- virtual std::shared_ptr<IDestructorCallback> registerForWakeup(IWakeup * toWakeup) = 0;
+ virtual std::unique_ptr<IDestructorCallback> registerForInvoke(std::function<void()> func) = 0;
};
}
diff --git a/vespalib/src/vespa/vespalib/util/wakeupservice.cpp b/vespalib/src/vespa/vespalib/util/wakeupservice.cpp
index fbb4b1c12e6..dd8c7cd3ed1 100644
--- a/vespalib/src/vespa/vespalib/util/wakeupservice.cpp
+++ b/vespalib/src/vespa/vespalib/util/wakeupservice.cpp
@@ -28,37 +28,36 @@ WakeupService::~WakeupService()
class WakeupService::Registration : public IDestructorCallback {
public:
- Registration(WakeupService * service, IWakeup * toWakeup) noexcept
+ Registration(WakeupService * service, VoidFunc func) noexcept
: _service(service),
- _toWakeup(toWakeup)
+ _func(func)
{ }
Registration(const Registration &) = delete;
Registration & operator=(const Registration &) = delete;
~Registration() override{
- _service->unregister(_toWakeup);
+ _service->unregister(_func);
}
private:
WakeupService * _service;
- IWakeup * _toWakeup;
+ VoidFunc _func;
};
-std::shared_ptr<IDestructorCallback>
-WakeupService::registerForWakeup(IWakeup * toWakeup) {
+std::unique_ptr<IDestructorCallback>
+WakeupService::registerForInvoke(VoidFunc func) {
std::lock_guard guard(_lock);
- auto found = std::find(_toWakeup.begin(), _toWakeup.end(), toWakeup);
- if (found != _toWakeup.end()) return std::shared_ptr<IDestructorCallback>();
-
- _toWakeup.push_back(toWakeup);
+ _toWakeup.push_back(func);
if ( ! _thread) {
_thread = std::make_unique<std::thread>(WakeupService::run, this);
}
- return std::make_shared<Registration>(this, toWakeup);
+ return std::make_unique<Registration>(this, func);
}
void
-WakeupService::unregister(IWakeup * toWakeup) {
+WakeupService::unregister(VoidFunc func) {
std::lock_guard guard(_lock);
- auto found = std::find(_toWakeup.begin(), _toWakeup.end(), toWakeup);
+ auto found = std::find_if(_toWakeup.begin(), _toWakeup.end(), [&func](const VoidFunc & a) {
+ return func.target<VoidFunc>() == a.target<VoidFunc>();
+ });
assert (found != _toWakeup.end());
_toWakeup.erase(found);
}
@@ -69,8 +68,8 @@ WakeupService::runLoop() {
while ( ! done ) {
{
std::lock_guard guard(_lock);
- for (IWakeup *toWakeup: _toWakeup) {
- toWakeup->wakeup();
+ for (VoidFunc & func: _toWakeup) {
+ func();
}
done = _closed;
}
diff --git a/vespalib/src/vespa/vespalib/util/wakeupservice.h b/vespalib/src/vespa/vespalib/util/wakeupservice.h
index 2437a7e4a54..c2f46a766a8 100644
--- a/vespalib/src/vespa/vespalib/util/wakeupservice.h
+++ b/vespalib/src/vespa/vespalib/util/wakeupservice.h
@@ -17,6 +17,7 @@ namespace vespalib {
* 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;
@@ -25,17 +26,17 @@ public:
/**
* Register the one to be woken up
*/
- std::shared_ptr<IDestructorCallback> registerForWakeup(IWakeup * toWakeup) override;
+ std::unique_ptr<IDestructorCallback> registerForInvoke(VoidFunc func) override;
private:
class Registration;
- void unregister(IWakeup * toWakeup);
+ void unregister(VoidFunc func);
void runLoop();
static void run(WakeupService *);
- duration _naptime;
- std::mutex _lock;
- bool _closed;
- std::vector<IWakeup *> _toWakeup;
- std::unique_ptr<std::thread> _thread;
+ duration _naptime;
+ std::mutex _lock;
+ bool _closed;
+ std::vector<VoidFunc> _toWakeup;
+ std::unique_ptr<std::thread> _thread;
};
}