diff options
author | Adrian Salido <salidoa@google.com> | 2017-04-25 13:23:45 -0700 |
---|---|---|
committer | Adrian Salido <salidoa@google.com> | 2017-04-25 13:25:08 -0700 |
commit | ba494c8efc0a210b1bd4af6358b60f52900dd8af (patch) | |
tree | 8243851cfdfe326144a1cd04c7838b2e9d1d9496 | |
parent | fa37f67815c29aaa17501569a19037b18512db99 (diff) | |
download | drm_hwcomposer-ba494c8efc0a210b1bd4af6358b60f52900dd8af.tar.gz |
drm_hwcomposer: avoid potential race condition between worker init and exit
Protect worker initialization and exit with lock in case Exit is called
in different thread.
Change-Id: Ibddfce192deeea9d33575de8abf859fdca5f74ed
Signed-off-by: Adrian Salido <salidoa@google.com>
-rw-r--r-- | worker.cpp | 10 |
1 files changed, 6 insertions, 4 deletions
@@ -30,21 +30,23 @@ Worker::~Worker() { } int Worker::InitWorker() { + std::lock_guard<std::mutex> lk(mutex_); if (initialized()) return -EALREADY; thread_ = std::unique_ptr<std::thread>( new std::thread(&Worker::InternalRoutine, this)); initialized_ = true; + exit_ = false; return 0; } void Worker::Exit() { + std::unique_lock<std::mutex> lk(mutex_); + exit_ = true; if (initialized()) { - Lock(); - exit_ = true; - Unlock(); + lk.unlock(); cond_.notify_all(); thread_->join(); initialized_ = false; @@ -68,7 +70,7 @@ int Worker::WaitForSignalOrExitLocked(int64_t max_nanoseconds) { if (should_exit()) ret = -EINTR; - // release leaves lock unlocked when returning + // release leaves mutex locked when going out of scope lk.release(); return ret; |