Skip to content

Commit 747fe8a

Browse files
committed
Refactor patch to avoid code duplication
Signed-off-by: Dmitry Vodopyanov <[email protected]>
1 parent cc440f2 commit 747fe8a

File tree

2 files changed

+30
-34
lines changed

2 files changed

+30
-34
lines changed

sycl/source/detail/scheduler/scheduler.cpp

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,7 @@ EventImplPtr Scheduler::addCG(std::unique_ptr<detail::CG> CommandGroup,
6767
const bool IsKernel = CommandGroup->getType() == CG::KERNEL;
6868
{
6969
std::unique_lock<std::shared_timed_mutex> Lock(MGraphLock, std::defer_lock);
70-
#ifdef _WIN32
71-
while (!Lock.owns_lock()) {
72-
Lock.try_lock();
73-
}
74-
#else
75-
Lock.lock();
76-
#endif // _WIN32
70+
lockSharedTimedMutex(Lock);
7771

7872
switch (CommandGroup->getType()) {
7973
case CG::UPDATE_HOST:
@@ -99,13 +93,7 @@ EventImplPtr Scheduler::addCG(std::unique_ptr<detail::CG> CommandGroup,
9993

10094
EventImplPtr Scheduler::addCopyBack(Requirement *Req) {
10195
std::unique_lock<std::shared_timed_mutex> Lock(MGraphLock, std::defer_lock);
102-
#ifdef _WIN32
103-
while (!Lock.owns_lock()) {
104-
Lock.try_lock();
105-
}
106-
#else
107-
Lock.lock();
108-
#endif // _WIN32
96+
lockSharedTimedMutex(Lock);
10997
Command *NewCmd = MGraphBuilder.addCopyBack(Req);
11098
// Command was not creted because there were no operations with
11199
// buffer.
@@ -162,13 +150,7 @@ void Scheduler::cleanupFinishedCommands(EventImplPtr FinishedEvent) {
162150

163151
void Scheduler::removeMemoryObject(detail::SYCLMemObjI *MemObj) {
164152
std::unique_lock<std::shared_timed_mutex> Lock(MGraphLock, std::defer_lock);
165-
#ifdef _WIN32
166-
while (!Lock.owns_lock()) {
167-
Lock.try_lock();
168-
}
169-
#else
170-
Lock.lock();
171-
#endif // _WIN32
153+
lockSharedTimedMutex(Lock);
172154

173155
MemObjRecord *Record = MGraphBuilder.getMemObjRecord(MemObj);
174156
if (!Record)
@@ -183,19 +165,7 @@ void Scheduler::removeMemoryObject(detail::SYCLMemObjI *MemObj) {
183165
EventImplPtr Scheduler::addHostAccessor(Requirement *Req,
184166
const bool destructor) {
185167
std::unique_lock<std::shared_timed_mutex> Lock(MGraphLock, std::defer_lock);
186-
// Avoiding deadlock situation for MSVC. std::shared_timed_mutex specification
187-
// does not specify a priority for shared and exclusive accesses. It will be a
188-
// deadlock in MSVC's std::shared_timed_mutex implementation, if exclusive
189-
// access occurs after shared access.
190-
// TODO: after switching to C++17, change std::shared_timed_mutex to
191-
// std::shared_mutex and use std::lock_guard here both for Windows and Linux.
192-
#ifdef _WIN32
193-
while (!Lock.owns_lock()) {
194-
Lock.try_lock();
195-
}
196-
#else
197-
Lock.lock();
198-
#endif // _WIN32
168+
lockSharedTimedMutex(Lock);
199169

200170
Command *NewCmd = MGraphBuilder.addHostAccessor(Req, destructor);
201171

@@ -231,6 +201,25 @@ Scheduler::Scheduler() {
231201
QueueOrder::Ordered, /*PropList=*/{}));
232202
}
233203

204+
void Scheduler::lockSharedTimedMutex(
205+
std::unique_lock<std::shared_timed_mutex> &Lock) {
206+
#ifdef _WIN32
207+
// Avoiding deadlock situation for MSVC. std::shared_timed_mutex specification
208+
// does not specify a priority for shared and exclusive accesses. It will be a
209+
// deadlock in MSVC's std::shared_timed_mutex implementation, if exclusive
210+
// access occurs after shared access.
211+
// TODO: after switching to C++17, change std::shared_timed_mutex to
212+
// std::shared_mutex and use std::lock_guard here both for Windows and Linux.
213+
while (!Lock.owns_lock()) {
214+
Lock.try_lock();
215+
}
216+
#else
217+
// It is a deadlock on UNIX in implementation of lock and lock_shared, if
218+
// try_lock in the loop above will be executed, so using a single lock here
219+
Lock.lock();
220+
#endif // _WIN32
221+
}
222+
234223
} // namespace detail
235224
} // namespace sycl
236225
} // __SYCL_INLINE_NAMESPACE(cl)

sycl/source/detail/scheduler/scheduler.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,13 @@ class Scheduler {
428428
Scheduler();
429429
static Scheduler instance;
430430

431+
/// Provides exclusive access to std::shared_timed_mutex object with deadlock
432+
/// avoidance
433+
///
434+
/// \param Lock is an instance of std::unique_lock<std::shared_timed_mutex>
435+
/// class
436+
void lockSharedTimedMutex(std::unique_lock<std::shared_timed_mutex> &Lock);
437+
431438
/// Graph builder class.
432439
///
433440
/// The graph builder provides means to change an existing graph (e.g. add

0 commit comments

Comments
 (0)