From 24497d08bc5aa0ee1f587b93ef46bb1838cb9b33 Mon Sep 17 00:00:00 2001 From: Alois Wohlschlager Date: Fri, 10 Oct 2025 16:20:28 +0200 Subject: [PATCH] nix-eval-jobs: use std::thread As the worker processes are now properly spawned using fork-exec, they no longer inherit the stack size from the non-main coordinator thread, but get a main thread with reasonable stack size on their own. For this reason the coordinator threads can use the default stack size, and turn std::thread can be used as the only reason for the custom wrapper was to enlarge the stack. Change-Id: I1192474885abe9b0625ac483840b6e1a6a6a6964 --- .../nix-eval-jobs/src/nix-eval-jobs.cc | 52 +------------------ 1 file changed, 1 insertion(+), 51 deletions(-) diff --git a/subprojects/nix-eval-jobs/src/nix-eval-jobs.cc b/subprojects/nix-eval-jobs/src/nix-eval-jobs.cc index 0b9204475..f8a87fb86 100644 --- a/subprojects/nix-eval-jobs/src/nix-eval-jobs.cc +++ b/subprojects/nix-eval-jobs/src/nix-eval-jobs.cc @@ -92,56 +92,6 @@ struct Proc { } }; -// We'd highly prefer using std::thread here; but this won't let us configure the stack -// size. macOS uses 512KiB size stacks for non-main threads, and musl defaults to 128k. -// While Nix configures a 64MiB size for the main thread, this doesn't propagate to the -// threads we launch here. It turns out, running the evaluator under an anemic stack of -// 0.5MiB has it overflow way too quickly. Hence, we have our own custom Thread struct. -struct Thread { - pthread_t thread; - - Thread(const Thread &) = delete; - Thread(Thread &&) noexcept = default; - - Thread(std::function f) { - int s; - pthread_attr_t attr; - - auto func = std::make_unique>(std::move(f)); - - if ((s = pthread_attr_init(&attr)) != 0) { - throw SysError(s, "calling pthread_attr_init"); - } - if ((s = pthread_attr_setstacksize(&attr, 64 * 1024 * 1024)) != 0) { - throw SysError(s, "calling pthread_attr_setstacksize"); - } - if ((s = pthread_create(&thread, &attr, Thread::init, - func.release())) != 0) { - throw SysError(s, "calling pthread_launch"); - } - if ((s = pthread_attr_destroy(&attr)) != 0) { - throw SysError(s, "calling pthread_attr_destroy"); - } - } - - void join() { - int s; - s = pthread_join(thread, nullptr); - if (s != 0) { - throw SysError(s, "calling pthread_join"); - } - } - - private: - static void *init(void *ptr) { - std::unique_ptr> func; - func.reset(static_cast *>(ptr)); - - (*func)(); - return 0; - } -}; - struct State { std::set todo = JSON::array({JSON::array()}); std::set active; @@ -382,7 +332,7 @@ int main(int argc, char **argv) { Sync state_; /* Start a collector thread per worker process. */ - std::vector threads; + std::vector threads; std::condition_variable wakeup; for (size_t i = 0; i < myArgs.nrWorkers; i++) { threads.emplace_back(std::bind(collector, std::ref(myArgs),