EventLoop.cpp 3.5 KB

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