Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SYCL] Avoid overuse of CPU on wait read-write lock loop #2525

Merged
merged 3 commits into from
Sep 24, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions sycl/source/detail/scheduler/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
#include <detail/scheduler/scheduler.hpp>
#include <detail/stream_impl.hpp>

#include <chrono>
#include <memory>
#include <mutex>
#include <set>
#include <thread>
#include <vector>

__SYCL_INLINE_NAMESPACE(cl) {
Expand Down Expand Up @@ -153,11 +155,9 @@ void Scheduler::waitForEvent(EventImplPtr Event) {
}

void Scheduler::cleanupFinishedCommands(EventImplPtr FinishedEvent) {
// Avoiding deadlock situation, where one thread is in the process of
// enqueueing (with a locked mutex) a currently blocked task that waits for
// another thread which is stuck at attempting cleanup.
std::unique_lock<std::shared_timed_mutex> Lock(MGraphLock, std::try_to_lock);
if (Lock.owns_lock()) {
std::unique_lock<std::shared_timed_mutex> Lock(MGraphLock, std::defer_lock);
{
lockSharedTimedMutex(Lock);
Command *FinishedCmd = static_cast<Command *>(FinishedEvent->getCommand());
// The command might have been cleaned up (and set to nullptr) by another
// thread
Expand Down Expand Up @@ -268,7 +268,8 @@ void Scheduler::lockSharedTimedMutex(
// TODO: after switching to C++17, change std::shared_timed_mutex to
// std::shared_mutex and use std::lock_guard here both for Windows and Linux.
while (!Lock.owns_lock()) {
Lock.try_lock();
Lock.try_lock_for(std::chrono::milliseconds(10));
std::this_thread::yield();
}
#else
// It is a deadlock on UNIX in implementation of lock and lock_shared, if
Expand Down