Skip to content

Commit

Permalink
Merge pull request #20551 from mochalins/std_thread_pool_fix
Browse files Browse the repository at this point in the history
fix: Update `spawn`'s `runFn` signature
  • Loading branch information
jacobly0 authored Jul 10, 2024
2 parents f58ee38 + c8e0095 commit 1f6b3d1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/std/Thread.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,7 @@ test {
_ = Semaphore;
_ = Condition;
_ = RwLock;
_ = Pool;
}

fn testIncrementNotify(value: *usize, event: *ResetEvent) void {
Expand Down
23 changes: 22 additions & 1 deletion lib/std/Thread/Pool.zig
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ pub fn spawn(pool: *Pool, comptime func: anytype, args: anytype) !void {
pool: *Pool,
run_node: RunQueue.Node = .{ .data = .{ .runFn = runFn } },

fn runFn(runnable: *Runnable) 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);
Expand Down Expand Up @@ -254,6 +254,27 @@ pub fn spawn(pool: *Pool, comptime func: anytype, args: anytype) !void {
pool.cond.signal();
}

test spawn {
const TestFn = struct {
fn checkRun(completed: *bool) void {
completed.* = true;
}
};

var completed: bool = false;

{
var pool: Pool = undefined;
try pool.init(.{
.allocator = std.testing.allocator,
});
defer pool.deinit();
try pool.spawn(TestFn.checkRun, .{&completed});
}

try std.testing.expectEqual(true, completed);
}

fn worker(pool: *Pool) void {
pool.mutex.lock();
defer pool.mutex.unlock();
Expand Down

0 comments on commit 1f6b3d1

Please sign in to comment.