authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2024-01-28 00:37:34+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2024-02-01 15:22:36+02:00
log07dbff4f4479597c6d93d273b7ba7471ba082f49
tree9df4e57af752856eec1988121c5a90cc38db0df4
parent220d3264c929187cb72111fcf75a223f9e4a7399

std.start: remove event loop integration


4 files changed, 9 insertions(+), 86 deletions(-)

lib/std/io.zig+1-2
...@@ -20,8 +20,7 @@ pub const Mode = enum {...@@ -20,8 +20,7 @@ pub const Mode = enum {
20 evented,20 evented,
21};21};
2222
23const mode = std.options.io_mode;23pub const is_async = false;
24pub const is_async = mode != .blocking;
2524
26/// This is an enum value to use for I/O mode at runtime, since it takes up zero bytes at runtime,25/// This is an enum value to use for I/O mode at runtime, since it takes up zero bytes at runtime,
27/// and makes expressions comptime-known when `is_async` is `false`.26/// and makes expressions comptime-known when `is_async` is `false`.
lib/std/start.zig+8-82
...@@ -347,7 +347,7 @@ fn WinStartup() callconv(std.os.windows.WINAPI) noreturn {...@@ -347,7 +347,7 @@ fn WinStartup() callconv(std.os.windows.WINAPI) noreturn {
347347
348 std.debug.maybeEnableSegfaultHandler();348 std.debug.maybeEnableSegfaultHandler();
349349
350 std.os.windows.ntdll.RtlExitUserProcess(initEventLoopAndCallMain());350 std.os.windows.ntdll.RtlExitUserProcess(callMain());
351}351}
352352
353fn wWinMainCRTStartup() callconv(std.os.windows.WINAPI) noreturn {353fn wWinMainCRTStartup() callconv(std.os.windows.WINAPI) noreturn {
...@@ -358,7 +358,7 @@ fn wWinMainCRTStartup() callconv(std.os.windows.WINAPI) noreturn {...@@ -358,7 +358,7 @@ fn wWinMainCRTStartup() callconv(std.os.windows.WINAPI) noreturn {
358358
359 std.debug.maybeEnableSegfaultHandler();359 std.debug.maybeEnableSegfaultHandler();
360360
361 const result: std.os.windows.INT = initEventLoopAndCallWinMain();361 const result: std.os.windows.INT = call_wWinMain();
362 std.os.windows.ntdll.RtlExitUserProcess(@as(std.os.windows.UINT, @bitCast(result)));362 std.os.windows.ntdll.RtlExitUserProcess(@as(std.os.windows.UINT, @bitCast(result)));
363}363}
364364
...@@ -422,7 +422,7 @@ fn posixCallMainAndExit() callconv(.C) noreturn {...@@ -422,7 +422,7 @@ fn posixCallMainAndExit() callconv(.C) noreturn {
422 expandStackSize(phdrs);422 expandStackSize(phdrs);
423 }423 }
424424
425 std.os.exit(@call(.always_inline, callMainWithArgs, .{ argc, argv, envp }));425 std.os.exit(callMainWithArgs(argc, argv, envp));
426}426}
427427
428fn expandStackSize(phdrs: []elf.Phdr) void {428fn expandStackSize(phdrs: []elf.Phdr) void {
...@@ -459,14 +459,14 @@ fn expandStackSize(phdrs: []elf.Phdr) void {...@@ -459,14 +459,14 @@ fn expandStackSize(phdrs: []elf.Phdr) void {
459 }459 }
460}460}
461461
462fn callMainWithArgs(argc: usize, argv: [*][*:0]u8, envp: [][*:0]u8) u8 {462inline fn callMainWithArgs(argc: usize, argv: [*][*:0]u8, envp: [][*:0]u8) u8 {
463 std.os.argv = argv[0..argc];463 std.os.argv = argv[0..argc];
464 std.os.environ = envp;464 std.os.environ = envp;
465465
466 std.debug.maybeEnableSegfaultHandler();466 std.debug.maybeEnableSegfaultHandler();
467 std.os.maybeIgnoreSigpipe();467 std.os.maybeIgnoreSigpipe();
468468
469 return initEventLoopAndCallMain();469 return callMain();
470}470}
471471
472fn main(c_argc: c_int, c_argv: [*][*:0]c_char, c_envp: [*:null]?[*:0]c_char) callconv(.C) c_int {472fn main(c_argc: c_int, c_argv: [*][*:0]c_char, c_envp: [*:null]?[*:0]c_char) callconv(.C) c_int {
...@@ -481,92 +481,18 @@ fn main(c_argc: c_int, c_argv: [*][*:0]c_char, c_envp: [*:null]?[*:0]c_char) cal...@@ -481,92 +481,18 @@ fn main(c_argc: c_int, c_argv: [*][*:0]c_char, c_envp: [*:null]?[*:0]c_char) cal
481 expandStackSize(phdrs);481 expandStackSize(phdrs);
482 }482 }
483483
484 return @call(.always_inline, callMainWithArgs, .{ @as(usize, @intCast(c_argc)), @as([*][*:0]u8, @ptrCast(c_argv)), envp });484 return callMainWithArgs(@as(usize, @intCast(c_argc)), @as([*][*:0]u8, @ptrCast(c_argv)), envp);
485}485}
486486
487fn mainWithoutEnv(c_argc: c_int, c_argv: [*][*:0]c_char) callconv(.C) c_int {487fn mainWithoutEnv(c_argc: c_int, c_argv: [*][*:0]c_char) callconv(.C) c_int {
488 std.os.argv = @as([*][*:0]u8, @ptrCast(c_argv))[0..@as(usize, @intCast(c_argc))];488 std.os.argv = @as([*][*:0]u8, @ptrCast(c_argv))[0..@as(usize, @intCast(c_argc))];
489 return @call(.always_inline, callMain, .{});489 return callMain();
490}490}
491491
492// General error message for a malformed return type492// General error message for a malformed return type
493const bad_main_ret = "expected return type of main to be 'void', '!void', 'noreturn', 'u8', or '!u8'";493const bad_main_ret = "expected return type of main to be 'void', '!void', 'noreturn', 'u8', or '!u8'";
494494
495// This is marked inline because for some reason LLVM in release mode fails to inline it,495pub inline fn callMain() u8 {
496// and we want fewer call frames in stack traces.
497inline fn initEventLoopAndCallMain() u8 {
498 if (std.event.Loop.instance) |loop| {
499 if (loop == std.event.Loop.default_instance) {
500 loop.init() catch |err| {
501 std.log.err("{s}", .{@errorName(err)});
502 if (@errorReturnTrace()) |trace| {
503 std.debug.dumpStackTrace(trace.*);
504 }
505 return 1;
506 };
507 defer loop.deinit();
508
509 var result: u8 = undefined;
510 var frame: @Frame(callMainAsync) = undefined;
511 _ = @asyncCall(&frame, &result, callMainAsync, .{loop});
512 loop.run();
513 return result;
514 }
515 }
516
517 // This is marked inline because for some reason LLVM in release mode fails to inline it,
518 // and we want fewer call frames in stack traces.
519 return @call(.always_inline, callMain, .{});
520}
521
522// This is marked inline because for some reason LLVM in release mode fails to inline it,
523// and we want fewer call frames in stack traces.
524// TODO This function is duplicated from initEventLoopAndCallMain instead of using generics
525// because it is working around stage1 compiler bugs.
526inline fn initEventLoopAndCallWinMain() std.os.windows.INT {
527 if (std.event.Loop.instance) |loop| {
528 if (loop == std.event.Loop.default_instance) {
529 loop.init() catch |err| {
530 std.log.err("{s}", .{@errorName(err)});
531 if (@errorReturnTrace()) |trace| {
532 std.debug.dumpStackTrace(trace.*);
533 }
534 return 1;
535 };
536 defer loop.deinit();
537
538 var result: std.os.windows.INT = undefined;
539 var frame: @Frame(callWinMainAsync) = undefined;
540 _ = @asyncCall(&frame, &result, callWinMainAsync, .{loop});
541 loop.run();
542 return result;
543 }
544 }
545
546 // This is marked inline because for some reason LLVM in release mode fails to inline it,
547 // and we want fewer call frames in stack traces.
548 return @call(.always_inline, call_wWinMain, .{});
549}
550
551fn callMainAsync(loop: *std.event.Loop) callconv(.Async) u8 {
552 // This prevents the event loop from terminating at least until main() has returned.
553 // TODO This shouldn't be needed here; it should be in the event loop code.
554 loop.beginOneEvent();
555 defer loop.finishOneEvent();
556 return callMain();
557}
558
559fn callWinMainAsync(loop: *std.event.Loop) callconv(.Async) std.os.windows.INT {
560 // This prevents the event loop from terminating at least until main() has returned.
561 // TODO This shouldn't be needed here; it should be in the event loop code.
562 loop.beginOneEvent();
563 defer loop.finishOneEvent();
564 return call_wWinMain();
565}
566
567// This is not marked inline because it is called with @asyncCall when
568// there is an event loop.
569pub fn callMain() u8 {
570 switch (@typeInfo(@typeInfo(@TypeOf(root.main)).Fn.return_type.?)) {496 switch (@typeInfo(@typeInfo(@TypeOf(root.main)).Fn.return_type.?)) {
571 .NoReturn => {497 .NoReturn => {
572 root.main();498 root.main();
test/cases/compile_errors/missing_main_fn_in_executable.zig-1
...@@ -7,4 +7,3 @@...@@ -7,4 +7,3 @@
7// : note: struct declared here7// : note: struct declared here
8// : note: called from here8// : note: called from here
9// : note: called from here9// : note: called from here
10// : note: called from here
test/cases/compile_errors/private_main_fn.zig-1
...@@ -9,4 +9,3 @@ fn main() void {}...@@ -9,4 +9,3 @@ fn main() void {}
9// :1:1: note: declared here9// :1:1: note: declared here
10// : note: called from here10// : note: called from here
11// : note: called from here11// : note: called from here
12// : note: called from here