Skip to content

Commit b8d5fac

Browse files
Add missing lock in MapOperationsHandler
Signed-off-by: Bartosz Dunajski <[email protected]>
1 parent e24322f commit b8d5fac

File tree

3 files changed

+88
-3
lines changed

3 files changed

+88
-3
lines changed

opencl/source/mem_obj/map_operations_handler.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2018-2021 Intel Corporation
2+
* Copyright (C) 2018-2022 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -62,6 +62,8 @@ bool MapOperationsHandler::find(void *mappedPtr, MapInfo &outMapInfo) {
6262
}
6363

6464
bool NEO::MapOperationsHandler::findInfoForHostPtr(const void *ptr, size_t size, MapInfo &outMapInfo) {
65+
std::lock_guard<std::mutex> lock(mtx);
66+
6567
for (auto &mapInfo : mappedPointers) {
6668
void *ptrStart = mapInfo.ptr;
6769
void *ptrEnd = ptrOffset(mapInfo.ptr, mapInfo.ptrLength);
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#
2-
# Copyright (C) 2018-2021 Intel Corporation
2+
# Copyright (C) 2018-2022 Intel Corporation
33
#
44
# SPDX-License-Identifier: MIT
55
#
66

77
set(IGDRCL_SRCS_mt_tests_helpers
88
# local files
99
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
10-
${CMAKE_CURRENT_SOURCE_DIR}/interlocked_max_mt_tests.cpp
1110
${CMAKE_CURRENT_SOURCE_DIR}/base_object_tests_mt.cpp
11+
${CMAKE_CURRENT_SOURCE_DIR}/interlocked_max_mt_tests.cpp
12+
${CMAKE_CURRENT_SOURCE_DIR}/map_operations_handler_mt_tests.cpp
1213
)
1314
target_sources(igdrcl_mt_tests PRIVATE ${IGDRCL_SRCS_mt_tests_helpers})
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (C) 2018-2022 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#include "shared/test/common/mocks/mock_graphics_allocation.h"
9+
10+
#include "opencl/source/mem_obj/map_operations_handler.h"
11+
12+
#include "gtest/gtest.h"
13+
14+
#include <vector>
15+
16+
namespace NEO {
17+
18+
struct MockMapOperationsHandler : public MapOperationsHandler {
19+
using MapOperationsHandler::isOverlapping;
20+
using MapOperationsHandler::mappedPointers;
21+
};
22+
23+
struct MapOperationsHandlerMtTests : public ::testing::Test {
24+
MockMapOperationsHandler mockHandler;
25+
MockGraphicsAllocation mockAllocation;
26+
std::vector<void *> mappedPtrs;
27+
28+
void SetUp() override {
29+
MemObjSizeArray size = {{10, 10, 10}};
30+
MemObjOffsetArray offset = {{0, 0, 0}};
31+
cl_map_flags mapFlags = CL_MAP_READ;
32+
33+
for (size_t i = 1; i <= 10; i++) {
34+
auto ptr = reinterpret_cast<void *>(0x1000lu * i);
35+
36+
mappedPtrs.push_back(ptr);
37+
EXPECT_TRUE(mockHandler.add(ptr, 10, mapFlags, size, offset, 0, &mockAllocation));
38+
}
39+
}
40+
};
41+
42+
TEST_F(MapOperationsHandlerMtTests, giveMapOperationsHandlerWhenAddingFindingAndRemovingThenExecuteSafely) {
43+
std::atomic<bool> removed = false;
44+
std::atomic<bool> t1Started = false;
45+
std::atomic<bool> t2Started = false;
46+
std::atomic<bool> t3Started = false;
47+
48+
auto find = [&](std::atomic<bool> *threadStarted) {
49+
while (!removed.load()) {
50+
for (auto &ptr : mappedPtrs) {
51+
MapInfo out;
52+
mockHandler.findInfoForHostPtr(ptr, 1, out);
53+
}
54+
55+
threadStarted->store(true);
56+
std::this_thread::yield();
57+
}
58+
};
59+
60+
auto remove = [&]() {
61+
while (!t1Started.load() || !t2Started.load() || !t3Started.load()) {
62+
std::this_thread::yield();
63+
}
64+
65+
for (auto &ptr : mappedPtrs) {
66+
mockHandler.remove(ptr);
67+
}
68+
69+
removed.store(true);
70+
};
71+
72+
std::thread t1(find, &t1Started);
73+
std::thread t2(find, &t2Started);
74+
std::thread t3(find, &t3Started);
75+
std::thread t4(remove);
76+
77+
t1.join();
78+
t2.join();
79+
t3.join();
80+
t4.join();
81+
}
82+
} // namespace NEO

0 commit comments

Comments
 (0)