EventLoopImplementationWindows.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2024, stasoid <stasoid@yahoo.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibCore/EventLoopImplementationWindows.h>
  8. #include <LibCore/Notifier.h>
  9. #include <LibCore/System.h>
  10. #include <LibCore/ThreadEventQueue.h>
  11. #include <AK/Windows.h>
  12. struct Handle {
  13. HANDLE handle = NULL;
  14. explicit Handle(HANDLE h = NULL)
  15. : handle(h)
  16. {
  17. }
  18. Handle(Handle&& h)
  19. {
  20. handle = h.handle;
  21. h.handle = NULL;
  22. }
  23. void operator=(Handle&& h)
  24. {
  25. VERIFY(!handle);
  26. handle = h.handle;
  27. h.handle = NULL;
  28. }
  29. ~Handle()
  30. {
  31. if (handle)
  32. CloseHandle(handle);
  33. }
  34. bool operator==(Handle const& h) const { return handle == h.handle; }
  35. bool operator==(HANDLE h) const { return handle == h; }
  36. };
  37. template<>
  38. struct Traits<Handle> : DefaultTraits<Handle> {
  39. static unsigned hash(Handle const& h) { return Traits<HANDLE>::hash(h.handle); }
  40. };
  41. template<>
  42. constexpr bool IsHashCompatible<HANDLE, Handle> = true;
  43. namespace Core {
  44. struct EventLoopTimer {
  45. WeakPtr<EventReceiver> owner;
  46. TimerShouldFireWhenNotVisible fire_when_not_visible = TimerShouldFireWhenNotVisible::No;
  47. };
  48. struct ThreadData {
  49. static ThreadData& the()
  50. {
  51. thread_local OwnPtr<ThreadData> thread_data = make<ThreadData>();
  52. return *thread_data;
  53. }
  54. ThreadData()
  55. {
  56. wake_event.handle = CreateEvent(NULL, FALSE, FALSE, NULL);
  57. VERIFY(wake_event.handle);
  58. }
  59. // Each thread has its own timers, notifiers and a wake event.
  60. HashMap<Handle, EventLoopTimer> timers;
  61. HashMap<Handle, Notifier*> notifiers;
  62. // The wake event is used to notify another event loop that someone has called wake().
  63. Handle wake_event;
  64. };
  65. EventLoopImplementationWindows::EventLoopImplementationWindows()
  66. : m_wake_event(ThreadData::the().wake_event.handle)
  67. {
  68. }
  69. int EventLoopImplementationWindows::exec()
  70. {
  71. for (;;) {
  72. if (m_exit_requested)
  73. return m_exit_code;
  74. pump(PumpMode::WaitForEvents);
  75. }
  76. VERIFY_NOT_REACHED();
  77. }
  78. size_t EventLoopImplementationWindows::pump(PumpMode)
  79. {
  80. auto& thread_data = ThreadData::the();
  81. auto& notifiers = thread_data.notifiers;
  82. auto& timers = thread_data.timers;
  83. size_t event_count = 1 + notifiers.size() + timers.size();
  84. // If 64 events limit proves to be insufficient RegisterWaitForSingleObject or other methods
  85. // can be used instead as mentioned in https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitformultipleobjects
  86. // TODO: investigate if event_count can realistically exceed 64
  87. VERIFY(event_count <= MAXIMUM_WAIT_OBJECTS);
  88. HANDLE event_handles[event_count];
  89. event_handles[0] = thread_data.wake_event.handle;
  90. size_t index = 1;
  91. for (auto& entry : notifiers)
  92. event_handles[index++] = entry.key.handle;
  93. for (auto& entry : timers)
  94. event_handles[index++] = entry.key.handle;
  95. DWORD result = WaitForMultipleObjects(event_count, event_handles, FALSE, INFINITE);
  96. index = result - WAIT_OBJECT_0;
  97. VERIFY(index < event_count);
  98. if (index != 0) {
  99. if (index <= notifiers.size()) {
  100. Notifier* notifier = *notifiers.get(event_handles[index]);
  101. ThreadEventQueue::current().post_event(*notifier, make<NotifierActivationEvent>(notifier->fd(), notifier->type()));
  102. } else {
  103. auto& timer = *timers.get(event_handles[index]);
  104. if (auto strong_owner = timer.owner.strong_ref())
  105. if (timer.fire_when_not_visible == TimerShouldFireWhenNotVisible::Yes || strong_owner->is_visible_for_timer_purposes())
  106. ThreadEventQueue::current().post_event(*strong_owner, make<TimerEvent>());
  107. }
  108. }
  109. return ThreadEventQueue::current().process();
  110. }
  111. void EventLoopImplementationWindows::quit(int code)
  112. {
  113. m_exit_requested = true;
  114. m_exit_code = code;
  115. }
  116. void EventLoopImplementationWindows::unquit()
  117. {
  118. m_exit_requested = false;
  119. m_exit_code = 0;
  120. }
  121. bool EventLoopImplementationWindows::was_exit_requested() const
  122. {
  123. return m_exit_requested;
  124. }
  125. void EventLoopImplementationWindows::post_event(EventReceiver& receiver, NonnullOwnPtr<Event>&& event)
  126. {
  127. m_thread_event_queue.post_event(receiver, move(event));
  128. if (&m_thread_event_queue != &ThreadEventQueue::current())
  129. wake();
  130. }
  131. void EventLoopImplementationWindows::wake()
  132. {
  133. SetEvent(m_wake_event);
  134. }
  135. void EventLoopImplementationWindows::notify_forked_and_in_child()
  136. {
  137. dbgln("Core::EventLoopManagerWindows::notify_forked_and_in_child() is not implemented");
  138. VERIFY_NOT_REACHED();
  139. }
  140. static int notifier_type_to_network_event(NotificationType type)
  141. {
  142. switch (type) {
  143. case NotificationType::Read:
  144. return FD_READ;
  145. case NotificationType::Write:
  146. return FD_WRITE;
  147. default:
  148. dbgln("This notification type is not implemented: {}", (int)type);
  149. VERIFY_NOT_REACHED();
  150. }
  151. }
  152. void EventLoopManagerWindows::register_notifier(Notifier& notifier)
  153. {
  154. HANDLE event = CreateEvent(NULL, FALSE, FALSE, NULL);
  155. VERIFY(event);
  156. SOCKET socket = (SOCKET)System::fd_to_handle(notifier.fd());
  157. VERIFY(socket != INVALID_SOCKET);
  158. int rc = WSAEventSelect(socket, event, notifier_type_to_network_event(notifier.type()));
  159. VERIFY(rc == 0);
  160. auto& notifiers = ThreadData::the().notifiers;
  161. VERIFY(!notifiers.get(event).has_value());
  162. notifiers.set(Handle(event), &notifier);
  163. }
  164. void EventLoopManagerWindows::unregister_notifier(Notifier& notifier)
  165. {
  166. // remove_first_matching would be clearer, but currently there is no such method in HashMap
  167. ThreadData::the().notifiers.remove_all_matching([&](auto&, auto value) { return value == &notifier; });
  168. }
  169. intptr_t EventLoopManagerWindows::register_timer(EventReceiver& object, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible fire_when_not_visible)
  170. {
  171. VERIFY(milliseconds >= 0);
  172. HANDLE timer = CreateWaitableTimer(NULL, FALSE, NULL);
  173. VERIFY(timer);
  174. LARGE_INTEGER first_time = {};
  175. // Measured in 0.1μs intervals, negative means starting from now
  176. first_time.QuadPart = -10'000 * milliseconds;
  177. BOOL rc = SetWaitableTimer(timer, &first_time, should_reload ? milliseconds : 0, NULL, NULL, FALSE);
  178. VERIFY(rc);
  179. auto& timers = ThreadData::the().timers;
  180. VERIFY(!timers.get(timer).has_value());
  181. timers.set(Handle(timer), { object, fire_when_not_visible });
  182. return (intptr_t)timer;
  183. }
  184. void EventLoopManagerWindows::unregister_timer(intptr_t timer_id)
  185. {
  186. ThreadData::the().timers.remove((HANDLE)timer_id);
  187. }
  188. int EventLoopManagerWindows::register_signal(int signal_number, Function<void(int)> handler)
  189. {
  190. dbgln("Core::EventLoopManagerWindows::register_signal() is not implemented");
  191. VERIFY_NOT_REACHED();
  192. }
  193. void EventLoopManagerWindows::unregister_signal(int handler_id)
  194. {
  195. dbgln("Core::EventLoopManagerWindows::unregister_signal() is not implemented");
  196. VERIFY_NOT_REACHED();
  197. }
  198. void EventLoopManagerWindows::did_post_event()
  199. {
  200. }
  201. NonnullOwnPtr<EventLoopImplementation> EventLoopManagerWindows::make_implementation()
  202. {
  203. return make<EventLoopImplementationWindows>();
  204. }
  205. }