mirror of
https://git.robbyzambito.me/zits
synced 2026-02-04 03:34:48 +00:00
Cleaner SIGINT handling
Use a Mutex to wait for the signal handler to fire instead of checking an atomic boolean over and over again.
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const Allocator = std.mem.Allocator;
|
const Allocator = std.mem.Allocator;
|
||||||
const AtomicValue = std.atomic.Value;
|
|
||||||
const DebugAllocator = std.heap.DebugAllocator;
|
const DebugAllocator = std.heap.DebugAllocator;
|
||||||
const Sigaction = std.posix.Sigaction;
|
const Sigaction = std.posix.Sigaction;
|
||||||
|
|
||||||
@@ -17,14 +16,27 @@ const Server = zits.Server;
|
|||||||
|
|
||||||
const safe_build = builtin.mode == .Debug or builtin.mode == .ReleaseSafe;
|
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 {
|
fn handleSigInt(sig: std.os.linux.SIG) callconv(.c) void {
|
||||||
_ = sig;
|
_ = sig;
|
||||||
keep_running.store(false, .monotonic);
|
exit_lock.unlock(io);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main(outer_alloc: Allocator, server_config: ServerInfo) !void {
|
pub fn main(outer_alloc: Allocator, server_config: ServerInfo) !void {
|
||||||
|
{
|
||||||
|
var dba: DebugAllocator(.{}) = .init;
|
||||||
|
dba.backing_allocator = outer_alloc;
|
||||||
|
defer _ = dba.deinit();
|
||||||
|
const alloc = if (safe_build) dba.allocator() else outer_alloc;
|
||||||
|
|
||||||
|
var threaded: Threaded = .init(alloc, .{});
|
||||||
|
defer threaded.deinit();
|
||||||
|
io = threaded.io();
|
||||||
|
|
||||||
|
try exit_lock.lock(io);
|
||||||
|
|
||||||
// Configure the signal action
|
// Configure the signal action
|
||||||
const act = Sigaction{
|
const act = Sigaction{
|
||||||
.handler = .{ .handler = handleSigInt },
|
.handler = .{ .handler = handleSigInt },
|
||||||
@@ -35,16 +47,6 @@ pub fn main(outer_alloc: Allocator, server_config: ServerInfo) !void {
|
|||||||
// Register the handler for SIGINT (Ctrl+C)
|
// Register the handler for SIGINT (Ctrl+C)
|
||||||
std.posix.sigaction(std.posix.SIG.INT, &act, null);
|
std.posix.sigaction(std.posix.SIG.INT, &act, null);
|
||||||
|
|
||||||
{
|
|
||||||
var dba: DebugAllocator(.{}) = .init;
|
|
||||||
dba.backing_allocator = outer_alloc;
|
|
||||||
defer _ = dba.deinit();
|
|
||||||
const alloc = if (safe_build) dba.allocator() else outer_alloc;
|
|
||||||
|
|
||||||
var threaded: Threaded = .init(alloc, .{});
|
|
||||||
defer threaded.deinit();
|
|
||||||
const io = threaded.io();
|
|
||||||
|
|
||||||
var server: Server = .{
|
var server: Server = .{
|
||||||
.info = server_config,
|
.info = server_config,
|
||||||
};
|
};
|
||||||
@@ -54,10 +56,7 @@ pub fn main(outer_alloc: Allocator, server_config: ServerInfo) !void {
|
|||||||
defer server_task.cancel(io) catch {};
|
defer server_task.cancel(io) catch {};
|
||||||
|
|
||||||
// Block until Ctrl+C
|
// Block until Ctrl+C
|
||||||
while (keep_running.load(.monotonic)) {
|
try exit_lock.lock(io);
|
||||||
try io.sleep(.fromMilliseconds(1), .awake);
|
|
||||||
}
|
|
||||||
|
|
||||||
std.debug.print("\n", .{});
|
std.debug.print("\n", .{});
|
||||||
std.log.info("Shutting down...", .{});
|
std.log.info("Shutting down...", .{});
|
||||||
server_task.cancel(io) catch {};
|
server_task.cancel(io) catch {};
|
||||||
|
|||||||
Reference in New Issue
Block a user