From c6ba32fc4189ce28ab124f8d0f582fd69719f215 Mon Sep 17 00:00:00 2001 From: foreman Date: Mon, 7 May 2018 18:50:50 -0400 Subject: [PATCH] P4 to Git Change 1551367 by lmoriche@lmoriche_opencl_dev2 on 2018/05/07 18:40:02 SWDEV-145570 - [HIP] - Fix a race condition in the CommandQueue::Thread destruction. Affected files ... ... //depot/stg/opencl/drivers/opencl/runtime/os/os_posix.cpp#44 edit ... //depot/stg/opencl/drivers/opencl/runtime/platform/commandqueue.cpp#26 edit [ROCm/clr commit: 7fe8b95168a90beb5499cedd00597f2360fd1741] --- projects/clr/rocclr/runtime/os/os_posix.cpp | 4 +++- .../rocclr/runtime/platform/commandqueue.cpp | 20 +++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/projects/clr/rocclr/runtime/os/os_posix.cpp b/projects/clr/rocclr/runtime/os/os_posix.cpp index 074996c264..9b7e33e317 100644 --- a/projects/clr/rocclr/runtime/os/os_posix.cpp +++ b/projects/clr/rocclr/runtime/os/os_posix.cpp @@ -390,7 +390,9 @@ void* Thread::entry(Thread* thread) { return thread->main(); } -bool Os::isThreadAlive(const Thread& thread) { return true; } +bool Os::isThreadAlive(const Thread& thread) { + return ::pthread_kill((pthread_t)thread.handle(), 0) == 0; +} const void* Os::createOsThread(amd::Thread* thread) { pthread_attr_t threadAttr; diff --git a/projects/clr/rocclr/runtime/platform/commandqueue.cpp b/projects/clr/rocclr/runtime/platform/commandqueue.cpp index 8710adc3c5..705f381267 100644 --- a/projects/clr/rocclr/runtime/platform/commandqueue.cpp +++ b/projects/clr/rocclr/runtime/platform/commandqueue.cpp @@ -31,9 +31,25 @@ HostQueue::HostQueue(Context& context, Device& device, cl_command_queue_properti bool HostQueue::terminate() { if (Os::isThreadAlive(thread_)) { + Command* marker = nullptr; + + // Send a finish if the queue is still accepting commands. + { ScopedLock sl(queueLock_); + if (thread_.acceptingCommands_) { + marker = new Marker(*this, false); + if (marker != nullptr) { + append(*marker); + queueLock_.notify(); + } + } + } + if (marker != nullptr) { + marker->awaitCompletion(); + marker->release(); + } + // Wake-up the command loop, so it can exit - { - ScopedLock sl(queueLock_); + { ScopedLock sl(queueLock_); thread_.acceptingCommands_ = false; queueLock_.notify(); }