EventLoop.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2018-2020, 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. #pragma once
  9. #include <AK/Forward.h>
  10. #include <AK/Function.h>
  11. #include <AK/HashMap.h>
  12. #include <AK/Noncopyable.h>
  13. #include <AK/NonnullOwnPtr.h>
  14. #include <AK/NonnullRefPtr.h>
  15. #include <AK/Time.h>
  16. #include <AK/Vector.h>
  17. #include <AK/WeakPtr.h>
  18. #include <LibCore/DeferredInvocationContext.h>
  19. #include <LibCore/Event.h>
  20. #include <LibCore/Forward.h>
  21. #include <LibThreading/MutexProtected.h>
  22. #include <sys/time.h>
  23. #include <sys/types.h>
  24. namespace Core {
  25. extern Threading::MutexProtected<EventLoop*> s_main_event_loop;
  26. class EventLoop {
  27. public:
  28. enum class MakeInspectable {
  29. No,
  30. Yes,
  31. };
  32. enum class ShouldWake {
  33. No,
  34. Yes
  35. };
  36. explicit EventLoop(MakeInspectable = MakeInspectable::No);
  37. ~EventLoop();
  38. static void initialize_wake_pipes();
  39. int exec();
  40. enum class WaitMode {
  41. WaitForEvents,
  42. PollForEvents,
  43. };
  44. // processe events, generally called by exec() in a loop.
  45. // this should really only be used for integrating with other event loops
  46. size_t pump(WaitMode = WaitMode::WaitForEvents);
  47. void spin_until(Function<bool()>);
  48. void post_event(Object& receiver, NonnullOwnPtr<Event>&&, ShouldWake = ShouldWake::No);
  49. template<typename Callback>
  50. static decltype(auto) with_main_locked(Callback callback)
  51. {
  52. return s_main_event_loop.with_locked([&callback](auto*& event_loop) {
  53. VERIFY(event_loop != nullptr);
  54. return callback(event_loop);
  55. });
  56. }
  57. static EventLoop& current();
  58. bool was_exit_requested() const { return m_exit_requested; }
  59. static int register_timer(Object&, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible);
  60. static bool unregister_timer(int timer_id);
  61. static void register_notifier(Badge<Notifier>, Notifier&);
  62. static void unregister_notifier(Badge<Notifier>, Notifier&);
  63. void quit(int);
  64. void unquit();
  65. void take_pending_events_from(EventLoop& other)
  66. {
  67. m_queued_events.extend(move(other.m_queued_events));
  68. }
  69. static void wake_current();
  70. void wake();
  71. static int register_signal(int signo, Function<void(int)> handler);
  72. static void unregister_signal(int handler_id);
  73. // Note: Boost uses Parent/Child/Prepare, but we don't really have anything
  74. // interesting to do in the parent or before forking.
  75. enum class ForkEvent {
  76. Child,
  77. };
  78. static void notify_forked(ForkEvent);
  79. static bool has_been_instantiated();
  80. void deferred_invoke(Function<void()> invokee)
  81. {
  82. auto context = DeferredInvocationContext::construct();
  83. post_event(context, make<Core::DeferredInvocationEvent>(context, move(invokee)));
  84. }
  85. private:
  86. void wait_for_event(WaitMode);
  87. Optional<Time> get_next_timer_expiration();
  88. static void dispatch_signal(int);
  89. static void handle_signal(int);
  90. struct QueuedEvent {
  91. AK_MAKE_NONCOPYABLE(QueuedEvent);
  92. public:
  93. QueuedEvent(Object& receiver, NonnullOwnPtr<Event>);
  94. QueuedEvent(QueuedEvent&&);
  95. ~QueuedEvent() = default;
  96. WeakPtr<Object> receiver;
  97. NonnullOwnPtr<Event> event;
  98. };
  99. Vector<QueuedEvent, 64> m_queued_events;
  100. static pid_t s_pid;
  101. bool m_exit_requested { false };
  102. int m_exit_code { 0 };
  103. static thread_local int s_wake_pipe_fds[2];
  104. static thread_local bool s_wake_pipe_initialized;
  105. // The wake pipe of this event loop needs to be accessible from other threads.
  106. int (*m_wake_pipe_fds)[2];
  107. struct Private;
  108. NonnullOwnPtr<Private> m_private;
  109. };
  110. inline void deferred_invoke(Function<void()> invokee)
  111. {
  112. EventLoop::current().deferred_invoke(move(invokee));
  113. }
  114. }