diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2022-12-21 17:22:36 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-21 17:22:36 +0100 |
commit | 766ee34d38c47deb1a234590eba6413d075d822a (patch) | |
tree | ae4f920aa5c70672b7a67069bdd7e48626d93923 | |
parent | e7efc2c90f128bc801ae29abe071c0871a277726 (diff) | |
parent | 73019d17bc04d6d0d18d5d317db47e7616820d0e (diff) |
Merge pull request #25322 from vespa-engine/balder/ensure-fast-shutdown-for-invokerservice
Notify invokerservice that it is being closed.
-rw-r--r-- | vespalib/src/vespa/vespalib/util/invokeserviceimpl.cpp | 23 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/invokeserviceimpl.h | 2 |
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; |