aboutsummaryrefslogtreecommitdiff
path: root/lib/std/Thread
diff options
context:
space:
mode:
authorJacob Young <jacobly0@users.noreply.github.com>2024-07-08 09:05:30 -0400
committerJacob Young <jacobly0@users.noreply.github.com>2024-07-08 11:00:38 -0400
commit65ced4a33436fa762de75e22a986ae08a8c0d9cc (patch)
treed19b6b0cdb71a7e0f6bc65379441992753979a4a /lib/std/Thread
parentc36e2bb9802ab4317980a98ea518483010fe2c80 (diff)
downloadzig-65ced4a33436fa762de75e22a986ae08a8c0d9cc.tar.gz
zig-65ced4a33436fa762de75e22a986ae08a8c0d9cc.zip
Compilation: put supported codegen backends on a separate thread
(There are no supported backends.)
Diffstat (limited to 'lib/std/Thread')
-rw-r--r--lib/std/Thread/Pool.zig23
1 files changed, 10 insertions, 13 deletions
diff --git a/lib/std/Thread/Pool.zig b/lib/std/Thread/Pool.zig
index d501b66520..179f2f8521 100644
--- a/lib/std/Thread/Pool.zig
+++ b/lib/std/Thread/Pool.zig
@@ -21,11 +21,11 @@ const Runnable = struct {
runFn: RunProto,
};
-const RunProto = *const fn (*Runnable, id: ?u32) void;
+const RunProto = *const fn (*Runnable, id: ?usize) void;
pub const Options = struct {
allocator: std.mem.Allocator,
- n_jobs: ?u32 = null,
+ n_jobs: ?usize = null,
track_ids: bool = false,
};
@@ -109,7 +109,7 @@ pub fn spawnWg(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, args
run_node: RunQueue.Node = .{ .data = .{ .runFn = runFn } },
wait_group: *WaitGroup,
- fn runFn(runnable: *Runnable, _: ?u32) void {
+ fn runFn(runnable: *Runnable, _: ?usize) void {
const run_node: *RunQueue.Node = @fieldParentPtr("data", runnable);
const closure: *@This() = @alignCast(@fieldParentPtr("run_node", run_node));
@call(.auto, func, closure.arguments);
@@ -150,7 +150,7 @@ pub fn spawnWg(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, args
/// Runs `func` in the thread pool, calling `WaitGroup.start` beforehand, and
/// `WaitGroup.finish` after it returns.
///
-/// The first argument passed to `func` is a dense `u32` thread id, the rest
+/// The first argument passed to `func` is a dense `usize` thread id, the rest
/// of the arguments are passed from `args`. Requires the pool to have been
/// initialized with `.track_ids = true`.
///
@@ -172,7 +172,7 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar
run_node: RunQueue.Node = .{ .data = .{ .runFn = runFn } },
wait_group: *WaitGroup,
- fn runFn(runnable: *Runnable, id: ?u32) void {
+ fn runFn(runnable: *Runnable, id: ?usize) void {
const run_node: *RunQueue.Node = @fieldParentPtr("data", runnable);
const closure: *@This() = @alignCast(@fieldParentPtr("run_node", run_node));
@call(.auto, func, .{id.?} ++ closure.arguments);
@@ -191,7 +191,7 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar
pool.mutex.lock();
const closure = pool.allocator.create(Closure) catch {
- const id = pool.ids.getIndex(std.Thread.getCurrentId());
+ const id: ?usize = pool.ids.getIndex(std.Thread.getCurrentId());
pool.mutex.unlock();
@call(.auto, func, .{id.?} ++ args);
wait_group.finish();
@@ -258,7 +258,7 @@ fn worker(pool: *Pool) void {
pool.mutex.lock();
defer pool.mutex.unlock();
- const id: ?u32 = if (pool.ids.count() > 0) @intCast(pool.ids.count()) else null;
+ const id: ?usize = if (pool.ids.count() > 0) @intCast(pool.ids.count()) else null;
if (id) |_| pool.ids.putAssumeCapacityNoClobber(std.Thread.getCurrentId(), {});
while (true) {
@@ -280,15 +280,12 @@ fn worker(pool: *Pool) void {
}
pub fn waitAndWork(pool: *Pool, wait_group: *WaitGroup) void {
- var id: ?u32 = null;
+ var id: ?usize = null;
while (!wait_group.isDone()) {
pool.mutex.lock();
if (pool.run_queue.popFirst()) |run_node| {
- id = id orelse if (pool.ids.getIndex(std.Thread.getCurrentId())) |index|
- @intCast(index)
- else
- null;
+ id = id orelse pool.ids.getIndex(std.Thread.getCurrentId());
pool.mutex.unlock();
run_node.data.runFn(&run_node.data, id);
continue;
@@ -300,6 +297,6 @@ pub fn waitAndWork(pool: *Pool, wait_group: *WaitGroup) void {
}
}
-pub fn getIdCount(pool: *Pool) u32 {
+pub fn getIdCount(pool: *Pool) usize {
return @intCast(1 + pool.threads.len);
}