wsd: improved SocketPoll start-thread guard

Change-Id: I92e2bcf4efacd67fc336ab40194d8cc51da7e4f4
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
This commit is contained in:
Ashod Nakashian 2021-03-10 08:38:31 -05:00 committed by Ashod Nakashian
parent 0f9f7629b5
commit 6e7c5b6013
2 changed files with 13 additions and 10 deletions

View file

@ -116,7 +116,7 @@ namespace {
SocketPoll::SocketPoll(const std::string& threadName)
: _name(threadName),
_stop(false),
_threadStarted(false),
_threadStarted(0),
_threadFinished(false),
_runOnClientThread(false),
_owner(std::this_thread::get_id())
@ -166,23 +166,25 @@ bool SocketPoll::startThread()
{
assert(!_runOnClientThread);
if (!_threadStarted)
// In a race, only the first gets in.
if (_threadStarted++ == 0)
{
_threadStarted = true;
_threadFinished = false;
_stop = false;
try
{
LOG_TRC("starting thread for poll " << _name);
LOG_TRC("Creating thread for poll " << _name);
_thread = std::thread(&SocketPoll::pollingThreadEntry, this);
return true;
}
catch (const std::exception& exc)
{
LOG_ERR("Failed to start poll thread: " << exc.what());
_threadStarted = false;
LOG_ERR("Failed to start poll thread [" << _name << "]: " << exc.what());
_threadStarted = 0;
}
}
else
LOG_ERR("SocketPoll [" << _name << "] thread is already started.");
return false;
}
@ -205,7 +207,7 @@ void SocketPoll::joinThread()
else
{
_thread.join();
_threadStarted = false;
_threadStarted = 0;
}
}
}

View file

@ -663,8 +663,9 @@ public:
void wakeup()
{
if (!isAlive())
LOG_WRN("Waking up dead poll thread [" << _name << "], started: " <<
_threadStarted << ", finished: " << _threadFinished);
LOG_WRN("Waking up dead poll thread ["
<< _name << "], started: " << (_threadStarted ? "true" : "false")
<< ", finished: " << _threadFinished);
wakeup(_wakeup[1]);
}
@ -810,7 +811,7 @@ private:
std::atomic<bool> _stop;
/// The polling thread.
std::thread _thread;
std::atomic<bool> _threadStarted;
std::atomic<int64_t> _threadStarted;
std::atomic<bool> _threadFinished;
std::atomic<bool> _runOnClientThread;
std::thread::id _owner;