1
1
mirror of https://github.com/rui314/mold.git synced 2024-10-05 17:17:40 +03:00
This commit is contained in:
Rui Ueyama 2024-07-04 16:17:25 +09:00
parent 3936134823
commit c6b54532e9
3 changed files with 17 additions and 12 deletions

View File

@ -375,11 +375,9 @@ int elf_main(int argc, char **argv) {
<< ": " << errno_string();
// Fork a subprocess unless --no-fork is given.
std::function<void()> 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();

View File

@ -1367,7 +1367,8 @@ void print_map(Context<E> &ctx);
// subprocess.cc
//
std::function<void()> fork_child();
void fork_child();
void notify_parent();
template <typename E>
[[noreturn]]

View File

@ -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<void()> fork_child() {
void fork_child() {
int pipefd[2];
if (pipe(pipefd) == -1) {
perror("pipe");
@ -50,12 +52,16 @@ std::function<void()> 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