From c6b54532e9519f121d4683e553eacafdf2598fad Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Thu, 4 Jul 2024 16:17:25 +0900 Subject: [PATCH] Refactor --- elf/main.cc | 8 +++----- elf/mold.h | 3 ++- elf/subprocess.cc | 18 ++++++++++++------ 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/elf/main.cc b/elf/main.cc index bb329790..c9ace457 100644 --- a/elf/main.cc +++ b/elf/main.cc @@ -375,11 +375,9 @@ int elf_main(int argc, char **argv) { << ": " << errno_string(); // Fork a subprocess unless --no-fork is given. - std::function on_complete; - #if !defined(_WIN32) && !defined(__APPLE__) if (ctx.arg.fork) - on_complete = fork_child(); + fork_child(); #endif acquire_global_lock(); @@ -706,8 +704,8 @@ int elf_main(int argc, char **argv) { std::cout << std::flush; std::cerr << std::flush; - if (on_complete) - on_complete(); + if (ctx.arg.fork) + notify_parent(); release_global_lock(); diff --git a/elf/mold.h b/elf/mold.h index bba6f235..1e8d3bc6 100644 --- a/elf/mold.h +++ b/elf/mold.h @@ -1367,7 +1367,8 @@ void print_map(Context &ctx); // subprocess.cc // -std::function fork_child(); +void fork_child(); +void notify_parent(); template [[noreturn]] diff --git a/elf/subprocess.cc b/elf/subprocess.cc index 51be8972..b8eceda5 100644 --- a/elf/subprocess.cc +++ b/elf/subprocess.cc @@ -14,10 +14,12 @@ namespace mold::elf { #ifdef MOLD_X86_64 +static int pipe_write_fd = -1; + // Exiting from a program with large memory usage is slow -- // it may take a few hundred milliseconds. To hide the latency, // we fork a child and let it do the actual linking work. -std::function fork_child() { +void fork_child() { int pipefd[2]; if (pipe(pipefd) == -1) { perror("pipe"); @@ -50,12 +52,16 @@ std::function fork_child() { // Child close(pipefd[0]); + pipe_write_fd = pipefd[1]; +} - return [=] { - char buf[] = {1}; - [[maybe_unused]] int n = write(pipefd[1], buf, 1); - assert(n == 1); - }; +void notify_parent() { + if (pipe_write_fd == -1) + return; + + char buf[] = {1}; + [[maybe_unused]] int n = write(pipe_write_fd, buf, 1); + assert(n == 1); } #endif