EventLoop.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/EventReceiver.h>
  13. #include <LibCore/Promise.h>
  14. #include <LibCore/ThreadEventQueue.h>
  15. namespace Core {
  16. namespace {
  17. OwnPtr<Vector<EventLoop&>>& event_loop_stack_uninitialized()
  18. {
  19. thread_local OwnPtr<Vector<EventLoop&>> s_event_loop_stack = nullptr;
  20. return s_event_loop_stack;
  21. }
  22. Vector<EventLoop&>& event_loop_stack()
  23. {
  24. auto& the_stack = event_loop_stack_uninitialized();
  25. if (the_stack == nullptr)
  26. the_stack = make<Vector<EventLoop&>>();
  27. return *the_stack;
  28. }
  29. }
  30. EventLoop::EventLoop()
  31. : m_impl(EventLoopManager::the().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. bool EventLoop::is_running()
  44. {
  45. auto& stack = event_loop_stack_uninitialized();
  46. return stack != nullptr && !stack->is_empty();
  47. }
  48. EventLoop& EventLoop::current()
  49. {
  50. return event_loop_stack().last();
  51. }
  52. void EventLoop::quit(int code)
  53. {
  54. ThreadEventQueue::current().cancel_all_pending_jobs();
  55. m_impl->quit(code);
  56. }
  57. void EventLoop::unquit()
  58. {
  59. m_impl->unquit();
  60. }
  61. struct EventLoopPusher {
  62. public:
  63. EventLoopPusher(EventLoop& event_loop)
  64. {
  65. event_loop_stack().append(event_loop);
  66. }
  67. ~EventLoopPusher()
  68. {
  69. event_loop_stack().take_last();
  70. }
  71. };
  72. int EventLoop::exec()
  73. {
  74. EventLoopPusher pusher(*this);
  75. return m_impl->exec();
  76. }
  77. void EventLoop::spin_until(Function<bool()> goal_condition)
  78. {
  79. EventLoopPusher pusher(*this);
  80. while (!m_impl->was_exit_requested() && !goal_condition())
  81. pump();
  82. }
  83. size_t EventLoop::pump(WaitMode mode)
  84. {
  85. return m_impl->pump(mode == WaitMode::WaitForEvents ? EventLoopImplementation::PumpMode::WaitForEvents : EventLoopImplementation::PumpMode::DontWaitForEvents);
  86. }
  87. void EventLoop::post_event(EventReceiver& receiver, NonnullOwnPtr<Event>&& event)
  88. {
  89. m_impl->post_event(receiver, move(event));
  90. }
  91. void EventLoop::add_job(NonnullRefPtr<Promise<NonnullRefPtr<EventReceiver>>> job_promise)
  92. {
  93. ThreadEventQueue::current().add_job(move(job_promise));
  94. }
  95. int EventLoop::register_signal(int signal_number, Function<void(int)> handler)
  96. {
  97. return EventLoopManager::the().register_signal(signal_number, move(handler));
  98. }
  99. void EventLoop::unregister_signal(int handler_id)
  100. {
  101. EventLoopManager::the().unregister_signal(handler_id);
  102. }
  103. void EventLoop::notify_forked(ForkEvent)
  104. {
  105. current().m_impl->notify_forked_and_in_child();
  106. }
  107. int EventLoop::register_timer(EventReceiver& object, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible fire_when_not_visible)
  108. {
  109. return EventLoopManager::the().register_timer(object, milliseconds, should_reload, fire_when_not_visible);
  110. }
  111. bool EventLoop::unregister_timer(int timer_id)
  112. {
  113. return EventLoopManager::the().unregister_timer(timer_id);
  114. }
  115. void EventLoop::register_notifier(Badge<Notifier>, Notifier& notifier)
  116. {
  117. EventLoopManager::the().register_notifier(notifier);
  118. }
  119. void EventLoop::unregister_notifier(Badge<Notifier>, Notifier& notifier)
  120. {
  121. EventLoopManager::the().unregister_notifier(notifier);
  122. }
  123. void EventLoop::wake()
  124. {
  125. m_impl->wake();
  126. }
  127. void EventLoop::deferred_invoke(Function<void()> invokee)
  128. {
  129. auto context = DeferredInvocationContext::construct();
  130. post_event(context, make<Core::DeferredInvocationEvent>(context, move(invokee)));
  131. }
  132. void deferred_invoke(Function<void()> invokee)
  133. {
  134. EventLoop::current().deferred_invoke(move(invokee));
  135. }
  136. bool EventLoop::was_exit_requested() const
  137. {
  138. return m_impl->was_exit_requested();
  139. }
  140. }