authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-08-05 22:48:50+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-08-06 21:50:16+01:00
loge17a050bc695f7d117b89adb1d258813593ca111
treef281876485848c783fb9e2a10616b98cda0b2e09
parent3de8bbd3d4e262df11a582fb52401b8077b5f352

link: prevent deadlock when prelink tasks fail

If an error occured which prevented a prelink task from being queued, then `pending_prelink_tasks` would never be decremented, which could cause deadlocks in some cases. So, instead of calculating ahead of time the number of prelink tasks to expect, we use a simpler strategy which is much like a wait group: we add 1 to a value when we spawn a worker, and in the worker function, `defer` decrementing the value. The initial value is 1, and there's a decrement after all of the workers are spawned, so once it hits 0, prelink is done (be it with a failure or a success).

5 files changed, 90 insertions(+), 69 deletions(-)

src/Compilation.zig+45-36
......@@ -2384,7 +2384,6 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
23842384 };
23852385 comp.c_object_table.putAssumeCapacityNoClobber(c_object, {});
23862386 }
2387 comp.link_task_queue.pending_prelink_tasks += @intCast(comp.c_object_table.count());
23882387
23892388 // Add a `Win32Resource` for each `rc_source_files` and one for `manifest_file`.
23902389 const win32_resource_count =
......@@ -2392,10 +2391,6 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
23922391 if (win32_resource_count > 0) {
23932392 dev.check(.win32_resource);
23942393 try comp.win32_resource_table.ensureTotalCapacity(gpa, win32_resource_count);
2395 // Add this after adding logic to updateWin32Resource to pass the
2396 // result into link.loadInput. loadInput integration is not implemented
2397 // for Windows linking logic yet.
2398 //comp.link_task_queue.pending_prelink_tasks += @intCast(win32_resource_count);
23992394 for (options.rc_source_files) |rc_source_file| {
24002395 const win32_resource = try gpa.create(Win32Resource);
24012396 errdefer gpa.destroy(win32_resource);
......@@ -2454,58 +2449,47 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
24542449
24552450 if (musl.needsCrt0(comp.config.output_mode, comp.config.link_mode, comp.config.pie)) |f| {
24562451 comp.queued_jobs.musl_crt_file[@intFromEnum(f)] = true;
2457 comp.link_task_queue.pending_prelink_tasks += 1;
24582452 }
24592453 switch (comp.config.link_mode) {
24602454 .static => comp.queued_jobs.musl_crt_file[@intFromEnum(musl.CrtFile.libc_a)] = true,
24612455 .dynamic => comp.queued_jobs.musl_crt_file[@intFromEnum(musl.CrtFile.libc_so)] = true,
24622456 }
2463 comp.link_task_queue.pending_prelink_tasks += 1;
24642457 } else if (target.isGnuLibC()) {
24652458 if (!std.zig.target.canBuildLibC(target)) return error.LibCUnavailable;
24662459
24672460 if (glibc.needsCrt0(comp.config.output_mode)) |f| {
24682461 comp.queued_jobs.glibc_crt_file[@intFromEnum(f)] = true;
2469 comp.link_task_queue.pending_prelink_tasks += 1;
24702462 }
24712463 comp.queued_jobs.glibc_shared_objects = true;
2472 comp.link_task_queue.pending_prelink_tasks += glibc.sharedObjectsCount(target);
24732464
24742465 comp.queued_jobs.glibc_crt_file[@intFromEnum(glibc.CrtFile.libc_nonshared_a)] = true;
2475 comp.link_task_queue.pending_prelink_tasks += 1;
24762466 } else if (target.isFreeBSDLibC()) {
24772467 if (!std.zig.target.canBuildLibC(target)) return error.LibCUnavailable;
24782468
24792469 if (freebsd.needsCrt0(comp.config.output_mode)) |f| {
24802470 comp.queued_jobs.freebsd_crt_file[@intFromEnum(f)] = true;
2481 comp.link_task_queue.pending_prelink_tasks += 1;
24822471 }
24832472
24842473 comp.queued_jobs.freebsd_shared_objects = true;
2485 comp.link_task_queue.pending_prelink_tasks += freebsd.sharedObjectsCount();
24862474 } else if (target.isNetBSDLibC()) {
24872475 if (!std.zig.target.canBuildLibC(target)) return error.LibCUnavailable;
24882476
24892477 if (netbsd.needsCrt0(comp.config.output_mode)) |f| {
24902478 comp.queued_jobs.netbsd_crt_file[@intFromEnum(f)] = true;
2491 comp.link_task_queue.pending_prelink_tasks += 1;
24922479 }
24932480
24942481 comp.queued_jobs.netbsd_shared_objects = true;
2495 comp.link_task_queue.pending_prelink_tasks += netbsd.sharedObjectsCount();
24962482 } else if (target.isWasiLibC()) {
24972483 if (!std.zig.target.canBuildLibC(target)) return error.LibCUnavailable;
24982484
24992485 comp.queued_jobs.wasi_libc_crt_file[@intFromEnum(wasi_libc.execModelCrtFile(comp.config.wasi_exec_model))] = true;
25002486 comp.queued_jobs.wasi_libc_crt_file[@intFromEnum(wasi_libc.CrtFile.libc_a)] = true;
2501 comp.link_task_queue.pending_prelink_tasks += 2;
25022487 } else if (target.isMinGW()) {
25032488 if (!std.zig.target.canBuildLibC(target)) return error.LibCUnavailable;
25042489
25052490 const main_crt_file: mingw.CrtFile = if (is_dyn_lib) .dllcrt2_o else .crt2_o;
25062491 comp.queued_jobs.mingw_crt_file[@intFromEnum(main_crt_file)] = true;
25072492 comp.queued_jobs.mingw_crt_file[@intFromEnum(mingw.CrtFile.libmingw32_lib)] = true;
2508 comp.link_task_queue.pending_prelink_tasks += 2;
25092493
25102494 // When linking mingw-w64 there are some import libs we always need.
25112495 try comp.windows_libs.ensureUnusedCapacity(gpa, mingw.always_link_libs.len);
......@@ -2519,7 +2503,6 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
25192503 target.isMinGW())
25202504 {
25212505 comp.queued_jobs.zigc_lib = true;
2522 comp.link_task_queue.pending_prelink_tasks += 1;
25232506 }
25242507 }
25252508
......@@ -2536,50 +2519,41 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
25362519 }
25372520 if (comp.wantBuildLibUnwindFromSource()) {
25382521 comp.queued_jobs.libunwind = true;
2539 comp.link_task_queue.pending_prelink_tasks += 1;
25402522 }
25412523 if (build_options.have_llvm and is_exe_or_dyn_lib and comp.config.link_libcpp) {
25422524 comp.queued_jobs.libcxx = true;
25432525 comp.queued_jobs.libcxxabi = true;
2544 comp.link_task_queue.pending_prelink_tasks += 2;
25452526 }
25462527 if (build_options.have_llvm and is_exe_or_dyn_lib and comp.config.any_sanitize_thread) {
25472528 comp.queued_jobs.libtsan = true;
2548 comp.link_task_queue.pending_prelink_tasks += 1;
25492529 }
25502530
25512531 if (can_build_compiler_rt) {
25522532 if (comp.compiler_rt_strat == .lib) {
25532533 log.debug("queuing a job to build compiler_rt_lib", .{});
25542534 comp.queued_jobs.compiler_rt_lib = true;
2555 comp.link_task_queue.pending_prelink_tasks += 1;
25562535 } else if (comp.compiler_rt_strat == .obj) {
25572536 log.debug("queuing a job to build compiler_rt_obj", .{});
25582537 // In this case we are making a static library, so we ask
25592538 // for a compiler-rt object to put in it.
25602539 comp.queued_jobs.compiler_rt_obj = true;
2561 comp.link_task_queue.pending_prelink_tasks += 1;
25622540 } else if (comp.compiler_rt_strat == .dyn_lib) {
25632541 // hack for stage2_x86_64 + coff
25642542 log.debug("queuing a job to build compiler_rt_dyn_lib", .{});
25652543 comp.queued_jobs.compiler_rt_dyn_lib = true;
2566 comp.link_task_queue.pending_prelink_tasks += 1;
25672544 }
25682545
25692546 if (comp.ubsan_rt_strat == .lib) {
25702547 log.debug("queuing a job to build ubsan_rt_lib", .{});
25712548 comp.queued_jobs.ubsan_rt_lib = true;
2572 comp.link_task_queue.pending_prelink_tasks += 1;
25732549 } else if (comp.ubsan_rt_strat == .obj) {
25742550 log.debug("queuing a job to build ubsan_rt_obj", .{});
25752551 comp.queued_jobs.ubsan_rt_obj = true;
2576 comp.link_task_queue.pending_prelink_tasks += 1;
25772552 }
25782553
25792554 if (is_exe_or_dyn_lib and comp.config.any_fuzz) {
25802555 log.debug("queuing a job to build libfuzzer", .{});
25812556 comp.queued_jobs.fuzzer_lib = true;
2582 comp.link_task_queue.pending_prelink_tasks += 1;
25832557 }
25842558 }
25852559 }
......@@ -2587,8 +2561,6 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
25872561 try comp.link_task_queue.queued_prelink.append(gpa, .load_explicitly_provided);
25882562 }
25892563 log.debug("queued prelink tasks: {d}", .{comp.link_task_queue.queued_prelink.items.len});
2590 log.debug("pending prelink tasks: {d}", .{comp.link_task_queue.pending_prelink_tasks});
2591
25922564 return comp;
25932565}
25942566
......@@ -4408,10 +4380,8 @@ fn performAllTheWork(
44084380 comp.link_task_wait_group.reset();
44094381 defer comp.link_task_wait_group.wait();
44104382
4411 comp.link_prog_node.increaseEstimatedTotalItems(
4412 comp.link_task_queue.queued_prelink.items.len + // already queued prelink tasks
4413 comp.link_task_queue.pending_prelink_tasks, // prelink tasks which will be queued
4414 );
4383 // Already-queued prelink tasks
4384 comp.link_prog_node.increaseEstimatedTotalItems(comp.link_task_queue.queued_prelink.items.len);
44154385 comp.link_task_queue.start(comp);
44164386
44174387 if (comp.emit_docs != null) {
......@@ -4427,6 +4397,7 @@ fn performAllTheWork(
44274397 // compiler-rt due to LLD bugs as well, e.g.:
44284398 //
44294399 // https://github.com/llvm/llvm-project/issues/43698#issuecomment-2542660611
4400 comp.link_task_queue.startPrelinkItem();
44304401 comp.link_task_wait_group.spawnManager(buildRt, .{
44314402 comp,
44324403 "compiler_rt.zig",
......@@ -4444,6 +4415,7 @@ fn performAllTheWork(
44444415 }
44454416
44464417 if (comp.queued_jobs.compiler_rt_obj and comp.compiler_rt_obj == null) {
4418 comp.link_task_queue.startPrelinkItem();
44474419 comp.link_task_wait_group.spawnManager(buildRt, .{
44484420 comp,
44494421 "compiler_rt.zig",
......@@ -4462,6 +4434,7 @@ fn performAllTheWork(
44624434
44634435 // hack for stage2_x86_64 + coff
44644436 if (comp.queued_jobs.compiler_rt_dyn_lib and comp.compiler_rt_dyn_lib == null) {
4437 comp.link_task_queue.startPrelinkItem();
44654438 comp.link_task_wait_group.spawnManager(buildRt, .{
44664439 comp,
44674440 "compiler_rt.zig",
......@@ -4479,6 +4452,7 @@ fn performAllTheWork(
44794452 }
44804453
44814454 if (comp.queued_jobs.fuzzer_lib and comp.fuzzer_lib == null) {
4455 comp.link_task_queue.startPrelinkItem();
44824456 comp.link_task_wait_group.spawnManager(buildRt, .{
44834457 comp,
44844458 "fuzzer.zig",
......@@ -4493,6 +4467,7 @@ fn performAllTheWork(
44934467 }
44944468
44954469 if (comp.queued_jobs.ubsan_rt_lib and comp.ubsan_rt_lib == null) {
4470 comp.link_task_queue.startPrelinkItem();
44964471 comp.link_task_wait_group.spawnManager(buildRt, .{
44974472 comp,
44984473 "ubsan_rt.zig",
......@@ -4509,6 +4484,7 @@ fn performAllTheWork(
45094484 }
45104485
45114486 if (comp.queued_jobs.ubsan_rt_obj and comp.ubsan_rt_obj == null) {
4487 comp.link_task_queue.startPrelinkItem();
45124488 comp.link_task_wait_group.spawnManager(buildRt, .{
45134489 comp,
45144490 "ubsan_rt.zig",
......@@ -4525,40 +4501,49 @@ fn performAllTheWork(
45254501 }
45264502
45274503 if (comp.queued_jobs.glibc_shared_objects) {
4504 comp.link_task_queue.startPrelinkItem();
45284505 comp.link_task_wait_group.spawnManager(buildGlibcSharedObjects, .{ comp, main_progress_node });
45294506 }
45304507
45314508 if (comp.queued_jobs.freebsd_shared_objects) {
4509 comp.link_task_queue.startPrelinkItem();
45324510 comp.link_task_wait_group.spawnManager(buildFreeBSDSharedObjects, .{ comp, main_progress_node });
45334511 }
45344512
45354513 if (comp.queued_jobs.netbsd_shared_objects) {
4514 comp.link_task_queue.startPrelinkItem();
45364515 comp.link_task_wait_group.spawnManager(buildNetBSDSharedObjects, .{ comp, main_progress_node });
45374516 }
45384517
45394518 if (comp.queued_jobs.libunwind) {
4519 comp.link_task_queue.startPrelinkItem();
45404520 comp.link_task_wait_group.spawnManager(buildLibUnwind, .{ comp, main_progress_node });
45414521 }
45424522
45434523 if (comp.queued_jobs.libcxx) {
4524 comp.link_task_queue.startPrelinkItem();
45444525 comp.link_task_wait_group.spawnManager(buildLibCxx, .{ comp, main_progress_node });
45454526 }
45464527
45474528 if (comp.queued_jobs.libcxxabi) {
4529 comp.link_task_queue.startPrelinkItem();
45484530 comp.link_task_wait_group.spawnManager(buildLibCxxAbi, .{ comp, main_progress_node });
45494531 }
45504532
45514533 if (comp.queued_jobs.libtsan) {
4534 comp.link_task_queue.startPrelinkItem();
45524535 comp.link_task_wait_group.spawnManager(buildLibTsan, .{ comp, main_progress_node });
45534536 }
45544537
45554538 if (comp.queued_jobs.zigc_lib and comp.zigc_static_lib == null) {
4539 comp.link_task_queue.startPrelinkItem();
45564540 comp.link_task_wait_group.spawnManager(buildLibZigC, .{ comp, main_progress_node });
45574541 }
45584542
45594543 for (0..@typeInfo(musl.CrtFile).@"enum".fields.len) |i| {
45604544 if (comp.queued_jobs.musl_crt_file[i]) {
45614545 const tag: musl.CrtFile = @enumFromInt(i);
4546 comp.link_task_queue.startPrelinkItem();
45624547 comp.link_task_wait_group.spawnManager(buildMuslCrtFile, .{ comp, tag, main_progress_node });
45634548 }
45644549 }
......@@ -4566,6 +4551,7 @@ fn performAllTheWork(
45664551 for (0..@typeInfo(glibc.CrtFile).@"enum".fields.len) |i| {
45674552 if (comp.queued_jobs.glibc_crt_file[i]) {
45684553 const tag: glibc.CrtFile = @enumFromInt(i);
4554 comp.link_task_queue.startPrelinkItem();
45694555 comp.link_task_wait_group.spawnManager(buildGlibcCrtFile, .{ comp, tag, main_progress_node });
45704556 }
45714557 }
......@@ -4573,6 +4559,7 @@ fn performAllTheWork(
45734559 for (0..@typeInfo(freebsd.CrtFile).@"enum".fields.len) |i| {
45744560 if (comp.queued_jobs.freebsd_crt_file[i]) {
45754561 const tag: freebsd.CrtFile = @enumFromInt(i);
4562 comp.link_task_queue.startPrelinkItem();
45764563 comp.link_task_wait_group.spawnManager(buildFreeBSDCrtFile, .{ comp, tag, main_progress_node });
45774564 }
45784565 }
......@@ -4580,6 +4567,7 @@ fn performAllTheWork(
45804567 for (0..@typeInfo(netbsd.CrtFile).@"enum".fields.len) |i| {
45814568 if (comp.queued_jobs.netbsd_crt_file[i]) {
45824569 const tag: netbsd.CrtFile = @enumFromInt(i);
4570 comp.link_task_queue.startPrelinkItem();
45834571 comp.link_task_wait_group.spawnManager(buildNetBSDCrtFile, .{ comp, tag, main_progress_node });
45844572 }
45854573 }
......@@ -4587,6 +4575,7 @@ fn performAllTheWork(
45874575 for (0..@typeInfo(wasi_libc.CrtFile).@"enum".fields.len) |i| {
45884576 if (comp.queued_jobs.wasi_libc_crt_file[i]) {
45894577 const tag: wasi_libc.CrtFile = @enumFromInt(i);
4578 comp.link_task_queue.startPrelinkItem();
45904579 comp.link_task_wait_group.spawnManager(buildWasiLibcCrtFile, .{ comp, tag, main_progress_node });
45914580 }
45924581 }
......@@ -4594,6 +4583,7 @@ fn performAllTheWork(
45944583 for (0..@typeInfo(mingw.CrtFile).@"enum".fields.len) |i| {
45954584 if (comp.queued_jobs.mingw_crt_file[i]) {
45964585 const tag: mingw.CrtFile = @enumFromInt(i);
4586 comp.link_task_queue.startPrelinkItem();
45974587 comp.link_task_wait_group.spawnManager(buildMingwCrtFile, .{ comp, tag, main_progress_node });
45984588 }
45994589 }
......@@ -4665,12 +4655,14 @@ fn performAllTheWork(
46654655 }
46664656
46674657 while (comp.c_object_work_queue.readItem()) |c_object| {
4658 comp.link_task_queue.startPrelinkItem();
46684659 comp.thread_pool.spawnWg(&comp.link_task_wait_group, workerUpdateCObject, .{
46694660 comp, c_object, main_progress_node,
46704661 });
46714662 }
46724663
46734664 while (comp.win32_resource_work_queue.readItem()) |win32_resource| {
4665 comp.link_task_queue.startPrelinkItem();
46744666 comp.thread_pool.spawnWg(&comp.link_task_wait_group, workerUpdateWin32Resource, .{
46754667 comp, win32_resource, main_progress_node,
46764668 });
......@@ -4773,15 +4765,14 @@ fn performAllTheWork(
47734765 }
47744766 };
47754767
4768 // We aren't going to queue any more prelink tasks.
4769 comp.link_task_queue.finishPrelinkItem(comp);
4770
47764771 if (!comp.separateCodegenThreadOk()) {
47774772 // Waits until all input files have been parsed.
47784773 comp.link_task_wait_group.wait();
47794774 comp.link_task_wait_group.reset();
47804775 std.log.scoped(.link).debug("finished waiting for link_task_wait_group", .{});
4781 if (comp.link_task_queue.pending_prelink_tasks > 0) {
4782 // Indicates an error occurred preventing prelink phase from completing.
4783 return;
4784 }
47854776 }
47864777
47874778 if (comp.zcu != null) {
......@@ -5568,6 +5559,7 @@ fn workerUpdateCObject(
55685559 c_object: *CObject,
55695560 progress_node: std.Progress.Node,
55705561) void {
5562 defer comp.link_task_queue.finishPrelinkItem(comp);
55715563 comp.updateCObject(c_object, progress_node) catch |err| switch (err) {
55725564 error.AnalysisFail => return,
55735565 else => {
......@@ -5585,6 +5577,7 @@ fn workerUpdateWin32Resource(
55855577 win32_resource: *Win32Resource,
55865578 progress_node: std.Progress.Node,
55875579) void {
5580 defer comp.link_task_queue.finishPrelinkItem(comp);
55885581 comp.updateWin32Resource(win32_resource, progress_node) catch |err| switch (err) {
55895582 error.AnalysisFail => return,
55905583 else => {
......@@ -5628,6 +5621,7 @@ fn buildRt(
56285621 options: RtOptions,
56295622 out: *?CrtFile,
56305623) void {
5624 defer comp.link_task_queue.finishPrelinkItem(comp);
56315625 comp.buildOutputFromZig(
56325626 root_source_name,
56335627 root_name,
......@@ -5646,6 +5640,7 @@ fn buildRt(
56465640}
56475641
56485642fn buildMuslCrtFile(comp: *Compilation, crt_file: musl.CrtFile, prog_node: std.Progress.Node) void {
5643 defer comp.link_task_queue.finishPrelinkItem(comp);
56495644 if (musl.buildCrtFile(comp, crt_file, prog_node)) |_| {
56505645 comp.queued_jobs.musl_crt_file[@intFromEnum(crt_file)] = false;
56515646 } else |err| switch (err) {
......@@ -5657,6 +5652,7 @@ fn buildMuslCrtFile(comp: *Compilation, crt_file: musl.CrtFile, prog_node: std.P
56575652}
56585653
56595654fn buildGlibcCrtFile(comp: *Compilation, crt_file: glibc.CrtFile, prog_node: std.Progress.Node) void {
5655 defer comp.link_task_queue.finishPrelinkItem(comp);
56605656 if (glibc.buildCrtFile(comp, crt_file, prog_node)) |_| {
56615657 comp.queued_jobs.glibc_crt_file[@intFromEnum(crt_file)] = false;
56625658 } else |err| switch (err) {
......@@ -5668,6 +5664,7 @@ fn buildGlibcCrtFile(comp: *Compilation, crt_file: glibc.CrtFile, prog_node: std
56685664}
56695665
56705666fn buildGlibcSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) void {
5667 defer comp.link_task_queue.finishPrelinkItem(comp);
56715668 if (glibc.buildSharedObjects(comp, prog_node)) |_| {
56725669 // The job should no longer be queued up since it succeeded.
56735670 comp.queued_jobs.glibc_shared_objects = false;
......@@ -5680,6 +5677,7 @@ fn buildGlibcSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) voi
56805677}
56815678
56825679fn buildFreeBSDCrtFile(comp: *Compilation, crt_file: freebsd.CrtFile, prog_node: std.Progress.Node) void {
5680 defer comp.link_task_queue.finishPrelinkItem(comp);
56835681 if (freebsd.buildCrtFile(comp, crt_file, prog_node)) |_| {
56845682 comp.queued_jobs.freebsd_crt_file[@intFromEnum(crt_file)] = false;
56855683 } else |err| switch (err) {
......@@ -5691,6 +5689,7 @@ fn buildFreeBSDCrtFile(comp: *Compilation, crt_file: freebsd.CrtFile, prog_node:
56915689}
56925690
56935691fn buildFreeBSDSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) void {
5692 defer comp.link_task_queue.finishPrelinkItem(comp);
56945693 if (freebsd.buildSharedObjects(comp, prog_node)) |_| {
56955694 // The job should no longer be queued up since it succeeded.
56965695 comp.queued_jobs.freebsd_shared_objects = false;
......@@ -5703,6 +5702,7 @@ fn buildFreeBSDSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) v
57035702}
57045703
57055704fn buildNetBSDCrtFile(comp: *Compilation, crt_file: netbsd.CrtFile, prog_node: std.Progress.Node) void {
5705 defer comp.link_task_queue.finishPrelinkItem(comp);
57065706 if (netbsd.buildCrtFile(comp, crt_file, prog_node)) |_| {
57075707 comp.queued_jobs.netbsd_crt_file[@intFromEnum(crt_file)] = false;
57085708 } else |err| switch (err) {
......@@ -5714,6 +5714,7 @@ fn buildNetBSDCrtFile(comp: *Compilation, crt_file: netbsd.CrtFile, prog_node: s
57145714}
57155715
57165716fn buildNetBSDSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) void {
5717 defer comp.link_task_queue.finishPrelinkItem(comp);
57175718 if (netbsd.buildSharedObjects(comp, prog_node)) |_| {
57185719 // The job should no longer be queued up since it succeeded.
57195720 comp.queued_jobs.netbsd_shared_objects = false;
......@@ -5726,6 +5727,7 @@ fn buildNetBSDSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) vo
57265727}
57275728
57285729fn buildMingwCrtFile(comp: *Compilation, crt_file: mingw.CrtFile, prog_node: std.Progress.Node) void {
5730 defer comp.link_task_queue.finishPrelinkItem(comp);
57295731 if (mingw.buildCrtFile(comp, crt_file, prog_node)) |_| {
57305732 comp.queued_jobs.mingw_crt_file[@intFromEnum(crt_file)] = false;
57315733 } else |err| switch (err) {
......@@ -5737,6 +5739,7 @@ fn buildMingwCrtFile(comp: *Compilation, crt_file: mingw.CrtFile, prog_node: std
57375739}
57385740
57395741fn buildWasiLibcCrtFile(comp: *Compilation, crt_file: wasi_libc.CrtFile, prog_node: std.Progress.Node) void {
5742 defer comp.link_task_queue.finishPrelinkItem(comp);
57405743 if (wasi_libc.buildCrtFile(comp, crt_file, prog_node)) |_| {
57415744 comp.queued_jobs.wasi_libc_crt_file[@intFromEnum(crt_file)] = false;
57425745 } else |err| switch (err) {
......@@ -5748,6 +5751,7 @@ fn buildWasiLibcCrtFile(comp: *Compilation, crt_file: wasi_libc.CrtFile, prog_no
57485751}
57495752
57505753fn buildLibUnwind(comp: *Compilation, prog_node: std.Progress.Node) void {
5754 defer comp.link_task_queue.finishPrelinkItem(comp);
57515755 if (libunwind.buildStaticLib(comp, prog_node)) |_| {
57525756 comp.queued_jobs.libunwind = false;
57535757 } else |err| switch (err) {
......@@ -5757,6 +5761,7 @@ fn buildLibUnwind(comp: *Compilation, prog_node: std.Progress.Node) void {
57575761}
57585762
57595763fn buildLibCxx(comp: *Compilation, prog_node: std.Progress.Node) void {
5764 defer comp.link_task_queue.finishPrelinkItem(comp);
57605765 if (libcxx.buildLibCxx(comp, prog_node)) |_| {
57615766 comp.queued_jobs.libcxx = false;
57625767 } else |err| switch (err) {
......@@ -5766,6 +5771,7 @@ fn buildLibCxx(comp: *Compilation, prog_node: std.Progress.Node) void {
57665771}
57675772
57685773fn buildLibCxxAbi(comp: *Compilation, prog_node: std.Progress.Node) void {
5774 defer comp.link_task_queue.finishPrelinkItem(comp);
57695775 if (libcxx.buildLibCxxAbi(comp, prog_node)) |_| {
57705776 comp.queued_jobs.libcxxabi = false;
57715777 } else |err| switch (err) {
......@@ -5775,6 +5781,7 @@ fn buildLibCxxAbi(comp: *Compilation, prog_node: std.Progress.Node) void {
57755781}
57765782
57775783fn buildLibTsan(comp: *Compilation, prog_node: std.Progress.Node) void {
5784 defer comp.link_task_queue.finishPrelinkItem(comp);
57785785 if (libtsan.buildTsan(comp, prog_node)) |_| {
57795786 comp.queued_jobs.libtsan = false;
57805787 } else |err| switch (err) {
......@@ -5784,6 +5791,7 @@ fn buildLibTsan(comp: *Compilation, prog_node: std.Progress.Node) void {
57845791}
57855792
57865793fn buildLibZigC(comp: *Compilation, prog_node: std.Progress.Node) void {
5794 defer comp.link_task_queue.finishPrelinkItem(comp);
57875795 comp.buildOutputFromZig(
57885796 "c.zig",
57895797 "zigc",
......@@ -7721,6 +7729,7 @@ pub fn queuePrelinkTaskMode(comp: *Compilation, path: Cache.Path, config: *const
77217729/// Only valid to call during `update`. Automatically handles queuing up a
77227730/// linker worker task if there is not already one.
77237731pub fn queuePrelinkTasks(comp: *Compilation, tasks: []const link.PrelinkTask) void {
7732 comp.link_prog_node.increaseEstimatedTotalItems(tasks.len);
77247733 comp.link_task_queue.enqueuePrelink(comp, tasks) catch |err| switch (err) {
77257734 error.OutOfMemory => return comp.setAllocFailure(),
77267735 };
src/libs/freebsd.zig-4
......@@ -977,10 +977,6 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
977977 });
978978}
979979
980pub fn sharedObjectsCount() u8 {
981 return libs.len;
982}
983
984980fn queueSharedObjects(comp: *Compilation, so_files: BuiltSharedObjects) void {
985981 assert(comp.freebsd_so_files == null);
986982 comp.freebsd_so_files = so_files;
src/libs/glibc.zig-12
......@@ -1130,18 +1130,6 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
11301130 });
11311131}
11321132
1133pub fn sharedObjectsCount(target: *const std.Target) u8 {
1134 const target_version = target.os.versionRange().gnuLibCVersion() orelse return 0;
1135 var count: u8 = 0;
1136 for (libs) |lib| {
1137 if (lib.removed_in) |rem_in| {
1138 if (target_version.order(rem_in) != .lt) continue;
1139 }
1140 count += 1;
1141 }
1142 return count;
1143}
1144
11451133fn queueSharedObjects(comp: *Compilation, so_files: BuiltSharedObjects) void {
11461134 const target_version = comp.getTarget().os.versionRange().gnuLibCVersion().?;
11471135
src/libs/netbsd.zig-4
......@@ -642,10 +642,6 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
642642 });
643643}
644644
645pub fn sharedObjectsCount() u8 {
646 return libs.len;
647}
648
649645fn queueSharedObjects(comp: *Compilation, so_files: BuiltSharedObjects) void {
650646 assert(comp.netbsd_so_files == null);
651647 comp.netbsd_so_files = so_files;
src/link/Queue.zig+45-13
......@@ -16,9 +16,9 @@ mutex: std.Thread.Mutex,
1616/// Validates that only one `flushTaskQueue` thread is running at a time.
1717flush_safety: std.debug.SafetyLock,
1818
19/// This is the number of prelink tasks which are expected but have not yet been enqueued.
20/// Guarded by `mutex`.
21pending_prelink_tasks: u32,
19/// This value is positive while there are still prelink tasks yet to be queued. Once they are
20/// all queued, this value becomes 0, and ZCU tasks can be run. Guarded by `mutex`.
21prelink_wait_count: u32,
2222
2323/// Prelink tasks which have been enqueued and are not yet owned by the worker thread.
2424/// Allocated into `gpa`, guarded by `mutex`.
......@@ -59,7 +59,7 @@ state: union(enum) {
5959 /// The link thread is currently running or queued to run.
6060 running,
6161 /// The link thread is not running or queued, because it has exhausted all immediately available
62 /// tasks. It should be spawned when more tasks are enqueued. If `pending_prelink_tasks` is not
62 /// tasks. It should be spawned when more tasks are enqueued. If `prelink_wait_count` is not
6363 /// zero, we are specifically waiting for prelink tasks.
6464 finished,
6565 /// The link thread is not running or queued, because it is waiting for this MIR to be populated.
......@@ -73,11 +73,11 @@ state: union(enum) {
7373const max_air_bytes_in_flight = 10 * 1024 * 1024;
7474
7575/// The initial `Queue` state, containing no tasks, expecting no prelink tasks, and with no running worker thread.
76/// The `pending_prelink_tasks` and `queued_prelink` fields may be modified as needed before calling `start`.
76/// The `queued_prelink` field may be appended to before calling `start`.
7777pub const empty: Queue = .{
7878 .mutex = .{},
7979 .flush_safety = .{},
80 .pending_prelink_tasks = 0,
80 .prelink_wait_count = undefined, // set in `start`
8181 .queued_prelink = .empty,
8282 .wip_prelink = .empty,
8383 .queued_zcu = .empty,
......@@ -100,17 +100,49 @@ pub fn deinit(q: *Queue, comp: *Compilation) void {
100100}
101101
102102/// This is expected to be called exactly once, after which the caller must not directly access
103/// `queued_prelink` or `pending_prelink_tasks` any longer. This will spawn the link thread if
104/// necessary.
103/// `queued_prelink` any longer. This will spawn the link thread if necessary.
105104pub fn start(q: *Queue, comp: *Compilation) void {
106105 assert(q.state == .finished);
107106 assert(q.queued_zcu.items.len == 0);
107 // Reset this to 1. We can't init it to 1 in `empty`, because it would fall to 0 on successive
108 // incremental updates, but we still need the initial 1.
109 q.prelink_wait_count = 1;
108110 if (q.queued_prelink.items.len != 0) {
109111 q.state = .running;
110112 comp.thread_pool.spawnWgId(&comp.link_task_wait_group, flushTaskQueue, .{ q, comp });
111113 }
112114}
113115
116/// Every call to this must be paired with a call to `finishPrelinkItem`.
117pub fn startPrelinkItem(q: *Queue) void {
118 q.mutex.lock();
119 defer q.mutex.unlock();
120 assert(q.prelink_wait_count > 0); // must not have finished everything already
121 q.prelink_wait_count += 1;
122}
123/// This function must be called exactly one more time than `startPrelinkItem` is. The final call
124/// indicates that we have finished calling `startPrelinkItem`, so once all pending items finish,
125/// we are ready to move on to ZCU tasks.
126pub fn finishPrelinkItem(q: *Queue, comp: *Compilation) void {
127 {
128 q.mutex.lock();
129 defer q.mutex.unlock();
130 q.prelink_wait_count -= 1;
131 if (q.prelink_wait_count != 0) return;
132 // The prelink task count dropped to 0; restart the linker thread if necessary.
133 switch (q.state) {
134 .wait_for_mir => unreachable, // we've not started zcu tasks yet
135 .running => return,
136 .finished => {},
137 }
138 assert(q.queued_prelink.items.len == 0);
139 // Even if there are no ZCU tasks, we must restart the linker thread to make sure
140 // that `link.File.prelink()` is called.
141 q.state = .running;
142 }
143 comp.thread_pool.spawnWgId(&comp.link_task_wait_group, flushTaskQueue, .{ q, comp });
144}
145
114146/// Called by codegen workers after they have populated a `ZcuTask.LinkFunc.SharedMir`. If the link
115147/// thread was waiting for this MIR, it can resume.
116148pub fn mirReady(q: *Queue, comp: *Compilation, func_index: InternPool.Index, mir: *ZcuTask.LinkFunc.SharedMir) void {
......@@ -130,14 +162,14 @@ pub fn mirReady(q: *Queue, comp: *Compilation, func_index: InternPool.Index, mir
130162 comp.thread_pool.spawnWgId(&comp.link_task_wait_group, flushTaskQueue, .{ q, comp });
131163}
132164
133/// Enqueues all prelink tasks in `tasks`. Asserts that they were expected, i.e. that `tasks.len` is
134/// less than or equal to `q.pending_prelink_tasks`. Also asserts that `tasks.len` is not 0.
165/// Enqueues all prelink tasks in `tasks`. Asserts that they were expected, i.e. that
166/// `prelink_wait_count` is not yet 0. Also asserts that `tasks.len` is not 0.
135167pub fn enqueuePrelink(q: *Queue, comp: *Compilation, tasks: []const PrelinkTask) Allocator.Error!void {
136168 {
137169 q.mutex.lock();
138170 defer q.mutex.unlock();
171 assert(q.prelink_wait_count > 0);
139172 try q.queued_prelink.appendSlice(comp.gpa, tasks);
140 q.pending_prelink_tasks -= @intCast(tasks.len);
141173 switch (q.state) {
142174 .wait_for_mir => unreachable, // we've not started zcu tasks yet
143175 .running => return,
......@@ -167,7 +199,7 @@ pub fn enqueueZcu(q: *Queue, comp: *Compilation, task: ZcuTask) Allocator.Error!
167199 try q.queued_zcu.append(comp.gpa, task);
168200 switch (q.state) {
169201 .running, .wait_for_mir => return,
170 .finished => if (q.pending_prelink_tasks != 0) return,
202 .finished => if (q.prelink_wait_count > 0) return,
171203 }
172204 // Restart the linker thread, unless it would immediately be blocked
173205 if (task == .link_func and task.link_func.mir.status.load(.acquire) == .pending) {
......@@ -194,7 +226,7 @@ fn flushTaskQueue(tid: usize, q: *Queue, comp: *Compilation) void {
194226 defer q.mutex.unlock();
195227 std.mem.swap(std.ArrayListUnmanaged(PrelinkTask), &q.queued_prelink, &q.wip_prelink);
196228 if (q.wip_prelink.items.len == 0) {
197 if (q.pending_prelink_tasks == 0) {
229 if (q.prelink_wait_count == 0) {
198230 break :prelink; // prelink is done
199231 } else {
200232 // We're expecting more prelink tasks so can't move on to ZCU tasks.