From c0192e7543f35ba4fcd62b771a65bdcf4b6f7e62 Mon Sep 17 00:00:00 2001 From: moudgils <47460854+moudgils@users.noreply.github.com> Date: Mon, 15 Nov 2021 09:56:28 -0800 Subject: [PATCH] Remove CPU Spin lock to help conserve power/perf (#5597) Signed-off-by: moudgils --- Gems/Atom/RHI/Code/Include/Atom/RHI/CommandQueue.h | 2 ++ Gems/Atom/RHI/Code/Source/RHI/CommandQueue.cpp | 12 +++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Gems/Atom/RHI/Code/Include/Atom/RHI/CommandQueue.h b/Gems/Atom/RHI/Code/Include/Atom/RHI/CommandQueue.h index 2985581040..4ae560c816 100644 --- a/Gems/Atom/RHI/Code/Include/Atom/RHI/CommandQueue.h +++ b/Gems/Atom/RHI/Code/Include/Atom/RHI/CommandQueue.h @@ -72,6 +72,8 @@ namespace AZ AZStd::mutex m_workQueueMutex; AZStd::queue m_workQueue; AZStd::condition_variable m_workQueueCondition; + AZStd::mutex m_flushCommandsMutex; + AZStd::condition_variable m_flushCommandsCondition; AZStd::atomic_bool m_isWorkQueueEmpty; AZStd::atomic_bool m_isQuitting; }; diff --git a/Gems/Atom/RHI/Code/Source/RHI/CommandQueue.cpp b/Gems/Atom/RHI/Code/Source/RHI/CommandQueue.cpp index a365b23e94..1cee382189 100644 --- a/Gems/Atom/RHI/Code/Source/RHI/CommandQueue.cpp +++ b/Gems/Atom/RHI/Code/Source/RHI/CommandQueue.cpp @@ -71,6 +71,7 @@ namespace AZ { m_isQuitting = true; m_workQueueCondition.notify_all(); + m_flushCommandsCondition.notify_all(); if (m_thread.joinable()) { m_thread.join(); @@ -102,9 +103,10 @@ namespace AZ void CommandQueue::FlushCommands() { AZ_PROFILE_SCOPE(RHI, "CommandQueue: FlushCommands"); - while (!m_isWorkQueueEmpty && !m_isQuitting) + AZStd::unique_lock lock(m_flushCommandsMutex); + if (!m_isWorkQueueEmpty && !m_isQuitting) { - AZStd::this_thread::yield(); + m_flushCommandsCondition.wait(lock, [this]() { return m_isWorkQueueEmpty.load() || m_isQuitting.load(); }); } } @@ -119,7 +121,11 @@ namespace AZ if (m_workQueue.empty()) { - m_isWorkQueueEmpty = true; + { + AZStd::unique_lock flushCommandsLock(m_flushCommandsMutex); + m_isWorkQueueEmpty = true; + m_flushCommandsCondition.notify_all(); + } m_workQueueCondition.wait(lock, [this]() { return !m_workQueue.empty() || m_isQuitting; }); }