From 87a35262870b54fc49e9c18038cd9b9e08662419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com> Date: Wed, 15 Sep 2021 13:16:35 -0400 Subject: [PATCH] ThreadLoop: use enum class for ThreadState Change-Id: I48cf424123ef137dc1c6fee0ab68c94ab6ac4c56 --- src/threadloop.cpp | 31 +++++++++---------------------- src/threadloop.h | 8 ++++---- 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/src/threadloop.cpp b/src/threadloop.cpp index e532ebd14a..9d0ebb1cfa 100644 --- a/src/threadloop.cpp +++ b/src/threadloop.cpp @@ -34,7 +34,7 @@ ThreadLoop::mainloop(std::thread::id& tid, tid = std::this_thread::get_id(); try { if (setup()) { - while (state_ == RUNNING) + while (state_ == ThreadState::RUNNING) process(); cleanup(); } else { @@ -70,28 +70,27 @@ ThreadLoop::start() { const auto s = state_.load(); - if (s == RUNNING) { + if (s == ThreadState::RUNNING) { JAMI_ERR("already started"); return; } // stop pending but not processed by thread yet? - if (s == STOPPING and thread_.joinable()) { + if (s == ThreadState::STOPPING and thread_.joinable()) { JAMI_DBG("stop pending"); thread_.join(); } - state_ = RUNNING; - thread_ - = std::thread(&ThreadLoop::mainloop, this, std::ref(threadId_), setup_, process_, cleanup_); + state_ = ThreadState::RUNNING; + thread_ = std::thread(&ThreadLoop::mainloop, this, std::ref(threadId_), setup_, process_, cleanup_); threadId_ = thread_.get_id(); } void ThreadLoop::stop() { - if (state_ == RUNNING) - state_ = STOPPING; + if (state_ == ThreadState::RUNNING) + state_ = ThreadState::STOPPING; } void @@ -120,24 +119,12 @@ bool ThreadLoop::isRunning() const noexcept { #ifdef _WIN32 - return state_ == RUNNING; + return state_ == ThreadState::RUNNING; #else - return thread_.joinable() and state_ == RUNNING; + return thread_.joinable() and state_ == ThreadState::RUNNING; #endif } -bool -ThreadLoop::isStopping() const noexcept -{ - return state_ == STOPPING; -} - -std::thread::id -ThreadLoop::get_id() const noexcept -{ - return threadId_; -} - void InterruptedThreadLoop::stop() { diff --git a/src/threadloop.h b/src/threadloop.h index 1f36d2c60c..b8e8c3cd19 100644 --- a/src/threadloop.h +++ b/src/threadloop.h @@ -40,7 +40,7 @@ struct ThreadLoopException : public std::runtime_error class ThreadLoop { public: - enum ThreadState { READY, RUNNING, STOPPING }; + enum class ThreadState { READY, RUNNING, STOPPING }; ThreadLoop(const std::function<bool()>& setup, const std::function<void()>& process, @@ -54,8 +54,8 @@ public: void waitForCompletion(); // thread will stop itself bool isRunning() const noexcept; - bool isStopping() const noexcept; - std::thread::id get_id() const noexcept; + bool isStopping() const noexcept { return state_ == ThreadState::STOPPING; } + std::thread::id get_id() const noexcept { return threadId_; } private: ThreadLoop(const ThreadLoop&) = delete; @@ -73,7 +73,7 @@ private: const std::function<void()> process, const std::function<void()> cleanup); - std::atomic<ThreadState> state_ {READY}; + std::atomic<ThreadState> state_ {ThreadState::READY}; std::thread::id threadId_; std::thread thread_; }; -- GitLab