Skip to content

Commit c9c1de9

Browse files
author
Alexander Batashev
authored
[SYCL] Join thread pool threads on Scheduler destruction (intel#3090)
Threads from default queue thread pool can access scheduler after its destruction has begun. To avoid such illegal access, wait for all threads to join in scheduler destructor.
1 parent 692228c commit c9c1de9

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

sycl/source/detail/queue_impl.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,12 @@ class queue_impl {
366366
return *MHostTaskThreadPool;
367367
}
368368

369+
void stopThreadPool() {
370+
if (MHostTaskThreadPool) {
371+
MHostTaskThreadPool->finishAndWait();
372+
}
373+
}
374+
369375
/// Gets the native handle of the SYCL queue.
370376
///
371377
/// \return a native handle.

sycl/source/detail/scheduler/scheduler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ Scheduler::Scheduler() {
306306
}
307307

308308
Scheduler::~Scheduler() {
309+
DefaultHostQueue->stopThreadPool();
309310
// By specification there are several possible sync points: buffer
310311
// destruction, wait() method of a queue or event. Stream doesn't introduce
311312
// any synchronization point. It is guaranteed that stream is flushed and

sycl/unittests/scheduler/StreamInitDependencyOnHost.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ class MockHandler : public sycl::handler {
2222
static_cast<sycl::handler *>(this)->MCGType = Type;
2323
}
2424

25+
template <typename KernelType, typename ArgType, int Dims,
26+
typename KernelName>
27+
void setHostKernel(KernelType Kernel) {
28+
static_cast<sycl::handler *>(this)->MHostKernel.reset(
29+
new sycl::detail::HostKernel<KernelType, ArgType, Dims, KernelName>(
30+
Kernel));
31+
}
32+
33+
template <int Dims> void setNDRangeDesc(sycl::nd_range<Dims> Range) {
34+
static_cast<sycl::handler *>(this)->MNDRDesc.set(std::move(Range));
35+
}
36+
2537
void addStream(const detail::StreamImplPtr &Stream) {
2638
sycl::handler::addStream(Stream);
2739
}
@@ -87,6 +99,13 @@ TEST_F(SchedulerTest, StreamInitDependencyOnHost) {
8799
MockHandler MockCGH(HQueueImpl, true);
88100
MockCGH.setType(detail::CG::KERNEL);
89101

102+
auto EmptyKernel = [](sycl::nd_item<1>) {};
103+
MockCGH
104+
.setHostKernel<decltype(EmptyKernel), sycl::nd_item<1>, 1, class Empty>(
105+
EmptyKernel);
106+
MockCGH.setNDRangeDesc(
107+
sycl::nd_range<1>{sycl::range<1>{1}, sycl::range<1>{1}});
108+
90109
// Emulating construction of stream object inside command group
91110
detail::StreamImplPtr StreamImpl =
92111
std::make_shared<detail::stream_impl>(1024, 200, MockCGH);

0 commit comments

Comments
 (0)