authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-10-23 14:46:46-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-10-23 16:27:39-07:00
log504ad56815c3deb38319ac7989dd6c50c559b780
tree29e01c26936f36e8f563a295110f5455f7af8f41
parentba71079837a071b53cab289d78e5bacb4925fd25

link.flushTaskQueue: move safety lock

The safety lock needs to happen after check()

2 files changed, 23 insertions(+), 8 deletions(-)

src/ThreadSafeQueue.zig+5-4
......@@ -52,12 +52,13 @@ pub fn ThreadSafeQueue(comptime T: type) type {
5252 self.mutex.lock();
5353 defer self.mutex.unlock();
5454 try self.shared.appendSlice(gpa, items);
55 const was_waiting = switch (self.state) {
55 return switch (self.state) {
5656 .run => false,
57 .wait => true,
57 .wait => {
58 self.state = .run;
59 return true;
60 },
5861 };
59 self.state = .run;
60 return was_waiting;
6162 }
6263
6364 /// Safe only to call exactly once when initially starting the worker.
src/link.zig+18-4
......@@ -1365,11 +1365,11 @@ pub const File = struct {
13651365/// from the rest of compilation. All tasks performed here are
13661366/// single-threaded with respect to one another.
13671367pub fn flushTaskQueue(tid: usize, comp: *Compilation) void {
1368 comp.link_task_queue_safety.lock();
1369 defer comp.link_task_queue_safety.unlock();
1370 const prog_node = comp.work_queue_progress_node.start("Parse Linker Inputs", 0);
1371 defer prog_node.end();
1368 // As soon as check() is called, another `flushTaskQueue` call could occur,
1369 // so the safety lock must go after the check.
13721370 while (comp.link_task_queue.check()) |tasks| {
1371 comp.link_task_queue_safety.lock();
1372 defer comp.link_task_queue_safety.unlock();
13731373 for (tasks) |task| doTask(comp, tid, task);
13741374 }
13751375}
......@@ -1412,6 +1412,8 @@ pub fn doTask(comp: *Compilation, tid: usize, task: Task) void {
14121412 const diags = &comp.link_diags;
14131413 switch (task) {
14141414 .load_explicitly_provided => if (comp.bin_file) |base| {
1415 const prog_node = comp.work_queue_progress_node.start("Parse Linker Inputs", comp.link_inputs.len);
1416 defer prog_node.end();
14151417 for (comp.link_inputs) |input| {
14161418 base.loadInput(input) catch |err| switch (err) {
14171419 error.LinkFailure => return, // error reported via diags
......@@ -1423,9 +1425,13 @@ pub fn doTask(comp: *Compilation, tid: usize, task: Task) void {
14231425 .dso_exact => diags.addError("failed to handle dso_exact: {s}", .{@errorName(e)}),
14241426 },
14251427 };
1428 prog_node.completeOne();
14261429 }
14271430 },
14281431 .load_host_libc => if (comp.bin_file) |base| {
1432 const prog_node = comp.work_queue_progress_node.start("Linker Parse Host libc", 0);
1433 defer prog_node.end();
1434
14291435 const target = comp.root_mod.resolved_target.result;
14301436 const flags = target_util.libcFullLinkFlags(target);
14311437 const crt_dir = comp.libc_installation.?.crt_dir.?;
......@@ -1482,18 +1488,24 @@ pub fn doTask(comp: *Compilation, tid: usize, task: Task) void {
14821488 }
14831489 },
14841490 .load_object => |path| if (comp.bin_file) |base| {
1491 const prog_node = comp.work_queue_progress_node.start("Linker Parse Object", 0);
1492 defer prog_node.end();
14851493 base.openLoadObject(path) catch |err| switch (err) {
14861494 error.LinkFailure => return, // error reported via diags
14871495 else => |e| diags.addParseError(path, "failed to parse object: {s}", .{@errorName(e)}),
14881496 };
14891497 },
14901498 .load_archive => |path| if (comp.bin_file) |base| {
1499 const prog_node = comp.work_queue_progress_node.start("Linker Parse Archive", 0);
1500 defer prog_node.end();
14911501 base.openLoadArchive(path, null) catch |err| switch (err) {
14921502 error.LinkFailure => return, // error reported via link_diags
14931503 else => |e| diags.addParseError(path, "failed to parse archive: {s}", .{@errorName(e)}),
14941504 };
14951505 },
14961506 .load_dso => |path| if (comp.bin_file) |base| {
1507 const prog_node = comp.work_queue_progress_node.start("Linker Parse Shared Library", 0);
1508 defer prog_node.end();
14971509 base.openLoadDso(path, .{
14981510 .preferred_mode = .dynamic,
14991511 .search_strategy = .paths_first,
......@@ -1503,6 +1515,8 @@ pub fn doTask(comp: *Compilation, tid: usize, task: Task) void {
15031515 };
15041516 },
15051517 .load_input => |input| if (comp.bin_file) |base| {
1518 const prog_node = comp.work_queue_progress_node.start("Linker Parse Input", 0);
1519 defer prog_node.end();
15061520 base.loadInput(input) catch |err| switch (err) {
15071521 error.LinkFailure => return, // error reported via link_diags
15081522 else => |e| {