Skip to content
Snippets Groups Projects
Commit efd4a93a authored by Guillaume Roguez's avatar Guillaume Roguez Committed by gerrit2
Browse files

threadloop: fix race condition in get_id access

threadloop::get_id() can be called with a non-initialized thread_ variable.
If this call is done under the loop thread, right after its creation,
and if InterruptedThreadLoop::wait_for is call, an non catched exception
is thrown and std::terminate stops all the application.
This patch proposes to fix this race condition and fix the non-catched
exception to let the application in a safe state.

Issue: #81210
Change-Id: I3d8409ac505be181d1a54c5bdd0e679fdb81bf88
parent 66015aef
No related branches found
No related tags found
No related merge requests found
...@@ -24,10 +24,12 @@ ...@@ -24,10 +24,12 @@
namespace ring { namespace ring {
void void
ThreadLoop::mainloop(const std::function<bool()> setup, ThreadLoop::mainloop(std::thread::id& tid,
const std::function<bool()> setup,
const std::function<void()> process, const std::function<void()> process,
const std::function<void()> cleanup) const std::function<void()> cleanup)
{ {
tid = std::this_thread::get_id();
try { try {
if (setup()) { if (setup()) {
while (state_ == RUNNING) while (state_ == RUNNING)
...@@ -37,7 +39,9 @@ ThreadLoop::mainloop(const std::function<bool()> setup, ...@@ -37,7 +39,9 @@ ThreadLoop::mainloop(const std::function<bool()> setup,
RING_ERR("setup failed"); RING_ERR("setup failed");
} }
} catch (const ThreadLoopException& e) { } catch (const ThreadLoopException& e) {
RING_ERR("%s", e.what()); RING_ERR("[threadloop:%p] ThreadLoopException: %s", this, e.what());
} catch (const std::exception& e) {
RING_ERR("[threadloop:%p] Unwaited exception: %s", this, e.what());
} }
} }
...@@ -85,7 +89,8 @@ ThreadLoop::start() ...@@ -85,7 +89,8 @@ ThreadLoop::start()
} }
state_ = RUNNING; state_ = RUNNING;
thread_ = std::thread(&ThreadLoop::mainloop, this, setup_, process_, cleanup_); thread_ = std::thread(&ThreadLoop::mainloop, this, std::ref(threadId_), setup_, process_, cleanup_);
threadId_ = thread_.get_id();
} }
void void
...@@ -124,7 +129,7 @@ ThreadLoop::isStopping() const noexcept ...@@ -124,7 +129,7 @@ ThreadLoop::isStopping() const noexcept
std::thread::id std::thread::id
ThreadLoop::get_id() const noexcept ThreadLoop::get_id() const noexcept
{ {
return thread_.get_id(); return threadId_;
} }
void void
......
...@@ -66,11 +66,13 @@ private: ...@@ -66,11 +66,13 @@ private:
std::function<void()> process_; std::function<void()> process_;
std::function<void()> cleanup_; std::function<void()> cleanup_;
void mainloop(const std::function<bool()> setup, void mainloop(std::thread::id& tid,
const std::function<bool()> setup,
const std::function<void()> process, const std::function<void()> process,
const std::function<void()> cleanup); const std::function<void()> cleanup);
std::atomic<ThreadState> state_ {READY}; std::atomic<ThreadState> state_ {READY};
std::thread::id threadId_;
std::thread thread_; std::thread thread_;
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment