authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-12-06 09:53:20+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-12-06 09:54:43+01:00
log621e1d7b1e560901e2a5cbdca39f173f6592a004
tree68c4c96378dc74ba129f459d6fcecfb807e6747d
parent28c5cc390c909db06782eb2366d315c5da501a5f
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

Compilation: fix appendFileSystemInput race between main thread and C object workers

Co-authored-by: Matthew Lugg <mlugg@mlugg.co.uk>

1 files changed, 5 insertions(+), 4 deletions(-)

src/Compilation.zig+5-4
...@@ -3231,6 +3231,7 @@ pub fn update(comp: *Compilation, main_progress_node: std.Progress.Node) UpdateE...@@ -3231,6 +3231,7 @@ pub fn update(comp: *Compilation, main_progress_node: std.Progress.Node) UpdateE
3231 }3231 }
3232}3232}
32333233
3234/// Thread-safe. Assumes that `comp.mutex` is *not* already held by the caller.
3234pub fn appendFileSystemInput(comp: *Compilation, path: Compilation.Path) Allocator.Error!void {3235pub fn appendFileSystemInput(comp: *Compilation, path: Compilation.Path) Allocator.Error!void {
3235 const gpa = comp.gpa;3236 const gpa = comp.gpa;
3236 const fsi = comp.file_system_inputs orelse return;3237 const fsi = comp.file_system_inputs orelse return;
...@@ -3251,6 +3252,10 @@ pub fn appendFileSystemInput(comp: *Compilation, path: Compilation.Path) Allocat...@@ -3251,6 +3252,10 @@ pub fn appendFileSystemInput(comp: *Compilation, path: Compilation.Path) Allocat
3251 .{ @tagName(path.root), want_prefix_dir, path.sub_path },3252 .{ @tagName(path.root), want_prefix_dir, path.sub_path },
3252 );3253 );
32533254
3255 // There may be concurrent calls to this function from C object workers and/or the main thread.
3256 comp.mutex.lock();
3257 defer comp.mutex.unlock();
3258
3254 try fsi.ensureUnusedCapacity(gpa, path.sub_path.len + 3);3259 try fsi.ensureUnusedCapacity(gpa, path.sub_path.len + 3);
3255 if (fsi.items.len > 0) fsi.appendAssumeCapacity(0);3260 if (fsi.items.len > 0) fsi.appendAssumeCapacity(0);
3256 fsi.appendAssumeCapacity(prefix);3261 fsi.appendAssumeCapacity(prefix);
...@@ -6443,10 +6448,6 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: std.Pr...@@ -6443,10 +6448,6 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: std.Pr
6443 return error.InvalidDepFile;6448 return error.InvalidDepFile;
6444 },6449 },
6445 };6450 };
6446
6447 // There may be concurrent calls to `appendFileSystemInput` from other C objects.
6448 comp.mutex.lock();
6449 defer comp.mutex.unlock();
6450 try comp.appendFileSystemInput(input_path);6451 try comp.appendFileSystemInput(input_path);
6451 }6452 }
6452 }6453 }