Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 4 additions & 7 deletions sycl/source/detail/queue_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,10 @@ event queue_impl::memcpyFromDeviceGlobal(
}

sycl::detail::optional<event> queue_impl::getLastEvent() {
{
// The external event is required to finish last if set, so it is considered
// the last event if present.
std::lock_guard<std::mutex> Lock(MInOrderExternalEventMtx);
if (MInOrderExternalEvent)
return *MInOrderExternalEvent;
}
// The external event is required to finish last if set, so it is considered
// the last event if present.
if (std::optional<event> ExternalEvent = MInOrderExternalEvent.read())
return ExternalEvent;

std::lock_guard<std::mutex> Lock{MMutex};
if (MGraph.expired() && !MDefaultGraphDeps.LastEventPtr)
Expand Down
47 changes: 40 additions & 7 deletions sycl/source/detail/queue_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -710,14 +710,18 @@ class queue_impl {
void *getTraceEvent() { return MTraceEvent; }

void setExternalEvent(const event &Event) {
std::lock_guard<std::mutex> Lock(MInOrderExternalEventMtx);
MInOrderExternalEvent = Event;
MInOrderExternalEvent.set([&](std::optional<event> &InOrderExternalEvent) {
InOrderExternalEvent = Event;
});
}

std::optional<event> popExternalEvent() {
std::lock_guard<std::mutex> Lock(MInOrderExternalEventMtx);
std::optional<event> Result = std::nullopt;
std::swap(Result, MInOrderExternalEvent);

MInOrderExternalEvent.unset(
[&](std::optional<event> &InOrderExternalEvent) {
std::swap(Result, InOrderExternalEvent);
});
return Result;
}

Expand Down Expand Up @@ -1025,6 +1029,36 @@ class queue_impl {
}
} MDefaultGraphDeps, MExtGraphDeps;

// Implement check-lock-check pattern to not lock empty MData as the locks
// come with runtime overhead.
template <typename DataType> class CheckLockCheck {
DataType MData;
std::atomic_bool MIsSet = false;
mutable std::mutex MDataMtx;

public:
template <typename F> void set(F &&func) {
std::lock_guard<std::mutex> Lock(MDataMtx);
MIsSet.store(true, std::memory_order_release);
std::forward<F>(func)(MData);
}
template <typename F> void unset(F &&func) {
if (MIsSet.load(std::memory_order_acquire)) {
std::lock_guard<std::mutex> Lock(MDataMtx);
if (MIsSet.load(std::memory_order_acquire)) {
std::forward<F>(func)(MData);
MIsSet.store(false, std::memory_order_release);
}
}
}
DataType read() {
if (!MIsSet.load(std::memory_order_acquire))
return DataType{};
std::lock_guard<std::mutex> Lock(MDataMtx);
return MData;
}
};

const bool MIsInorder;

std::vector<EventImplPtr> MStreamsServiceEvents;
Expand All @@ -1045,10 +1079,9 @@ class queue_impl {

// This event can be optionally provided by users for in-order queues to add
// an additional dependency for the subsequent submission in to the queue.
// Access to the event should be guarded with MInOrderExternalEventMtx.
// Access to the event should be guarded with mutex.
// NOTE: std::optional must not be exposed in the ABI.
std::optional<event> MInOrderExternalEvent;
mutable std::mutex MInOrderExternalEventMtx;
CheckLockCheck<std::optional<event>> MInOrderExternalEvent;

public:
// Queue constructed with the discard_events property
Expand Down