Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Mar 18, 2024
1 parent 8375741 commit e7dc90d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 38 deletions.
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
7 changes: 7 additions & 0 deletions common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,13 @@ std::filesystem::path to_abs_path(std::filesystem::path path);
std::optional<std::string_view> demangle_cpp(std::string_view name);
std::optional<std::string_view> demangle_rust(std::string_view name);

//
// jbos.cc
//

void acquire_global_lock();
void release_global_lock();

//
// compress.cc
//
Expand Down
43 changes: 16 additions & 27 deletions elf/jobs.cc → common/jobs-unix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <fcntl.h>
# include <pwd.h>
# include <sys/stat.h>
# include <sys/types.h>
# include <unistd.h>
#endif
#include <fcntl.h>
#include <pwd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

namespace mold::elf {
namespace mold {

template <typename E>
void acquire_global_lock(Context<E> &ctx) {
#ifndef _WIN32
static int lock_fd = -1;

void acquire_global_lock() {
char *jobs = getenv("MOLD_JOBS");
if (!jobs || jobs != "1"s)
return;
Expand All @@ -40,22 +39,12 @@ void acquire_global_lock(Context<E> &ctx) {

if (lockf(fd, F_LOCK, 0) == -1)
return;

ctx.global_lock_fd = fd;
#endif
lock_fd = fd;
}

template <typename E>
void release_global_lock(Context<E> &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<E> &);
template void release_global_lock(Context<E> &);

} // namespace mold::elf
} // namespace mold
6 changes: 6 additions & 0 deletions common/jobs-win32.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace mold {

void acquire_global_lock() {}
void release_global_lock() {}

} // namespace mold
5 changes: 3 additions & 2 deletions elf/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 0 additions & 8 deletions elf/mold.h
Original file line number Diff line number Diff line change
Expand Up @@ -1343,13 +1343,6 @@ template <typename E>
[[noreturn]]
void process_run_subcommand(Context<E> &ctx, int argc, char **argv);

//
// jobs.cc
//

template <typename E> void acquire_global_lock(Context<E> &ctx);
template <typename E> void release_global_lock(Context<E> &ctx);

//
// cmdline.cc
//
Expand Down Expand Up @@ -1770,7 +1763,6 @@ struct Context {
std::vector<DynamicPattern> dynamic_list_patterns;
i64 default_version = VER_NDX_UNSPECIFIED;
i64 page_size = E::page_size;
std::optional<int> global_lock_fd;

// Reader context
bool as_needed = false;
Expand Down

0 comments on commit e7dc90d

Please sign in to comment.