Skip to content

Commit f0794e9

Browse files
Address comments
Signed-off-by: Sergey Semenov <sergey.semenov@intel.com>
1 parent d07e3b4 commit f0794e9

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

sycl/source/detail/program_impl.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,34 @@ program_impl::program_impl(
3939
throw runtime_error("Non-empty vector of programs expected",
4040
PI_INVALID_VALUE);
4141
}
42+
43+
// Sort the programs to avoid deadlocks due to locking multiple mutexes &
44+
// verify that all programs are unique.
45+
std::sort(ProgramList.begin(), ProgramList.end(),
46+
[](const shared_ptr_class<program_impl> &A,
47+
const shared_ptr_class<program_impl> &B) {
48+
return A.get() < B.get();
49+
});
50+
auto It = std::unique(ProgramList.begin(), ProgramList.end(),
51+
[](const shared_ptr_class<program_impl> &A,
52+
const shared_ptr_class<program_impl> &B) {
53+
return A.get() == B.get();
54+
});
55+
if (It != ProgramList.end()) {
56+
throw runtime_error("Attempting to link a program with itself",
57+
PI_INVALID_PROGRAM);
58+
}
59+
4260
MContext = ProgramList[0]->MContext;
4361
MDevices = ProgramList[0]->MDevices;
4462
vector_class<device> DevicesSorted;
4563
if (!is_host()) {
4664
DevicesSorted = sort_devices_by_cl_device_id(MDevices);
4765
}
4866
check_device_feature_support<info::device::is_linker_available>(MDevices);
49-
vector_class<std::unique_lock<std::mutex>> Locks;
67+
vector_class<unique_ptr_class<std::lock_guard<std::mutex>>> Locks;
5068
for (const auto &Prg : ProgramList) {
51-
Locks.emplace_back(Prg->MMutex);
69+
Locks.emplace_back(new std::lock_guard<std::mutex>(Prg->MMutex));
5270
Prg->throw_if_state_is_not(program_state::compiled);
5371
if (Prg->MContext != MContext) {
5472
throw invalid_object_error(
@@ -187,7 +205,7 @@ cl_program program_impl::get() const {
187205
void program_impl::compile_with_kernel_name(string_class KernelName,
188206
string_class CompileOptions,
189207
OSModuleHandle M) {
190-
std::unique_lock<std::mutex> Lock(MMutex);
208+
std::lock_guard<std::mutex> Lock(MMutex);
191209
throw_if_state_is_not(program_state::none);
192210
MProgramModuleHandle = M;
193211
if (!is_host()) {
@@ -199,7 +217,7 @@ void program_impl::compile_with_kernel_name(string_class KernelName,
199217

200218
void program_impl::compile_with_source(string_class KernelSource,
201219
string_class CompileOptions) {
202-
std::unique_lock<std::mutex> Lock(MMutex);
220+
std::lock_guard<std::mutex> Lock(MMutex);
203221
throw_if_state_is_not(program_state::none);
204222
// TODO should it throw if it's host?
205223
if (!is_host()) {
@@ -212,7 +230,7 @@ void program_impl::compile_with_source(string_class KernelSource,
212230
void program_impl::build_with_kernel_name(string_class KernelName,
213231
string_class BuildOptions,
214232
OSModuleHandle Module) {
215-
std::unique_lock<std::mutex> Lock(MMutex);
233+
std::lock_guard<std::mutex> Lock(MMutex);
216234
throw_if_state_is_not(program_state::none);
217235
MProgramModuleHandle = Module;
218236
if (!is_host()) {
@@ -233,7 +251,7 @@ void program_impl::build_with_kernel_name(string_class KernelName,
233251

234252
void program_impl::build_with_source(string_class KernelSource,
235253
string_class BuildOptions) {
236-
std::unique_lock<std::mutex> Lock(MMutex);
254+
std::lock_guard<std::mutex> Lock(MMutex);
237255
throw_if_state_is_not(program_state::none);
238256
// TODO should it throw if it's host?
239257
if (!is_host()) {
@@ -244,7 +262,7 @@ void program_impl::build_with_source(string_class KernelSource,
244262
}
245263

246264
void program_impl::link(string_class LinkOptions) {
247-
std::unique_lock<std::mutex> Lock(MMutex);
265+
std::lock_guard<std::mutex> Lock(MMutex);
248266
throw_if_state_is_not(program_state::compiled);
249267
if (!is_host()) {
250268
check_device_feature_support<info::device::is_linker_available>(MDevices);

0 commit comments

Comments
 (0)