Skip to content

Commit 3faeb77

Browse files
authored
[SYCL] Avoid overuse of CPU on wait read-write lock loop (#2525)
The while loop in lockSharedTimedMutex causes 100% usage of CPU if host application generates command groups in multiple threads That slow downs overall execution.
1 parent 15e62c2 commit 3faeb77

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

sycl/source/detail/scheduler/scheduler.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
#include <detail/scheduler/scheduler.hpp>
1313
#include <detail/stream_impl.hpp>
1414

15+
#include <chrono>
1516
#include <memory>
1617
#include <mutex>
1718
#include <set>
19+
#include <thread>
1820
#include <vector>
1921

2022
__SYCL_INLINE_NAMESPACE(cl) {
@@ -268,7 +270,11 @@ void Scheduler::lockSharedTimedMutex(
268270
// TODO: after switching to C++17, change std::shared_timed_mutex to
269271
// std::shared_mutex and use std::lock_guard here both for Windows and Linux.
270272
while (!Lock.owns_lock()) {
271-
Lock.try_lock();
273+
Lock.try_lock_for(std::chrono::milliseconds(10));
274+
// Without yield while loop acts like endless while loop and occupies the
275+
// whole CPU when multiple command groups are created in multiple host
276+
// threads
277+
std::this_thread::yield();
272278
}
273279
#else
274280
// It is a deadlock on UNIX in implementation of lock and lock_shared, if

0 commit comments

Comments
 (0)