EventLoop.cpp 4.1 KB

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