EventLoop.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Badge.h>
  28. #include <AK/HashMap.h>
  29. #include <AK/OwnPtr.h>
  30. #include <AK/Vector.h>
  31. #include <AK/WeakPtr.h>
  32. #include <LibCore/Event.h>
  33. #include <LibCore/LocalServer.h>
  34. #include <LibThread/Lock.h>
  35. #include <sys/select.h>
  36. #include <sys/time.h>
  37. #include <time.h>
  38. namespace Core {
  39. class Object;
  40. class Notifier;
  41. class EventLoop {
  42. public:
  43. EventLoop();
  44. ~EventLoop();
  45. int exec();
  46. enum class WaitMode {
  47. WaitForEvents,
  48. PollForEvents,
  49. };
  50. // processe events, generally called by exec() in a loop.
  51. // this should really only be used for integrating with other event loops
  52. void pump(WaitMode = WaitMode::WaitForEvents);
  53. void post_event(Object& receiver, NonnullOwnPtr<Event>&&);
  54. static EventLoop& main();
  55. static EventLoop& current();
  56. bool was_exit_requested() const { return m_exit_requested; }
  57. static int register_timer(Object&, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible);
  58. static bool unregister_timer(int timer_id);
  59. static void register_notifier(Badge<Notifier>, Notifier&);
  60. static void unregister_notifier(Badge<Notifier>, Notifier&);
  61. void quit(int);
  62. void unquit();
  63. void take_pending_events_from(EventLoop& other)
  64. {
  65. m_queued_events.append(move(other.m_queued_events));
  66. }
  67. static void wake();
  68. private:
  69. void wait_for_event(WaitMode);
  70. void get_next_timer_expiration(timeval&);
  71. struct QueuedEvent {
  72. WeakPtr<Object> receiver;
  73. NonnullOwnPtr<Event> event;
  74. };
  75. Vector<QueuedEvent, 64> m_queued_events;
  76. bool m_exit_requested { false };
  77. int m_exit_code { 0 };
  78. static int s_wake_pipe_fds[2];
  79. LibThread::Lock m_lock;
  80. struct EventLoopTimer {
  81. int timer_id { 0 };
  82. int interval { 0 };
  83. timeval fire_time { 0, 0 };
  84. bool should_reload { false };
  85. TimerShouldFireWhenNotVisible fire_when_not_visible { TimerShouldFireWhenNotVisible::No };
  86. WeakPtr<Object> owner;
  87. void reload(const timeval& now);
  88. bool has_expired(const timeval& now) const;
  89. };
  90. static HashMap<int, NonnullOwnPtr<EventLoopTimer>>* s_timers;
  91. static HashTable<Notifier*>* s_notifiers;
  92. static RefPtr<LocalServer> s_rpc_server;
  93. };
  94. }