From bdb5d522d137e85200738a9f17fc539c4fc2ed96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pa=CC=84vels=20Nadtoc=CC=8Cajevs?= <7645683+bruvzg@users.noreply.github.com> Date: Tue, 25 Feb 2025 23:26:43 +0200 Subject: [PATCH] Use atomic flag to prevent `flush_if_pending` from reading unlocked `command_mem`. --- core/templates/command_queue_mt.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/templates/command_queue_mt.h b/core/templates/command_queue_mt.h index 5950834804a..576b2ff8928 100644 --- a/core/templates/command_queue_mt.h +++ b/core/templates/command_queue_mt.h @@ -114,6 +114,7 @@ class CommandQueueMT { uint32_t sync_awaiters = 0; WorkerThreadPool::TaskID pump_task_id = WorkerThreadPool::INVALID_TASK_ID; uint64_t flush_read_ptr = 0; + std::atomic pending; template _FORCE_INLINE_ void create_command(Args &&...p_args) { @@ -126,6 +127,7 @@ class CommandQueueMT { *(uint64_t *)&command_mem[size] = alloc_size; void *cmd = &command_mem[size + sizeof(uint64_t)]; new (cmd) T(std::forward(p_args)...); + pending.store(true); } template @@ -186,6 +188,7 @@ class CommandQueueMT { } command_mem.clear(); + pending.store(false); flush_read_ptr = 0; _prevent_sync_wraparound(); @@ -226,7 +229,7 @@ public: } _FORCE_INLINE_ void flush_if_pending() { - if (unlikely(command_mem.size() > 0)) { + if (unlikely(pending.load())) { _flush(); } }