在之前描述的rearmTimerSkippingUpdateFor,最终调用了setTimer,
void VSyncDispatchTimerQueue::setTimer(nsecs_t targetTime, nsecs_t /*now*/) {
// 把这次定时器计划唤醒的目标时间保存到成员变量 mIntendedWakeupTime
mIntendedWakeupTime = targetTime;
// 在mIntendedWakeupTime这个时间点触发timerCallback
mTimeKeeper->alarmAt(std::bind(&VSyncDispatchTimerQueue::timerCallback, this),
mIntendedWakeupTime);
// 记录这次调用 setTimer() 时,系统当前时间是多少
mLastTimerSchedule = mTimeKeeper->now();
}
再看alarmAt,利用 Linux 的 timerfd(定时器文件描述符)机制,设置一个绝对时间的单次(One-shot)定时器
void Timer::alarmAt(std::function<void()> callback, nsecs_t time) {
std::lock_guard lock(mMutex);
using namespace std::literals;
static constexpr int ns_per_s =
std::chrono::duration_cast<std::chrono::nanoseconds>(1s).count();
// 把传入的回调函数保存到 Timer 类的成员变量中,以便时间到了之后能够拿出来执行
setCallback(std::move(callback));
struct itimerspec old_timer;
struct itimerspec new_timer {
// 设为 0,表示**单次定时器**,触发一次后停止,不周期循环。
// VSync 调度每次根据实际情况重新计算时间点
.it_interval = {
.tv_sec = 0, .tv_nsec =
292



被折叠的 条评论
为什么被折叠?



