diff --git a/src/threadloop.cpp b/src/threadloop.cpp index e532ebd14adee4b9e28345e7396f3e12c1855145..9d0ebb1cfa37fd8115523f0590e4c4c41eb59832 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 1f36d2c60ccef9af540428efad83c48128f2f63e..b8e8c3cd19bb2d52ec5078e8b7f2c86165a293b6 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_; };