diff options
author | Håvard Pettersen <havardpe@yahooinc.com> | 2023-02-20 11:39:12 +0000 |
---|---|---|
committer | Håvard Pettersen <havardpe@yahooinc.com> | 2023-02-20 12:33:29 +0000 |
commit | d9b2cc5dd6aa9210241efeac249be84772377b35 (patch) | |
tree | a78022b7588a4deba71544c79dd6efaa53792f94 /vespalib | |
parent | a5d5a7dd7bab499554691fa59e08b3771b5e32d3 (diff) |
remove document::Runnable
use std::thread directly instead
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/src/vespa/vespalib/util/CMakeLists.txt | 1 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/document_runnable.cpp | 103 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/document_runnable.h | 96 |
3 files changed, 0 insertions, 200 deletions
diff --git a/vespalib/src/vespa/vespalib/util/CMakeLists.txt b/vespalib/src/vespa/vespalib/util/CMakeLists.txt index 73e8b93a2ff..c8536fc68c1 100644 --- a/vespalib/src/vespa/vespalib/util/CMakeLists.txt +++ b/vespalib/src/vespa/vespalib/util/CMakeLists.txt @@ -24,7 +24,6 @@ vespa_add_library(vespalib_vespalib_util OBJECT cpu_usage.cpp crc.cpp destructor_callbacks.cpp - document_runnable.cpp doom.cpp dual_merge_director.cpp error.cpp diff --git a/vespalib/src/vespa/vespalib/util/document_runnable.cpp b/vespalib/src/vespa/vespalib/util/document_runnable.cpp deleted file mode 100644 index c0af72dbbb1..00000000000 --- a/vespalib/src/vespa/vespalib/util/document_runnable.cpp +++ /dev/null @@ -1,103 +0,0 @@ -// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. - -#include "document_runnable.h" -#include <vespa/vespalib/util/exceptions.h> -#include <cassert> - -namespace document { - -Runnable::Runnable() - : _stateLock(), - _stateCond(), - _state(NOT_RUNNING) -{ -} - -Runnable::~Runnable() { - std::lock_guard monitorGuard(_stateLock); - assert(getState() == NOT_RUNNING); -} - -bool Runnable::start(FastOS_ThreadPool& pool) -{ - std::unique_lock guard(_stateLock); - _stateCond.wait(guard, [&](){ return (getState() != STOPPING);}); - - if (getState() != NOT_RUNNING) return false; - set_state(STARTING); - if (pool.NewThread(this) == nullptr) { - throw vespalib::IllegalStateException("Failed starting a new thread", VESPA_STRLOC); - } - return true; -} - -void Runnable::set_state(State new_state) noexcept -{ - _state.store(new_state, std::memory_order_relaxed); -} - -bool Runnable::stopping() const noexcept -{ - State s(getState()); - return (s == STOPPING) || (s == RUNNING && GetThread()->GetBreakFlag()); -} - -bool Runnable::running() const noexcept -{ - State s(getState()); - // Must check break-flag too, as threadpool will use that to close - // down. - return (s == STARTING || (s == RUNNING && !GetThread()->GetBreakFlag())); -} - -bool Runnable::stop() -{ - std::lock_guard monitor(_stateLock); - if (getState() == STOPPING || getState() == NOT_RUNNING) return false; - GetThread()->SetBreakFlag(); - set_state(STOPPING); - return onStop(); -} - -bool Runnable::onStop() -{ - return true; -} - -bool Runnable::join() const -{ - std::unique_lock guard(_stateLock); - assert ((getState() != STARTING) && (getState() != RUNNING)); - _stateCond.wait(guard, [&](){ return (getState() == NOT_RUNNING);}); - return true; -} - -FastOS_ThreadId Runnable::native_thread_id() const noexcept -{ - return GetThread()->GetThreadId(); -} - -void Runnable::Run(FastOS_ThreadInterface*, void*) -{ - { - std::lock_guard guard(_stateLock); - // Don't set state if its already at stopping. (And let run() be - // called even though about to stop for consistency) - if (getState() == STARTING) { - set_state(RUNNING); - } - } - - // By not catching exceptions, they should abort whole application. - // We should thus not need to have a catch-all to set state to not - // running. - run(); - - { - std::lock_guard guard(_stateLock); - set_state(NOT_RUNNING); - _stateCond.notify_all(); - } -} - -} diff --git a/vespalib/src/vespa/vespalib/util/document_runnable.h b/vespalib/src/vespa/vespalib/util/document_runnable.h deleted file mode 100644 index 89388bac34c..00000000000 --- a/vespalib/src/vespa/vespalib/util/document_runnable.h +++ /dev/null @@ -1,96 +0,0 @@ -// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -/** - * @class document::Runnable - * @ingroup util - * - * @brief Implementation of FastOS_Runnable that implements threadsafe stop. - * - * FastOS_Runnable can easily be used unsafe. If you use the thread pointer for - * anything after your runnable had returned from Run(), it could affect another - * runnable now using that thread. - * - * Using this class should be foolproof to avoid synchronization issues during - * thread starting and stopping :) - * - * @author H�kon Humberset - * @date 2005-09-19 - */ - -#pragma once - -#include <vespa/fastos/thread.h> -#include <atomic> - -namespace document { - -class Runnable : private FastOS_Runnable { -public: - enum State { NOT_RUNNING, STARTING, RUNNING, STOPPING }; - -private: - mutable std::mutex _stateLock; - mutable std::condition_variable _stateCond; - std::atomic<State> _state; - - void Run(FastOS_ThreadInterface*, void*) override; - void set_state(State new_state) noexcept; // _stateLock must be held -public: - /** - * Create a runnable. - * @param pool If set, runnable will be started in constructor. - */ - Runnable(); - ~Runnable() override; - - /** - * Start this runnable. - * @param pool The threadpool from which a thread is acquired. - * @return True if thread was started, false if thread was already running. - */ - bool start(FastOS_ThreadPool& pool); - - /** - * Stop this runnable. - * @return True if thread was stopped, false if thread was not running. - */ - bool stop(); - - /** - * Called in stop(). Implement, to for instance notify any monitors that - * can be waiting. - */ - virtual bool onStop(); - - /** - * Wait for this thread to finish, if it is in the process of stopping. - * @return True if thread finished (or not running), false if thread is - * running normally and no stop is scheduled. - */ - bool join() const; - - /** - * Implement this to make the runnable actually do something. - */ - virtual void run() = 0; - - /** - * Get the current state of this runnable. - * Thread safe (but relaxed) read; may be stale if done outside _stateLock. - */ - [[nodiscard]] State getState() const noexcept { - return _state.load(std::memory_order_relaxed); - } - - /** Check if system is in the process of stopping. */ - [[nodiscard]] bool stopping() const noexcept; - - /** - * Checks if runnable is running or not. (Started is considered running) - */ - [[nodiscard]] bool running() const noexcept; - - FastOS_ThreadId native_thread_id() const noexcept; -}; - -} - |