summaryrefslogtreecommitdiff
path: root/src/subcommand
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2026-01-06 22:26:49 -0500
committerRobby Zambito <contact@robbyzambito.me>2026-01-06 23:03:21 -0500
commitb6762ccb7c71eb2fa3585a530ae5687176f30c6c (patch)
tree1df8f9fc0091dfda5f037887a6fc372f5ff78e65 /src/subcommand
parent3b490fc3c8864eec0c3f4e92a2a2014711ff09c8 (diff)
Cleaner SIGINT handling
Use a Mutex to wait for the signal handler to fire instead of checking an atomic boolean over and over again.
Diffstat (limited to 'src/subcommand')
-rw-r--r--src/subcommand/server.zig35
1 files changed, 17 insertions, 18 deletions
diff --git a/src/subcommand/server.zig b/src/subcommand/server.zig
index 02a96e5..54258a9 100644
--- a/src/subcommand/server.zig
+++ b/src/subcommand/server.zig
@@ -1,6 +1,5 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
-const AtomicValue = std.atomic.Value;
const DebugAllocator = std.heap.DebugAllocator;
const Sigaction = std.posix.Sigaction;
@@ -17,24 +16,15 @@ const Server = zits.Server;
const safe_build = builtin.mode == .Debug or builtin.mode == .ReleaseSafe;
-var keep_running = AtomicValue(bool).init(true);
+var exit_lock: std.Io.Mutex = .init;
+var io: Io = undefined;
fn handleSigInt(sig: std.os.linux.SIG) callconv(.c) void {
_ = sig;
- keep_running.store(false, .monotonic);
+ exit_lock.unlock(io);
}
pub fn main(outer_alloc: Allocator, server_config: ServerInfo) !void {
- // Configure the signal action
- const act = Sigaction{
- .handler = .{ .handler = handleSigInt },
- .mask = std.posix.sigemptyset(),
- .flags = 0,
- };
-
- // Register the handler for SIGINT (Ctrl+C)
- std.posix.sigaction(std.posix.SIG.INT, &act, null);
-
{
var dba: DebugAllocator(.{}) = .init;
dba.backing_allocator = outer_alloc;
@@ -43,7 +33,19 @@ pub fn main(outer_alloc: Allocator, server_config: ServerInfo) !void {
var threaded: Threaded = .init(alloc, .{});
defer threaded.deinit();
- const io = threaded.io();
+ io = threaded.io();
+
+ try exit_lock.lock(io);
+
+ // Configure the signal action
+ const act = Sigaction{
+ .handler = .{ .handler = handleSigInt },
+ .mask = std.posix.sigemptyset(),
+ .flags = 0,
+ };
+
+ // Register the handler for SIGINT (Ctrl+C)
+ std.posix.sigaction(std.posix.SIG.INT, &act, null);
var server: Server = .{
.info = server_config,
@@ -54,10 +56,7 @@ pub fn main(outer_alloc: Allocator, server_config: ServerInfo) !void {
defer server_task.cancel(io) catch {};
// Block until Ctrl+C
- while (keep_running.load(.monotonic)) {
- try io.sleep(.fromMilliseconds(1), .awake);
- }
-
+ try exit_lock.lock(io);
std.debug.print("\n", .{});
std.log.info("Shutting down...", .{});
server_task.cancel(io) catch {};