Skip to content

Commit 01545c9

Browse files
authored
Merge pull request #103298 from bruvzg/comque_race
Use atomic flag to prevent `flush_if_pending` from reading unlocked `command_mem`.
2 parents a398f4f + bdb5d52 commit 01545c9

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

core/templates/command_queue_mt.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ class CommandQueueMT {
114114
uint32_t sync_awaiters = 0;
115115
WorkerThreadPool::TaskID pump_task_id = WorkerThreadPool::INVALID_TASK_ID;
116116
uint64_t flush_read_ptr = 0;
117+
std::atomic<bool> pending;
117118

118119
template <typename T, typename... Args>
119120
_FORCE_INLINE_ void create_command(Args &&...p_args) {
@@ -126,6 +127,7 @@ class CommandQueueMT {
126127
*(uint64_t *)&command_mem[size] = alloc_size;
127128
void *cmd = &command_mem[size + sizeof(uint64_t)];
128129
new (cmd) T(std::forward<Args>(p_args)...);
130+
pending.store(true);
129131
}
130132

131133
template <typename T, bool NeedsSync, typename... Args>
@@ -186,6 +188,7 @@ class CommandQueueMT {
186188
}
187189

188190
command_mem.clear();
191+
pending.store(false);
189192
flush_read_ptr = 0;
190193

191194
_prevent_sync_wraparound();
@@ -226,7 +229,7 @@ class CommandQueueMT {
226229
}
227230

228231
_FORCE_INLINE_ void flush_if_pending() {
229-
if (unlikely(command_mem.size() > 0)) {
232+
if (unlikely(pending.load())) {
230233
_flush();
231234
}
232235
}

0 commit comments

Comments
 (0)