Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/aotcompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1827,10 +1827,16 @@ static void construct_vars(Module &M, Partition &partition, StringRef suffix) {
gidxs_var->setDSOLocal(true);
}

extern "C" void lambda_trampoline(void* arg) {
std::function<void()>* func = static_cast<std::function<void()>*>(arg);
(*func)();
delete func;
template<typename CB>
static inline void schedule_uv_thread(uv_thread_t *worker, CB &&cb)
{
auto func = new CB(std::move(cb));
// Use libuv thread to avoid issues with stack sizes
uv_thread_create(worker, [] (void *arg) {
auto func = static_cast<CB*>(arg);
(*func)();
delete func;
}, func);
}

// Entrypoint to optionally-multithreaded image compilation. This handles global coordination of the threading,
Expand Down Expand Up @@ -1930,7 +1936,7 @@ static SmallVector<AOTOutputs, 16> add_output(Module &M, TargetMachine &TM, Stri
JL_TIMING(NATIVE_AOT, NATIVE_Opt);
std::vector<uv_thread_t> workers(threads);
for (unsigned i = 0; i < threads; i++) {
std::function<void()> func = [&, i]() {
schedule_uv_thread(&workers[i], [&, i]() {
LLVMContext ctx;
ctx.setDiscardValueNames(true);
// Lazily deserialize the entire module
Expand Down Expand Up @@ -1961,9 +1967,7 @@ static SmallVector<AOTOutputs, 16> add_output(Module &M, TargetMachine &TM, Stri
timers[i].construct.stopTimer();

outputs[i] = add_output_impl(*M, TM, timers[i], unopt_out, opt_out, obj_out, asm_out);
};
auto arg = new std::function<void()>(func);
uv_thread_create(&workers[i], lambda_trampoline, arg); // Use libuv thread to avoid issues with stack sizes
});
}

// Wait for all of the worker threads to finish
Expand Down