#include "pch.h" #include "WaitTimer.h" using Clock = std::chrono::steady_clock; using Deadline = Clock::time_point; using TimerDuration = std::chrono::nanoseconds; namespace { Deadline DeadlineFromDueTime(uint64_t dueTime) noexcept { return Deadline(std::chrono::duration_cast(TimerDuration(dueTime))); } uint64_t DueTimeFromDeadline(Deadline deadline) noexcept { return static_cast( std::chrono::duration_cast(deadline.time_since_epoch()).count()); } // The timer whose callback is currently executing on this thread, if any, // and the threadpool callback instance it is running under. Terminate uses // this to detect a callback that tears down its own timer. thread_local void* t_callbackTimer = nullptr; thread_local PTP_CALLBACK_INSTANCE t_callbackInstance = nullptr; } namespace OS { class WaitTimerImpl { public: WaitTimerImpl() : m_timer(nullptr) {} ~WaitTimerImpl() { Terminate(); } HRESULT Initialize(_In_opt_ void* context, _In_ WaitTimerCallback* callback) { m_context = context; m_callback = callback; m_timer = CreateThreadpoolTimer(WaitCallback, this, nullptr); RETURN_LAST_ERROR_IF_NULL(m_timer); return S_OK; } void Terminate() { if (m_timer != nullptr) { SetThreadpoolTimer(m_timer, nullptr, 0, 0); // A timer callback can tear down its own timer, e.g. when the // task queue port that owns this timer is released for the last // time from inside the callback. WaitForThreadpoolTimerCallbacks // would then wait for the very callback that is calling it and // deadlock. Disassociate the current callback first so we only // wait for other in-flight callbacks; the calling callback must // not touch this object after Terminate returns (WaitCallback // doesn't). if (t_callbackTimer == this) { DisassociateCurrentThreadFromCallback(t_callbackInstance); t_callbackTimer = nullptr; t_callbackInstance = nullptr; } WaitForThreadpoolTimerCallbacks(m_timer, TRUE); CloseThreadpoolTimer(m_timer); m_timer = nullptr; } } void Start(_In_ uint64_t dueTime) { LARGE_INTEGER li; FILETIME ft; // The threadpool timer can run on its existing one-shot mechanism, // but the due time we store in TaskQueue is now a steady-clock // deadline. Convert that deadline back into a relative wait here so // queue correctness no longer depends on wall-clock precision. Deadline now = Clock::now(); Deadline dueDeadline = DeadlineFromDueTime(dueTime); TimerDuration remaining = dueDeadline > now ? std::chrono::duration_cast(dueDeadline - now) : TimerDuration::zero(); constexpr int64_t nanosPerTick = 100; int64_t relativeTicks = (static_cast(remaining.count()) + nanosPerTick - 1) / nanosPerTick; // SetThreadpoolTimer expects negative values for relative waits. // Clamp zero-or-past deadlines to the smallest relative delay so an // already-due timer is queued immediately without switching back to // absolute wall-clock FILETIME semantics. if (relativeTicks <= 0) { relativeTicks = 1; } li.QuadPart = -relativeTicks; ft.dwHighDateTime = li.HighPart; ft.dwLowDateTime = li.LowPart; SetThreadpoolTimer(m_timer, &ft, 0, 0); } void Cancel() { SetThreadpoolTimer(m_timer, nullptr, 0, 0); } private: PTP_TIMER m_timer; void* m_context; WaitTimerCallback* m_callback; static VOID CALLBACK WaitCallback( _Inout_ PTP_CALLBACK_INSTANCE instance, _Inout_opt_ void* context, _Inout_ PTP_TIMER) { if (context != nullptr) { WaitTimerImpl* pthis = static_cast(context); t_callbackTimer = pthis; t_callbackInstance = instance; pthis->m_callback(pthis->m_context); // pthis may have been destroyed by the callback; don't touch it. t_callbackTimer = nullptr; t_callbackInstance = nullptr; } } }; WaitTimer::WaitTimer() noexcept : m_impl(nullptr) {} WaitTimer::~WaitTimer() noexcept { Terminate(); } HRESULT WaitTimer::Initialize(_In_opt_ void* context, _In_ WaitTimerCallback* callback) noexcept { std::lock_guard lock{ m_mutex }; if (m_impl.load() != nullptr || callback == nullptr) { ASSERT(FALSE); return E_UNEXPECTED; } std::unique_ptr timer(new (std::nothrow) WaitTimerImpl); RETURN_IF_NULL_ALLOC(timer.get()); RETURN_IF_FAILED(timer->Initialize(context, callback)); m_impl = timer.release(); return S_OK; } void WaitTimer::Terminate() noexcept { std::unique_ptr timer; { std::lock_guard lock{ m_mutex }; timer.reset(m_impl.exchange(nullptr)); } if (timer != nullptr) { timer->Terminate(); } } void WaitTimer::Start(_In_ uint64_t dueTime) noexcept { std::lock_guard lock{ m_mutex }; if (WaitTimerImpl* timer = m_impl.load()) { timer->Start(dueTime); } } void WaitTimer::Cancel() noexcept { std::lock_guard lock{ m_mutex }; if (WaitTimerImpl* timer = m_impl.load()) { timer->Cancel(); } } uint64_t WaitTimer::GetCurrentTime() noexcept { return DueTimeFromDeadline(Clock::now()); } uint64_t WaitTimer::GetDueTime(_In_ uint32_t msFromNow) noexcept { Deadline deadline = Clock::now() + std::chrono::milliseconds(msFromNow); return DueTimeFromDeadline(deadline); } } // Namespace