File tree Expand file tree Collapse file tree 3 files changed +44
-2
lines changed Expand file tree Collapse file tree 3 files changed +44
-2
lines changed Original file line number Diff line number Diff line change @@ -228,8 +228,11 @@ void SYCLMemObjT::detachMemoryObject(
228
228
229
229
void SYCLMemObjT::handleWriteAccessorCreation () {
230
230
const auto InitialUserPtr = MUserPtr;
231
- MCreateShadowCopy ();
232
- MCreateShadowCopy = []() -> void {};
231
+ {
232
+ std::lock_guard<std::mutex> Lock (MCreateShadowCopyMtx);
233
+ MCreateShadowCopy ();
234
+ MCreateShadowCopy = []() -> void {};
235
+ }
233
236
if (MRecord != nullptr && MUserPtr != InitialUserPtr) {
234
237
for (auto &it : MRecord->MAllocaCommands ) {
235
238
if (it->MMemAllocation == InitialUserPtr) {
Original file line number Diff line number Diff line change 23
23
#include < atomic>
24
24
#include < cstring>
25
25
#include < memory>
26
+ #include < mutex>
26
27
#include < type_traits>
27
28
28
29
namespace sycl {
@@ -196,6 +197,7 @@ class SYCLMemObjT : public SYCLMemObjI {
196
197
MUserPtr = HostPtr;
197
198
} else if (canReadHostPtr (HostPtr, RequiredAlign)) {
198
199
MUserPtr = HostPtr;
200
+ std::lock_guard<std::mutex> Lock (MCreateShadowCopyMtx);
199
201
MCreateShadowCopy = [this , RequiredAlign, HostPtr]() -> void {
200
202
setAlign (RequiredAlign);
201
203
MShadowCopy = allocateHostMem ();
@@ -229,6 +231,7 @@ class SYCLMemObjT : public SYCLMemObjI {
229
231
MUserPtr = HostPtr.get ();
230
232
} else if (canReadHostPtr (HostPtr.get (), RequiredAlign)) {
231
233
MUserPtr = HostPtr.get ();
234
+ std::lock_guard<std::mutex> Lock (MCreateShadowCopyMtx);
232
235
MCreateShadowCopy = [this , RequiredAlign, HostPtr]() -> void {
233
236
setAlign (RequiredAlign);
234
237
MShadowCopy = allocateHostMem ();
@@ -375,6 +378,7 @@ class SYCLMemObjT : public SYCLMemObjI {
375
378
// defer the memory allocation and copying to the point where a writable
376
379
// accessor is created.
377
380
std::function<void (void )> MCreateShadowCopy = []() -> void {};
381
+ std::mutex MCreateShadowCopyMtx;
378
382
bool MOwnNativeHandle = true ;
379
383
};
380
384
} // namespace detail
Original file line number Diff line number Diff line change
1
+ // RUN: %{build} -o %t.out
2
+ // RUN: %{run} %t.out
3
+ #include < sycl/detail/core.hpp>
4
+
5
+ #include < cassert>
6
+ #include < thread>
7
+ #include < vector>
8
+
9
+ constexpr int NThreads = 8 ;
10
+
11
+ class KernelA ;
12
+
13
+ void threadFunction (sycl::buffer<int , 1 > &Buf) {
14
+ sycl::queue Q;
15
+ Q.submit ([&](sycl::handler &Cgh) {
16
+ auto Acc = Buf.get_access <sycl::access::mode::read_write>(Cgh);
17
+ Cgh.single_task <class KernelA >([=]() { Acc[0 ] += 1 ; });
18
+ });
19
+ }
20
+ int main () {
21
+ std::vector<std::thread> Threads;
22
+ Threads.reserve (NThreads);
23
+
24
+ int Val = 0 ;
25
+ {
26
+ sycl::buffer<int , 1 > Buf (&Val, sycl::range<1 >(1 ));
27
+ sycl::queue Q;
28
+
29
+ for (int I = 0 ; I < NThreads; ++I)
30
+ Threads.emplace_back (threadFunction, std::ref (Buf));
31
+ for (auto &t : Threads)
32
+ t.join ();
33
+ }
34
+ assert (Val == NThreads);
35
+ }
You can’t perform that action at this time.
0 commit comments