authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-06-21 11:03:44-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-06-30 13:54:02-07:00
log86e68dbf53b40480e6b57b0f184308fd83801b1f
treeb34b1a9da3e28accff2d913de4932be7cf61378b
parentdc207da184087eec7693fbf17c412c4b67c2080c

make the main thread call waitAndWork instead of just wait

The main thread has an implicit thread token which makes loitering illegal.

3 files changed, 15 insertions(+), 20 deletions(-)

src/Compilation.zig+12-15
...@@ -222,9 +222,6 @@ emit_asm: ?EmitLoc,...@@ -222,9 +222,6 @@ emit_asm: ?EmitLoc,
222emit_llvm_ir: ?EmitLoc,222emit_llvm_ir: ?EmitLoc,
223emit_llvm_bc: ?EmitLoc,223emit_llvm_bc: ?EmitLoc,
224224
225work_queue_wait_group: WaitGroup = .{},
226astgen_wait_group: WaitGroup = .{},
227
228llvm_opt_bisect_limit: c_int,225llvm_opt_bisect_limit: c_int,
229226
230pub const Emit = struct {227pub const Emit = struct {
...@@ -3251,13 +3248,13 @@ pub fn performAllTheWork(...@@ -3251,13 +3248,13 @@ pub fn performAllTheWork(
3251 // (at least for now) single-threaded main work queue. However, C object compilation3248 // (at least for now) single-threaded main work queue. However, C object compilation
3252 // only needs to be finished by the end of this function.3249 // only needs to be finished by the end of this function.
32533250
3254 comp.work_queue_wait_group.reset();3251 var work_queue_wait_group: WaitGroup = .{};
3255 defer comp.work_queue_wait_group.wait();3252 defer comp.thread_pool.waitAndWork(&work_queue_wait_group);
32563253
3257 if (!build_options.only_c and !build_options.only_core_functionality) {3254 if (!build_options.only_c and !build_options.only_core_functionality) {
3258 if (comp.docs_emit != null) {3255 if (comp.docs_emit != null) {
3259 comp.thread_pool.spawnWg(&comp.work_queue_wait_group, workerDocsCopy, .{comp});3256 comp.thread_pool.spawnWg(&work_queue_wait_group, workerDocsCopy, .{comp});
3260 comp.work_queue_wait_group.spawnManager(workerDocsWasm, .{ comp, main_progress_node });3257 work_queue_wait_group.spawnManager(workerDocsWasm, .{ comp, main_progress_node });
3261 }3258 }
3262 }3259 }
32633260
...@@ -3268,8 +3265,8 @@ pub fn performAllTheWork(...@@ -3268,8 +3265,8 @@ pub fn performAllTheWork(
3268 const zir_prog_node = main_progress_node.start("AST Lowering", 0);3265 const zir_prog_node = main_progress_node.start("AST Lowering", 0);
3269 defer zir_prog_node.end();3266 defer zir_prog_node.end();
32703267
3271 comp.astgen_wait_group.reset();3268 var astgen_wait_group: WaitGroup = .{};
3272 defer comp.astgen_wait_group.wait();3269 defer comp.thread_pool.waitAndWork(&astgen_wait_group);
32733270
3274 // builtin.zig is handled specially for two reasons:3271 // builtin.zig is handled specially for two reasons:
3275 // 1. to avoid race condition of zig processes truncating each other's builtin.zig files3272 // 1. to avoid race condition of zig processes truncating each other's builtin.zig files
...@@ -3291,33 +3288,33 @@ pub fn performAllTheWork(...@@ -3291,33 +3288,33 @@ pub fn performAllTheWork(
32913288
3292 const file = mod.builtin_file orelse continue;3289 const file = mod.builtin_file orelse continue;
32933290
3294 comp.thread_pool.spawnWg(&comp.astgen_wait_group, workerUpdateBuiltinZigFile, .{3291 comp.thread_pool.spawnWg(&astgen_wait_group, workerUpdateBuiltinZigFile, .{
3295 comp, mod, file,3292 comp, mod, file,
3296 });3293 });
3297 }3294 }
3298 }3295 }
32993296
3300 while (comp.astgen_work_queue.readItem()) |file| {3297 while (comp.astgen_work_queue.readItem()) |file| {
3301 comp.thread_pool.spawnWg(&comp.astgen_wait_group, workerAstGenFile, .{3298 comp.thread_pool.spawnWg(&astgen_wait_group, workerAstGenFile, .{
3302 comp, file, zir_prog_node, &comp.astgen_wait_group, .root,3299 comp, file, zir_prog_node, &astgen_wait_group, .root,
3303 });3300 });
3304 }3301 }
33053302
3306 while (comp.embed_file_work_queue.readItem()) |embed_file| {3303 while (comp.embed_file_work_queue.readItem()) |embed_file| {
3307 comp.thread_pool.spawnWg(&comp.astgen_wait_group, workerCheckEmbedFile, .{3304 comp.thread_pool.spawnWg(&astgen_wait_group, workerCheckEmbedFile, .{
3308 comp, embed_file,3305 comp, embed_file,
3309 });3306 });
3310 }3307 }
33113308
3312 while (comp.c_object_work_queue.readItem()) |c_object| {3309 while (comp.c_object_work_queue.readItem()) |c_object| {
3313 comp.thread_pool.spawnWg(&comp.work_queue_wait_group, workerUpdateCObject, .{3310 comp.thread_pool.spawnWg(&work_queue_wait_group, workerUpdateCObject, .{
3314 comp, c_object, main_progress_node,3311 comp, c_object, main_progress_node,
3315 });3312 });
3316 }3313 }
33173314
3318 if (!build_options.only_core_functionality) {3315 if (!build_options.only_core_functionality) {
3319 while (comp.win32_resource_work_queue.readItem()) |win32_resource| {3316 while (comp.win32_resource_work_queue.readItem()) |win32_resource| {
3320 comp.thread_pool.spawnWg(&comp.work_queue_wait_group, workerUpdateWin32Resource, .{3317 comp.thread_pool.spawnWg(&work_queue_wait_group, workerUpdateWin32Resource, .{
3321 comp, win32_resource, main_progress_node,3318 comp, win32_resource, main_progress_node,
3322 });3319 });
3323 }3320 }
src/link/MachO/hasher.zig+2-4
...@@ -12,8 +12,6 @@ pub fn ParallelHasher(comptime Hasher: type) type {...@@ -12,8 +12,6 @@ pub fn ParallelHasher(comptime Hasher: type) type {
12 const tracy = trace(@src());12 const tracy = trace(@src());
13 defer tracy.end();13 defer tracy.end();
1414
15 var wg: WaitGroup = .{};
16
17 const file_size = blk: {15 const file_size = blk: {
18 const file_size = opts.max_file_size orelse try file.getEndPos();16 const file_size = opts.max_file_size orelse try file.getEndPos();
19 break :blk std.math.cast(usize, file_size) orelse return error.Overflow;17 break :blk std.math.cast(usize, file_size) orelse return error.Overflow;
...@@ -27,8 +25,8 @@ pub fn ParallelHasher(comptime Hasher: type) type {...@@ -27,8 +25,8 @@ pub fn ParallelHasher(comptime Hasher: type) type {
27 defer self.allocator.free(results);25 defer self.allocator.free(results);
2826
29 {27 {
30 wg.reset();28 var wg: WaitGroup = .{};
31 defer wg.wait();29 defer self.thread_pool.waitAndWork(&wg);
3230
33 for (out, results, 0..) |*out_buf, *result, i| {31 for (out, results, 0..) |*out_buf, *result, i| {
34 const fstart = i * chunk_size;32 const fstart = i * chunk_size;
src/main.zig+1-1
...@@ -5062,7 +5062,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {...@@ -5062,7 +5062,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
5062 job_queue.thread_pool.spawnWg(&job_queue.wait_group, Package.Fetch.workerRun, .{5062 job_queue.thread_pool.spawnWg(&job_queue.wait_group, Package.Fetch.workerRun, .{
5063 &fetch, "root",5063 &fetch, "root",
5064 });5064 });
5065 job_queue.wait_group.wait();5065 job_queue.thread_pool.waitAndWork(&job_queue.wait_group);
50665066
5067 try job_queue.consolidateErrors();5067 try job_queue.consolidateErrors();
50685068