diff --git a/CMakeLists.txt b/CMakeLists.txt index 23b8ce24..52227967 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -305,7 +305,6 @@ list(APPEND MOLD_ELF_TEMPLATE_FILES elf/icf.cc elf/input-files.cc elf/input-sections.cc - elf/jobs.cc elf/linker-script.cc elf/lto.cc elf/main.cc @@ -378,6 +377,12 @@ target_sources(mold PRIVATE third-party/rust-demangle/rust-demangle.c ) +if(WIN32) + target_sources(mold PRIVATE common/jobs-win32.cc) +else() + target_sources(mold PRIVATE common/jobs-unix.cc) +endif() + # Add frequently included header files for pre-compiling. # target_precompile_headers is supported by CMake 3.16.0 or newer. if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.16.0") diff --git a/common/common.h b/common/common.h index b7e002a6..75e1fcce 100644 --- a/common/common.h +++ b/common/common.h @@ -885,6 +885,13 @@ std::filesystem::path to_abs_path(std::filesystem::path path); std::optional demangle_cpp(std::string_view name); std::optional demangle_rust(std::string_view name); +// +// jbos.cc +// + +void acquire_global_lock(); +void release_global_lock(); + // // compress.cc // diff --git a/elf/jobs.cc b/common/jobs-unix.cc similarity index 62% rename from elf/jobs.cc rename to common/jobs-unix.cc index 12b4a6a9..9912ab52 100644 --- a/elf/jobs.cc +++ b/common/jobs-unix.cc @@ -9,21 +9,20 @@ // mold processes to just 1 for each user. It is intended to be used as // `MOLD_JOBS=1 ninja` or `MOLD_JOBS=1 make -j$(nproc)`. -#include "mold.h" +#include "common.h" -#ifndef _WIN32 -# include -# include -# include -# include -# include -#endif +#include +#include +#include +#include +#include +#include -namespace mold::elf { +namespace mold { -template -void acquire_global_lock(Context &ctx) { -#ifndef _WIN32 +static int lock_fd = -1; + +void acquire_global_lock() { char *jobs = getenv("MOLD_JOBS"); if (!jobs || jobs != "1"s) return; @@ -40,22 +39,12 @@ void acquire_global_lock(Context &ctx) { if (lockf(fd, F_LOCK, 0) == -1) return; - - ctx.global_lock_fd = fd; -#endif + lock_fd = fd; } -template -void release_global_lock(Context &ctx) { -#ifndef _WIN32 - if (ctx.global_lock_fd) - close(*ctx.global_lock_fd); -#endif +void release_global_lock() { + if (lock_fd != -1) + close(lock_fd); } -using E = MOLD_TARGET; - -template void acquire_global_lock(Context &); -template void release_global_lock(Context &); - -} // namespace mold::elf +} // namespace mold diff --git a/common/jobs-win32.cc b/common/jobs-win32.cc new file mode 100644 index 00000000..8a7c1942 --- /dev/null +++ b/common/jobs-win32.cc @@ -0,0 +1,6 @@ +namespace mold { + +void acquire_global_lock() {} +void release_global_lock() {} + +} // namespace mold diff --git a/elf/main.cc b/elf/main.cc index e3d07833..86d40c51 100644 --- a/elf/main.cc +++ b/elf/main.cc @@ -364,7 +364,7 @@ int elf_main(int argc, char **argv) { on_complete = fork_child(); #endif - acquire_global_lock(ctx); + acquire_global_lock(); tbb::global_control tbb_cont(tbb::global_control::max_allowed_parallelism, ctx.arg.thread_count); @@ -682,10 +682,11 @@ int elf_main(int argc, char **argv) { std::cout << std::flush; std::cerr << std::flush; + if (on_complete) on_complete(); - release_global_lock(ctx); + release_global_lock(); if (ctx.arg.quick_exit) _exit(0); diff --git a/elf/mold.h b/elf/mold.h index 4fac6611..51b218eb 100644 --- a/elf/mold.h +++ b/elf/mold.h @@ -1343,13 +1343,6 @@ template [[noreturn]] void process_run_subcommand(Context &ctx, int argc, char **argv); -// -// jobs.cc -// - -template void acquire_global_lock(Context &ctx); -template void release_global_lock(Context &ctx); - // // cmdline.cc // @@ -1770,7 +1763,6 @@ struct Context { std::vector dynamic_list_patterns; i64 default_version = VER_NDX_UNSPECIFIED; i64 page_size = E::page_size; - std::optional global_lock_fd; // Reader context bool as_needed = false;