EventLoop.cpp 3.6 KB

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