mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-14 15:56:13 +03:00
have to use an async handle to wake up event loop for max timer
This commit is contained in:
@ -25,6 +25,10 @@ pub fn MaxTimer(comptime cb: fn (*libuv.Timer) void) type {
|
|||||||
/// The last time this timer ran.
|
/// The last time this timer ran.
|
||||||
last: u64 = 0,
|
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(
|
pub fn init(
|
||||||
loop: libuv.Loop,
|
loop: libuv.Loop,
|
||||||
data: ?*anyopaque,
|
data: ?*anyopaque,
|
||||||
@ -35,6 +39,13 @@ pub fn MaxTimer(comptime cb: fn (*libuv.Timer) void) type {
|
|||||||
var timer = try libuv.Timer.init(alloc, loop);
|
var timer = try libuv.Timer.init(alloc, loop);
|
||||||
timer.setData(data);
|
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
|
// The maximum time can't be less than the interval otherwise this
|
||||||
// will just constantly fire.
|
// will just constantly fire.
|
||||||
if (max < min) return error.MaxShorterThanTimer;
|
if (max < min) return error.MaxShorterThanTimer;
|
||||||
@ -42,17 +53,24 @@ pub fn MaxTimer(comptime cb: fn (*libuv.Timer) void) type {
|
|||||||
.timer = timer,
|
.timer = timer,
|
||||||
.min = min,
|
.min = min,
|
||||||
.max = max,
|
.max = max,
|
||||||
|
.async_h = async_h,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *Self) void {
|
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 {
|
self.timer.close((struct {
|
||||||
fn callback(t: *libuv.Timer) void {
|
fn callback(t: *libuv.Timer) void {
|
||||||
const alloc = t.loop().getData(Allocator).?.*;
|
const alloc = t.loop().getData(Allocator).?.*;
|
||||||
t.deinit(alloc);
|
t.deinit(alloc);
|
||||||
}
|
}
|
||||||
}).callback);
|
}).callback);
|
||||||
self.* = undefined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This should be called from the callback to update the last called time.
|
/// This should be called from the callback to update the last called time.
|
||||||
@ -70,6 +88,10 @@ pub fn MaxTimer(comptime cb: fn (*libuv.Timer) void) type {
|
|||||||
// a tick as soon as possible.
|
// a tick as soon as possible.
|
||||||
if (!try self.timer.isActive()) {
|
if (!try self.timer.isActive()) {
|
||||||
try self.timer.start(cb, self.min, self.min);
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user