Skip to content

Commit

Permalink
Revert "Enable thread_pool function to throw errors (#20260)"
Browse files Browse the repository at this point in the history
This reverts commit d346d07.

I would like a chance to review this, please.
  • Loading branch information
andrewrk committed Nov 11, 2024
1 parent d346d07 commit 8622665
Showing 1 changed file with 8 additions and 36 deletions.
44 changes: 8 additions & 36 deletions lib/std/Thread/Pool.zig
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub fn spawnWg(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, args
wait_group.start();

if (builtin.single_threaded) {
callFn(func, args);
@call(.auto, func, args);
wait_group.finish();
return;
}
Expand All @@ -112,7 +112,7 @@ pub fn spawnWg(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, args
fn runFn(runnable: *Runnable, _: ?usize) void {
const run_node: *RunQueue.Node = @fieldParentPtr("data", runnable);
const closure: *@This() = @alignCast(@fieldParentPtr("run_node", run_node));
callFn(func, closure.arguments);
@call(.auto, func, closure.arguments);
closure.wait_group.finish();

// The thread pool's allocator is protected by the mutex.
Expand All @@ -129,7 +129,7 @@ pub fn spawnWg(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, args

const closure = pool.allocator.create(Closure) catch {
pool.mutex.unlock();
callFn(func, args);
@call(.auto, func, args);
wait_group.finish();
return;
};
Expand Down Expand Up @@ -160,7 +160,7 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar
wait_group.start();

if (builtin.single_threaded) {
callFn(func, .{0} ++ args);
@call(.auto, func, .{0} ++ args);
wait_group.finish();
return;
}
Expand All @@ -175,7 +175,7 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar
fn runFn(runnable: *Runnable, id: ?usize) void {
const run_node: *RunQueue.Node = @fieldParentPtr("data", runnable);
const closure: *@This() = @alignCast(@fieldParentPtr("run_node", run_node));
callFn(func, .{id.?} ++ closure.arguments);
@call(.auto, func, .{id.?} ++ closure.arguments);
closure.wait_group.finish();

// The thread pool's allocator is protected by the mutex.
Expand All @@ -193,7 +193,7 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar
const closure = pool.allocator.create(Closure) catch {
const id: ?usize = pool.ids.getIndex(std.Thread.getCurrentId());
pool.mutex.unlock();
callFn(func, .{id.?} ++ args);
@call(.auto, func, .{id.?} ++ args);
wait_group.finish();
return;
};
Expand All @@ -213,7 +213,7 @@ pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, ar

pub fn spawn(pool: *Pool, comptime func: anytype, args: anytype) !void {
if (builtin.single_threaded) {
callFn(func, args);
@call(.auto, func, args);
return;
}

Expand All @@ -226,7 +226,7 @@ pub fn spawn(pool: *Pool, comptime func: anytype, args: anytype) !void {
fn runFn(runnable: *Runnable, _: ?usize) void {
const run_node: *RunQueue.Node = @fieldParentPtr("data", runnable);
const closure: *@This() = @alignCast(@fieldParentPtr("run_node", run_node));
callFn(func, closure.arguments);
@call(.auto, func, closure.arguments);

// The thread pool's allocator is protected by the mutex.
const mutex = &closure.pool.mutex;
Expand Down Expand Up @@ -321,31 +321,3 @@ pub fn waitAndWork(pool: *Pool, wait_group: *WaitGroup) void {
pub fn getIdCount(pool: *Pool) usize {
return @intCast(1 + pool.threads.len);
}

inline fn callFn(comptime f: anytype, args: anytype) void {
const bad_fn_ret = "expected return type of runFn to be 'void', '!void', noreturn, or !noreturn";

switch (@typeInfo(@typeInfo(@TypeOf(f)).@"fn".return_type.?)) {
.void, .noreturn => {
@call(.auto, f, args);
},
.error_union => |info| {
switch (info.payload) {
void, noreturn => {
@call(.auto, f, args) catch |err| {
std.debug.print("error: {s}\n", .{@errorName(err)});
if (@errorReturnTrace()) |trace| {
std.debug.dumpStackTrace(trace.*);
}
};
},
else => {
@compileError(bad_fn_ret);
},
}
},
else => {
@compileError(bad_fn_ret);
},
}
}

0 comments on commit 8622665

Please sign in to comment.