Skip to content

Commit 7cfdb22

Browse files
committed
Make the getHandle api thread safe
1 parent 78eb375 commit 7cfdb22

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

source/loader/layers/sanitizer/asan_buffer.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ ur_result_t MemBuffer::getHandle(ur_device_handle_t Device, char *&Handle) {
7575
return UR_RESULT_SUCCESS;
7676
}
7777

78+
std::scoped_lock<ur_shared_mutex> Guard(Mutex);
7879
auto &Allocation = Allocations[Device];
7980
ur_result_t URes = UR_RESULT_SUCCESS;
8081
if (!Allocation) {
@@ -104,9 +105,16 @@ ur_result_t MemBuffer::getHandle(ur_device_handle_t Device, char *&Handle) {
104105
}
105106
}
106107

108+
Handle = Allocation;
109+
110+
if (!LastSyncedDevice.hDevice) {
111+
LastSyncedDevice = MemBuffer::Device_t{Device, Handle};
112+
return URes;
113+
}
114+
107115
// If the device required to allocate memory is not the previous one, we
108116
// need to do data migration.
109-
if (Device != LastSyncedDevice && LastSyncedDevice != nullptr) {
117+
if (Device != LastSyncedDevice.hDevice) {
110118
auto &HostAllocation = Allocations[nullptr];
111119
if (!HostAllocation) {
112120
ur_usm_desc_t USMDesc{};
@@ -125,11 +133,10 @@ ur_result_t MemBuffer::getHandle(ur_device_handle_t Device, char *&Handle) {
125133

126134
// Copy data from last synced device to host
127135
{
128-
ManagedQueue Queue(Context, LastSyncedDevice);
129-
char *Handle;
130-
UR_CALL(getHandle(LastSyncedDevice, Handle));
136+
ManagedQueue Queue(Context, LastSyncedDevice.hDevice);
131137
URes = getContext()->urDdiTable.Enqueue.pfnUSMMemcpy(
132-
Queue, true, HostAllocation, Handle, Size, 0, nullptr, nullptr);
138+
Queue, true, HostAllocation, LastSyncedDevice.MemHandle, Size,
139+
0, nullptr, nullptr);
133140
if (URes != UR_RESULT_SUCCESS) {
134141
getContext()->logger.error(
135142
"Failed to migrate memory buffer data");
@@ -151,8 +158,7 @@ ur_result_t MemBuffer::getHandle(ur_device_handle_t Device, char *&Handle) {
151158
}
152159
}
153160

154-
LastSyncedDevice = Device;
155-
Handle = Allocation;
161+
LastSyncedDevice = MemBuffer::Device_t{Device, Handle};
156162

157163
return URes;
158164
}

source/loader/layers/sanitizer/asan_buffer.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ struct MemBuffer {
4848

4949
ur_context_handle_t Context;
5050

51-
ur_device_handle_t LastSyncedDevice{};
51+
struct Device_t {
52+
ur_device_handle_t hDevice;
53+
char *MemHandle;
54+
};
55+
Device_t LastSyncedDevice{};
5256

5357
size_t Size;
5458

0 commit comments

Comments
 (0)