Skip to content

Commit 8c71dcb

Browse files
[SYCL] Fix circular reference between events and queues (#1226)
There is a potential circular reference between queues and events as a queue can hold a collection of shared pointers to events and events may have a shared pointer to a queue. If a circular reference is created, the only way to currently break the circle is to call wait on the queue, which is not ensured to happen. To mitigate this, this PR changes makes the reference from an event to a queue a weak pointer. Effectively this will mean that if all remaining references to a queue are from events the queue will die. This affects only an events ability to throw asynchronous errors through the async_handler of the queue if it is destroyed prematurely. Signed-off-by: Steffen Larsen <[email protected]>
1 parent dfb4173 commit 8c71dcb

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

sycl/source/detail/event_impl.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ void event_impl::wait_and_throw(
114114
if (Cmd)
115115
Cmd->getQueue()->throw_asynchronous();
116116
}
117-
if (MQueue)
118-
MQueue->throw_asynchronous();
117+
QueueImplPtr Queue = MQueue.lock();
118+
if (Queue)
119+
Queue->throw_asynchronous();
119120
}
120121

121122
template <>

sycl/source/detail/event_impl.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class context_impl;
2525
using ContextImplPtr = std::shared_ptr<cl::sycl::detail::context_impl>;
2626
class queue_impl;
2727
using QueueImplPtr = std::shared_ptr<cl::sycl::detail::queue_impl>;
28+
using QueueImplWPtr = std::weak_ptr<cl::sycl::detail::queue_impl>;
2829

2930
class event_impl {
3031
public:
@@ -147,7 +148,7 @@ class event_impl {
147148
private:
148149
RT::PiEvent MEvent = nullptr;
149150
ContextImplPtr MContext;
150-
QueueImplPtr MQueue;
151+
QueueImplWPtr MQueue;
151152
bool MOpenCLInterop = false;
152153
bool MHostEvent = true;
153154
std::unique_ptr<HostProfilingInfo> MHostProfilingInfo;

0 commit comments

Comments
 (0)