From 79621345b91316cc127154f28e2f2934fc1f64b4 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Fri, 7 Oct 2016 16:02:18 +0200 Subject: [PATCH] Better handling of slow timers Make sure the code deals more gracefully with timers that take a long time to execute, and therefore might miss their deadlines. --- common/rfb/Timer.cxx | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/common/rfb/Timer.cxx b/common/rfb/Timer.cxx index 676f24e1..efae36e2 100644 --- a/common/rfb/Timer.cxx +++ b/common/rfb/Timer.cxx @@ -1,4 +1,5 @@ /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. + * Copyright 2016 Pierre Ossman for Cendio AB * * This is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -58,20 +59,35 @@ inline static int diffTimeMillis(timeval later, timeval earlier) { std::list Timer::pending; int Timer::checkTimeouts() { + timeval start; + if (pending.empty()) return 0; - timeval now; - gettimeofday(&now, 0); - while (pending.front()->isBefore(now)) { - Timer* timer = pending.front(); + + gettimeofday(&start, 0); + while (pending.front()->isBefore(start)) { + Timer* timer; + timeval before; + + timer = pending.front(); pending.pop_front(); + + gettimeofday(&before, 0); if (timer->cb->handleTimeout(timer)) { + timeval now; + + gettimeofday(&now, 0); + timer->dueTime = addMillis(timer->dueTime, timer->timeoutMs); if (timer->isBefore(now)) { - // Time has jumped forwards! - vlog.info("time has moved forwards!"); - timer->dueTime = addMillis(now, timer->timeoutMs); + // Time has jumped forwards, or we're not getting enough + // CPU time for the timers + + timer->dueTime = addMillis(before, timer->timeoutMs); + if (timer->isBefore(now)) + timer->dueTime = now; } + insertTimer(timer); } else if (pending.empty()) { return 0; -- 2.39.5