commit 6481b02fdce735614e87c0b48ec169a2f3496610 (tree)
parent 0c037a85616400424a8489be0ff150e5bdd496e5
Author: Andrius Mitkus <andrius.mitkus@gmail.com>
Date: Mon, 20 Apr 2020 18:18:50 +0300
std: fix posix Thread.spawn to accept all startFn types
Diffstat:
1 file changed, 31 insertions(+), 6 deletions(-)
diff --git a/lib/std/thread.zig b/lib/std/thread.zig
@@ -252,12 +252,37 @@ pub const Thread = struct {
}
}
fn posixThreadMain(ctx: ?*c_void) callconv(.C) ?*c_void {
- if (@sizeOf(Context) == 0) {
- _ = startFn({});
- return null;
- } else {
- _ = startFn(@ptrCast(*const Context, @alignCast(@alignOf(Context), ctx)).*);
- return null;
+ const arg = if (@sizeOf(Context) == 0) {} else @ptrCast(*Context, @alignCast(@alignOf(Context), ctx)).*;
+
+ switch (@typeInfo(@TypeOf(startFn).ReturnType)) {
+ .NoReturn => {
+ startFn(arg);
+ },
+ .Void => {
+ startFn(arg);
+ return null;
+ },
+ .Int => |info| {
+ if (info.bits != 8) {
+ @compileError(bad_startfn_ret);
+ }
+ // pthreads don't support exit status, ignore value
+ _ = startFn(arg);
+ return null;
+ },
+ .ErrorUnion => |info| {
+ if (info.payload != void) {
+ @compileError(bad_startfn_ret);
+ }
+ startFn(arg) catch |err| {
+ std.debug.warn("error: {}\n", .{@errorName(err)});
+ if (@errorReturnTrace()) |trace| {
+ std.debug.dumpStackTrace(trace.*);
+ }
+ };
+ return 0;
+ },
+ else => @compileError(bad_startfn_ret),
}
}
};