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() {
331
331
332
332
// To account for potential clock drift between host clock and device clock.
333
333
// The value set is arbitrary: 200 seconds
334
+ std::shared_lock<std::shared_mutex> ReadLock (MDeviceHostBaseTimeMutex);
334
335
constexpr uint64_t TimeTillRefresh = 200e9 ;
335
336
assert (HostTime >= MDeviceHostBaseTime.second );
336
337
uint64_t Diff = HostTime - MDeviceHostBaseTime.second ;
337
338
338
339
// If getCurrentDeviceTime is called for the first time or we have to refresh.
339
340
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
+ }
340
349
const auto &Adapter = getAdapter ();
341
350
auto Result = Adapter->call_nocheck <UrApiKind::urDeviceGetGlobalTimestamps>(
342
351
MDevice, &MDeviceHostBaseTime.first , &MDeviceHostBaseTime.second );
Original file line number Diff line number Diff line change 19
19
20
20
#include < memory>
21
21
#include < mutex>
22
+ #include < shared_mutex>
22
23
#include < utility>
23
24
24
25
namespace sycl {
@@ -2203,7 +2204,7 @@ class device_impl : public std::enable_shared_from_this<device_impl> {
2203
2204
// This is used for getAdapter so should be above other properties.
2204
2205
std::shared_ptr<platform_impl> MPlatform;
2205
2206
2206
- // TODO: Does this have a race?
2207
+ std::shared_mutex MDeviceHostBaseTimeMutex;
2207
2208
std::pair<uint64_t , uint64_t > MDeviceHostBaseTime{0 , 0 };
2208
2209
2209
2210
const ur_device_handle_t MRootDevice;
You can’t perform that action at this time.
0 commit comments