authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-07-10 05:19:58-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-07-10 05:19:58-04:00
log1f6b3d16644c8727323a1512d555236afbca7d7f
treeb70a729faa558896265f29a6d3c3e249131ca88d
parentf58ee387c7c9a512d802f819f870190c900cb6e2
parentc8e00953623bd3f4a12c8654a83a1b6cac2b2b2f
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #20551 from mochalins/std_thread_pool_fix

fix: Update `spawn`'s `runFn` signature

2 files changed, 23 insertions(+), 1 deletions(-)

lib/std/Thread.zig+1
...@@ -1465,6 +1465,7 @@ test {...@@ -1465,6 +1465,7 @@ test {
1465 _ = Semaphore;1465 _ = Semaphore;
1466 _ = Condition;1466 _ = Condition;
1467 _ = RwLock;1467 _ = RwLock;
1468 _ = Pool;
1468}1469}
14691470
1470fn testIncrementNotify(value: *usize, event: *ResetEvent) void {1471fn testIncrementNotify(value: *usize, event: *ResetEvent) void {
lib/std/Thread/Pool.zig+22-1
...@@ -223,7 +223,7 @@ pub fn spawn(pool: *Pool, comptime func: anytype, args: anytype) !void {...@@ -223,7 +223,7 @@ pub fn spawn(pool: *Pool, comptime func: anytype, args: anytype) !void {
223 pool: *Pool,223 pool: *Pool,
224 run_node: RunQueue.Node = .{ .data = .{ .runFn = runFn } },224 run_node: RunQueue.Node = .{ .data = .{ .runFn = runFn } },
225225
226 fn runFn(runnable: *Runnable) void {226 fn runFn(runnable: *Runnable, _: ?usize) void {
227 const run_node: *RunQueue.Node = @fieldParentPtr("data", runnable);227 const run_node: *RunQueue.Node = @fieldParentPtr("data", runnable);
228 const closure: *@This() = @alignCast(@fieldParentPtr("run_node", run_node));228 const closure: *@This() = @alignCast(@fieldParentPtr("run_node", run_node));
229 @call(.auto, func, closure.arguments);229 @call(.auto, func, closure.arguments);
...@@ -254,6 +254,27 @@ pub fn spawn(pool: *Pool, comptime func: anytype, args: anytype) !void {...@@ -254,6 +254,27 @@ pub fn spawn(pool: *Pool, comptime func: anytype, args: anytype) !void {
254 pool.cond.signal();254 pool.cond.signal();
255}255}
256256
257test spawn {
258 const TestFn = struct {
259 fn checkRun(completed: *bool) void {
260 completed.* = true;
261 }
262 };
263
264 var completed: bool = false;
265
266 {
267 var pool: Pool = undefined;
268 try pool.init(.{
269 .allocator = std.testing.allocator,
270 });
271 defer pool.deinit();
272 try pool.spawn(TestFn.checkRun, .{&completed});
273 }
274
275 try std.testing.expectEqual(true, completed);
276}
277
257fn worker(pool: *Pool) void {278fn worker(pool: *Pool) void {
258 pool.mutex.lock();279 pool.mutex.lock();
259 defer pool.mutex.unlock();280 defer pool.mutex.unlock();