Commit 5b90fa05a4

Andrew Kelley <andrew@ziglang.org>
2023-02-13 21:39:06
extract ThreadPool and WaitGroup from compiler to std lib
1 parent 0b744d7
src/ThreadPool.zig → lib/std/Thread/Pool.zig
@@ -1,6 +1,6 @@
 const std = @import("std");
 const builtin = @import("builtin");
-const ThreadPool = @This();
+const Pool = @This();
 const WaitGroup = @import("WaitGroup.zig");
 
 mutex: std.Thread.Mutex = .{},
@@ -17,7 +17,7 @@ const Runnable = struct {
 
 const RunProto = *const fn (*Runnable) void;
 
-pub fn init(pool: *ThreadPool, allocator: std.mem.Allocator) !void {
+pub fn init(pool: *Pool, allocator: std.mem.Allocator) !void {
     pool.* = .{
         .allocator = allocator,
         .threads = &[_]std.Thread{},
@@ -41,12 +41,12 @@ pub fn init(pool: *ThreadPool, allocator: std.mem.Allocator) !void {
     }
 }
 
-pub fn deinit(pool: *ThreadPool) void {
+pub fn deinit(pool: *Pool) void {
     pool.join(pool.threads.len); // kill and join all threads.
     pool.* = undefined;
 }
 
-fn join(pool: *ThreadPool, spawned: usize) void {
+fn join(pool: *Pool, spawned: usize) void {
     if (builtin.single_threaded) {
         return;
     }
@@ -69,7 +69,7 @@ fn join(pool: *ThreadPool, spawned: usize) void {
     pool.allocator.free(pool.threads);
 }
 
-pub fn spawn(pool: *ThreadPool, comptime func: anytype, args: anytype) !void {
+pub fn spawn(pool: *Pool, comptime func: anytype, args: anytype) !void {
     if (builtin.single_threaded) {
         @call(.auto, func, args);
         return;
@@ -78,7 +78,7 @@ pub fn spawn(pool: *ThreadPool, comptime func: anytype, args: anytype) !void {
     const Args = @TypeOf(args);
     const Closure = struct {
         arguments: Args,
-        pool: *ThreadPool,
+        pool: *Pool,
         run_node: RunQueue.Node = .{ .data = .{ .runFn = runFn } },
 
         fn runFn(runnable: *Runnable) void {
@@ -112,7 +112,7 @@ pub fn spawn(pool: *ThreadPool, comptime func: anytype, args: anytype) !void {
     pool.cond.signal();
 }
 
-fn worker(pool: *ThreadPool) void {
+fn worker(pool: *Pool) void {
     pool.mutex.lock();
     defer pool.mutex.unlock();
 
@@ -135,7 +135,7 @@ fn worker(pool: *ThreadPool) void {
     }
 }
 
-pub fn waitAndWork(pool: *ThreadPool, wait_group: *WaitGroup) void {
+pub fn waitAndWork(pool: *Pool, wait_group: *WaitGroup) void {
     while (!wait_group.isDone()) {
         if (blk: {
             pool.mutex.lock();
src/WaitGroup.zig → lib/std/Thread/WaitGroup.zig
File renamed without changes
lib/std/Thread.zig
@@ -16,6 +16,8 @@ pub const Mutex = @import("Thread/Mutex.zig");
 pub const Semaphore = @import("Thread/Semaphore.zig");
 pub const Condition = @import("Thread/Condition.zig");
 pub const RwLock = @import("Thread/RwLock.zig");
+pub const Pool = @import("Thread/Pool.zig");
+pub const WaitGroup = @import("Thread/WaitGroup.zig");
 
 pub const use_pthreads = target.os.tag != .windows and target.os.tag != .wasi and builtin.link_libc;
 const is_gnu = target.abi.isGnu();
src/link/MachO/CodeSignature.zig
@@ -7,12 +7,12 @@ const log = std.log.scoped(.link);
 const macho = std.macho;
 const mem = std.mem;
 const testing = std.testing;
+const ThreadPool = std.Thread.Pool;
+const WaitGroup = std.Thread.WaitGroup;
 
 const Allocator = mem.Allocator;
 const Compilation = @import("../../Compilation.zig");
 const Sha256 = std.crypto.hash.sha2.Sha256;
-const ThreadPool = @import("../../ThreadPool.zig");
-const WaitGroup = @import("../../WaitGroup.zig");
 
 const hash_size = Sha256.digest_length;
 
src/Compilation.zig
@@ -7,6 +7,8 @@ const Allocator = std.mem.Allocator;
 const assert = std.debug.assert;
 const log = std.log.scoped(.compilation);
 const Target = std.Target;
+const ThreadPool = std.Thread.Pool;
+const WaitGroup = std.Thread.WaitGroup;
 
 const Value = @import("value.zig").Value;
 const Type = @import("type.zig").Type;
@@ -30,8 +32,6 @@ const Cache = std.Build.Cache;
 const translate_c = @import("translate_c.zig");
 const clang = @import("clang.zig");
 const c_codegen = @import("codegen/c.zig");
-const ThreadPool = @import("ThreadPool.zig");
-const WaitGroup = @import("WaitGroup.zig");
 const libtsan = @import("libtsan.zig");
 const Zir = @import("Zir.zig");
 const Autodoc = @import("Autodoc.zig");
src/main.zig
@@ -9,6 +9,7 @@ const Allocator = mem.Allocator;
 const ArrayList = std.ArrayList;
 const Ast = std.zig.Ast;
 const warn = std.log.warn;
+const ThreadPool = std.Thread.Pool;
 
 const tracy = @import("tracy.zig");
 const Compilation = @import("Compilation.zig");
@@ -22,7 +23,6 @@ const translate_c = @import("translate_c.zig");
 const clang = @import("clang.zig");
 const Cache = std.Build.Cache;
 const target_util = @import("target.zig");
-const ThreadPool = @import("ThreadPool.zig");
 const crash_report = @import("crash_report.zig");
 
 pub const std_options = struct {
src/Package.zig
@@ -8,11 +8,11 @@ const Allocator = mem.Allocator;
 const assert = std.debug.assert;
 const log = std.log.scoped(.package);
 const main = @import("main.zig");
+const ThreadPool = std.Thread.Pool;
+const WaitGroup = std.Thread.WaitGroup;
 
 const Compilation = @import("Compilation.zig");
 const Module = @import("Module.zig");
-const ThreadPool = @import("ThreadPool.zig");
-const WaitGroup = @import("WaitGroup.zig");
 const Cache = std.Build.Cache;
 const build_options = @import("build_options");
 const Manifest = @import("Manifest.zig");
src/test.zig
@@ -4,14 +4,14 @@ const Allocator = std.mem.Allocator;
 const CrossTarget = std.zig.CrossTarget;
 const print = std.debug.print;
 const assert = std.debug.assert;
+const ThreadPool = std.Thread.Pool;
+const WaitGroup = std.Thread.WaitGroup;
 
 const link = @import("link.zig");
 const Compilation = @import("Compilation.zig");
 const Package = @import("Package.zig");
 const introspect = @import("introspect.zig");
 const build_options = @import("build_options");
-const ThreadPool = @import("ThreadPool.zig");
-const WaitGroup = @import("WaitGroup.zig");
 const zig_h = link.File.C.zig_h;
 
 const enable_qemu: bool = build_options.enable_qemu;
CMakeLists.txt
@@ -506,7 +506,9 @@ set(ZIG_STAGE2_SOURCES
     "${CMAKE_SOURCE_DIR}/lib/std/Thread.zig"
     "${CMAKE_SOURCE_DIR}/lib/std/Thread/Futex.zig"
     "${CMAKE_SOURCE_DIR}/lib/std/Thread/Mutex.zig"
+    "${CMAKE_SOURCE_DIR}/lib/std/Thread/Pool.zig"
     "${CMAKE_SOURCE_DIR}/lib/std/Thread/ResetEvent.zig"
+    "${CMAKE_SOURCE_DIR}/lib/std/Thread/WaitGroup.zig"
     "${CMAKE_SOURCE_DIR}/lib/std/time.zig"
     "${CMAKE_SOURCE_DIR}/lib/std/treap.zig"
     "${CMAKE_SOURCE_DIR}/lib/std/unicode.zig"
@@ -530,9 +532,7 @@ set(ZIG_STAGE2_SOURCES
     "${CMAKE_SOURCE_DIR}/src/Package.zig"
     "${CMAKE_SOURCE_DIR}/src/RangeSet.zig"
     "${CMAKE_SOURCE_DIR}/src/Sema.zig"
-    "${CMAKE_SOURCE_DIR}/src/ThreadPool.zig"
     "${CMAKE_SOURCE_DIR}/src/TypedValue.zig"
-    "${CMAKE_SOURCE_DIR}/src/WaitGroup.zig"
     "${CMAKE_SOURCE_DIR}/src/Zir.zig"
     "${CMAKE_SOURCE_DIR}/src/arch/aarch64/CodeGen.zig"
     "${CMAKE_SOURCE_DIR}/src/arch/aarch64/Emit.zig"