diff --git a/src/Compilation.zig b/src/Compilation.zig index 138755773877254786b45dcb5a9cbc4bbaba6805..c40e0259552858315593284b3cb6acf4ecf6c8ea 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -3231,6 +3231,7 @@ pub fn update(comp: *Compilation, main_progress_node: std.Progress.Node) UpdateE } } +/// Thread-safe. Assumes that `comp.mutex` is *not* already held by the caller. pub fn appendFileSystemInput(comp: *Compilation, path: Compilation.Path) Allocator.Error!void { const gpa = comp.gpa; const fsi = comp.file_system_inputs orelse return; @@ -3251,6 +3252,10 @@ pub fn appendFileSystemInput(comp: *Compilation, path: Compilation.Path) Allocat .{ @tagName(path.root), want_prefix_dir, path.sub_path }, ); + // There may be concurrent calls to this function from C object workers and/or the main thread. + comp.mutex.lock(); + defer comp.mutex.unlock(); + try fsi.ensureUnusedCapacity(gpa, path.sub_path.len + 3); if (fsi.items.len > 0) fsi.appendAssumeCapacity(0); fsi.appendAssumeCapacity(prefix); @@ -6443,10 +6448,6 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: std.Pr return error.InvalidDepFile; }, }; - - // There may be concurrent calls to `appendFileSystemInput` from other C objects. - comp.mutex.lock(); - defer comp.mutex.unlock(); try comp.appendFileSystemInput(input_path); } }