Something went wrong. Try again.
prefect server in zig
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687// synchronization primitives//// zig 0.16 removed std.Thread.Mutex; the replacement std.Io.Mutex requires// an Io parameter at every lock/unlock site. that's too invasive for this// codebase, so we wrap it with a global Io.Threaded instance and expose the// old plain-lock-and-unlock API.//// main.zig must call initGlobal() before any mutex is locked. module-level// `var m: Mutex = .{};` is safe to declare before init since it only zeros// the embedded state.
const std = @import("std");
var global_threaded: ?std.Io.Threaded = null;var global_io_cached: ?std.Io = null;
pub fn initGlobal(allocator: std.mem.Allocator) void { if (global_threaded != null) return; global_threaded = std.Io.Threaded.init(allocator, .{}); global_io_cached = global_threaded.?.io();}
pub fn deinitGlobal() void { if (global_threaded) |*t| { t.deinit(); global_threaded = null; global_io_cached = null; }}
/// Deterministic simulation only: leases, the db pool and every `sync.Mutex`/// read the simulator's clock through `io`.pub fn initSimulated(io: std.Io) void { global_io_cached = io;}
pub fn globalIo() std.Io { if (global_io_cached) |io| return io; // tests bypass main(), so lazy-init with smp_allocator on first access. // production paths should still call initGlobal explicitly in main(). initGlobal(std.heap.smp_allocator); return global_io_cached.?;}
pub const Mutex = struct { inner: std.Io.Mutex = std.Io.Mutex.init,
pub fn lock(self: *Mutex) void { self.inner.lockUncancelable(globalIo()); }
pub fn unlock(self: *Mutex) void { self.inner.unlock(globalIo()); }
pub fn tryLock(self: *Mutex) bool { return self.inner.tryLock(); }};
pub const Condition = struct { inner: std.Io.Condition = std.Io.Condition.init,
pub fn wait(self: *Condition, mutex: *Mutex) void { self.inner.waitUncancelable(globalIo(), &mutex.inner); }
pub fn signal(self: *Condition) void { self.inner.signal(globalIo()); }
pub fn broadcast(self: *Condition) void { self.inner.broadcast(globalIo()); }
/// timed wait — std.Io.Condition has no native timeout. callers loop on a /// predicate and use this as a "release the lock briefly and come back" hook. /// the wakeup is not signal-driven; semantics are polling. pub fn timedWait(self: *Condition, mutex: *Mutex, timeout_ns: u64) !void { _ = self; mutex.unlock(); defer mutex.lock(); const poll_ns: u64 = @min(timeout_ns, 10 * std.time.ns_per_ms); globalIo().sleep(.{ .nanoseconds = @intCast(poll_ns) }, .awake) catch {}; }};