Skip to content

Commit

Permalink
link.flushTaskQueue: move safety lock
Browse files Browse the repository at this point in the history
The safety lock needs to happen after check()
  • Loading branch information
andrewrk committed Oct 23, 2024
1 parent ba71079 commit 504ad56
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/ThreadSafeQueue.zig
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ pub fn ThreadSafeQueue(comptime T: type) type {
self.mutex.lock();
defer self.mutex.unlock();
try self.shared.appendSlice(gpa, items);
const was_waiting = switch (self.state) {
return switch (self.state) {
.run => false,
.wait => true,
.wait => {
self.state = .run;
return true;
},
};
self.state = .run;
return was_waiting;
}

/// Safe only to call exactly once when initially starting the worker.
Expand Down
22 changes: 18 additions & 4 deletions src/link.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1365,11 +1365,11 @@ pub const File = struct {
/// from the rest of compilation. All tasks performed here are
/// single-threaded with respect to one another.
pub fn flushTaskQueue(tid: usize, comp: *Compilation) void {
comp.link_task_queue_safety.lock();
defer comp.link_task_queue_safety.unlock();
const prog_node = comp.work_queue_progress_node.start("Parse Linker Inputs", 0);
defer prog_node.end();
// As soon as check() is called, another `flushTaskQueue` call could occur,
// so the safety lock must go after the check.
while (comp.link_task_queue.check()) |tasks| {
comp.link_task_queue_safety.lock();
defer comp.link_task_queue_safety.unlock();
for (tasks) |task| doTask(comp, tid, task);
}
}
Expand Down Expand Up @@ -1412,6 +1412,8 @@ pub fn doTask(comp: *Compilation, tid: usize, task: Task) void {
const diags = &comp.link_diags;
switch (task) {
.load_explicitly_provided => if (comp.bin_file) |base| {
const prog_node = comp.work_queue_progress_node.start("Parse Linker Inputs", comp.link_inputs.len);
defer prog_node.end();
for (comp.link_inputs) |input| {
base.loadInput(input) catch |err| switch (err) {
error.LinkFailure => return, // error reported via diags
Expand All @@ -1423,9 +1425,13 @@ pub fn doTask(comp: *Compilation, tid: usize, task: Task) void {
.dso_exact => diags.addError("failed to handle dso_exact: {s}", .{@errorName(e)}),
},
};
prog_node.completeOne();
}
},
.load_host_libc => if (comp.bin_file) |base| {
const prog_node = comp.work_queue_progress_node.start("Linker Parse Host libc", 0);
defer prog_node.end();

const target = comp.root_mod.resolved_target.result;
const flags = target_util.libcFullLinkFlags(target);
const crt_dir = comp.libc_installation.?.crt_dir.?;
Expand Down Expand Up @@ -1482,18 +1488,24 @@ pub fn doTask(comp: *Compilation, tid: usize, task: Task) void {
}
},
.load_object => |path| if (comp.bin_file) |base| {
const prog_node = comp.work_queue_progress_node.start("Linker Parse Object", 0);
defer prog_node.end();
base.openLoadObject(path) catch |err| switch (err) {
error.LinkFailure => return, // error reported via diags
else => |e| diags.addParseError(path, "failed to parse object: {s}", .{@errorName(e)}),
};
},
.load_archive => |path| if (comp.bin_file) |base| {
const prog_node = comp.work_queue_progress_node.start("Linker Parse Archive", 0);
defer prog_node.end();
base.openLoadArchive(path, null) catch |err| switch (err) {
error.LinkFailure => return, // error reported via link_diags
else => |e| diags.addParseError(path, "failed to parse archive: {s}", .{@errorName(e)}),
};
},
.load_dso => |path| if (comp.bin_file) |base| {
const prog_node = comp.work_queue_progress_node.start("Linker Parse Shared Library", 0);
defer prog_node.end();
base.openLoadDso(path, .{
.preferred_mode = .dynamic,
.search_strategy = .paths_first,
Expand All @@ -1503,6 +1515,8 @@ pub fn doTask(comp: *Compilation, tid: usize, task: Task) void {
};
},
.load_input => |input| if (comp.bin_file) |base| {
const prog_node = comp.work_queue_progress_node.start("Linker Parse Input", 0);
defer prog_node.end();
base.loadInput(input) catch |err| switch (err) {
error.LinkFailure => return, // error reported via link_diags
else => |e| {
Expand Down

0 comments on commit 504ad56

Please sign in to comment.