EventLoopImplementationUnix.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/BinaryHeap.h>
  7. #include <AK/Singleton.h>
  8. #include <AK/TemporaryChange.h>
  9. #include <AK/Time.h>
  10. #include <AK/WeakPtr.h>
  11. #include <LibCore/Event.h>
  12. #include <LibCore/EventLoopImplementationUnix.h>
  13. #include <LibCore/EventReceiver.h>
  14. #include <LibCore/Notifier.h>
  15. #include <LibCore/Socket.h>
  16. #include <LibCore/System.h>
  17. #include <LibCore/ThreadEventQueue.h>
  18. #include <sys/select.h>
  19. #include <unistd.h>
  20. namespace Core {
  21. namespace {
  22. struct ThreadData;
  23. class TimeoutSet;
  24. thread_local ThreadData* s_thread_data;
  25. short notification_type_to_poll_events(NotificationType type)
  26. {
  27. short events = 0;
  28. if (has_flag(type, NotificationType::Read))
  29. events |= POLLIN;
  30. if (has_flag(type, NotificationType::Write))
  31. events |= POLLOUT;
  32. return events;
  33. }
  34. bool has_flag(int value, int flag)
  35. {
  36. return (value & flag) == flag;
  37. }
  38. class EventLoopTimeout {
  39. public:
  40. static constexpr ssize_t INVALID_INDEX = NumericLimits<ssize_t>::max();
  41. EventLoopTimeout() { }
  42. virtual ~EventLoopTimeout() = default;
  43. virtual void fire(TimeoutSet& timeout_set, MonotonicTime time) = 0;
  44. MonotonicTime fire_time() const { return m_fire_time; }
  45. void absolutize(Badge<TimeoutSet>, MonotonicTime current_time)
  46. {
  47. m_fire_time = current_time + m_duration;
  48. }
  49. ssize_t& index(Badge<TimeoutSet>) { return m_index; }
  50. void set_index(Badge<TimeoutSet>, ssize_t index) { m_index = index; }
  51. bool is_scheduled() const { return m_index != INVALID_INDEX; }
  52. protected:
  53. union {
  54. Duration m_duration;
  55. MonotonicTime m_fire_time;
  56. };
  57. private:
  58. ssize_t m_index = INVALID_INDEX;
  59. };
  60. class TimeoutSet {
  61. public:
  62. TimeoutSet() = default;
  63. Optional<MonotonicTime> next_timer_expiration()
  64. {
  65. if (!m_heap.is_empty()) {
  66. return m_heap.peek_min()->fire_time();
  67. } else {
  68. return {};
  69. }
  70. }
  71. void absolutize_relative_timeouts(MonotonicTime current_time)
  72. {
  73. for (auto timeout : m_scheduled_timeouts) {
  74. timeout->absolutize({}, current_time);
  75. m_heap.insert(timeout);
  76. }
  77. m_scheduled_timeouts.clear();
  78. }
  79. size_t fire_expired(MonotonicTime current_time)
  80. {
  81. size_t fired_count = 0;
  82. while (!m_heap.is_empty()) {
  83. auto& timeout = *m_heap.peek_min();
  84. if (timeout.fire_time() <= current_time) {
  85. ++fired_count;
  86. m_heap.pop_min();
  87. timeout.set_index({}, EventLoopTimeout::INVALID_INDEX);
  88. timeout.fire(*this, current_time);
  89. } else {
  90. break;
  91. }
  92. }
  93. return fired_count;
  94. }
  95. void schedule_relative(EventLoopTimeout* timeout)
  96. {
  97. timeout->set_index({}, -1 - static_cast<ssize_t>(m_scheduled_timeouts.size()));
  98. m_scheduled_timeouts.append(timeout);
  99. }
  100. void schedule_absolute(EventLoopTimeout* timeout)
  101. {
  102. m_heap.insert(timeout);
  103. }
  104. void unschedule(EventLoopTimeout* timeout)
  105. {
  106. if (timeout->index({}) < 0) {
  107. size_t i = -1 - timeout->index({});
  108. size_t j = m_scheduled_timeouts.size() - 1;
  109. VERIFY(m_scheduled_timeouts[i] == timeout);
  110. swap(m_scheduled_timeouts[i], m_scheduled_timeouts[j]);
  111. swap(m_scheduled_timeouts[i]->index({}), m_scheduled_timeouts[j]->index({}));
  112. (void)m_scheduled_timeouts.take_last();
  113. } else {
  114. m_heap.pop(timeout->index({}));
  115. }
  116. timeout->set_index({}, EventLoopTimeout::INVALID_INDEX);
  117. }
  118. void clear()
  119. {
  120. for (auto* timeout : m_heap.nodes_in_arbitrary_order())
  121. timeout->set_index({}, EventLoopTimeout::INVALID_INDEX);
  122. m_heap.clear();
  123. for (auto* timeout : m_scheduled_timeouts)
  124. timeout->set_index({}, EventLoopTimeout::INVALID_INDEX);
  125. m_scheduled_timeouts.clear();
  126. }
  127. private:
  128. IntrusiveBinaryHeap<
  129. EventLoopTimeout*,
  130. decltype([](EventLoopTimeout* a, EventLoopTimeout* b) {
  131. return a->fire_time() < b->fire_time();
  132. }),
  133. decltype([](EventLoopTimeout* timeout, size_t index) {
  134. timeout->set_index({}, static_cast<ssize_t>(index));
  135. }),
  136. 8>
  137. m_heap;
  138. Vector<EventLoopTimeout*, 8> m_scheduled_timeouts;
  139. };
  140. class EventLoopTimer final : public EventLoopTimeout {
  141. public:
  142. static constexpr auto delay_tolerance = Duration::from_milliseconds(5);
  143. EventLoopTimer() = default;
  144. void reload(MonotonicTime const& now) { m_fire_time = now + interval; }
  145. virtual void fire(TimeoutSet& timeout_set, MonotonicTime current_time) override
  146. {
  147. auto strong_owner = owner.strong_ref();
  148. if (!strong_owner)
  149. return;
  150. if (should_reload) {
  151. MonotonicTime next_fire_time = m_fire_time + interval;
  152. if (next_fire_time <= current_time) {
  153. auto delay = current_time - next_fire_time;
  154. if (delay >= delay_tolerance && !interval.is_zero()) {
  155. auto iterations = delay.to_milliseconds() / max<i64>(1, interval.to_milliseconds()) + 1;
  156. dbgln("Can't keep up! Skipping approximately {} iteration(s) of a reloading timer (delayed by {}ms).", iterations, delay.to_milliseconds());
  157. }
  158. next_fire_time = current_time + interval;
  159. }
  160. m_fire_time = next_fire_time;
  161. if (next_fire_time != current_time) {
  162. timeout_set.schedule_absolute(this);
  163. } else {
  164. // NOTE: Unfortunately we need to treat timeouts with the zero interval in a
  165. // special way. TimeoutSet::schedule_absolute for them will result in an
  166. // infinite loop. TimeoutSet::schedule_relative, on the other hand, will do a
  167. // correct thing of scheduling them for the next iteration of the loop.
  168. m_duration = {};
  169. timeout_set.schedule_relative(this);
  170. }
  171. }
  172. // FIXME: While TimerShouldFireWhenNotVisible::Yes prevents the timer callback from being
  173. // called, it doesn't allow event loop to sleep since it needs to constantly check if
  174. // is_visible_for_timer_purposes changed. A better solution will be to unregister a
  175. // timer and register it back again when needed. This also has an added benefit of
  176. // making fire_when_not_visible and is_visible_for_timer_purposes obsolete.
  177. if (fire_when_not_visible == TimerShouldFireWhenNotVisible::Yes || strong_owner->is_visible_for_timer_purposes())
  178. ThreadEventQueue::current().post_event(*strong_owner, make<TimerEvent>());
  179. }
  180. Duration interval;
  181. bool should_reload { false };
  182. TimerShouldFireWhenNotVisible fire_when_not_visible { TimerShouldFireWhenNotVisible::No };
  183. WeakPtr<EventReceiver> owner;
  184. };
  185. struct ThreadData {
  186. static ThreadData& the()
  187. {
  188. if (!s_thread_data) {
  189. // FIXME: Don't leak this.
  190. s_thread_data = new ThreadData;
  191. }
  192. return *s_thread_data;
  193. }
  194. ThreadData()
  195. {
  196. pid = getpid();
  197. initialize_wake_pipe();
  198. }
  199. void initialize_wake_pipe()
  200. {
  201. if (wake_pipe_fds[0] != -1)
  202. close(wake_pipe_fds[0]);
  203. if (wake_pipe_fds[1] != -1)
  204. close(wake_pipe_fds[1]);
  205. wake_pipe_fds = MUST(Core::System::pipe2(O_CLOEXEC));
  206. // The wake pipe informs us of POSIX signals as well as manual calls to wake()
  207. VERIFY(poll_fds.size() == 0);
  208. poll_fds.append({ .fd = wake_pipe_fds[0], .events = POLLIN, .revents = 0 });
  209. notifier_by_index.append(nullptr);
  210. }
  211. // Each thread has its own timers, notifiers and a wake pipe.
  212. TimeoutSet timeouts;
  213. Vector<pollfd> poll_fds;
  214. HashMap<Notifier*, size_t> notifier_by_ptr;
  215. Vector<Notifier*> notifier_by_index;
  216. // The wake pipe is used to notify another event loop that someone has called wake(), or a signal has been received.
  217. // wake() writes 0i32 into the pipe, signals write the signal number (guaranteed non-zero).
  218. Array<int, 2> wake_pipe_fds { -1, -1 };
  219. pid_t pid { 0 };
  220. };
  221. }
  222. EventLoopImplementationUnix::EventLoopImplementationUnix()
  223. : m_wake_pipe_fds(ThreadData::the().wake_pipe_fds)
  224. {
  225. }
  226. EventLoopImplementationUnix::~EventLoopImplementationUnix() = default;
  227. int EventLoopImplementationUnix::exec()
  228. {
  229. for (;;) {
  230. if (m_exit_requested)
  231. return m_exit_code;
  232. pump(PumpMode::WaitForEvents);
  233. }
  234. VERIFY_NOT_REACHED();
  235. }
  236. size_t EventLoopImplementationUnix::pump(PumpMode mode)
  237. {
  238. static_cast<EventLoopManagerUnix&>(EventLoopManager::the()).wait_for_events(mode);
  239. return ThreadEventQueue::current().process();
  240. }
  241. void EventLoopImplementationUnix::quit(int code)
  242. {
  243. m_exit_requested = true;
  244. m_exit_code = code;
  245. }
  246. void EventLoopImplementationUnix::unquit()
  247. {
  248. m_exit_requested = false;
  249. m_exit_code = 0;
  250. }
  251. bool EventLoopImplementationUnix::was_exit_requested() const
  252. {
  253. return m_exit_requested;
  254. }
  255. void EventLoopImplementationUnix::post_event(EventReceiver& receiver, NonnullOwnPtr<Event>&& event)
  256. {
  257. m_thread_event_queue.post_event(receiver, move(event));
  258. if (&m_thread_event_queue != &ThreadEventQueue::current())
  259. wake();
  260. }
  261. void EventLoopImplementationUnix::wake()
  262. {
  263. int wake_event = 0;
  264. MUST(Core::System::write(m_wake_pipe_fds[1], { &wake_event, sizeof(wake_event) }));
  265. }
  266. void EventLoopManagerUnix::wait_for_events(EventLoopImplementation::PumpMode mode)
  267. {
  268. auto& thread_data = ThreadData::the();
  269. retry:
  270. bool has_pending_events = ThreadEventQueue::current().has_pending_events();
  271. auto time_at_iteration_start = MonotonicTime::now_coarse();
  272. thread_data.timeouts.absolutize_relative_timeouts(time_at_iteration_start);
  273. // Figure out how long to wait at maximum.
  274. // This mainly depends on the PumpMode and whether we have pending events, but also the next expiring timer.
  275. int timeout = 0;
  276. bool should_wait_forever = false;
  277. if (mode == EventLoopImplementation::PumpMode::WaitForEvents && !has_pending_events) {
  278. auto next_timer_expiration = thread_data.timeouts.next_timer_expiration();
  279. if (next_timer_expiration.has_value()) {
  280. auto computed_timeout = next_timer_expiration.value() - time_at_iteration_start;
  281. if (computed_timeout.is_negative())
  282. computed_timeout = Duration::zero();
  283. i64 true_timeout = computed_timeout.to_milliseconds();
  284. timeout = static_cast<i32>(min<i64>(AK::NumericLimits<i32>::max(), true_timeout));
  285. } else {
  286. should_wait_forever = true;
  287. }
  288. }
  289. try_select_again:
  290. // select() and wait for file system events, calls to wake(), POSIX signals, or timer expirations.
  291. ErrorOr<int> error_or_marked_fd_count = System::poll(thread_data.poll_fds, should_wait_forever ? -1 : timeout);
  292. auto time_after_poll = MonotonicTime::now_coarse();
  293. // Because POSIX, we might spuriously return from select() with EINTR; just select again.
  294. if (error_or_marked_fd_count.is_error()) {
  295. if (error_or_marked_fd_count.error().code() == EINTR)
  296. goto try_select_again;
  297. dbgln("EventLoopImplementationUnix::wait_for_events: {}", error_or_marked_fd_count.error());
  298. VERIFY_NOT_REACHED();
  299. }
  300. // We woke up due to a call to wake() or a POSIX signal.
  301. // Handle signals and see whether we need to handle events as well.
  302. if (has_flag(thread_data.poll_fds[0].revents, POLLIN)) {
  303. int wake_events[8];
  304. ssize_t nread;
  305. // We might receive another signal while read()ing here. The signal will go to the handle_signal properly,
  306. // but we get interrupted. Therefore, just retry while we were interrupted.
  307. do {
  308. errno = 0;
  309. nread = read(thread_data.wake_pipe_fds[0], wake_events, sizeof(wake_events));
  310. if (nread == 0)
  311. break;
  312. } while (nread < 0 && errno == EINTR);
  313. if (nread < 0) {
  314. perror("EventLoopImplementationUnix::wait_for_events: read from wake pipe");
  315. VERIFY_NOT_REACHED();
  316. }
  317. VERIFY(nread > 0);
  318. bool wake_requested = false;
  319. int event_count = nread / sizeof(wake_events[0]);
  320. for (int i = 0; i < event_count; i++) {
  321. if (wake_events[i] != 0)
  322. dispatch_signal(wake_events[i]);
  323. else
  324. wake_requested = true;
  325. }
  326. if (!wake_requested && nread == sizeof(wake_events))
  327. goto retry;
  328. }
  329. if (error_or_marked_fd_count.value() != 0) {
  330. // Handle file system notifiers by making them normal events.
  331. for (size_t i = 1; i < thread_data.poll_fds.size(); ++i) {
  332. auto& revents = thread_data.poll_fds[i].revents;
  333. auto& notifier = *thread_data.notifier_by_index[i];
  334. NotificationType type = NotificationType::None;
  335. if (has_flag(revents, POLLIN))
  336. type |= NotificationType::Read;
  337. if (has_flag(revents, POLLOUT))
  338. type |= NotificationType::Write;
  339. if (has_flag(revents, POLLHUP))
  340. type |= NotificationType::HangUp;
  341. if (has_flag(revents, POLLERR))
  342. type |= NotificationType::Error;
  343. type &= notifier.type();
  344. if (type != NotificationType::None)
  345. ThreadEventQueue::current().post_event(notifier, make<NotifierActivationEvent>(notifier.fd(), type));
  346. }
  347. }
  348. // Handle expired timers.
  349. thread_data.timeouts.fire_expired(time_after_poll);
  350. }
  351. class SignalHandlers : public RefCounted<SignalHandlers> {
  352. AK_MAKE_NONCOPYABLE(SignalHandlers);
  353. AK_MAKE_NONMOVABLE(SignalHandlers);
  354. public:
  355. SignalHandlers(int signal_number, void (*handle_signal)(int));
  356. ~SignalHandlers();
  357. void dispatch();
  358. int add(Function<void(int)>&& handler);
  359. bool remove(int handler_id);
  360. bool is_empty() const
  361. {
  362. if (m_calling_handlers) {
  363. for (auto& handler : m_handlers_pending) {
  364. if (handler.value)
  365. return false; // an add is pending
  366. }
  367. }
  368. return m_handlers.is_empty();
  369. }
  370. bool have(int handler_id) const
  371. {
  372. if (m_calling_handlers) {
  373. auto it = m_handlers_pending.find(handler_id);
  374. if (it != m_handlers_pending.end()) {
  375. if (!it->value)
  376. return false; // a deletion is pending
  377. }
  378. }
  379. return m_handlers.contains(handler_id);
  380. }
  381. int m_signal_number;
  382. void (*m_original_handler)(int); // TODO: can't use sighandler_t?
  383. HashMap<int, Function<void(int)>> m_handlers;
  384. HashMap<int, Function<void(int)>> m_handlers_pending;
  385. bool m_calling_handlers { false };
  386. };
  387. struct SignalHandlersInfo {
  388. HashMap<int, NonnullRefPtr<SignalHandlers>> signal_handlers;
  389. int next_signal_id { 0 };
  390. };
  391. static Singleton<SignalHandlersInfo> s_signals;
  392. template<bool create_if_null = true>
  393. inline SignalHandlersInfo* signals_info()
  394. {
  395. return s_signals.ptr();
  396. }
  397. void EventLoopManagerUnix::dispatch_signal(int signal_number)
  398. {
  399. auto& info = *signals_info();
  400. auto handlers = info.signal_handlers.find(signal_number);
  401. if (handlers != info.signal_handlers.end()) {
  402. // Make sure we bump the ref count while dispatching the handlers!
  403. // This allows a handler to unregister/register while the handlers
  404. // are being called!
  405. auto handler = handlers->value;
  406. handler->dispatch();
  407. }
  408. }
  409. void EventLoopImplementationUnix::notify_forked_and_in_child()
  410. {
  411. auto& thread_data = ThreadData::the();
  412. thread_data.timeouts.clear();
  413. thread_data.poll_fds.clear();
  414. thread_data.notifier_by_ptr.clear();
  415. thread_data.notifier_by_index.clear();
  416. thread_data.initialize_wake_pipe();
  417. if (auto* info = signals_info<false>()) {
  418. info->signal_handlers.clear();
  419. info->next_signal_id = 0;
  420. }
  421. thread_data.pid = getpid();
  422. }
  423. SignalHandlers::SignalHandlers(int signal_number, void (*handle_signal)(int))
  424. : m_signal_number(signal_number)
  425. , m_original_handler(signal(signal_number, handle_signal))
  426. {
  427. }
  428. SignalHandlers::~SignalHandlers()
  429. {
  430. signal(m_signal_number, m_original_handler);
  431. }
  432. void SignalHandlers::dispatch()
  433. {
  434. TemporaryChange change(m_calling_handlers, true);
  435. for (auto& handler : m_handlers)
  436. handler.value(m_signal_number);
  437. if (!m_handlers_pending.is_empty()) {
  438. // Apply pending adds/removes
  439. for (auto& handler : m_handlers_pending) {
  440. if (handler.value) {
  441. auto result = m_handlers.set(handler.key, move(handler.value));
  442. VERIFY(result == AK::HashSetResult::InsertedNewEntry);
  443. } else {
  444. m_handlers.remove(handler.key);
  445. }
  446. }
  447. m_handlers_pending.clear();
  448. }
  449. }
  450. int SignalHandlers::add(Function<void(int)>&& handler)
  451. {
  452. int id = ++signals_info()->next_signal_id; // TODO: worry about wrapping and duplicates?
  453. if (m_calling_handlers)
  454. m_handlers_pending.set(id, move(handler));
  455. else
  456. m_handlers.set(id, move(handler));
  457. return id;
  458. }
  459. bool SignalHandlers::remove(int handler_id)
  460. {
  461. VERIFY(handler_id != 0);
  462. if (m_calling_handlers) {
  463. auto it = m_handlers.find(handler_id);
  464. if (it != m_handlers.end()) {
  465. // Mark pending remove
  466. m_handlers_pending.set(handler_id, {});
  467. return true;
  468. }
  469. it = m_handlers_pending.find(handler_id);
  470. if (it != m_handlers_pending.end()) {
  471. if (!it->value)
  472. return false; // already was marked as deleted
  473. it->value = nullptr;
  474. return true;
  475. }
  476. return false;
  477. }
  478. return m_handlers.remove(handler_id);
  479. }
  480. void EventLoopManagerUnix::handle_signal(int signal_number)
  481. {
  482. VERIFY(signal_number != 0);
  483. auto& thread_data = ThreadData::the();
  484. // We MUST check if the current pid still matches, because there
  485. // is a window between fork() and exec() where a signal delivered
  486. // to our fork could be inadvertently routed to the parent process!
  487. if (getpid() == thread_data.pid) {
  488. int nwritten = write(thread_data.wake_pipe_fds[1], &signal_number, sizeof(signal_number));
  489. if (nwritten < 0) {
  490. perror("EventLoopImplementationUnix::register_signal: write");
  491. VERIFY_NOT_REACHED();
  492. }
  493. } else {
  494. // We're a fork who received a signal, reset thread_data.pid.
  495. thread_data.pid = getpid();
  496. }
  497. }
  498. int EventLoopManagerUnix::register_signal(int signal_number, Function<void(int)> handler)
  499. {
  500. VERIFY(signal_number != 0);
  501. auto& info = *signals_info();
  502. auto handlers = info.signal_handlers.find(signal_number);
  503. if (handlers == info.signal_handlers.end()) {
  504. auto signal_handlers = adopt_ref(*new SignalHandlers(signal_number, EventLoopManagerUnix::handle_signal));
  505. auto handler_id = signal_handlers->add(move(handler));
  506. info.signal_handlers.set(signal_number, move(signal_handlers));
  507. return handler_id;
  508. } else {
  509. return handlers->value->add(move(handler));
  510. }
  511. }
  512. void EventLoopManagerUnix::unregister_signal(int handler_id)
  513. {
  514. VERIFY(handler_id != 0);
  515. int remove_signal_number = 0;
  516. auto& info = *signals_info();
  517. for (auto& h : info.signal_handlers) {
  518. auto& handlers = *h.value;
  519. if (handlers.remove(handler_id)) {
  520. if (handlers.is_empty())
  521. remove_signal_number = handlers.m_signal_number;
  522. break;
  523. }
  524. }
  525. if (remove_signal_number != 0)
  526. info.signal_handlers.remove(remove_signal_number);
  527. }
  528. intptr_t EventLoopManagerUnix::register_timer(EventReceiver& object, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible fire_when_not_visible)
  529. {
  530. VERIFY(milliseconds >= 0);
  531. auto& thread_data = ThreadData::the();
  532. auto timer = new EventLoopTimer;
  533. timer->owner = object;
  534. timer->interval = Duration::from_milliseconds(milliseconds);
  535. timer->reload(MonotonicTime::now_coarse());
  536. timer->should_reload = should_reload;
  537. timer->fire_when_not_visible = fire_when_not_visible;
  538. thread_data.timeouts.schedule_absolute(timer);
  539. return bit_cast<intptr_t>(timer);
  540. }
  541. void EventLoopManagerUnix::unregister_timer(intptr_t timer_id)
  542. {
  543. auto& thread_data = ThreadData::the();
  544. auto* timer = bit_cast<EventLoopTimer*>(timer_id);
  545. if (timer->is_scheduled())
  546. thread_data.timeouts.unschedule(timer);
  547. delete timer;
  548. }
  549. void EventLoopManagerUnix::register_notifier(Notifier& notifier)
  550. {
  551. auto& thread_data = ThreadData::the();
  552. thread_data.notifier_by_ptr.set(&notifier, thread_data.poll_fds.size());
  553. thread_data.notifier_by_index.append(&notifier);
  554. thread_data.poll_fds.append({
  555. .fd = notifier.fd(),
  556. .events = notification_type_to_poll_events(notifier.type()),
  557. .revents = 0,
  558. });
  559. }
  560. void EventLoopManagerUnix::unregister_notifier(Notifier& notifier)
  561. {
  562. auto& thread_data = ThreadData::the();
  563. auto it = thread_data.notifier_by_ptr.find(&notifier);
  564. VERIFY(it != thread_data.notifier_by_ptr.end());
  565. size_t notifier_index = it->value;
  566. thread_data.notifier_by_ptr.remove(it);
  567. if (notifier_index + 1 != thread_data.poll_fds.size()) {
  568. swap(thread_data.poll_fds[notifier_index], thread_data.poll_fds.last());
  569. swap(thread_data.notifier_by_index[notifier_index], thread_data.notifier_by_index.last());
  570. thread_data.notifier_by_ptr.set(thread_data.notifier_by_index[notifier_index], notifier_index);
  571. }
  572. thread_data.poll_fds.take_last();
  573. thread_data.notifier_by_index.take_last();
  574. }
  575. void EventLoopManagerUnix::did_post_event()
  576. {
  577. }
  578. EventLoopManagerUnix::~EventLoopManagerUnix() = default;
  579. NonnullOwnPtr<EventLoopImplementation> EventLoopManagerUnix::make_implementation()
  580. {
  581. return adopt_own(*new EventLoopImplementationUnix);
  582. }
  583. }