authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-11-11 14:07:07-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-11-11 14:07:07-08:00
log862266514ae184eb743959bd0a67db0628b5247a
tree66e6d8479ad4152b9fee6f958f1061f86c180584
parentd346d074ebe5347f730a70d3a88b12f279bb405d

Revert "Enable thread_pool function to throw errors (#20260)"

This reverts commit d346d074ebe5347f730a70d3a88b12f279bb405d. I would like a chance to review this, please.

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

lib/std/Thread/Pool.zig+8-36
...@@ -97,7 +97,7 @@ pub fn spawnWg(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, args...@@ -97,7 +97,7 @@ pub fn spawnWg(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, args
97 wait_group.start();97 wait_group.start();
9898
99 if (builtin.single_threaded) {99 if (builtin.single_threaded) {
100 callFn(func, args);100 @call(.auto, func, args);
101 wait_group.finish();101 wait_group.finish();
102 return;102 return;
103 }103 }
...@@ -112,7 +112,7 @@ pub fn spawnWg(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, args...@@ -112,7 +112,7 @@ pub fn spawnWg(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, args
112 fn runFn(runnable: *Runnable, _: ?usize) void {112 fn runFn(runnable: *Runnable, _: ?usize) void {
113 const run_node: *RunQueue.Node = @fieldParentPtr("data", runnable);113 const run_node: *RunQueue.Node = @fieldParentPtr("data", runnable);
114 const closure: *@This() = @alignCast(@fieldParentPtr("run_node", run_node));114 const closure: *@This() = @alignCast(@fieldParentPtr("run_node", run_node));
115 callFn(func, closure.arguments);115 @call(.auto, func, closure.arguments);
116 closure.wait_group.finish();116 closure.wait_group.finish();
117117
118 // The thread pool's allocator is protected by the mutex.118 // 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...@@ -129,7 +129,7 @@ pub fn spawnWg(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, args
129129
130 const closure = pool.allocator.create(Closure) catch {130 const closure = pool.allocator.create(Closure) catch {
131 pool.mutex.unlock();131 pool.mutex.unlock();
132 callFn(func, args);132 @call(.auto, func, args);
133 wait_group.finish();133 wait_group.finish();
134 return;134 return;
135 };135 };
...@@ -160,7 +160,7 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar...@@ -160,7 +160,7 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar
160 wait_group.start();160 wait_group.start();
161161
162 if (builtin.single_threaded) {162 if (builtin.single_threaded) {
163 callFn(func, .{0} ++ args);163 @call(.auto, func, .{0} ++ args);
164 wait_group.finish();164 wait_group.finish();
165 return;165 return;
166 }166 }
...@@ -175,7 +175,7 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar...@@ -175,7 +175,7 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar
175 fn runFn(runnable: *Runnable, id: ?usize) void {175 fn runFn(runnable: *Runnable, id: ?usize) void {
176 const run_node: *RunQueue.Node = @fieldParentPtr("data", runnable);176 const run_node: *RunQueue.Node = @fieldParentPtr("data", runnable);
177 const closure: *@This() = @alignCast(@fieldParentPtr("run_node", run_node));177 const closure: *@This() = @alignCast(@fieldParentPtr("run_node", run_node));
178 callFn(func, .{id.?} ++ closure.arguments);178 @call(.auto, func, .{id.?} ++ closure.arguments);
179 closure.wait_group.finish();179 closure.wait_group.finish();
180180
181 // The thread pool's allocator is protected by the mutex.181 // 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...@@ -193,7 +193,7 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar
193 const closure = pool.allocator.create(Closure) catch {193 const closure = pool.allocator.create(Closure) catch {
194 const id: ?usize = pool.ids.getIndex(std.Thread.getCurrentId());194 const id: ?usize = pool.ids.getIndex(std.Thread.getCurrentId());
195 pool.mutex.unlock();195 pool.mutex.unlock();
196 callFn(func, .{id.?} ++ args);196 @call(.auto, func, .{id.?} ++ args);
197 wait_group.finish();197 wait_group.finish();
198 return;198 return;
199 };199 };
...@@ -213,7 +213,7 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar...@@ -213,7 +213,7 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar
213213
214pub fn spawn(pool: *Pool, comptime func: anytype, args: anytype) !void {214pub fn spawn(pool: *Pool, comptime func: anytype, args: anytype) !void {
215 if (builtin.single_threaded) {215 if (builtin.single_threaded) {
216 callFn(func, args);216 @call(.auto, func, args);
217 return;217 return;
218 }218 }
219219
...@@ -226,7 +226,7 @@ pub fn spawn(pool: *Pool, comptime func: anytype, args: anytype) !void {...@@ -226,7 +226,7 @@ pub fn spawn(pool: *Pool, comptime func: anytype, args: anytype) !void {
226 fn runFn(runnable: *Runnable, _: ?usize) 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 callFn(func, closure.arguments);229 @call(.auto, func, closure.arguments);
230230
231 // The thread pool's allocator is protected by the mutex.231 // The thread pool's allocator is protected by the mutex.
232 const mutex = &closure.pool.mutex;232 const mutex = &closure.pool.mutex;
...@@ -321,31 +321,3 @@ pub fn waitAndWork(pool: *Pool, wait_group: *WaitGroup) void {...@@ -321,31 +321,3 @@ pub fn waitAndWork(pool: *Pool, wait_group: *WaitGroup) void {
321pub fn getIdCount(pool: *Pool) usize {321pub fn getIdCount(pool: *Pool) usize {
322 return @intCast(1 + pool.threads.len);322 return @intCast(1 + pool.threads.len);
323}323}
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}