From 29383d1082bde4a48d5d96fe2bf7f49f55a38e8d Mon Sep 17 00:00:00 2001 From: Jan Date: Mon, 22 Oct 2018 16:49:13 +0200 Subject: [PATCH] don't fiddle with thread structs that were not initialized yet (#6979) --- lib/Basics/Thread.cpp | 20 ++++++++++++++------ lib/Basics/Thread.h | 1 + 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/Basics/Thread.cpp b/lib/Basics/Thread.cpp index fcc0ff9414..0583aee860 100644 --- a/lib/Basics/Thread.cpp +++ b/lib/Basics/Thread.cpp @@ -138,6 +138,7 @@ std::string Thread::stringify(ThreadState state) { /// @brief constructs a thread Thread::Thread(std::string const& name, bool deleteOnExit) : _deleteOnExit(deleteOnExit), + _threadStructInitialized(false), _name(name), _thread(), _threadNumber(0), @@ -155,12 +156,14 @@ Thread::~Thread() { << "delete(" << _name << "), state: " << stringify(state); if (state == ThreadState::STOPPED) { - if (TRI_IsSelfThread(&_thread)) { - // we must ignore any errors here, but TRI_DetachThread will log them - TRI_DetachThread(&_thread); - } else { - // we must ignore any errors here, but TRI_JoinThread will log them - TRI_JoinThread(&_thread); + if (_threadStructInitialized) { + if (TRI_IsSelfThread(&_thread)) { + // we must ignore any errors here, but TRI_DetachThread will log them + TRI_DetachThread(&_thread); + } else { + // we must ignore any errors here, but TRI_JoinThread will log them + TRI_JoinThread(&_thread); + } } _state.store(ThreadState::DETACHED); @@ -282,6 +285,9 @@ bool Thread::start(ConditionVariable* finishedCondition) { return false; } + TRI_ASSERT(!_threadStructInitialized); + memset(&_thread, 0, sizeof(thread_t)); + bool ok = TRI_StartThread(&_thread, &_threadId, _name.c_str(), &startThread, this); @@ -295,6 +301,8 @@ bool Thread::start(ConditionVariable* finishedCondition) { cleanupMe(); } + _threadStructInitialized = true; + return ok; } diff --git a/lib/Basics/Thread.h b/lib/Basics/Thread.h index 4c06cf4f51..1524ac8145 100644 --- a/lib/Basics/Thread.h +++ b/lib/Basics/Thread.h @@ -151,6 +151,7 @@ class Thread { private: bool const _deleteOnExit; + bool _threadStructInitialized; // name of the thread std::string const _name;