Skip to content

Commit

Permalink
LibCore: Add 'notify_forked()' to tear down the eventloop in forked c…
Browse files Browse the repository at this point in the history
…hild

This makes the forked process capable of constructing a new event loop,
should it choose to.
  • Loading branch information
alimpfard authored and awesomekling committed Sep 9, 2020
1 parent aa2df92 commit c3dbe77
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
20 changes: 20 additions & 0 deletions Libraries/LibCore/EventLoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,26 @@ void EventLoop::unregister_signal(int handler_id)
s_signal_handlers.remove(remove_signo);
}

void EventLoop::notify_forked(ForkEvent event)
{
switch (event) {
case ForkEvent::Child:
s_main_event_loop = nullptr;
s_event_loop_stack->clear();
s_timers->clear();
s_notifiers->clear();
s_signal_handlers.clear();
s_handling_signal = 0;
s_next_signal_id = 0;
s_pid = 0;
s_rpc_server = nullptr;
s_rpc_clients.clear();
return;
}

ASSERT_NOT_REACHED();
}

void EventLoop::wait_for_event(WaitMode mode)
{
fd_set rfds;
Expand Down
9 changes: 8 additions & 1 deletion Libraries/LibCore/EventLoop.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ class EventLoop {
static int register_signal(int signo, Function<void(int)> handler);
static void unregister_signal(int handler_id);

// Note: Boost uses Parent/Child/Prepare, but we don't really have anything
// interesting to do in the parent or before forking.
enum class ForkEvent {
Child,
};
static void notify_forked(ForkEvent);

private:
bool start_rpc_server();
void wait_for_event(WaitMode);
Expand All @@ -102,8 +109,8 @@ class EventLoop {

class SignalHandlers {
AK_MAKE_NONCOPYABLE(SignalHandlers);
public:

public:
SignalHandlers(SignalHandlers&& from)
: m_signo(from.m_signo)
, m_original_handler(from.m_original_handler)
Expand Down

0 comments on commit c3dbe77

Please sign in to comment.