File tree Expand file tree Collapse file tree 2 files changed +11
-1
lines changed Expand file tree Collapse file tree 2 files changed +11
-1
lines changed Original file line number Diff line number Diff line change @@ -331,12 +331,21 @@ uint64_t device_impl::getCurrentDeviceTime() {
331331
332332 // To account for potential clock drift between host clock and device clock.
333333 // The value set is arbitrary: 200 seconds
334+ std::shared_lock<std::shared_mutex> ReadLock (MDeviceHostBaseTimeMutex);
334335 constexpr uint64_t TimeTillRefresh = 200e9 ;
335336 assert (HostTime >= MDeviceHostBaseTime.second );
336337 uint64_t Diff = HostTime - MDeviceHostBaseTime.second ;
337338
338339 // If getCurrentDeviceTime is called for the first time or we have to refresh.
339340 if (!MDeviceHostBaseTime.second || Diff > TimeTillRefresh) {
341+ ReadLock.unlock ();
342+ std::unique_lock<std::shared_mutex> WriteLock (MDeviceHostBaseTimeMutex);
343+ // Recheck the condition after acquiring the write lock.
344+ if (MDeviceHostBaseTime.second && Diff <= TimeTillRefresh) {
345+ // If we are here, it means that another thread has already updated
346+ // MDeviceHostBaseTime, so we can just return the current device time.
347+ return MDeviceHostBaseTime.first + Diff;
348+ }
340349 const auto &Adapter = getAdapter ();
341350 auto Result = Adapter->call_nocheck <UrApiKind::urDeviceGetGlobalTimestamps>(
342351 MDevice, &MDeviceHostBaseTime.first , &MDeviceHostBaseTime.second );
Original file line number Diff line number Diff line change 1919
2020#include < memory>
2121#include < mutex>
22+ #include < shared_mutex>
2223#include < utility>
2324
2425namespace sycl {
@@ -2203,7 +2204,7 @@ class device_impl : public std::enable_shared_from_this<device_impl> {
22032204 // This is used for getAdapter so should be above other properties.
22042205 std::shared_ptr<platform_impl> MPlatform;
22052206
2206- // TODO: Does this have a race?
2207+ std::shared_mutex MDeviceHostBaseTimeMutex;
22072208 std::pair<uint64_t , uint64_t > MDeviceHostBaseTime{0 , 0 };
22082209
22092210 const ur_device_handle_t MRootDevice;
You can’t perform that action at this time.
0 commit comments