EventLoop.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. void pump(WaitMode = WaitMode::WaitForEvents);
  38. void post_event(Object& receiver, NonnullOwnPtr<Event>&&);
  39. static EventLoop& main();
  40. static EventLoop& current();
  41. bool was_exit_requested() const { return m_exit_requested; }
  42. static int register_timer(Object&, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible);
  43. static bool unregister_timer(int timer_id);
  44. static void register_notifier(Badge<Notifier>, Notifier&);
  45. static void unregister_notifier(Badge<Notifier>, Notifier&);
  46. void quit(int);
  47. void unquit();
  48. void take_pending_events_from(EventLoop& other)
  49. {
  50. m_queued_events.extend(move(other.m_queued_events));
  51. }
  52. static void wake();
  53. static int register_signal(int signo, Function<void(int)> handler);
  54. static void unregister_signal(int handler_id);
  55. // Note: Boost uses Parent/Child/Prepare, but we don't really have anything
  56. // interesting to do in the parent or before forking.
  57. enum class ForkEvent {
  58. Child,
  59. };
  60. static void notify_forked(ForkEvent);
  61. static bool has_been_instantiated();
  62. void deferred_invoke(Function<void()> invokee)
  63. {
  64. auto context = DeferredInvocationContext::construct();
  65. post_event(context, make<Core::DeferredInvocationEvent>(context, move(invokee)));
  66. }
  67. private:
  68. void wait_for_event(WaitMode);
  69. Optional<Time> get_next_timer_expiration();
  70. static void dispatch_signal(int);
  71. static void handle_signal(int);
  72. struct QueuedEvent {
  73. AK_MAKE_NONCOPYABLE(QueuedEvent);
  74. public:
  75. QueuedEvent(Object& receiver, NonnullOwnPtr<Event>);
  76. QueuedEvent(QueuedEvent&&);
  77. ~QueuedEvent();
  78. WeakPtr<Object> receiver;
  79. NonnullOwnPtr<Event> event;
  80. };
  81. Vector<QueuedEvent, 64> m_queued_events;
  82. static pid_t s_pid;
  83. bool m_exit_requested { false };
  84. int m_exit_code { 0 };
  85. static int s_wake_pipe_fds[2];
  86. struct Private;
  87. NonnullOwnPtr<Private> m_private;
  88. };
  89. inline void deferred_invoke(Function<void()> invokee)
  90. {
  91. EventLoop::current().deferred_invoke(move(invokee));
  92. }
  93. }