Skip to content

Commit

Permalink
Move some Compilation mutex locking into PerThread.zig
Browse files Browse the repository at this point in the history
  • Loading branch information
antlilja committed Aug 13, 2024
1 parent 2dff2fb commit c1aa904
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
10 changes: 2 additions & 8 deletions src/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3601,12 +3601,6 @@ fn performAllTheWorkInner(

if (comp.module) |zcu| {
{
// Worker threads may append to zcu.files and zcu.import_table
// so we must hold the lock while spawning those tasks, since
// we access those tables in this loop.
comp.mutex.lock();
defer comp.mutex.unlock();

while (comp.astgen_work_queue.readItem()) |file_index| {
// Pre-load these things from our single-threaded context since they
// will be needed by the worker threads.
Expand Down Expand Up @@ -4344,10 +4338,10 @@ fn workerAstGenFile(
if (mem.eql(u8, import_path, "builtin")) continue;

const import_result, const imported_path_digest, const imported_root_type = blk: {
const res = pt.importFile(file, import_path) catch continue;

comp.mutex.lock();
defer comp.mutex.unlock();

const res = pt.importFile(file, import_path) catch continue;
if (!res.is_pkg) {
res.file.addReference(pt.zcu, .{ .import = .{
.file = file_index,
Expand Down
9 changes: 9 additions & 0 deletions src/Zcu/PerThread.zig
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@ pub fn updateZirRefs(pt: Zcu.PerThread) Allocator.Error!void {
const ip = &zcu.intern_pool;
const gpa = zcu.gpa;

zcu.comp.mutex.lock();
defer zcu.comp.mutex.unlock();

// We need to visit every updated File for every TrackedInst in InternPool.
var updated_files: std.ArrayListUnmanaged(UpdatedFile) = .{};
defer cleanupUpdatedFiles(gpa, &updated_files);
Expand Down Expand Up @@ -1370,6 +1373,9 @@ pub fn importPkg(pt: Zcu.PerThread, mod: *Module) !Zcu.ImportFileResult {
var keep_resolved_path = false;
defer if (!keep_resolved_path) gpa.free(resolved_path);

zcu.comp.mutex.lock();
defer zcu.comp.mutex.unlock();

const gop = try zcu.import_table.getOrPut(gpa, resolved_path);
errdefer _ = zcu.import_table.pop();
if (gop.found_existing) {
Expand Down Expand Up @@ -1481,6 +1487,9 @@ pub fn importFile(
var keep_resolved_path = false;
defer if (!keep_resolved_path) gpa.free(resolved_path);

zcu.comp.mutex.lock();
defer zcu.comp.mutex.unlock();

const gop = try zcu.import_table.getOrPut(gpa, resolved_path);
errdefer _ = zcu.import_table.pop();
if (gop.found_existing) {
Expand Down

0 comments on commit c1aa904

Please sign in to comment.