From 5df64a36e3e6cc32513f1bf40e802f4412b133f6 Mon Sep 17 00:00:00 2001 From: "Neil R. Spruit" Date: Thu, 13 Jun 2024 09:23:20 -0700 Subject: [PATCH] [L0] Add compare and swap for setting the LastCommandEvent in all cases Signed-off-by: Neil R. Spruit --- source/adapters/level_zero/queue.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/source/adapters/level_zero/queue.cpp b/source/adapters/level_zero/queue.cpp index d70d19ca20..4f34bee5c5 100644 --- a/source/adapters/level_zero/queue.cpp +++ b/source/adapters/level_zero/queue.cpp @@ -1696,6 +1696,7 @@ ur_result_t ur_queue_handle_t_::synchronize() { // If event is discarded then it can be in reset state or underlying level // zero handle can have device scope, so we can't synchronize the last // event. + auto savedLastCommandEvent = LastCommandEvent; if (isInOrderQueue() && !LastCommandEvent->IsDiscarded) { ZE2UR_CALL(zeHostSynchronize, (LastCommandEvent->ZeEvent)); @@ -1737,7 +1738,14 @@ ur_result_t ur_queue_handle_t_::synchronize() { } } } - LastCommandEvent = nullptr; + // If the current version of the LastCommandEvent == savedLastCommandEvent, + // then LastCommandEvent = nullptr; Otherwise, if LastCommandEvent != + // savedLastCommandEvent, then LastCommandEvent is unchanged. + std::atomic LastCommandEventAtomic = LastCommandEvent; + if (LastCommandEventAtomic.compare_exchange_strong(savedLastCommandEvent, + nullptr)) { + LastCommandEvent = LastCommandEventAtomic.load(); + } } // Since all timestamp recordings should have finished with the