Something went wrong. Try again.
A port of FlightAware's dump1090, a Mode S and ADS-B decoder for software defined radios, to Zig 0.16.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: GPL-2.0-or-later
//! One place for the receiver to say what it is doing, safely.//!//! Several tasks have something to report: the decoding loop, each network//! accept loop, every outbound feed, and the FlightAware uplink. They were//! all handed the same `Io.Writer` and all called `print` on it directly,//! which is a data race rather than merely untidy — a writer keeps its buffer//! and its end offset in plain fields, so two tasks writing at once interleave//! their bytes and can advance the offset past what either of them wrote.//!//! The fix is not a queue or a logging thread. It is a mutex held for exactly//! as long as one complete message, which is what makes a line the unit that//! arrives whole.
const std = @import("std");const Io = std.Io;
/// A writer several tasks may use.////// It carries its own `Io` so that the things reporting through it need not/// thread one into every call site that has something to say.pub const Log = struct { io: Io, writer: *Io.Writer, mutex: Io.Mutex = .init,
pub fn init(io: Io, writer: *Io.Writer) Log { return .{ .io = io, .writer = writer }; }
/// Write one complete message and flush it. /// /// Errors are dropped. Somewhere to report a failure to report is not a /// thing this has, and a receiver that stopped decoding because its log /// could not be written would be worse than a quiet one. pub fn print(self: *Log, comptime format: []const u8, args: anytype) void { self.mutex.lockUncancelable(self.io); defer self.mutex.unlock(self.io); self.writer.print(format, args) catch return; self.writer.flush() catch {}; }
/// The same, for a message that is already formatted. pub fn write(self: *Log, bytes: []const u8) void { self.mutex.lockUncancelable(self.io); defer self.mutex.unlock(self.io); self.writer.writeAll(bytes) catch return; self.writer.flush() catch {}; }};
test "messages from several tasks arrive whole" { const testing = std.testing;
var threaded: Io.Threaded = .init(testing.allocator, .{}); defer threaded.deinit(); const io = threaded.io();
var out: Io.Writer.Allocating = .init(testing.allocator); defer out.deinit();
var log: Log = .init(io, &out.writer);
const task = struct { fn run(l: *Log, tag: u8) void { for (0..200) |_| l.print("{c}{c}{c}{c}{c}\n", .{ tag, tag, tag, tag, tag }); } }.run;
var group: Io.Group = .init; defer group.cancel(io); try group.concurrent(io, task, .{ &log, 'a' }); try group.concurrent(io, task, .{ &log, 'b' }); try group.await(io);
// Every line is one task's or the other's, never a mixture. Without the // lock this is where the interleaving shows up. var lines = std.mem.tokenizeScalar(u8, out.written(), '\n'); var count: usize = 0; while (lines.next()) |line| { count += 1; try testing.expectEqual(@as(usize, 5), line.len); for (line) |c| try testing.expectEqual(line[0], c); } try testing.expectEqual(@as(usize, 400), count);}