authorgravatar for andrius.mitkus@gmail.comAndrius Mitkus <andrius.mitkus@gmail.com> 2020-04-20 18:18:50+03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-25 16:15:25-04:00
log6481b02fdce735614e87c0b48ec169a2f3496610
tree7a486ef71f527afb70e1109698935d406bb736ba
parent0c037a85616400424a8489be0ff150e5bdd496e5

std: fix posix Thread.spawn to accept all startFn types


1 files changed, 31 insertions(+), 6 deletions(-)

lib/std/thread.zig+31-6
......@@ -252,12 +252,37 @@ pub const Thread = struct {
252252 }
253253 }
254254 fn posixThreadMain(ctx: ?*c_void) callconv(.C) ?*c_void {
255 if (@sizeOf(Context) == 0) {
256 _ = startFn({});
257 return null;
258 } else {
259 _ = startFn(@ptrCast(*const Context, @alignCast(@alignOf(Context), ctx)).*);
260 return null;
255 const arg = if (@sizeOf(Context) == 0) {} else @ptrCast(*Context, @alignCast(@alignOf(Context), ctx)).*;
256
257 switch (@typeInfo(@TypeOf(startFn).ReturnType)) {
258 .NoReturn => {
259 startFn(arg);
260 },
261 .Void => {
262 startFn(arg);
263 return null;
264 },
265 .Int => |info| {
266 if (info.bits != 8) {
267 @compileError(bad_startfn_ret);
268 }
269 // pthreads don't support exit status, ignore value
270 _ = startFn(arg);
271 return null;
272 },
273 .ErrorUnion => |info| {
274 if (info.payload != void) {
275 @compileError(bad_startfn_ret);
276 }
277 startFn(arg) catch |err| {
278 std.debug.warn("error: {}\n", .{@errorName(err)});
279 if (@errorReturnTrace()) |trace| {
280 std.debug.dumpStackTrace(trace.*);
281 }
282 };
283 return 0;
284 },
285 else => @compileError(bad_startfn_ret),
261286 }
262287 }
263288 };