EventLoop.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. EventLoop::EventLoop()
  30. : m_impl(make<EventLoopImplementationUnix>())
  31. {
  32. if (event_loop_stack().is_empty()) {
  33. event_loop_stack().append(*this);
  34. }
  35. }
  36. EventLoop::~EventLoop()
  37. {
  38. if (!event_loop_stack().is_empty() && &event_loop_stack().last() == this) {
  39. event_loop_stack().take_last();
  40. }
  41. }
  42. EventLoop& EventLoop::current()
  43. {
  44. return event_loop_stack().last();
  45. }
  46. void EventLoop::quit(int code)
  47. {
  48. m_impl->quit(code);
  49. }
  50. void EventLoop::unquit()
  51. {
  52. m_impl->unquit();
  53. }
  54. struct EventLoopPusher {
  55. public:
  56. EventLoopPusher(EventLoop& event_loop)
  57. {
  58. event_loop_stack().append(event_loop);
  59. }
  60. ~EventLoopPusher()
  61. {
  62. event_loop_stack().take_last();
  63. }
  64. };
  65. int EventLoop::exec()
  66. {
  67. EventLoopPusher pusher(*this);
  68. return m_impl->exec();
  69. }
  70. void EventLoop::spin_until(Function<bool()> goal_condition)
  71. {
  72. EventLoopPusher pusher(*this);
  73. while (!goal_condition())
  74. pump();
  75. }
  76. size_t EventLoop::pump(WaitMode mode)
  77. {
  78. return m_impl->pump(mode == WaitMode::WaitForEvents ? EventLoopImplementation::PumpMode::WaitForEvents : EventLoopImplementation::PumpMode::DontWaitForEvents);
  79. }
  80. void EventLoop::post_event(Object& receiver, NonnullOwnPtr<Event>&& event)
  81. {
  82. m_impl->post_event(receiver, move(event));
  83. }
  84. void EventLoop::add_job(NonnullRefPtr<Promise<NonnullRefPtr<Object>>> job_promise)
  85. {
  86. ThreadEventQueue::current().add_job(move(job_promise));
  87. }
  88. int EventLoop::register_signal(int signal_number, Function<void(int)> handler)
  89. {
  90. if (!has_event_loop())
  91. return 0;
  92. return current().m_impl->register_signal(signal_number, move(handler));
  93. }
  94. void EventLoop::unregister_signal(int handler_id)
  95. {
  96. if (!has_event_loop())
  97. return;
  98. current().m_impl->unregister_signal(handler_id);
  99. }
  100. void EventLoop::notify_forked(ForkEvent)
  101. {
  102. current().m_impl->notify_forked_and_in_child();
  103. }
  104. int EventLoop::register_timer(Object& object, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible fire_when_not_visible)
  105. {
  106. if (!has_event_loop())
  107. return 0;
  108. return current().m_impl->register_timer(object, milliseconds, should_reload, fire_when_not_visible);
  109. }
  110. bool EventLoop::unregister_timer(int timer_id)
  111. {
  112. if (!has_event_loop())
  113. return false;
  114. return current().m_impl->unregister_timer(timer_id);
  115. }
  116. void EventLoop::register_notifier(Badge<Notifier>, Notifier& notifier)
  117. {
  118. if (!has_event_loop())
  119. return;
  120. current().m_impl->register_notifier(notifier);
  121. }
  122. void EventLoop::unregister_notifier(Badge<Notifier>, Notifier& notifier)
  123. {
  124. if (!has_event_loop())
  125. return;
  126. current().m_impl->unregister_notifier(notifier);
  127. }
  128. void EventLoop::wake()
  129. {
  130. m_impl->wake();
  131. }
  132. void EventLoop::deferred_invoke(Function<void()> invokee)
  133. {
  134. auto context = DeferredInvocationContext::construct();
  135. post_event(context, make<Core::DeferredInvocationEvent>(context, move(invokee)));
  136. }
  137. void deferred_invoke(Function<void()> invokee)
  138. {
  139. EventLoop::current().deferred_invoke(move(invokee));
  140. }
  141. bool EventLoop::was_exit_requested() const
  142. {
  143. return m_impl->was_exit_requested();
  144. }
  145. }