From 81cd48c920c1cdd428d1e23d405256dd4a781f9b Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Wed, 14 Oct 2020 10:04:11 +0000 Subject: Use std::mutex/std::condition_variable over vespalib::Monitor --- .../src/vespa/vespalib/util/document_runnable.cpp | 20 +++++++++++--------- .../src/vespa/vespalib/util/document_runnable.h | 15 ++++----------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/staging_vespalib/src/vespa/vespalib/util/document_runnable.cpp b/staging_vespalib/src/vespa/vespalib/util/document_runnable.cpp index 0f7e6155276..e9e5cbbf953 100644 --- a/staging_vespalib/src/vespa/vespalib/util/document_runnable.cpp +++ b/staging_vespalib/src/vespa/vespalib/util/document_runnable.cpp @@ -8,19 +8,21 @@ namespace document { Runnable::Runnable() : _stateLock(), + _stateCond(), _state(NOT_RUNNING) { } Runnable::~Runnable() { - vespalib::MonitorGuard monitorGuard(_stateLock); + std::lock_guard monitorGuard(_stateLock); assert(_state == NOT_RUNNING); } bool Runnable::start(FastOS_ThreadPool& pool) { - vespalib::MonitorGuard monitor(_stateLock); - while (_state == STOPPING) monitor.wait(); + std::unique_lock guard(_stateLock); + _stateCond.wait(guard, [&](){ return (_state != STOPPING);}); + if (_state != NOT_RUNNING) return false; _state = STARTING; if (pool.NewThread(this) == nullptr) { @@ -31,7 +33,7 @@ bool Runnable::start(FastOS_ThreadPool& pool) bool Runnable::stop() { - vespalib::MonitorGuard monitor(_stateLock); + std::lock_guard monitor(_stateLock); if (_state == STOPPING || _state == NOT_RUNNING) return false; GetThread()->SetBreakFlag(); _state = STOPPING; @@ -45,16 +47,16 @@ bool Runnable::onStop() bool Runnable::join() const { - vespalib::MonitorGuard monitor(_stateLock); + std::unique_lock guard(_stateLock); assert ((_state != STARTING) && (_state != RUNNING)); - while (_state != NOT_RUNNING) monitor.wait(); + _stateCond.wait(guard, [&](){ return (_state == NOT_RUNNING);}); return true; } void Runnable::Run(FastOS_ThreadInterface*, void*) { { - vespalib::MonitorGuard monitor(_stateLock); + std::lock_guard guard(_stateLock); // Dont set state if its alreadyt at stopping. (And let run() be // called even though about to stop for consistency) if (_state == STARTING) { @@ -68,9 +70,9 @@ void Runnable::Run(FastOS_ThreadInterface*, void*) run(); { - vespalib::MonitorGuard monitor(_stateLock); + std::lock_guard guard(_stateLock); _state = NOT_RUNNING; - monitor.broadcast(); + _stateCond.notify_all(); } } diff --git a/staging_vespalib/src/vespa/vespalib/util/document_runnable.h b/staging_vespalib/src/vespa/vespalib/util/document_runnable.h index 60a67189b5b..756cbdfa899 100644 --- a/staging_vespalib/src/vespa/vespalib/util/document_runnable.h +++ b/staging_vespalib/src/vespa/vespalib/util/document_runnable.h @@ -14,12 +14,10 @@ * * @author H�kon Humberset * @date 2005-09-19 - * @version $Id$ */ #pragma once -#include #include namespace document { @@ -29,14 +27,11 @@ public: enum State { NOT_RUNNING, STARTING, RUNNING, STOPPING }; private: - mutable vespalib::Monitor _stateLock; + mutable std::mutex _stateLock; + mutable std::condition_variable _stateCond; State _state; void Run(FastOS_ThreadInterface*, void*) override; - - Runnable(const Runnable&); - Runnable& operator=(const Runnable&); - public: /** * Create a runnable. @@ -83,8 +78,7 @@ public: bool stopping() const { State s(getState()); - return (s == STOPPING) || - (s == RUNNING && GetThread()->GetBreakFlag()); + return (s == STOPPING) || (s == RUNNING && GetThread()->GetBreakFlag()); } /** @@ -95,8 +89,7 @@ public: State s(getState()); // Must check breakflag too, as threadpool will use that to close // down. - return (s == STARTING || - (s == RUNNING && !GetThread()->GetBreakFlag())); + return (s == STARTING || (s == RUNNING && !GetThread()->GetBreakFlag())); } }; -- cgit v1.2.3