authorgravatar for 69125922+ippsav@users.noreply.github.comippsav <69125922+ippsav@users.noreply.github.com> 2024-11-11 22:34:24+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-11-11 15:34:24-06:00
logd346d074ebe5347f730a70d3a88b12f279bb405d
tree7060ccbffc33f60591299f0d1f9e72795157970b
parent28bdab385a80aaff736707a9227af2d1a17e7fa4
signaturebadge-check Signed by PGP key B5690EEEBB952194

Enable thread_pool function to throw errors (#20260)

* std.ThreadPool: allow error union return type * allow noreturn in Pool.zig

1 files changed, 36 insertions(+), 8 deletions(-)

lib/std/Thread/Pool.zig+36-8
......@@ -97,7 +97,7 @@ pub fn spawnWg(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, args
9797 wait_group.start();
9898
9999 if (builtin.single_threaded) {
100 @call(.auto, func, args);
100 callFn(func, args);
101101 wait_group.finish();
102102 return;
103103 }
......@@ -112,7 +112,7 @@ pub fn spawnWg(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, args
112112 fn runFn(runnable: *Runnable, _: ?usize) void {
113113 const run_node: *RunQueue.Node = @fieldParentPtr("data", runnable);
114114 const closure: *@This() = @alignCast(@fieldParentPtr("run_node", run_node));
115 @call(.auto, func, closure.arguments);
115 callFn(func, closure.arguments);
116116 closure.wait_group.finish();
117117
118118 // The thread pool's allocator is protected by the mutex.
......@@ -129,7 +129,7 @@ pub fn spawnWg(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, args
129129
130130 const closure = pool.allocator.create(Closure) catch {
131131 pool.mutex.unlock();
132 @call(.auto, func, args);
132 callFn(func, args);
133133 wait_group.finish();
134134 return;
135135 };
......@@ -160,7 +160,7 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar
160160 wait_group.start();
161161
162162 if (builtin.single_threaded) {
163 @call(.auto, func, .{0} ++ args);
163 callFn(func, .{0} ++ args);
164164 wait_group.finish();
165165 return;
166166 }
......@@ -175,7 +175,7 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar
175175 fn runFn(runnable: *Runnable, id: ?usize) void {
176176 const run_node: *RunQueue.Node = @fieldParentPtr("data", runnable);
177177 const closure: *@This() = @alignCast(@fieldParentPtr("run_node", run_node));
178 @call(.auto, func, .{id.?} ++ closure.arguments);
178 callFn(func, .{id.?} ++ closure.arguments);
179179 closure.wait_group.finish();
180180
181181 // The thread pool's allocator is protected by the mutex.
......@@ -193,7 +193,7 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar
193193 const closure = pool.allocator.create(Closure) catch {
194194 const id: ?usize = pool.ids.getIndex(std.Thread.getCurrentId());
195195 pool.mutex.unlock();
196 @call(.auto, func, .{id.?} ++ args);
196 callFn(func, .{id.?} ++ args);
197197 wait_group.finish();
198198 return;
199199 };
......@@ -213,7 +213,7 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar
213213
214214pub fn spawn(pool: *Pool, comptime func: anytype, args: anytype) !void {
215215 if (builtin.single_threaded) {
216 @call(.auto, func, args);
216 callFn(func, args);
217217 return;
218218 }
219219
......@@ -226,7 +226,7 @@ pub fn spawn(pool: *Pool, comptime func: anytype, args: anytype) !void {
226226 fn runFn(runnable: *Runnable, _: ?usize) void {
227227 const run_node: *RunQueue.Node = @fieldParentPtr("data", runnable);
228228 const closure: *@This() = @alignCast(@fieldParentPtr("run_node", run_node));
229 @call(.auto, func, closure.arguments);
229 callFn(func, closure.arguments);
230230
231231 // The thread pool's allocator is protected by the mutex.
232232 const mutex = &closure.pool.mutex;
......@@ -321,3 +321,31 @@ pub fn waitAndWork(pool: *Pool, wait_group: *WaitGroup) void {
321321pub fn getIdCount(pool: *Pool) usize {
322322 return @intCast(1 + pool.threads.len);
323323}
324
325inline fn callFn(comptime f: anytype, args: anytype) void {
326 const bad_fn_ret = "expected return type of runFn to be 'void', '!void', noreturn, or !noreturn";
327
328 switch (@typeInfo(@typeInfo(@TypeOf(f)).@"fn".return_type.?)) {
329 .void, .noreturn => {
330 @call(.auto, f, args);
331 },
332 .error_union => |info| {
333 switch (info.payload) {
334 void, noreturn => {
335 @call(.auto, f, args) catch |err| {
336 std.debug.print("error: {s}\n", .{@errorName(err)});
337 if (@errorReturnTrace()) |trace| {
338 std.debug.dumpStackTrace(trace.*);
339 }
340 };
341 },
342 else => {
343 @compileError(bad_fn_ret);
344 },
345 }
346 },
347 else => {
348 @compileError(bad_fn_ret);
349 },
350 }
351}