Skip to content

Commit 13ddde5

Browse files
authored
[SYCL] Optimize ZeCache by using std::call_once instead of a lock. (#7882)
This avoids blocking when the value is already computed.
1 parent f71b335 commit 13ddde5

File tree

1 file changed

+2
-7
lines changed
  • sycl/plugins/unified_runtime/ur

1 file changed

+2
-7
lines changed

sycl/plugins/unified_runtime/ur/ur.hpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,13 @@ template <class T> struct ZeCache : private T {
115115
//
116116
using InitFunctionType = std::function<void(T &)>;
117117
InitFunctionType Compute{nullptr};
118-
bool Computed{false};
119-
pi_mutex ZeCacheMutex;
118+
std::once_flag Computed;
120119

121120
ZeCache() : T{} {}
122121

123122
// Access to the fields of the original T data structure.
124123
T *operator->() {
125-
std::unique_lock<pi_mutex> Lock(ZeCacheMutex);
126-
if (!Computed) {
127-
Compute(*this);
128-
Computed = true;
129-
}
124+
std::call_once(Computed, Compute, static_cast<T&>(*this));
130125
return this;
131126
}
132127
};

0 commit comments

Comments
 (0)