Skip to content

Commit a877430

Browse files
authored
[NATIVECPU][UR] added mutex to backend queue (#18923)
Before this PR one thread could add new events to the queue while another removes events, both modifying and potentially corrupting NativeCPU queue::events. This PR adds a mutex to the NativeCPU queue handle to prevent this potential corruption. Aims to at least fix: `SYCL/HostInteropTask/host-task-two-queues.cpp`
1 parent daca6a3 commit a877430

File tree

1 file changed

+17
-4
lines changed
  • unified-runtime/source/adapters/native_cpu

1 file changed

+17
-4
lines changed

unified-runtime/source/adapters/native_cpu/queue.hpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,30 @@ struct ur_queue_handle_t_ : RefCounted {
2727

2828
ur_context_handle_t getContext() const { return context; }
2929

30-
void addEvent(ur_event_handle_t event) { events.insert(event); }
30+
void addEvent(ur_event_handle_t event) {
31+
std::lock_guard<std::mutex> lock(mutex);
32+
events.insert(event);
33+
}
3134

32-
void removeEvent(ur_event_handle_t event) { events.erase(event); }
35+
void removeEvent(ur_event_handle_t event) {
36+
std::lock_guard<std::mutex> lock(mutex);
37+
events.erase(event);
38+
}
3339

3440
void finish() {
41+
std::unique_lock<std::mutex> lock(mutex);
3542
while (!events.empty()) {
3643
auto ev = *events.begin();
3744
// ur_event_handle_t_::wait removes itself from the events set in the
38-
// queue
45+
// queue.
46+
ev->incrementReferenceCount();
47+
// Unlocking mutex for removeEvent and for event callbacks that may need
48+
// to acquire it.
49+
lock.unlock();
3950
ev->wait();
51+
decrementOrDelete(ev);
52+
lock.lock();
4053
}
41-
events.clear();
4254
}
4355

4456
~ur_queue_handle_t_() { finish(); }
@@ -58,4 +70,5 @@ struct ur_queue_handle_t_ : RefCounted {
5870
std::set<ur_event_handle_t> events;
5971
const bool inOrder;
6072
const bool profilingEnabled;
73+
std::mutex mutex;
6174
};

0 commit comments

Comments
 (0)