|
1 | 1 | // SPDX-License-Identifier: MPL-2.0 |
2 | | -// Copyright (c) Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk> |
| 2 | +// Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk> |
3 | 3 | // |
4 | 4 | // cartridge_shim.zig — Shared helpers for the ADR-0006 five-symbol |
5 | 5 | // cartridge ABI (`boj_cartridge_init / deinit / name / version / invoke`). |
@@ -94,8 +94,116 @@ pub fn writeResult( |
94 | 94 | return RC_SUCCESS; |
95 | 95 | } |
96 | 96 |
|
| 97 | +// ── Shared runtime Io (Zig 0.16 compat) ────────────────────────────── |
| 98 | +// |
| 99 | +// Zig 0.16 moved blocking primitives and the wall clock onto the |
| 100 | +// `std.Io` interface (`std.Thread.Mutex` and `std.time.*Timestamp` |
| 101 | +// were removed from the stdlib). The shim owns one process-wide Io |
| 102 | +// backed by `std.Io.Threaded` so cartridge code keeps drop-in call |
| 103 | +// sites — `var m: shim.Mutex = .{}; m.lock(); defer m.unlock();` and |
| 104 | +// `shim.milliTimestamp()` — without threading an Io handle through the |
| 105 | +// C ABI. Cartridges that need richer Io (http, fs, net) should use |
| 106 | +// this same `shim.io()` rather than constructing their own runtime. |
| 107 | + |
| 108 | +var shared_threaded: std.Io.Threaded = undefined; |
| 109 | +var shared_io_state: std.atomic.Value(u8) = .init(0); // 0=uninit 1=initing 2=ready |
| 110 | + |
| 111 | +/// The process-wide `std.Io`, lazily initialised on first use. |
| 112 | +/// Thread-safe: a single CAS winner runs `Threaded.init`; racing |
| 113 | +/// callers yield until it is published. |
| 114 | +pub fn io() std.Io { |
| 115 | + if (shared_io_state.load(.acquire) != 2) { |
| 116 | + if (shared_io_state.cmpxchgStrong(0, 1, .acq_rel, .acquire) == null) { |
| 117 | + shared_threaded = std.Io.Threaded.init(std.heap.smp_allocator, .{}); |
| 118 | + shared_io_state.store(2, .release); |
| 119 | + } else { |
| 120 | + while (shared_io_state.load(.acquire) != 2) std.Thread.yield() catch {}; |
| 121 | + } |
| 122 | + } |
| 123 | + return shared_threaded.io(); |
| 124 | +} |
| 125 | + |
| 126 | +/// Drop-in replacement for the removed `std.Thread.Mutex`, backed by |
| 127 | +/// `std.Io.Mutex` over the shim's shared Io. Zero-initialisable: |
| 128 | +/// `var m: shim.Mutex = .{};` |
| 129 | +pub const Mutex = struct { |
| 130 | + inner: std.Io.Mutex = .init, |
| 131 | + |
| 132 | + pub fn lock(m: *Mutex) void { |
| 133 | + m.inner.lockUncancelable(io()); |
| 134 | + } |
| 135 | + |
| 136 | + pub fn unlock(m: *Mutex) void { |
| 137 | + m.inner.unlock(io()); |
| 138 | + } |
| 139 | + |
| 140 | + pub fn tryLock(m: *Mutex) bool { |
| 141 | + return m.inner.tryLock(); |
| 142 | + } |
| 143 | +}; |
| 144 | + |
| 145 | +/// Nanoseconds since the POSIX epoch (drop-in for the removed |
| 146 | +/// `std.time.nanoTimestamp`). |
| 147 | +pub fn nanoTimestamp() i128 { |
| 148 | + const ts = std.Io.Clock.Timestamp.now(io(), .real); |
| 149 | + return @intCast(ts.raw.nanoseconds); |
| 150 | +} |
| 151 | + |
| 152 | +/// Milliseconds since the POSIX epoch (drop-in for the removed |
| 153 | +/// `std.time.milliTimestamp`). |
| 154 | +pub fn milliTimestamp() i64 { |
| 155 | + return @intCast(@divTrunc(nanoTimestamp(), std.time.ns_per_ms)); |
| 156 | +} |
| 157 | + |
| 158 | +/// Seconds since the POSIX epoch (drop-in for the removed |
| 159 | +/// `std.time.timestamp`). |
| 160 | +pub fn timestamp() i64 { |
| 161 | + return @intCast(@divTrunc(nanoTimestamp(), std.time.ns_per_s)); |
| 162 | +} |
| 163 | + |
| 164 | +/// Fill `buffer` with cryptographically secure random bytes (drop-in for |
| 165 | +/// the removed `std.crypto.random.bytes`). |
| 166 | +pub fn randomBytes(buffer: []u8) void { |
| 167 | + io().random(buffer); |
| 168 | +} |
| 169 | + |
| 170 | +/// Cryptographically secure random integer (drop-in for the removed |
| 171 | +/// `std.crypto.random.int(T)`). |
| 172 | +pub fn randomInt(comptime T: type) T { |
| 173 | + var buf: [@sizeOf(T)]u8 = undefined; |
| 174 | + io().random(&buf); |
| 175 | + return @bitCast(buf); |
| 176 | +} |
| 177 | + |
| 178 | +/// Process environment lookup (drop-in for the removed |
| 179 | +/// `std.posix.getenv`). Only analysed when referenced; callers must link |
| 180 | +/// libc (`root_module.link_libc = true` — the standard ffi build shape). |
| 181 | +pub fn getenv(name: [*:0]const u8) ?[:0]const u8 { |
| 182 | + const p = std.c.getenv(name) orelse return null; |
| 183 | + return std.mem.sliceTo(p, 0); |
| 184 | +} |
| 185 | + |
97 | 186 | // ── Tests ──────────────────────────────────────────────────────────── |
98 | 187 |
|
| 188 | +test "Mutex: lock/unlock and tryLock round-trip" { |
| 189 | + var m: Mutex = .{}; |
| 190 | + m.lock(); |
| 191 | + try std.testing.expect(!m.tryLock()); |
| 192 | + m.unlock(); |
| 193 | + try std.testing.expect(m.tryLock()); |
| 194 | + m.unlock(); |
| 195 | +} |
| 196 | + |
| 197 | +test "timestamps: monotone-ish and unit-consistent" { |
| 198 | + const ns = nanoTimestamp(); |
| 199 | + const ms = milliTimestamp(); |
| 200 | + const s = timestamp(); |
| 201 | + try std.testing.expect(ns > 0); |
| 202 | + // Same instant expressed in three units must agree to within a step. |
| 203 | + try std.testing.expect(@abs(@divTrunc(ns, std.time.ns_per_s) - s) <= 1); |
| 204 | + try std.testing.expect(@abs(@divTrunc(ms, std.time.ms_per_s) - s) <= 1); |
| 205 | +} |
| 206 | + |
99 | 207 | test "writeResult: body fits, writes and sets length" { |
100 | 208 | var buf: [64]u8 = undefined; |
101 | 209 | var len: usize = buf.len; |
|
0 commit comments