search for standard sites pub-search.waow.tech
search zig blog atproto
at multi-platform-schema 41 lines 884 B view raw
1const std = @import("std"); 2 3// ring buffer for real-time search activity 4pub const SLOTS = 60; 5const TICK_MS = 100; 6 7var counts: [SLOTS]u16 = .{0} ** SLOTS; 8var slot: usize = 0; 9var mutex: std.Thread.Mutex = .{}; 10var thread: ?std.Thread = null; 11 12fn tickLoop() void { 13 while (true) { 14 std.Thread.sleep(TICK_MS * std.time.ns_per_ms); 15 mutex.lock(); 16 slot = (slot + 1) % SLOTS; 17 counts[slot] = 0; 18 mutex.unlock(); 19 } 20} 21 22pub fn init() void { 23 thread = std.Thread.spawn(.{}, tickLoop, .{}) catch null; 24} 25 26pub fn getCounts() [SLOTS]u16 { 27 mutex.lock(); 28 defer mutex.unlock(); 29 var result: [SLOTS]u16 = undefined; 30 for (0..SLOTS) |i| { 31 const idx = (slot + 1 + i) % SLOTS; 32 result[i] = counts[idx]; 33 } 34 return result; 35} 36 37pub fn record() void { 38 mutex.lock(); 39 defer mutex.unlock(); 40 counts[slot] +|= 1; 41}