// Copyright (c) Microsoft Corporation. // Licensed under the MIT license. #include "eventWaitHandle/eventWaitHandle.h" #include "object/refCountedObject.h" #include "queueService.h" #include "taskQueue.h" #include "winrt/Windows.Foundation.h" using namespace winrt; using namespace Windows::Foundation; #include "winrt/Windows.System.h" #include "winrt/Microsoft.UI.Dispatching.h" namespace Mso { namespace { using IInspectable = winrt::Windows::Foundation::IInspectable; // TODO: consider to move it into its own liblet template struct ThreadSafeMap { ThreadSafeMap(std::recursive_mutex &mutex) noexcept : m_mutex{mutex} {} std::optional Get(TKey const &key) noexcept { std::scoped_lock lock{m_mutex}; auto it = m_map.find(key); if (it != m_map.end()) { return std::optional{it->second}; } else { return std::optional{}; } } void Set(TKey const &key, TValue &&value) noexcept { TValue oldValue; // to destroy outside of lock { std::scoped_lock lock{m_mutex}; auto &valueRef = m_map[key]; oldValue = std::move(valueRef); valueRef = std::move(value); } } void Remove(TKey const &key) noexcept { TValue value; // to destroy outside of lock { std::scoped_lock lock{m_mutex}; auto it = m_map.find(key); if (it != m_map.end()) { value = std::move(it->second); m_map.erase(it); } } } private: std::recursive_mutex &m_mutex; std::map> m_map; }; } // namespace template struct UISchedulerWinRT; template struct DispatcherTraits {}; template struct TaskDispatcherHandler; struct WindowsTypeTag; using WindowsTaskDispatcherHandler = TaskDispatcherHandler>; template <> struct DispatcherTraits { using DispatcherQueueHandler = Windows::System::DispatcherQueueHandler; using DispatcherQueue = Windows::System::DispatcherQueue; using DispatcherQueue_ShutdownCompleted_revoker = Windows::System::DispatcherQueue::ShutdownCompleted_revoker; using TaskDispatcherHandler = WindowsTaskDispatcherHandler; }; using WindowsDispatcherTraits = DispatcherTraits; struct MicrosoftTypeTag; using MicrosoftTaskDispatcherHandler = TaskDispatcherHandler>; template <> struct DispatcherTraits { using DispatcherQueueHandler = Microsoft::UI::Dispatching::DispatcherQueueHandler; using DispatcherQueue = Microsoft::UI::Dispatching::DispatcherQueue; using DispatcherQueue_ShutdownCompleted_revoker = Microsoft::UI::Dispatching::DispatcherQueue::ShutdownCompleted_revoker; using TaskDispatcherHandler = MicrosoftTaskDispatcherHandler; }; using MicrosoftDispatcherTraits = DispatcherTraits; //! TaskDispatcherHandler is a DispatcherQueueHandler delegate that we pass to DispatcherQueue. //! We use custom ref counting to avoid extra memory allocations and to handle reference to DispatchTask. template struct TaskDispatcherHandler final : impl::abi_t { TaskDispatcherHandler(UISchedulerWinRT *scheduler) noexcept; int32_t __stdcall QueryInterface(guid const &id, void **result) noexcept final; uint32_t __stdcall AddRef() noexcept final; uint32_t __stdcall Release() noexcept final; int32_t __stdcall Invoke() noexcept final; private: UISchedulerWinRT *m_scheduler; }; using WindowsTaskDispatcherHandler = TaskDispatcherHandler>; template struct UISchedulerWinRT : Mso::UnknownObject { UISchedulerWinRT(typename TDispatcherTraits::DispatcherQueue &&dispatcher) noexcept; ~UISchedulerWinRT() noexcept override; uint32_t AddHandlerRef() noexcept; uint32_t ReleaseHandlerRef() noexcept; typename TDispatcherTraits::DispatcherQueueHandler MakeDispatcherQueueHandler() noexcept; bool TryTakeTask(Mso::CntPtr &queue, DispatchTask &task) noexcept; static DispatchQueue GetOrCreateUIThreadQueue() noexcept; using DispatchQueueRegistry = ThreadSafeMap>; static DispatchQueueRegistry &GetDispatchQueueRegistry() noexcept; public: // IDispatchQueueScheduler void InitializeScheduler(Mso::WeakPtr &&queue) noexcept override; bool HasThreadAccess() noexcept override; bool IsSerial() noexcept override; void Post() noexcept override; void Shutdown() noexcept override; void AwaitTermination() noexcept override; private: friend DispatchQueueStatic; struct CleanupContext { CleanupContext(UISchedulerWinRT *scheduler) noexcept; ~CleanupContext() noexcept; void CheckShutdown() noexcept; void CheckTermination() noexcept; private: UISchedulerWinRT *m_scheduler; bool m_isShutdown{false}; bool m_isTerminated{false}; }; private: typename TDispatcherTraits::DispatcherQueue m_dispatcher{nullptr}; typename TDispatcherTraits::TaskDispatcherHandler m_dispatcherHandler{this}; ManualResetEvent m_terminationEvent; ThreadMutex m_mutex; Mso::WeakPtr m_queue; Mso::CntPtr m_self; uint32_t m_handlerRefCount{0}; uint32_t m_taskCount{0}; bool m_isShutdown{false}; std::thread::id m_threadId{std::this_thread::get_id()}; typename TDispatcherTraits::DispatcherQueue_ShutdownCompleted_revoker m_shutdownCompletedRevoker; }; //============================================================================= // TaskDispatcherHandler implementation //============================================================================= template TaskDispatcherHandler::TaskDispatcherHandler(UISchedulerWinRT *scheduler) noexcept : m_scheduler{scheduler} {} template int32_t __stdcall TaskDispatcherHandler::QueryInterface(guid const &id, void **result) noexcept { if (is_guid_of(id) || is_guid_of(id) || is_guid_of(id)) { *result = static_cast *>(this); AddRef(); return impl::error_ok; } if (is_guid_of(id)) { return make_marshaler(this, result); } *result = nullptr; return impl::error_no_interface; } template uint32_t __stdcall TaskDispatcherHandler::AddRef() noexcept { return m_scheduler->AddHandlerRef(); } template uint32_t __stdcall TaskDispatcherHandler::Release() noexcept { return m_scheduler->ReleaseHandlerRef(); } template int32_t __stdcall TaskDispatcherHandler::Invoke() noexcept { Mso::CntPtr queue; DispatchTask task; if (m_scheduler->TryTakeTask(queue, task)) { queue->InvokeTask(std::move(task), std::chrono::steady_clock::now() + std::chrono::milliseconds(1000 / 60)); } return impl::error_ok; } //============================================================================= // UISchedulerWinRT implementation //============================================================================= template UISchedulerWinRT::UISchedulerWinRT(typename TDispatcherTraits::DispatcherQueue &&dispatcher) noexcept : m_dispatcher{std::move(dispatcher)} { m_shutdownCompletedRevoker = m_dispatcher.ShutdownCompleted( winrt::auto_revoke, [](typename TDispatcherTraits::DispatcherQueue const &, IInspectable const &) noexcept { GetDispatchQueueRegistry().Remove(std::this_thread::get_id()); }); } template UISchedulerWinRT::~UISchedulerWinRT() noexcept { GetDispatchQueueRegistry().Remove(m_threadId); AwaitTermination(); } template uint32_t UISchedulerWinRT::AddHandlerRef() noexcept { std::lock_guard lock{m_mutex}; return ++m_handlerRefCount; } template uint32_t UISchedulerWinRT::ReleaseHandlerRef() noexcept { Mso::CntPtr self; CleanupContext context{this}; { std::lock_guard lock{m_mutex}; VerifyElseCrashSz(m_handlerRefCount, "Ref count cannot be negative"); uint32_t refCount = --m_handlerRefCount; if (refCount == 0) { self = std::move(m_self); // Release pointer to self outside of lock. context.CheckShutdown(); context.CheckTermination(); } return refCount; } } template bool UISchedulerWinRT::TryTakeTask( Mso::CntPtr &queue, DispatchTask &task) noexcept { { std::lock_guard lock{m_mutex}; VerifyElseCrashSz(m_taskCount, "Task count cannot be negative"); --m_taskCount; } queue = m_queue.GetStrongPtr(); if (queue) { return queue->TryDequeTask(task); } return false; } template typename TDispatcherTraits::DispatcherQueueHandler UISchedulerWinRT::MakeDispatcherQueueHandler() noexcept { VerifyElseCrash(m_mutex.IsLockedByMe()); if (m_handlerRefCount == 0) { m_self = this; // Keep reference to self while DispatcherQueue owns DispatcherQueueHandler. } ++m_handlerRefCount; return {static_cast(&m_dispatcherHandler), take_ownership_from_abi}; } template void UISchedulerWinRT::InitializeScheduler(Mso::WeakPtr &&queue) noexcept { m_queue = std::move(queue); } template bool UISchedulerWinRT::HasThreadAccess() noexcept { // m_dispatcher.HasThreadAccess() is implemented only in Windows 19H1. // We must use an alternative implementation. return m_threadId == std::this_thread::get_id(); } template bool UISchedulerWinRT::IsSerial() noexcept { return true; } template void UISchedulerWinRT::Post() noexcept { typename TDispatcherTraits::DispatcherQueueHandler handler; { std::lock_guard lock{m_mutex}; if (!m_isShutdown) { ++m_taskCount; handler = MakeDispatcherQueueHandler(); } } if (handler) { m_dispatcher.TryEnqueue(handler); } } template void UISchedulerWinRT::Shutdown() noexcept { CleanupContext context{this}; { std::lock_guard lock{m_mutex}; m_isShutdown = true; context.CheckTermination(); } } template void UISchedulerWinRT::AwaitTermination() noexcept { Shutdown(); if (m_threadId != std::this_thread::get_id()) { m_terminationEvent.Wait(); } } template /*static*/ DispatchQueue UISchedulerWinRT::GetOrCreateUIThreadQueue() noexcept { std::thread::id threadId{std::this_thread::get_id()}; std::optional> weakQueue = GetDispatchQueueRegistry().Get(threadId); DispatchQueue queue{weakQueue.value_or(nullptr).GetStrongPtr()}; if (queue) { return queue; } decltype(TDispatcherTraits::DispatcherQueue::GetForCurrentThread()) dispatcher{nullptr}; try { dispatcher = TDispatcherTraits::DispatcherQueue::GetForCurrentThread(); } catch (winrt::hresult_error const &) { } if (!dispatcher) { return queue; } queue = DispatchQueue(Mso::Make(Mso::Make(std::move(dispatcher)))); GetDispatchQueueRegistry().Set(threadId, Mso::WeakPtr{*GetRawState(queue)}); return queue; } template /*static*/ typename UISchedulerWinRT::DispatchQueueRegistry & UISchedulerWinRT::GetDispatchQueueRegistry() noexcept { static std::recursive_mutex mutex; static DispatchQueueRegistry registry{mutex}; return registry; } //============================================================================= // UISchedulerWinRT::CleanupContext implementation //============================================================================= template UISchedulerWinRT::CleanupContext::CleanupContext( UISchedulerWinRT *scheduler) noexcept : m_scheduler{scheduler} {} template UISchedulerWinRT::CleanupContext::~CleanupContext() noexcept { if (m_isTerminated) { m_scheduler->m_terminationEvent.Set(); } if (m_isShutdown) { if (auto queue = m_scheduler->m_queue.GetStrongPtr()) { queue->Shutdown(PendingTaskAction::Cancel); } } } template void UISchedulerWinRT::CleanupContext::CheckShutdown() noexcept { // See if dispatcher queue released all handlers without invoking them. if (m_scheduler->m_taskCount != 0 && m_scheduler->m_handlerRefCount == 0) { m_isShutdown = true; m_scheduler->m_taskCount = 0; m_scheduler->m_isShutdown = true; } } template void UISchedulerWinRT::CleanupContext::CheckTermination() noexcept { m_isTerminated = m_scheduler->m_isShutdown && (m_scheduler->m_handlerRefCount == 0); } //============================================================================= // DispatchQueueStatic::GetCurrentUIThreadQueue implementation //============================================================================= DispatchQueue DispatchQueueStatic::GetCurrentUIThreadQueue() noexcept { DispatchQueue queue{nullptr}; queue = UISchedulerWinRT::GetOrCreateUIThreadQueue(); if (!queue) { queue = UISchedulerWinRT::GetOrCreateUIThreadQueue(); } return queue; } } // namespace Mso