Skip to content
Merged
6 changes: 3 additions & 3 deletions zig/src/abi/runtime.zig
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ pub const DaemonRuntime = struct {
},
},
);
errdefer self.daemon.deinit();
errdefer self.daemon.destroy();

// Bind the platform to the underlying OS callbacks
self.platform.attach();
Expand All @@ -169,8 +169,8 @@ pub const DaemonRuntime = struct {
return self;
}

pub fn deinit(self: *DaemonRuntime, allocator: std.mem.Allocator) void {
self.daemon.deinit();
pub fn destroy(self: *DaemonRuntime, allocator: std.mem.Allocator) void {
self.daemon.destroy();
self.platform.deinit();
self.registry.deinit(allocator);
self.options.deinit(allocator);
Expand Down
14 changes: 8 additions & 6 deletions zig/src/c/exports.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,29 @@ pub const CryptoBackend = enum {
if (@hasDecl(crypto, "PARTOUT_CRYPTO_OPENSSL")) return .openssl;
if (@hasDecl(crypto, "PARTOUT_CRYPTO_MBEDTLS")) return .native;
if (builtin.is_test) return .mock;
unreachable;
@compileError("no default crypto backend is available");
}
};

pub fn cryptoFunctionTable(backend: CryptoBackend) crypto.pp_crypto_fnt {
pub const CryptoFunctionTableError = error{UnsupportedCryptoBackend};

pub fn cryptoFunctionTable(backend: CryptoBackend) CryptoFunctionTableError!crypto.pp_crypto_fnt {
return switch (backend) {
.openssl => if (@hasDecl(crypto, "PARTOUT_CRYPTO_OPENSSL"))
crypto.pp_crypto_fnt_openssl()
else
unreachable,
error.UnsupportedCryptoBackend,
.mbedtls => if (@hasDecl(crypto, "PARTOUT_CRYPTO_MBEDTLS"))
crypto.pp_crypto_fnt_mbedtls()
else
unreachable,
error.UnsupportedCryptoBackend,
.native => if (@hasDecl(crypto, "PARTOUT_CRYPTO_MBEDTLS"))
crypto.pp_crypto_fnt_native()
else
unreachable,
error.UnsupportedCryptoBackend,
.mock => if (builtin.is_test)
crypto.pp_crypto_fnt_mock()
else
unreachable,
error.UnsupportedCryptoBackend,
};
}
2 changes: 1 addition & 1 deletion zig/src/core/actor.zig
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub fn ActorWithFinish(
return self;
}

pub fn deinit(self: *Self) void {
pub fn destroy(self: *Self) void {
self.shutdown();
self.cond.deinit();
self.mutex.deinit();
Expand Down
2 changes: 1 addition & 1 deletion zig/src/core/api_manual.zig
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ pub const Subnet = struct {
else switch (parsed_address.family) {
.v4 => 32,
.v6 => 128,
.hostname => unreachable,
.hostname => return null,
};
if (!parsed_address.family.isValidPrefixLength(prefix)) return null;
return .{
Expand Down
18 changes: 9 additions & 9 deletions zig/src/core/concurrency.zig
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ pub const RunAfter = struct {
self.mutex.unlock();
return;
},
.idle, .scheduled => unreachable,
.idle, .scheduled => @panic("invalid RunAfter state after callback"),
}
self.cond.broadcast();
self.mutex.unlock();
Expand Down Expand Up @@ -450,7 +450,7 @@ pub const RunAfter = struct {
self.state = switch (self.state) {
.idle, .scheduled => .scheduled,
.running, .running_scheduled => .running_scheduled,
.stopping => unreachable,
.stopping => @panic("cannot schedule a stopping RunAfter"),
};
self.cond.broadcast();
}
Expand Down Expand Up @@ -545,7 +545,7 @@ const PosixMutex = struct {
pub fn lock(self: *PosixMutex) void {
switch (std.c.pthread_mutex_lock(&self.raw)) {
.SUCCESS => {},
else => unreachable,
else => @panic("pthread_mutex_lock failed"),
}
}

Expand All @@ -555,7 +555,7 @@ const PosixMutex = struct {
pub fn unlock(self: *PosixMutex) void {
switch (std.c.pthread_mutex_unlock(&self.raw)) {
.SUCCESS => {},
else => unreachable,
else => @panic("pthread_mutex_unlock failed"),
}
}

Expand All @@ -565,7 +565,7 @@ const PosixMutex = struct {
pub fn deinit(self: *PosixMutex) void {
switch (std.c.pthread_mutex_destroy(&self.raw)) {
.SUCCESS => {},
else => unreachable,
else => @panic("pthread_mutex_destroy failed"),
}
}
};
Expand All @@ -582,7 +582,7 @@ const PosixCondition = struct {
pub fn wait(self: *PosixCondition, mutex: *Mutex) void {
switch (std.c.pthread_cond_wait(&self.raw, &mutex.raw)) {
.SUCCESS => {},
else => unreachable,
else => @panic("pthread_cond_wait failed"),
}
}

Expand All @@ -592,7 +592,7 @@ const PosixCondition = struct {
pub fn broadcast(self: *PosixCondition) void {
switch (std.c.pthread_cond_broadcast(&self.raw)) {
.SUCCESS => {},
else => unreachable,
else => @panic("pthread_cond_broadcast failed"),
}
}

Expand All @@ -602,7 +602,7 @@ const PosixCondition = struct {
pub fn deinit(self: *PosixCondition) void {
switch (std.c.pthread_cond_destroy(&self.raw)) {
.SUCCESS => {},
else => unreachable,
else => @panic("pthread_cond_destroy failed"),
}
}
};
Expand Down Expand Up @@ -659,7 +659,7 @@ const WindowsCondition = struct {
pub fn wait(self: *WindowsCondition, mutex: *Mutex) void {
switch (RtlSleepConditionVariableSRW(&self.raw, &mutex.raw, null, 0)) {
.SUCCESS => {},
else => unreachable,
else => @panic("RtlSleepConditionVariableSRW failed"),
}
}

Expand Down
20 changes: 18 additions & 2 deletions zig/src/core/logging.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: GPL-3.0

const std = @import("std");
const builtin = @import("builtin");

const api = @import("api.zig");
const concurrency = @import("concurrency.zig");
Expand Down Expand Up @@ -137,6 +138,20 @@ pub fn writeCString(level: Level, message: [*:0]const u8) void {
dispatchCString(logger, @intFromEnum(level), message);
}

pub fn writeAndFailDebug(message: [:0]const u8) void {
write(.err, message);
if (builtin.mode == .Debug) {
@panic(message);
}
}

pub fn writefAndFailDebug(comptime fmt: []const u8, args: anytype) void {
writef(.err, fmt, args);
if (builtin.mode == .Debug) {
std.debug.panic(fmt, args);
}
}

fn dispatchSlice(
logger: Logger,
level: c_int,
Expand Down Expand Up @@ -343,7 +358,8 @@ fn forLog(
value: anytype,
) ![]const u8 {
const T = @TypeOf(value);
const format = comptime logFormat(T) orelse unreachable;
const format = comptime logFormat(T) orelse
@compileError("unsupported type passed to forLog()");
return switch (format) {
.declared => T.logging_formatter(allocator, value),
.model => |adapter| formatWithAdapter(allocator, value, adapter),
Expand All @@ -354,7 +370,7 @@ fn forLog(
.array => switch (@typeInfo(T)) {
.array => formatArray(allocator, &value),
.pointer => formatArray(allocator, value),
else => unreachable,
else => @compileError("array log format requires an array or pointer type"),
},
.dictionary => formatDictionary(allocator, &value),
.dereference => forLog(allocator, value.*),
Expand Down
7 changes: 4 additions & 3 deletions zig/src/net/connection.zig
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,11 @@ pub const Connection = struct {
stop: *const fn (*anyopaque, u32, Events) void,
network_change: *const fn (*anyopaque, io.ReachabilityInfo, Events) void,
better_path: *const fn (*anyopaque, Events) void,
deinit: *const fn (*anyopaque) void,
/// Called synchronously when the shared daemon-owned looper
/// terminates, before runtime recovery or connection destruction.
looper_terminated: ?*const fn (*anyopaque, ?looper.Looper.Failure) void = null,
/// Destroys this object. This is the very last step of the lifecycle.
destroy: *const fn (*anyopaque) void,
};

pub fn start(self: Connection, events: Events) StartError!bool {
Expand Down Expand Up @@ -208,7 +209,7 @@ pub const Connection = struct {
block(self.ptr, failure);
}

pub fn deinit(self: Connection) void {
self.vtable.deinit(self.ptr);
pub fn destroy(self: Connection) void {
self.vtable.destroy(self.ptr);
}
};
15 changes: 7 additions & 8 deletions zig/src/net/daemon.zig
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ pub const Daemon = struct {
};
errdefer {
daemon.is_deinitializing = true;
actor.deinit();
actor.destroy();
}

if (activeConnectionModule(&profile) != null) {
Expand All @@ -194,7 +194,7 @@ pub const Daemon = struct {
return daemon;
}

pub fn deinit(self: *Daemon) void {
pub fn destroy(self: *Daemon) void {
// This is crucial to guarantee that there will not be in-flight
// handlers (reachability, better path, connection events) still calling
// into the daemon.
Expand All @@ -209,7 +209,7 @@ pub const Daemon = struct {
// Suppress the actor's unexpected-termination path: stop() already
// dismantled the connection runtime.
self.is_deinitializing = true;
self.actor.deinit();
self.actor.destroy();

self.monitor.setEventHandler(null);
self.monitor.stopObserving();
Expand Down Expand Up @@ -797,7 +797,7 @@ pub const Daemon = struct {
module,
sb,
);
errdefer connection.deinit();
errdefer connection.destroy();

// Publish a complete runtime before the looper can invoke its
// terminal callback.
Expand Down Expand Up @@ -840,11 +840,10 @@ pub const Daemon = struct {
// Keep the borrowed looper object alive until the connection has
// released every Session that refers to it, but first join its worker
// so the terminal callback cannot race connection deinitialization.
runtime.looper.stop() catch |err| switch (err) {
error.TerminalFailure => {},
else => log.writef(.debug, "Unable to stop connection looper: {s}", .{@errorName(err)}),
runtime.looper.stop() catch |err| {
log.writef(.debug, "Unable to stop connection looper: {s}", .{@errorName(err)});
};
runtime.connection.deinit();
runtime.connection.destroy();
runtime.looper.deinit();
self.allocator.destroy(runtime.looper);
self.connection_runtime = null;
Expand Down
Loading
Loading