EventLoop.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
  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/Noncopyable.h>
  12. #include <AK/NonnullOwnPtr.h>
  13. #include <AK/Time.h>
  14. #include <LibCore/Event.h>
  15. #include <LibCore/Forward.h>
  16. namespace Core {
  17. class EventLoopImplementation;
  18. class ThreadEventQueue;
  19. // The event loop enables asynchronous (not parallel or multi-threaded) computing by efficiently handling events from various sources.
  20. // Event loops are most important for GUI programs, where the various GUI updates and action callbacks run on the EventLoop,
  21. // as well as services, where asynchronous remote procedure calls of multiple clients are handled.
  22. // Event loops, through select(), allow programs to "go to sleep" for most of their runtime until some event happens.
  23. // EventLoop is too expensive to use in realtime scenarios (read: audio) where even the time required by a single select() system call is too large and unpredictable.
  24. //
  25. // There is at most one running event loop per thread.
  26. // Another event loop can be started while another event loop is already running; that new event loop will take over for the other event loop.
  27. // This is mainly used in LibGUI, where each modal window stacks another event loop until it is closed.
  28. // However, that means you need to be careful with storing the current event loop, as it might already be gone at the time of use.
  29. // Event loops currently handle these kinds of events:
  30. // - Deferred invocations caused by various objects. These are just a generic way of telling the EventLoop to run some function as soon as possible at a later point.
  31. // - Timers, which repeatedly (or once after a delay) run a function on the EventLoop. Note that timers are not super accurate.
  32. // - Filesystem notifications, i.e. whenever a file is read from, written to, etc.
  33. // - POSIX signals, which allow the event loop to act as a signal handler and dispatch those signals in a more user-friendly way.
  34. // - Fork events, because the child process event loop needs to clear its events and handlers.
  35. // - Quit events, i.e. the event loop should exit.
  36. // Any event that the event loop needs to wait on or needs to repeatedly handle is stored in a handle, e.g. s_timers.
  37. class EventLoop {
  38. friend struct EventLoopPusher;
  39. public:
  40. enum class WaitMode {
  41. WaitForEvents,
  42. PollForEvents,
  43. };
  44. EventLoop();
  45. ~EventLoop();
  46. // Pump the event loop until its exit is requested.
  47. int exec();
  48. // Process events, generally called by exec() in a loop.
  49. // This should really only be used for integrating with other event loops.
  50. // The wait mode determines whether pump() uses select() to wait for the next event.
  51. size_t pump(WaitMode = WaitMode::WaitForEvents);
  52. // Pump the event loop until some condition is met.
  53. void spin_until(Function<bool()>);
  54. // Post an event to this event loop.
  55. void post_event(EventReceiver& receiver, NonnullOwnPtr<Event>&&);
  56. void add_job(NonnullRefPtr<Promise<NonnullRefPtr<EventReceiver>>> job_promise);
  57. void deferred_invoke(Function<void()>);
  58. void wake();
  59. void quit(int);
  60. void unquit();
  61. bool was_exit_requested() const;
  62. // The registration functions act upon the current loop of the current thread.
  63. static int register_timer(EventReceiver&, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible);
  64. static bool unregister_timer(int timer_id);
  65. static void register_notifier(Badge<Notifier>, Notifier&);
  66. static void unregister_notifier(Badge<Notifier>, Notifier&);
  67. static int register_signal(int signo, Function<void(int)> handler);
  68. static void unregister_signal(int handler_id);
  69. // Note: Boost uses Parent/Child/Prepare, but we don't really have anything
  70. // interesting to do in the parent or before forking.
  71. enum class ForkEvent {
  72. Child,
  73. };
  74. static void notify_forked(ForkEvent);
  75. static bool is_running();
  76. static EventLoop& current();
  77. EventLoopImplementation& impl() { return *m_impl; }
  78. private:
  79. NonnullOwnPtr<EventLoopImplementation> m_impl;
  80. };
  81. void deferred_invoke(Function<void()>);
  82. }