Merge pull request #20551 from mochalins/std_thread_pool_fix

fix: Update `spawn`'s `runFn` signature
This commit is contained in:
Jacob Young
2024-07-10 05:19:58 -04:00
committed by GitHub
2 changed files with 23 additions and 1 deletions

View File

@@ -1465,6 +1465,7 @@ test {
_ = Semaphore;
_ = Condition;
_ = RwLock;
_ = Pool;
}
fn testIncrementNotify(value: *usize, event: *ResetEvent) void {

View File

@@ -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);
@@ -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();