From dc4e09a63a6823a3e661f73307bbca080dab4ac6 Mon Sep 17 00:00:00 2001 From: German Andryeyev Date: Fri, 29 May 2020 11:06:55 -0400 Subject: [PATCH] Avoid lock for last queued command Use atomics for last queued command update Change-Id: I759e9d78ea72f23c0d45dbede6250b231e122276 --- rocclr/platform/commandqueue.cpp | 22 ++++++++++++---------- rocclr/platform/commandqueue.hpp | 4 +--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/rocclr/platform/commandqueue.cpp b/rocclr/platform/commandqueue.cpp index 4ab1a2de69..ac17d90f86 100644 --- a/rocclr/platform/commandqueue.cpp +++ b/rocclr/platform/commandqueue.cpp @@ -192,25 +192,27 @@ bool HostQueue::isEmpty() { void HostQueue::setLastQueuedCommand(Command* lastCommand) { // Set last submitted command - ScopedLock l(lastCmdLock_); - if (lastEnqueueCommand_ != nullptr) { - lastEnqueueCommand_->release(); + Command* lastEnqueueCommand = + lastEnqueueCommand_.exchange(lastCommand, std::memory_order_acq_rel); + if (lastEnqueueCommand != nullptr) { + lastEnqueueCommand->release(); } - lastEnqueueCommand_ = lastCommand; if (lastCommand != nullptr) { - lastEnqueueCommand_->retain(); + lastCommand->retain(); } } Command* HostQueue::getLastQueuedCommand(bool retain) { // Get last submitted command - ScopedLock l(lastCmdLock_); - - if (retain && lastEnqueueCommand_ != nullptr) { - lastEnqueueCommand_->retain(); + Command* lastEnqueueCommand = lastEnqueueCommand_.load(std::memory_order_acquire); + if (lastEnqueueCommand == nullptr) { + return nullptr; } - return lastEnqueueCommand_; + if (retain) { + lastEnqueueCommand->retain(); + } + return lastEnqueueCommand; } DeviceQueue::~DeviceQueue() { diff --git a/rocclr/platform/commandqueue.hpp b/rocclr/platform/commandqueue.hpp index e051bdecd8..a5d22ede02 100644 --- a/rocclr/platform/commandqueue.hpp +++ b/rocclr/platform/commandqueue.hpp @@ -126,7 +126,6 @@ class CommandQueue : public RuntimeObject { rtCUs_(rtCUs), priority_(priority), queueLock_("CommandQueue::queueLock"), - lastCmdLock_("LastQueuedCommand"), device_(device), context_(context), cuMask_(cuMask){} @@ -135,7 +134,6 @@ class CommandQueue : public RuntimeObject { uint rtCUs_; //!< The number of used RT compute units Priority priority_; //!< Queue priority Monitor queueLock_; //!< Lock protecting the queue - Monitor lastCmdLock_; //!< Lock protecting the last queued command Device& device_; //!< The device SharedReference context_; //!< The context of this command queue const std::vector& cuMask_; //!< The CU mask @@ -187,7 +185,7 @@ class HostQueue : public CommandQueue { private: ConcurrentLinkedQueue queue_; //!< The queue. - Command* lastEnqueueCommand_; //!< The last submitted command + std::atomic lastEnqueueCommand_; //!< The last submitted command //! Await commands and execute them as they become ready. void loop(device::VirtualDevice* virtualDevice);