From c2ce158342f4b51a1e1dd24522f36b3b69faa28e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 24 Oct 2022 10:54:38 -0700 Subject: [PATCH] remove the max timer --- src/Window.zig | 1 - src/max_timer.zig | 109 ---------------------------------------------- 2 files changed, 110 deletions(-) delete mode 100644 src/max_timer.zig diff --git a/src/Window.zig b/src/Window.zig index 20c775c6b..91b82b8af 100644 --- a/src/Window.zig +++ b/src/Window.zig @@ -19,7 +19,6 @@ const font = @import("font/main.zig"); const Command = @import("Command.zig"); const SegmentedPool = @import("segmented_pool.zig").SegmentedPool; const trace = @import("tracy").trace; -const max_timer = @import("max_timer.zig"); const terminal = @import("terminal/main.zig"); const Config = @import("config.zig").Config; const input = @import("input.zig"); diff --git a/src/max_timer.zig b/src/max_timer.zig deleted file mode 100644 index bacfa0b92..000000000 --- a/src/max_timer.zig +++ /dev/null @@ -1,109 +0,0 @@ -const std = @import("std"); -const assert = std.debug.assert; -const Allocator = std.mem.Allocator; -const libuv = @import("libuv"); - -/// A coalescing timer that forces a run after a certain maximum time -/// since the last run. This is used for example by the renderer to try -/// to render at a high FPS but gracefully fall back under high IO load so -/// that we can process more data and increase throughput. -pub fn MaxTimer(comptime cb: fn (*libuv.Timer) void) type { - return struct { - const Self = @This(); - - /// The underlying libuv timer. - timer: libuv.Timer, - - /// The maximum time between timer calls. This is best effort based on - /// event loop load. If the event loop is busy, the timer will be run on - /// the next available tick. - max: u64, - - /// The fastest the timer will ever run. - min: u64, - - /// The last time this timer ran. - last: u64 = 0, - - /// This handle is used to wake up the event loop when the timer - /// is restarted. - async_h: libuv.Async, - - pub fn init( - loop: libuv.Loop, - data: ?*anyopaque, - min: u64, - max: u64, - ) !Self { - const alloc = loop.getData(Allocator).?.*; - var timer = try libuv.Timer.init(alloc, loop); - timer.setData(data); - - // The async handle is used to wake up the event loop. This is - // necessary since stop/starting a timer doesn't trigger the - // poll on the backend fd. - var async_h = try libuv.Async.init(alloc, loop, (struct { - fn callback(_: *libuv.Async) void {} - }).callback); - - // The maximum time can't be less than the interval otherwise this - // will just constantly fire. if (max < min) return error.MaxShorterThanTimer; - return Self{ - .timer = timer, - .min = min, - .max = max, - .async_h = async_h, - }; - } - - pub fn deinit(self: *Self) void { - self.async_h.close((struct { - fn callback(h: *libuv.Async) void { - const alloc = h.loop().getData(Allocator).?.*; - h.deinit(alloc); - } - }).callback); - - self.timer.close((struct { - fn callback(t: *libuv.Timer) void { - const alloc = t.loop().getData(Allocator).?.*; - t.deinit(alloc); - } - }).callback); - } - - /// This should be called from the callback to update the last called time. - pub fn tick(self: *Self) void { - self.timer.loop().updateTime(); - self.last = self.timer.loop().now(); - self.timer.stop() catch unreachable; - } - - /// Schedule the timer to run. If the timer is not started, it'll - /// run on the next min tick. If the timer is started, this will - /// delay the timer up to max time since the last run. - pub fn schedule(self: *Self) !void { - // If the timer hasn't been started, start it now and schedule - // a tick as soon as possible. - if (!try self.timer.isActive()) { - try self.timer.start(cb, self.min, self.min); - - // We have to send an async message to wake up the - // event loop. Starting a timer doesn't write to the fd. - try self.async_h.send(); - return; - } - - // If we are past the max time, we run the timer now. - try self.timer.stop(); - self.timer.loop().updateTime(); - const timeout = if (self.timer.loop().now() - self.last > self.max) - 0 - else - self.min; - - // We still have time, restart the timer so that it is min time away. - try self.timer.start(cb, timeout, 0); - } - }; -}