LibCore: Use Core::System::pipe2 for creating the event loops waking FDs

This commit is contained in:
Timothy Flynn 2024-03-12 14:59:17 -04:00 committed by Tim Flynn
parent 40beebca71
commit a973fe13cb
Notes: sideshowbarker 2024-07-17 07:25:39 +09:00
2 changed files with 5 additions and 13 deletions

View file

@ -239,15 +239,7 @@ struct ThreadData {
if (wake_pipe_fds[1] != -1) if (wake_pipe_fds[1] != -1)
close(wake_pipe_fds[1]); close(wake_pipe_fds[1]);
#if defined(SOCK_NONBLOCK) wake_pipe_fds = MUST(Core::System::pipe2(O_CLOEXEC));
int rc = pipe2(wake_pipe_fds, O_CLOEXEC);
#else
int rc = pipe(wake_pipe_fds);
fcntl(wake_pipe_fds[0], F_SETFD, FD_CLOEXEC);
fcntl(wake_pipe_fds[1], F_SETFD, FD_CLOEXEC);
#endif
VERIFY(rc == 0);
// The wake pipe informs us of POSIX signals as well as manual calls to wake() // The wake pipe informs us of POSIX signals as well as manual calls to wake()
VERIFY(poll_fds.size() == 0); VERIFY(poll_fds.size() == 0);
@ -264,14 +256,14 @@ struct ThreadData {
// The wake pipe is used to notify another event loop that someone has called wake(), or a signal has been received. // The wake pipe is used to notify another event loop that someone has called wake(), or a signal has been received.
// wake() writes 0i32 into the pipe, signals write the signal number (guaranteed non-zero). // wake() writes 0i32 into the pipe, signals write the signal number (guaranteed non-zero).
int wake_pipe_fds[2] { -1, -1 }; Array<int, 2> wake_pipe_fds { -1, -1 };
pid_t pid { 0 }; pid_t pid { 0 };
}; };
} }
EventLoopImplementationUnix::EventLoopImplementationUnix() EventLoopImplementationUnix::EventLoopImplementationUnix()
: m_wake_pipe_fds(&ThreadData::the().wake_pipe_fds) : m_wake_pipe_fds(ThreadData::the().wake_pipe_fds)
{ {
} }
@ -320,7 +312,7 @@ void EventLoopImplementationUnix::post_event(EventReceiver& receiver, NonnullOwn
void EventLoopImplementationUnix::wake() void EventLoopImplementationUnix::wake()
{ {
int wake_event = 0; int wake_event = 0;
MUST(Core::System::write((*m_wake_pipe_fds)[1], { &wake_event, sizeof(wake_event) })); MUST(Core::System::write(m_wake_pipe_fds[1], { &wake_event, sizeof(wake_event) }));
} }
void EventLoopManagerUnix::wait_for_events(EventLoopImplementation::PumpMode mode) void EventLoopManagerUnix::wait_for_events(EventLoopImplementation::PumpMode mode)

View file

@ -59,7 +59,7 @@ private:
int m_exit_code { 0 }; int m_exit_code { 0 };
// The wake pipe of this event loop needs to be accessible from other threads. // The wake pipe of this event loop needs to be accessible from other threads.
int (*m_wake_pipe_fds)[2]; Array<int, 2>& m_wake_pipe_fds;
}; };
} }