summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2022-12-21 15:39:02 +0000
committerHenning Baldersheim <balder@yahoo-inc.com>2022-12-21 15:39:02 +0000
commit73019d17bc04d6d0d18d5d317db47e7616820d0e (patch)
treef13c2fa7e04ce63947cb9219f6f2d3a7338188ed
parenta3d5e2ce14352ab181e940574f6e1a99d40cd520 (diff)
Notify invokerservice that it is being closed.
-rw-r--r--vespalib/src/vespa/vespalib/util/invokeserviceimpl.cpp23
-rw-r--r--vespalib/src/vespa/vespalib/util/invokeserviceimpl.h2
2 files changed, 12 insertions, 13 deletions
diff --git a/vespalib/src/vespa/vespalib/util/invokeserviceimpl.cpp b/vespalib/src/vespa/vespalib/util/invokeserviceimpl.cpp
index c10029b2f58..d4a4164fbf4 100644
--- a/vespalib/src/vespa/vespalib/util/invokeserviceimpl.cpp
+++ b/vespalib/src/vespa/vespalib/util/invokeserviceimpl.cpp
@@ -9,6 +9,7 @@ InvokeServiceImpl::InvokeServiceImpl(duration napTime)
: _naptime(napTime),
_now(steady_clock::now()),
_lock(),
+ _cond(),
_currId(0),
_closed(false),
_toInvoke(),
@@ -22,6 +23,7 @@ InvokeServiceImpl::~InvokeServiceImpl()
std::lock_guard guard(_lock);
assert(_toInvoke.empty());
_closed = true;
+ _cond.notify_all();
}
_thread->join();
}
@@ -39,7 +41,7 @@ public:
}
private:
InvokeServiceImpl * _service;
- uint64_t _id;
+ uint64_t _id;
};
std::unique_ptr<IDestructorCallback>
@@ -47,6 +49,7 @@ InvokeServiceImpl::registerInvoke(InvokeFunc func) {
std::lock_guard guard(_lock);
uint64_t id = _currId++;
_toInvoke.emplace_back(id, std::move(func));
+ _cond.notify_all();
return std::make_unique<Registration>(this, id);
}
@@ -58,25 +61,19 @@ InvokeServiceImpl::unregister(uint64_t id) {
});
assert (found != _toInvoke.end());
_toInvoke.erase(found);
+ _cond.notify_all();
}
void
InvokeServiceImpl::runLoop() {
- bool done = false;
- while ( ! done ) {
+ std::unique_lock guard(_lock);
+ while ( ! _closed ) {
_now.store(steady_clock::now(), std::memory_order_relaxed);
- {
- std::lock_guard guard(_lock);
- for (auto & func: _toInvoke) {
- func.second();
- }
- done = _closed;
- }
- if ( ! done) {
- std::this_thread::sleep_for(_naptime);
+ for (auto & func: _toInvoke) {
+ func.second();
}
+ _cond.wait_for(guard, _naptime);
}
-
}
}
diff --git a/vespalib/src/vespa/vespalib/util/invokeserviceimpl.h b/vespalib/src/vespa/vespalib/util/invokeserviceimpl.h
index beb57ca1ce0..ef0755e7614 100644
--- a/vespalib/src/vespa/vespalib/util/invokeserviceimpl.h
+++ b/vespalib/src/vespa/vespalib/util/invokeserviceimpl.h
@@ -7,6 +7,7 @@
#include <mutex>
#include <vector>
#include <thread>
+#include <condition_variable>
namespace vespalib {
@@ -29,6 +30,7 @@ private:
duration _naptime;
std::atomic<steady_time> _now;
std::mutex _lock;
+ std::condition_variable _cond;
uint64_t _currId;
bool _closed;
std::vector<IdAndFunc> _toInvoke;