EventLoop.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, kleines Filmröllchen <malu.bertsch@gmail.com>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/Badge.h>
  9. #include <LibCore/Event.h>
  10. #include <LibCore/EventLoop.h>
  11. #include <LibCore/EventLoopImplementationUnix.h>
  12. #include <LibCore/Object.h>
  13. #include <LibCore/Promise.h>
  14. #include <LibCore/ThreadEventQueue.h>
  15. namespace Core {
  16. namespace {
  17. thread_local Vector<EventLoop&>* s_event_loop_stack;
  18. Vector<EventLoop&>& event_loop_stack()
  19. {
  20. if (!s_event_loop_stack)
  21. s_event_loop_stack = new Vector<EventLoop&>;
  22. return *s_event_loop_stack;
  23. }
  24. bool has_event_loop()
  25. {
  26. return !event_loop_stack().is_empty();
  27. }
  28. }
  29. Function<NonnullOwnPtr<EventLoopImplementation>()> EventLoop::make_implementation = EventLoopImplementationUnix::create;
  30. EventLoop::EventLoop()
  31. : m_impl(make_implementation())
  32. {
  33. if (event_loop_stack().is_empty()) {
  34. event_loop_stack().append(*this);
  35. }
  36. }
  37. EventLoop::~EventLoop()
  38. {
  39. if (!event_loop_stack().is_empty() && &event_loop_stack().last() == this) {
  40. event_loop_stack().take_last();
  41. }
  42. }
  43. EventLoop& EventLoop::current()
  44. {
  45. return event_loop_stack().last();
  46. }
  47. void EventLoop::quit(int code)
  48. {
  49. m_impl->quit(code);
  50. }
  51. void EventLoop::unquit()
  52. {
  53. m_impl->unquit();
  54. }
  55. struct EventLoopPusher {
  56. public:
  57. EventLoopPusher(EventLoop& event_loop)
  58. {
  59. event_loop_stack().append(event_loop);
  60. }
  61. ~EventLoopPusher()
  62. {
  63. event_loop_stack().take_last();
  64. }
  65. };
  66. int EventLoop::exec()
  67. {
  68. EventLoopPusher pusher(*this);
  69. return m_impl->exec();
  70. }
  71. void EventLoop::spin_until(Function<bool()> goal_condition)
  72. {
  73. EventLoopPusher pusher(*this);
  74. while (!goal_condition())
  75. pump();
  76. }
  77. size_t EventLoop::pump(WaitMode mode)
  78. {
  79. return m_impl->pump(mode == WaitMode::WaitForEvents ? EventLoopImplementation::PumpMode::WaitForEvents : EventLoopImplementation::PumpMode::DontWaitForEvents);
  80. }
  81. void EventLoop::post_event(Object& receiver, NonnullOwnPtr<Event>&& event)
  82. {
  83. m_impl->post_event(receiver, move(event));
  84. }
  85. void EventLoop::add_job(NonnullRefPtr<Promise<NonnullRefPtr<Object>>> job_promise)
  86. {
  87. ThreadEventQueue::current().add_job(move(job_promise));
  88. }
  89. int EventLoop::register_signal(int signal_number, Function<void(int)> handler)
  90. {
  91. if (!has_event_loop())
  92. return 0;
  93. return current().m_impl->register_signal(signal_number, move(handler));
  94. }
  95. void EventLoop::unregister_signal(int handler_id)
  96. {
  97. if (!has_event_loop())
  98. return;
  99. current().m_impl->unregister_signal(handler_id);
  100. }
  101. void EventLoop::notify_forked(ForkEvent)
  102. {
  103. current().m_impl->notify_forked_and_in_child();
  104. }
  105. int EventLoop::register_timer(Object& object, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible fire_when_not_visible)
  106. {
  107. if (!has_event_loop())
  108. return 0;
  109. return current().m_impl->register_timer(object, milliseconds, should_reload, fire_when_not_visible);
  110. }
  111. bool EventLoop::unregister_timer(int timer_id)
  112. {
  113. if (!has_event_loop())
  114. return false;
  115. return current().m_impl->unregister_timer(timer_id);
  116. }
  117. void EventLoop::register_notifier(Badge<Notifier>, Notifier& notifier)
  118. {
  119. if (!has_event_loop())
  120. return;
  121. current().m_impl->register_notifier(notifier);
  122. }
  123. void EventLoop::unregister_notifier(Badge<Notifier>, Notifier& notifier)
  124. {
  125. if (!has_event_loop())
  126. return;
  127. current().m_impl->unregister_notifier(notifier);
  128. }
  129. void EventLoop::wake()
  130. {
  131. m_impl->wake();
  132. }
  133. void EventLoop::deferred_invoke(Function<void()> invokee)
  134. {
  135. auto context = DeferredInvocationContext::construct();
  136. post_event(context, make<Core::DeferredInvocationEvent>(context, move(invokee)));
  137. }
  138. void deferred_invoke(Function<void()> invokee)
  139. {
  140. EventLoop::current().deferred_invoke(move(invokee));
  141. }
  142. bool EventLoop::was_exit_requested() const
  143. {
  144. return m_impl->was_exit_requested();
  145. }
  146. void EventLoop::did_post_event(Badge<Core::ThreadEventQueue>)
  147. {
  148. m_impl->did_post_event();
  149. }
  150. }