EventLoop.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Forward.h>
  8. #include <AK/Function.h>
  9. #include <AK/HashMap.h>
  10. #include <AK/Noncopyable.h>
  11. #include <AK/NonnullOwnPtr.h>
  12. #include <AK/NonnullRefPtr.h>
  13. #include <AK/Time.h>
  14. #include <AK/Vector.h>
  15. #include <AK/WeakPtr.h>
  16. #include <LibCore/DeferredInvocationContext.h>
  17. #include <LibCore/Event.h>
  18. #include <LibCore/Forward.h>
  19. #include <sys/time.h>
  20. #include <sys/types.h>
  21. namespace Core {
  22. class EventLoop {
  23. public:
  24. enum class MakeInspectable {
  25. No,
  26. Yes,
  27. };
  28. explicit EventLoop(MakeInspectable = MakeInspectable::No);
  29. ~EventLoop();
  30. int exec();
  31. enum class WaitMode {
  32. WaitForEvents,
  33. PollForEvents,
  34. };
  35. // processe events, generally called by exec() in a loop.
  36. // this should really only be used for integrating with other event loops
  37. size_t pump(WaitMode = WaitMode::WaitForEvents);
  38. void spin_until(Function<bool()>);
  39. void post_event(Object& receiver, NonnullOwnPtr<Event>&&);
  40. static EventLoop& main();
  41. static EventLoop& current();
  42. bool was_exit_requested() const { return m_exit_requested; }
  43. static int register_timer(Object&, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible);
  44. static bool unregister_timer(int timer_id);
  45. static void register_notifier(Badge<Notifier>, Notifier&);
  46. static void unregister_notifier(Badge<Notifier>, Notifier&);
  47. void quit(int);
  48. void unquit();
  49. void take_pending_events_from(EventLoop& other)
  50. {
  51. m_queued_events.extend(move(other.m_queued_events));
  52. }
  53. static void wake();
  54. static int register_signal(int signo, Function<void(int)> handler);
  55. static void unregister_signal(int handler_id);
  56. // Note: Boost uses Parent/Child/Prepare, but we don't really have anything
  57. // interesting to do in the parent or before forking.
  58. enum class ForkEvent {
  59. Child,
  60. };
  61. static void notify_forked(ForkEvent);
  62. static bool has_been_instantiated();
  63. void deferred_invoke(Function<void()> invokee)
  64. {
  65. auto context = DeferredInvocationContext::construct();
  66. post_event(context, make<Core::DeferredInvocationEvent>(context, move(invokee)));
  67. }
  68. private:
  69. void wait_for_event(WaitMode);
  70. Optional<Time> get_next_timer_expiration();
  71. static void dispatch_signal(int);
  72. static void handle_signal(int);
  73. struct QueuedEvent {
  74. AK_MAKE_NONCOPYABLE(QueuedEvent);
  75. public:
  76. QueuedEvent(Object& receiver, NonnullOwnPtr<Event>);
  77. QueuedEvent(QueuedEvent&&);
  78. ~QueuedEvent();
  79. WeakPtr<Object> receiver;
  80. NonnullOwnPtr<Event> event;
  81. };
  82. Vector<QueuedEvent, 64> m_queued_events;
  83. static pid_t s_pid;
  84. bool m_exit_requested { false };
  85. int m_exit_code { 0 };
  86. static int s_wake_pipe_fds[2];
  87. struct Private;
  88. NonnullOwnPtr<Private> m_private;
  89. };
  90. inline void deferred_invoke(Function<void()> invokee)
  91. {
  92. EventLoop::current().deferred_invoke(move(invokee));
  93. }
  94. }