Skip to content
Snippets Groups Projects
Commit 87a35262 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

ThreadLoop: use enum class for ThreadState

Change-Id: I48cf424123ef137dc1c6fee0ab68c94ab6ac4c56
parent 1d4dc978
No related branches found
No related tags found
No related merge requests found
......@@ -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()
{
......
......@@ -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_;
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment