Skip to content

Commit c1cb3cc

Browse files
authored
[SYCLomatic] Using mutex to prevent kernel_function_ptr_map from potential conditional race (#2726)
Signed-off-by: intwanghao <hao3.wang@intel.com>
1 parent caf4c11 commit c1cb3cc

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

clang/runtime/dpct-rt/include/dpct/kernel.hpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "image.hpp"
3030
#include <fstream>
31+
#include <mutex>
3132
#include <random>
3233

3334
namespace dpct {
@@ -493,6 +494,7 @@ class kernel_launcher {
493494
static_cast<sycl::range<3>>(local_range));
494495
_local_mem_size = local_mem_size;
495496
};
497+
static inline std::mutex kernel_function_ptr_map_mutex;
496498

497499
public:
498500
/// Variables for storing execution configuration.
@@ -513,6 +515,7 @@ class kernel_launcher {
513515
const void *func,
514516
std::function<void(dim3, dim3, void **, unsigned int, queue_ptr)>
515517
launcher) {
518+
std::lock_guard<std::mutex> lock(kernel_function_ptr_map_mutex);
516519
kernel_function_ptr_map[func] = std::move(launcher);
517520
}
518521
/// Launches a kernel function with arguments provided directly through
@@ -543,8 +546,13 @@ class kernel_launcher {
543546
/// \param [in] que SYCL queue used to execute kernel.
544547
static void launch(const void *func, dim3 group_range, dim3 local_range,
545548
void **args, unsigned int local_mem_size, queue_ptr que) {
546-
kernel_function_ptr_map[func](group_range, local_range, args,
547-
local_mem_size, que);
549+
std::lock_guard<std::mutex> lock(kernel_function_ptr_map_mutex);
550+
auto Iter = kernel_function_ptr_map.find(func);
551+
if (Iter == kernel_function_ptr_map.end()) {
552+
throw std::runtime_error(
553+
"dpct::launch() : no registered kernel function wrapper found.");
554+
}
555+
(Iter->second)(group_range, local_range, args, local_mem_size, que);
548556
}
549557
/// Launches a kernel function with packed arguments through kernel
550558
/// function wrapper.

0 commit comments

Comments
 (0)