kernel/qtimerinfo_unix.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
2** -
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
5** -
6** This file is part of the QtCore module of the Qt Toolkit. -
7** -
8** $QT_BEGIN_LICENSE:LGPL$ -
9** Commercial License Usage -
10** Licensees holding valid commercial Qt licenses may use this file in -
11** accordance with the commercial license agreement provided with the -
12** Software or, alternatively, in accordance with the terms contained in -
13** a written agreement between you and Digia. For licensing terms and -
14** conditions see http://qt.digia.com/licensing. For further information -
15** use the contact form at http://qt.digia.com/contact-us. -
16** -
17** GNU Lesser General Public License Usage -
18** Alternatively, this file may be used under the terms of the GNU Lesser -
19** General Public License version 2.1 as published by the Free Software -
20** Foundation and appearing in the file LICENSE.LGPL included in the -
21** packaging of this file. Please review the following information to -
22** ensure the GNU Lesser General Public License version 2.1 requirements -
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -
24** -
25** In addition, as a special exception, Digia gives you certain additional -
26** rights. These rights are described in the Digia Qt LGPL Exception -
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -
28** -
29** GNU General Public License Usage -
30** Alternatively, this file may be used under the terms of the GNU -
31** General Public License version 3.0 as published by the Free Software -
32** Foundation and appearing in the file LICENSE.GPL included in the -
33** packaging of this file. Please review the following information to -
34** ensure the GNU General Public License version 3.0 requirements will be -
35** met: http://www.gnu.org/copyleft/gpl.html. -
36** -
37** -
38** $QT_END_LICENSE$ -
39** -
40****************************************************************************/ -
41 -
42#include <qelapsedtimer.h> -
43#include <qcoreapplication.h> -
44 -
45#include "private/qcore_unix_p.h" -
46#include "private/qtimerinfo_unix_p.h" -
47#include "private/qobject_p.h" -
48#include "private/qabstracteventdispatcher_p.h" -
49 -
50#ifdef QTIMERINFO_DEBUG -
51# include <QDebug> -
52# include <QThread> -
53#endif -
54 -
55#include <sys/times.h> -
56 -
57QT_BEGIN_NAMESPACE -
58 -
59Q_CORE_EXPORT bool qt_disable_lowpriority_timers=false; -
60 -
61/* -
62 * Internal functions for manipulating timer data structures. The -
63 * timerBitVec array is used for keeping track of timer identifiers. -
64 */ -
65 -
66QTimerInfoList::QTimerInfoList() -
67{ -
68#if (_POSIX_MONOTONIC_CLOCK-0 <= 0) && !defined(Q_OS_MAC) && !defined(Q_OS_NACL) -
69 if (!QElapsedTimer::isMonotonic()) {
partially evaluated: !QElapsedTimer::isMonotonic()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1717532
0-1717532
70 // not using monotonic timers, initialize the timeChanged() machinery -
71 previousTime = qt_gettime();
never executed (the execution status of this line is deduced): previousTime = qt_gettime();
-
72 -
73 tms unused;
never executed (the execution status of this line is deduced): tms unused;
-
74 previousTicks = times(&unused);
never executed (the execution status of this line is deduced): previousTicks = times(&unused);
-
75 -
76 ticksPerSecond = sysconf(_SC_CLK_TCK);
never executed (the execution status of this line is deduced): ticksPerSecond = sysconf(_SC_CLK_TCK);
-
77 msPerTick = 1000/ticksPerSecond;
never executed (the execution status of this line is deduced): msPerTick = 1000/ticksPerSecond;
-
78 } else {
never executed: }
0
79 // detected monotonic timers -
80 previousTime.tv_sec = previousTime.tv_usec = 0;
executed (the execution status of this line is deduced): previousTime.tv_sec = previousTime.tv_usec = 0;
-
81 previousTicks = 0;
executed (the execution status of this line is deduced): previousTicks = 0;
-
82 ticksPerSecond = 0;
executed (the execution status of this line is deduced): ticksPerSecond = 0;
-
83 msPerTick = 0;
executed (the execution status of this line is deduced): msPerTick = 0;
-
84 }
executed: }
Execution Count:1717531
1717531
85#endif -
86 -
87 firstTimerInfo = 0;
executed (the execution status of this line is deduced): firstTimerInfo = 0;
-
88}
executed: }
Execution Count:1717531
1717531
89 -
90timeval QTimerInfoList::updateCurrentTime() -
91{ -
92 return (currentTime = qt_gettime());
executed: return (currentTime = qt_gettime());
Execution Count:1062167
1062167
93} -
94 -
95#if ((_POSIX_MONOTONIC_CLOCK-0 <= 0) && !defined(Q_OS_MAC) && !defined(Q_OS_INTEGRITY)) || defined(QT_BOOTSTRAPPED) -
96 -
97template <> -
98timeval qAbs(const timeval &t) -
99{ -
100 timeval tmp = t;
never executed (the execution status of this line is deduced): timeval tmp = t;
-
101 if (tmp.tv_sec < 0) {
never evaluated: tmp.tv_sec < 0
0
102 tmp.tv_sec = -tmp.tv_sec - 1;
never executed (the execution status of this line is deduced): tmp.tv_sec = -tmp.tv_sec - 1;
-
103 tmp.tv_usec -= 1000000;
never executed (the execution status of this line is deduced): tmp.tv_usec -= 1000000;
-
104 }
never executed: }
0
105 if (tmp.tv_sec == 0 && tmp.tv_usec < 0) {
never evaluated: tmp.tv_sec == 0
never evaluated: tmp.tv_usec < 0
0
106 tmp.tv_usec = -tmp.tv_usec;
never executed (the execution status of this line is deduced): tmp.tv_usec = -tmp.tv_usec;
-
107 }
never executed: }
0
108 return normalizedTimeval(tmp);
never executed: return normalizedTimeval(tmp);
0
109} -
110 -
111/* -
112 Returns true if the real time clock has changed by more than 10% -
113 relative to the processor time since the last time this function was -
114 called. This presumably means that the system time has been changed. -
115 -
116 If /a delta is nonzero, delta is set to our best guess at how much the system clock was changed. -
117*/ -
118bool QTimerInfoList::timeChanged(timeval *delta) -
119{ -
120#ifdef Q_OS_NACL -
121 Q_UNUSED(delta) -
122 return false; // Calling "times" crashes. -
123#endif -
124 struct tms unused;
never executed (the execution status of this line is deduced): struct tms unused;
-
125 clock_t currentTicks = times(&unused);
never executed (the execution status of this line is deduced): clock_t currentTicks = times(&unused);
-
126 -
127 clock_t elapsedTicks = currentTicks - previousTicks;
never executed (the execution status of this line is deduced): clock_t elapsedTicks = currentTicks - previousTicks;
-
128 timeval elapsedTime = currentTime - previousTime;
never executed (the execution status of this line is deduced): timeval elapsedTime = currentTime - previousTime;
-
129 -
130 timeval elapsedTimeTicks;
never executed (the execution status of this line is deduced): timeval elapsedTimeTicks;
-
131 elapsedTimeTicks.tv_sec = elapsedTicks / ticksPerSecond;
never executed (the execution status of this line is deduced): elapsedTimeTicks.tv_sec = elapsedTicks / ticksPerSecond;
-
132 elapsedTimeTicks.tv_usec = (((elapsedTicks * 1000) / ticksPerSecond) % 1000) * 1000;
never executed (the execution status of this line is deduced): elapsedTimeTicks.tv_usec = (((elapsedTicks * 1000) / ticksPerSecond) % 1000) * 1000;
-
133 -
134 timeval dummy;
never executed (the execution status of this line is deduced): timeval dummy;
-
135 if (!delta)
never evaluated: !delta
0
136 delta = &dummy;
never executed: delta = &dummy;
0
137 *delta = elapsedTime - elapsedTimeTicks;
never executed (the execution status of this line is deduced): *delta = elapsedTime - elapsedTimeTicks;
-
138 -
139 previousTicks = currentTicks;
never executed (the execution status of this line is deduced): previousTicks = currentTicks;
-
140 previousTime = currentTime;
never executed (the execution status of this line is deduced): previousTime = currentTime;
-
141 -
142 // If tick drift is more than 10% off compared to realtime, we assume that the clock has -
143 // been set. Of course, we have to allow for the tick granularity as well. -
144 timeval tickGranularity;
never executed (the execution status of this line is deduced): timeval tickGranularity;
-
145 tickGranularity.tv_sec = 0;
never executed (the execution status of this line is deduced): tickGranularity.tv_sec = 0;
-
146 tickGranularity.tv_usec = msPerTick * 1000;
never executed (the execution status of this line is deduced): tickGranularity.tv_usec = msPerTick * 1000;
-
147 return elapsedTimeTicks < ((qAbs(*delta) - tickGranularity) * 10);
never executed: return elapsedTimeTicks < ((qAbs(*delta) - tickGranularity) * 10);
0
148} -
149 -
150/* -
151 repair broken timer -
152*/ -
153void QTimerInfoList::timerRepair(const timeval &diff) -
154{ -
155 // repair all timers -
156 for (int i = 0; i < size(); ++i) {
never evaluated: i < size()
0
157 register QTimerInfo *t = at(i);
never executed (the execution status of this line is deduced): register QTimerInfo *t = at(i);
-
158 t->timeout = t->timeout + diff;
never executed (the execution status of this line is deduced): t->timeout = t->timeout + diff;
-
159 }
never executed: }
0
160}
never executed: }
0
161 -
162void QTimerInfoList::repairTimersIfNeeded() -
163{ -
164 if (QElapsedTimer::isMonotonic())
partially evaluated: QElapsedTimer::isMonotonic()
TRUEFALSE
yes
Evaluation Count:667496
no
Evaluation Count:0
0-667496
165 return;
executed: return;
Execution Count:667493
667493
166 timeval delta;
never executed (the execution status of this line is deduced): timeval delta;
-
167 if (timeChanged(&delta))
never evaluated: timeChanged(&delta)
0
168 timerRepair(delta);
never executed: timerRepair(delta);
0
169}
never executed: }
0
170 -
171#else // !(_POSIX_MONOTONIC_CLOCK-0 <= 0) && !defined(QT_BOOTSTRAPPED) -
172 -
173void QTimerInfoList::repairTimersIfNeeded() -
174{ -
175} -
176 -
177#endif -
178 -
179/* -
180 insert timer info into list -
181*/ -
182void QTimerInfoList::timerInsert(QTimerInfo *ti) -
183{ -
184 int index = size();
executed (the execution status of this line is deduced): int index = size();
-
185 while (index--) {
evaluated: index--
TRUEFALSE
yes
Evaluation Count:319711
yes
Evaluation Count:63534
63534-319711
186 register const QTimerInfo * const t = at(index);
executed (the execution status of this line is deduced): register const QTimerInfo * const t = at(index);
-
187 if (!(ti->timeout < t->timeout))
evaluated: !(ti->timeout < t->timeout)
TRUEFALSE
yes
Evaluation Count:111022
yes
Evaluation Count:208689
111022-208689
188 break;
executed: break;
Execution Count:111022
111022
189 }
executed: }
Execution Count:208689
208689
190 insert(index+1, ti);
executed (the execution status of this line is deduced): insert(index+1, ti);
-
191}
executed: }
Execution Count:174556
174556
192 -
193inline timeval &operator+=(timeval &t1, int ms) -
194{ -
195 t1.tv_sec += ms / 1000;
executed (the execution status of this line is deduced): t1.tv_sec += ms / 1000;
-
196 t1.tv_usec += ms % 1000 * 1000;
executed (the execution status of this line is deduced): t1.tv_usec += ms % 1000 * 1000;
-
197 return normalizedTimeval(t1);
executed: return normalizedTimeval(t1);
Execution Count:211189
211189
198} -
199 -
200inline timeval operator+(const timeval &t1, int ms) -
201{ -
202 timeval t2 = t1;
executed (the execution status of this line is deduced): timeval t2 = t1;
-
203 return t2 += ms;
executed: return t2 += ms;
Execution Count:132417
132417
204} -
205 -
206static timeval roundToMillisecond(timeval val) -
207{ -
208 // always round up -
209 // worst case scenario is that the first trigger of a 1-ms timer is 0.999 ms late -
210 -
211 int us = val.tv_usec % 1000;
executed (the execution status of this line is deduced): int us = val.tv_usec % 1000;
-
212 val.tv_usec += 1000 - us;
executed (the execution status of this line is deduced): val.tv_usec += 1000 - us;
-
213 return normalizedTimeval(val);
executed: return normalizedTimeval(val);
Execution Count:263163
263163
214} -
215 -
216#ifdef QTIMERINFO_DEBUG -
217QDebug operator<<(QDebug s, timeval tv) -
218{ -
219 s.nospace() << tv.tv_sec << "." << qSetFieldWidth(6) << qSetPadChar(QChar(48)) << tv.tv_usec << reset; -
220 return s.space(); -
221} -
222QDebug operator<<(QDebug s, Qt::TimerType t) -
223{ -
224 s << (t == Qt::PreciseTimer ? "P" : -
225 t == Qt::CoarseTimer ? "C" : "VC"); -
226 return s; -
227} -
228#endif -
229 -
230static void calculateCoarseTimerTimeout(QTimerInfo *t, timeval currentTime) -
231{ -
232 // The coarse timer works like this: -
233 // - interval under 40 ms: round to even -
234 // - between 40 and 99 ms: round to multiple of 4 -
235 // - otherwise: try to wake up at a multiple of 25 ms, with a maximum error of 5% -
236 // -
237 // We try to wake up at the following second-fraction, in order of preference: -
238 // 0 ms -
239 // 500 ms -
240 // 250 ms or 750 ms -
241 // 200, 400, 600, 800 ms -
242 // other multiples of 100 -
243 // other multiples of 50 -
244 // other multiples of 25 -
245 // -
246 // The objective is to make most timers wake up at the same time, thereby reducing CPU wakeups. -
247 -
248 register uint interval = uint(t->interval);
executed (the execution status of this line is deduced): register uint interval = uint(t->interval);
-
249 register uint msec = uint(t->timeout.tv_usec) / 1000;
executed (the execution status of this line is deduced): register uint msec = uint(t->timeout.tv_usec) / 1000;
-
250 Q_ASSERT(interval >= 20);
executed (the execution status of this line is deduced): qt_noop();
-
251 -
252 // Calculate how much we can round and still keep within 5% error -
253 uint absMaxRounding = interval / 20;
executed (the execution status of this line is deduced): uint absMaxRounding = interval / 20;
-
254 -
255 if (interval < 100 && interval != 25 && interval != 50 && interval != 75) {
evaluated: interval < 100
TRUEFALSE
yes
Evaluation Count:1353
yes
Evaluation Count:8100
partially evaluated: interval != 25
TRUEFALSE
yes
Evaluation Count:1353
no
Evaluation Count:0
evaluated: interval != 50
TRUEFALSE
yes
Evaluation Count:1346
yes
Evaluation Count:7
partially evaluated: interval != 75
TRUEFALSE
yes
Evaluation Count:1346
no
Evaluation Count:0
0-8100
256 // special mode for timers of less than 100 ms -
257 if (interval < 50) {
evaluated: interval < 50
TRUEFALSE
yes
Evaluation Count:1329
yes
Evaluation Count:17
17-1329
258 // round to even -
259 // round towards multiples of 50 ms -
260 register bool roundUp = (msec % 50) >= 25;
executed (the execution status of this line is deduced): register bool roundUp = (msec % 50) >= 25;
-
261 msec >>= 1;
executed (the execution status of this line is deduced): msec >>= 1;
-
262 msec |= uint(roundUp);
executed (the execution status of this line is deduced): msec |= uint(roundUp);
-
263 msec <<= 1;
executed (the execution status of this line is deduced): msec <<= 1;
-
264 } else {
executed: }
Execution Count:1329
1329
265 // round to multiple of 4 -
266 // round towards multiples of 100 ms -
267 register bool roundUp = (msec % 100) >= 50;
executed (the execution status of this line is deduced): register bool roundUp = (msec % 100) >= 50;
-
268 msec >>= 2;
executed (the execution status of this line is deduced): msec >>= 2;
-
269 msec |= uint(roundUp);
executed (the execution status of this line is deduced): msec |= uint(roundUp);
-
270 msec <<= 2;
executed (the execution status of this line is deduced): msec <<= 2;
-
271 }
executed: }
Execution Count:17
17
272 } else { -
273 uint min = qMax<int>(0, msec - absMaxRounding);
executed (the execution status of this line is deduced): uint min = qMax<int>(0, msec - absMaxRounding);
-
274 uint max = qMin(1000u, msec + absMaxRounding);
executed (the execution status of this line is deduced): uint max = qMin(1000u, msec + absMaxRounding);
-
275 -
276 // find the boundary that we want, according to the rules above -
277 // extra rules: -
278 // 1) whatever the interval, we'll take any round-to-the-second timeout -
279 if (min == 0) {
evaluated: min == 0
TRUEFALSE
yes
Evaluation Count:1238
yes
Evaluation Count:6869
1238-6869
280 msec = 0;
executed (the execution status of this line is deduced): msec = 0;
-
281 goto recalculate;
executed: goto recalculate;
Execution Count:1238
1238
282 } else if (max == 1000) {
evaluated: max == 1000
TRUEFALSE
yes
Evaluation Count:393
yes
Evaluation Count:6476
393-6476
283 msec = 1000;
executed (the execution status of this line is deduced): msec = 1000;
-
284 goto recalculate;
executed: goto recalculate;
Execution Count:393
393
285 } -
286 -
287 uint wantedBoundaryMultiple;
executed (the execution status of this line is deduced): uint wantedBoundaryMultiple;
-
288 -
289 // 2) if the interval is a multiple of 500 ms and > 5000 ms, we'll always round -
290 // towards a round-to-the-second -
291 // 3) if the interval is a multiple of 500 ms, we'll round towards the nearest -
292 // multiple of 500 ms -
293 if ((interval % 500) == 0) {
evaluated: (interval % 500) == 0
TRUEFALSE
yes
Evaluation Count:4482
yes
Evaluation Count:1994
1994-4482
294 if (interval >= 5000) {
evaluated: interval >= 5000
TRUEFALSE
yes
Evaluation Count:228
yes
Evaluation Count:4254
228-4254
295 msec = msec >= 500 ? max : min;
evaluated: msec >= 500
TRUEFALSE
yes
Evaluation Count:168
yes
Evaluation Count:60
60-168
296 goto recalculate;
executed: goto recalculate;
Execution Count:228
228
297 } else { -
298 wantedBoundaryMultiple = 500;
executed (the execution status of this line is deduced): wantedBoundaryMultiple = 500;
-
299 }
executed: }
Execution Count:4254
4254
300 } else if ((interval % 50) == 0) {
evaluated: (interval % 50) == 0
TRUEFALSE
yes
Evaluation Count:1986
yes
Evaluation Count:8
8-1986
301 // 4) same for multiples of 250, 200, 100, 50 -
302 uint mult50 = interval / 50;
executed (the execution status of this line is deduced): uint mult50 = interval / 50;
-
303 if ((mult50 % 4) == 0) {
evaluated: (mult50 % 4) == 0
TRUEFALSE
yes
Evaluation Count:903
yes
Evaluation Count:1083
903-1083
304 // multiple of 200 -
305 wantedBoundaryMultiple = 200;
executed (the execution status of this line is deduced): wantedBoundaryMultiple = 200;
-
306 } else if ((mult50 % 2) == 0) {
executed: }
Execution Count:903
evaluated: (mult50 % 2) == 0
TRUEFALSE
yes
Evaluation Count:1023
yes
Evaluation Count:60
60-1023
307 // multiple of 100 -
308 wantedBoundaryMultiple = 100;
executed (the execution status of this line is deduced): wantedBoundaryMultiple = 100;
-
309 } else if ((mult50 % 5) == 0) {
executed: }
Execution Count:1023
evaluated: (mult50 % 5) == 0
TRUEFALSE
yes
Evaluation Count:6
yes
Evaluation Count:54
6-1023
310 // multiple of 250 -
311 wantedBoundaryMultiple = 250;
executed (the execution status of this line is deduced): wantedBoundaryMultiple = 250;
-
312 } else {
executed: }
Execution Count:6
6
313 // multiple of 50 -
314 wantedBoundaryMultiple = 50;
executed (the execution status of this line is deduced): wantedBoundaryMultiple = 50;
-
315 }
executed: }
Execution Count:54
54
316 } else { -
317 wantedBoundaryMultiple = 25;
executed (the execution status of this line is deduced): wantedBoundaryMultiple = 25;
-
318 }
executed: }
Execution Count:8
8
319 -
320 uint base = msec / wantedBoundaryMultiple * wantedBoundaryMultiple;
executed (the execution status of this line is deduced): uint base = msec / wantedBoundaryMultiple * wantedBoundaryMultiple;
-
321 uint middlepoint = base + wantedBoundaryMultiple / 2;
executed (the execution status of this line is deduced): uint middlepoint = base + wantedBoundaryMultiple / 2;
-
322 if (msec < middlepoint)
evaluated: msec < middlepoint
TRUEFALSE
yes
Evaluation Count:3594
yes
Evaluation Count:2654
2654-3594
323 msec = qMax(base, min);
executed: msec = qMax(base, min);
Execution Count:3594
3594
324 else -
325 msec = qMin(base + wantedBoundaryMultiple, max);
executed: msec = qMin(base + wantedBoundaryMultiple, max);
Execution Count:2654
2654
326 } -
327 -
328recalculate:
code before this statement executed: recalculate:
Execution Count:7594
7594
329 if (msec == 1000u) {
evaluated: msec == 1000u
TRUEFALSE
yes
Evaluation Count:393
yes
Evaluation Count:9060
393-9060
330 ++t->timeout.tv_sec;
executed (the execution status of this line is deduced): ++t->timeout.tv_sec;
-
331 t->timeout.tv_usec = 0;
executed (the execution status of this line is deduced): t->timeout.tv_usec = 0;
-
332 } else {
executed: }
Execution Count:393
393
333 t->timeout.tv_usec = msec * 1000;
executed (the execution status of this line is deduced): t->timeout.tv_usec = msec * 1000;
-
334 }
executed: }
Execution Count:9060
9060
335 -
336 if (t->timeout < currentTime)
partially evaluated: t->timeout < currentTime
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:9453
0-9453
337 t->timeout += interval;
never executed: t->timeout += interval;
0
338}
executed: }
Execution Count:9453
9453
339 -
340static void calculateNextTimeout(QTimerInfo *t, timeval currentTime) -
341{ -
342 switch (t->timerType) { -
343 case Qt::PreciseTimer: -
344 case Qt::CoarseTimer: -
345 t->timeout += t->interval;
executed (the execution status of this line is deduced): t->timeout += t->interval;
-
346 if (t->timeout < currentTime) {
evaluated: t->timeout < currentTime
TRUEFALSE
yes
Evaluation Count:36769
yes
Evaluation Count:5234
5234-36769
347 t->timeout = currentTime;
executed (the execution status of this line is deduced): t->timeout = currentTime;
-
348 t->timeout += t->interval;
executed (the execution status of this line is deduced): t->timeout += t->interval;
-
349 }
executed: }
Execution Count:36769
36769
350#ifdef QTIMERINFO_DEBUG -
351 t->expected += t->interval; -
352 if (t->expected < currentTime) { -
353 t->expected = currentTime; -
354 t->expected += t->interval; -
355 } -
356#endif -
357 if (t->timerType == Qt::CoarseTimer)
evaluated: t->timerType == Qt::CoarseTimer
TRUEFALSE
yes
Evaluation Count:2276
yes
Evaluation Count:39727
2276-39727
358 calculateCoarseTimerTimeout(t, currentTime);
executed: calculateCoarseTimerTimeout(t, currentTime);
Execution Count:2276
2276
359 return;
executed: return;
Execution Count:42003
42003
360 -
361 case Qt::VeryCoarseTimer: -
362 // we don't need to take care of the microsecond component of t->interval -
363 t->timeout.tv_sec += t->interval;
executed (the execution status of this line is deduced): t->timeout.tv_sec += t->interval;
-
364 if (t->timeout.tv_sec <= currentTime.tv_sec)
partially evaluated: t->timeout.tv_sec <= currentTime.tv_sec
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:136
0-136
365 t->timeout.tv_sec = currentTime.tv_sec + t->interval;
never executed: t->timeout.tv_sec = currentTime.tv_sec + t->interval;
0
366#ifdef QTIMERINFO_DEBUG -
367 t->expected.tv_sec += t->interval; -
368 if (t->expected.tv_sec <= currentTime.tv_sec) -
369 t->expected.tv_sec = currentTime.tv_sec + t->interval; -
370#endif -
371 return;
executed: return;
Execution Count:136
136
372 } -
373 -
374#ifdef QTIMERINFO_DEBUG -
375 if (t->timerType != Qt::PreciseTimer) -
376 qDebug() << "timer" << t->timerType << hex << t->id << dec << "interval" << t->interval -
377 << "originally expected at" << t->expected << "will fire at" << t->timeout -
378 << "or" << (t->timeout - t->expected) << "s late"; -
379#endif -
380}
never executed: }
0
381 -
382/* -
383 Returns the time to wait for the next timer, or null if no timers -
384 are waiting. -
385*/ -
386bool QTimerInfoList::timerWait(timeval &tm) -
387{ -
388 timeval currentTime = updateCurrentTime();
executed (the execution status of this line is deduced): timeval currentTime = updateCurrentTime();
-
389 repairTimersIfNeeded();
executed (the execution status of this line is deduced): repairTimersIfNeeded();
-
390 -
391 // Find first waiting timer not already active -
392 QTimerInfo *t = 0;
executed (the execution status of this line is deduced): QTimerInfo *t = 0;
-
393 for (QTimerInfoList::const_iterator it = constBegin(); it != constEnd(); ++it) {
evaluated: it != constEnd()
TRUEFALSE
yes
Evaluation Count:301604
yes
Evaluation Count:324962
301604-324962
394 if (!(*it)->activateRef) {
evaluated: !(*it)->activateRef
TRUEFALSE
yes
Evaluation Count:301591
yes
Evaluation Count:13
13-301591
395 t = *it;
executed (the execution status of this line is deduced): t = *it;
-
396 break;
executed: break;
Execution Count:301591
301591
397 } -
398 }
executed: }
Execution Count:13
13
399 -
400 if (!t)
evaluated: !t
TRUEFALSE
yes
Evaluation Count:324950
yes
Evaluation Count:301591
301591-324950
401 return false;
executed: return false;
Execution Count:324944
324944
402 -
403 if (currentTime < t->timeout) {
evaluated: currentTime < t->timeout
TRUEFALSE
yes
Evaluation Count:263162
yes
Evaluation Count:38429
38429-263162
404 // time to wait -
405 tm = roundToMillisecond(t->timeout - currentTime);
executed (the execution status of this line is deduced): tm = roundToMillisecond(t->timeout - currentTime);
-
406 } else {
executed: }
Execution Count:263162
263162
407 // no time to wait -
408 tm.tv_sec = 0;
executed (the execution status of this line is deduced): tm.tv_sec = 0;
-
409 tm.tv_usec = 0;
executed (the execution status of this line is deduced): tm.tv_usec = 0;
-
410 }
executed: }
Execution Count:38429
38429
411 -
412 return true;
executed: return true;
Execution Count:301591
301591
413} -
414 -
415/* -
416 Returns the timer's remaining time in milliseconds with the given timerId, or -
417 null if there is nothing left. If the timer id is not found in the list, the -
418 returned value will be -1. If the timer is overdue, the returned value will be 0. -
419*/ -
420int QTimerInfoList::timerRemainingTime(int timerId) -
421{ -
422 timeval currentTime = updateCurrentTime();
executed (the execution status of this line is deduced): timeval currentTime = updateCurrentTime();
-
423 repairTimersIfNeeded();
executed (the execution status of this line is deduced): repairTimersIfNeeded();
-
424 timeval tm = {0, 0};
executed (the execution status of this line is deduced): timeval tm = {0, 0};
-
425 -
426 for (int i = 0; i < count(); ++i) {
partially evaluated: i < count()
TRUEFALSE
yes
Evaluation Count:1
no
Evaluation Count:0
0-1
427 register QTimerInfo *t = at(i);
executed (the execution status of this line is deduced): register QTimerInfo *t = at(i);
-
428 if (t->id == timerId) {
partially evaluated: t->id == timerId
TRUEFALSE
yes
Evaluation Count:1
no
Evaluation Count:0
0-1
429 if (currentTime < t->timeout) {
partially evaluated: currentTime < t->timeout
TRUEFALSE
yes
Evaluation Count:1
no
Evaluation Count:0
0-1
430 // time to wait -
431 tm = roundToMillisecond(t->timeout - currentTime);
executed (the execution status of this line is deduced): tm = roundToMillisecond(t->timeout - currentTime);
-
432 return tm.tv_sec*1000 + tm.tv_usec/1000;
executed: return tm.tv_sec*1000 + tm.tv_usec/1000;
Execution Count:1
1
433 } else { -
434 return 0;
never executed: return 0;
0
435 } -
436 } -
437 }
never executed: }
0
438 -
439#ifndef QT_NO_DEBUG -
440 qWarning("QTimerInfoList::timerRemainingTime: timer id %i not found", timerId); -
441#endif -
442 -
443 return -1;
never executed: return -1;
0
444} -
445 -
446void QTimerInfoList::registerTimer(int timerId, int interval, Qt::TimerType timerType, QObject *object) -
447{ -
448 QTimerInfo *t = new QTimerInfo;
executed (the execution status of this line is deduced): QTimerInfo *t = new QTimerInfo;
-
449 t->id = timerId;
executed (the execution status of this line is deduced): t->id = timerId;
-
450 t->interval = interval;
executed (the execution status of this line is deduced): t->interval = interval;
-
451 t->timerType = timerType;
executed (the execution status of this line is deduced): t->timerType = timerType;
-
452 t->obj = object;
executed (the execution status of this line is deduced): t->obj = object;
-
453 t->activateRef = 0;
executed (the execution status of this line is deduced): t->activateRef = 0;
-
454 -
455 timeval expected = updateCurrentTime() + interval;
executed (the execution status of this line is deduced): timeval expected = updateCurrentTime() + interval;
-
456 -
457 switch (timerType) { -
458 case Qt::PreciseTimer: -
459 // high precision timer is based on millisecond precision -
460 // so no adjustment is necessary -
461 t->timeout = expected;
executed (the execution status of this line is deduced): t->timeout = expected;
-
462 break;
executed: break;
Execution Count:876
876
463 -
464 case Qt::CoarseTimer: -
465 // this timer has up to 5% coarseness -
466 // so our boundaries are 20 ms and 20 s -
467 // below 20 ms, 5% inaccuracy is below 1 ms, so we convert to high precision -
468 // above 20 s, 5% inaccuracy is above 1 s, so we convert to VeryCoarseTimer -
469 if (interval >= 20000) {
evaluated: interval >= 20000
TRUEFALSE
yes
Evaluation Count:4755
yes
Evaluation Count:126785
4755-126785
470 t->timerType = Qt::VeryCoarseTimer;
executed (the execution status of this line is deduced): t->timerType = Qt::VeryCoarseTimer;
-
471 // fall through -
472 } else {
executed: }
Execution Count:4755
4755
473 t->timeout = expected;
executed (the execution status of this line is deduced): t->timeout = expected;
-
474 if (interval <= 20) {
evaluated: interval <= 20
TRUEFALSE
yes
Evaluation Count:119608
yes
Evaluation Count:7177
7177-119608
475 t->timerType = Qt::PreciseTimer;
executed (the execution status of this line is deduced): t->timerType = Qt::PreciseTimer;
-
476 // no adjustment is necessary -
477 } else if (interval <= 20000) {
executed: }
Execution Count:119608
partially evaluated: interval <= 20000
TRUEFALSE
yes
Evaluation Count:7177
no
Evaluation Count:0
0-119608
478 calculateCoarseTimerTimeout(t, currentTime);
executed (the execution status of this line is deduced): calculateCoarseTimerTimeout(t, currentTime);
-
479 }
executed: }
Execution Count:7177
7177
480 break;
executed: break;
Execution Count:126785
126785
481 } -
482 // fall through -
483 case Qt::VeryCoarseTimer:
code before this statement executed: case Qt::VeryCoarseTimer:
Execution Count:4755
4755
484 // the very coarse timer is based on full second precision, -
485 // so we keep the interval in seconds (round to closest second) -
486 t->interval /= 500;
executed (the execution status of this line is deduced): t->interval /= 500;
-
487 t->interval += 1;
executed (the execution status of this line is deduced): t->interval += 1;
-
488 t->interval >>= 1;
executed (the execution status of this line is deduced): t->interval >>= 1;
-
489 t->timeout.tv_sec = currentTime.tv_sec + t->interval;
executed (the execution status of this line is deduced): t->timeout.tv_sec = currentTime.tv_sec + t->interval;
-
490 t->timeout.tv_usec = 0;
executed (the execution status of this line is deduced): t->timeout.tv_usec = 0;
-
491 -
492 // if we're past the half-second mark, increase the timeout again -
493 if (currentTime.tv_usec > 500*1000)
evaluated: currentTime.tv_usec > 500*1000
TRUEFALSE
yes
Evaluation Count:1748
yes
Evaluation Count:3008
1748-3008
494 ++t->timeout.tv_sec;
executed: ++t->timeout.tv_sec;
Execution Count:1748
1748
495 }
executed: }
Execution Count:4756
4756
496 -
497 timerInsert(t);
executed (the execution status of this line is deduced): timerInsert(t);
-
498 -
499#ifdef QTIMERINFO_DEBUG -
500 t->expected = expected; -
501 t->cumulativeError = 0; -
502 t->count = 0; -
503 if (t->timerType != Qt::PreciseTimer) -
504 qDebug() << "timer" << t->timerType << hex <<t->id << dec << "interval" << t->interval << "expected at" -
505 << t->expected << "will fire first at" << t->timeout; -
506#endif -
507}
executed: }
Execution Count:132417
132417
508 -
509bool QTimerInfoList::unregisterTimer(int timerId) -
510{ -
511 // set timer inactive -
512 for (int i = 0; i < count(); ++i) {
partially evaluated: i < count()
TRUEFALSE
yes
Evaluation Count:62560525
no
Evaluation Count:0
0-62560525
513 register QTimerInfo *t = at(i);
executed (the execution status of this line is deduced): register QTimerInfo *t = at(i);
-
514 if (t->id == timerId) {
evaluated: t->id == timerId
TRUEFALSE
yes
Evaluation Count:129527
yes
Evaluation Count:62430998
129527-62430998
515 // found it -
516 removeAt(i);
executed (the execution status of this line is deduced): removeAt(i);
-
517 if (t == firstTimerInfo)
evaluated: t == firstTimerInfo
TRUEFALSE
yes
Evaluation Count:12259
yes
Evaluation Count:117268
12259-117268
518 firstTimerInfo = 0;
executed: firstTimerInfo = 0;
Execution Count:12259
12259
519 if (t->activateRef)
evaluated: t->activateRef
TRUEFALSE
yes
Evaluation Count:12282
yes
Evaluation Count:117245
12282-117245
520 *(t->activateRef) = 0;
executed: *(t->activateRef) = 0;
Execution Count:12282
12282
521 delete t;
executed (the execution status of this line is deduced): delete t;
-
522 return true;
executed: return true;
Execution Count:129527
129527
523 } -
524 }
executed: }
Execution Count:62430998
62430998
525 // id not found -
526 return false;
never executed: return false;
0
527} -
528 -
529bool QTimerInfoList::unregisterTimers(QObject *object) -
530{ -
531 if (isEmpty())
evaluated: isEmpty()
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:1179
2-1179
532 return false;
executed: return false;
Execution Count:2
2
533 for (int i = 0; i < count(); ++i) {
evaluated: i < count()
TRUEFALSE
yes
Evaluation Count:3800
yes
Evaluation Count:1179
1179-3800
534 register QTimerInfo *t = at(i);
executed (the execution status of this line is deduced): register QTimerInfo *t = at(i);
-
535 if (t->obj == object) {
evaluated: t->obj == object
TRUEFALSE
yes
Evaluation Count:1228
yes
Evaluation Count:2572
1228-2572
536 // object found -
537 removeAt(i);
executed (the execution status of this line is deduced): removeAt(i);
-
538 if (t == firstTimerInfo)
partially evaluated: t == firstTimerInfo
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1228
0-1228
539 firstTimerInfo = 0;
never executed: firstTimerInfo = 0;
0
540 if (t->activateRef)
partially evaluated: t->activateRef
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1228
0-1228
541 *(t->activateRef) = 0;
never executed: *(t->activateRef) = 0;
0
542 delete t;
executed (the execution status of this line is deduced): delete t;
-
543 // move back one so that we don't skip the new current item -
544 --i;
executed (the execution status of this line is deduced): --i;
-
545 }
executed: }
Execution Count:1228
1228
546 }
executed: }
Execution Count:3800
3800
547 return true;
executed: return true;
Execution Count:1179
1179
548} -
549 -
550QList<QAbstractEventDispatcher::TimerInfo> QTimerInfoList::registeredTimers(QObject *object) const -
551{ -
552 QList<QAbstractEventDispatcher::TimerInfo> list;
executed (the execution status of this line is deduced): QList<QAbstractEventDispatcher::TimerInfo> list;
-
553 for (int i = 0; i < count(); ++i) {
evaluated: i < count()
TRUEFALSE
yes
Evaluation Count:31186
yes
Evaluation Count:23300
23300-31186
554 register const QTimerInfo * const t = at(i);
executed (the execution status of this line is deduced): register const QTimerInfo * const t = at(i);
-
555 if (t->obj == object) {
evaluated: t->obj == object
TRUEFALSE
yes
Evaluation Count:9
yes
Evaluation Count:31177
9-31177
556 list << QAbstractEventDispatcher::TimerInfo(t->id,
executed (the execution status of this line is deduced): list << QAbstractEventDispatcher::TimerInfo(t->id,
-
557 (t->timerType == Qt::VeryCoarseTimer
executed (the execution status of this line is deduced): (t->timerType == Qt::VeryCoarseTimer
-
558 ? t->interval * 1000
executed (the execution status of this line is deduced): ? t->interval * 1000
-
559 : t->interval),
executed (the execution status of this line is deduced): : t->interval),
-
560 t->timerType);
executed (the execution status of this line is deduced): t->timerType);
-
561 }
executed: }
Execution Count:9
9
562 }
executed: }
Execution Count:31186
31186
563 return list;
executed: return list;
Execution Count:23301
23301
564} -
565 -
566/* -
567 Activate pending timers, returning how many where activated. -
568*/ -
569int QTimerInfoList::activateTimers() -
570{ -
571 if (qt_disable_lowpriority_timers || isEmpty())
partially evaluated: qt_disable_lowpriority_timers
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:40996
evaluated: isEmpty()
TRUEFALSE
yes
Evaluation Count:13
yes
Evaluation Count:40983
0-40996
572 return 0; // nothing to do
executed: return 0;
Execution Count:13
13
573 -
574 int n_act = 0, maxCount = 0;
executed (the execution status of this line is deduced): int n_act = 0, maxCount = 0;
-
575 firstTimerInfo = 0;
executed (the execution status of this line is deduced): firstTimerInfo = 0;
-
576 -
577 timeval currentTime = updateCurrentTime();
executed (the execution status of this line is deduced): timeval currentTime = updateCurrentTime();
-
578 // qDebug() << "Thread" << QThread::currentThreadId() << "woken up at" << currentTime; -
579 repairTimersIfNeeded();
executed (the execution status of this line is deduced): repairTimersIfNeeded();
-
580 -
581 -
582 // Find out how many timer have expired -
583 for (QTimerInfoList::const_iterator it = constBegin(); it != constEnd(); ++it) {
evaluated: it != constEnd()
TRUEFALSE
yes
Evaluation Count:54292
yes
Evaluation Count:28977
28977-54292
584 if (currentTime < (*it)->timeout)
evaluated: currentTime < (*it)->timeout
TRUEFALSE
yes
Evaluation Count:12006
yes
Evaluation Count:42286
12006-42286
585 break;
executed: break;
Execution Count:12006
12006
586 maxCount++;
executed (the execution status of this line is deduced): maxCount++;
-
587 }
executed: }
Execution Count:42286
42286
588 -
589 //fire the timers. -
590 while (maxCount--) {
evaluated: maxCount--
TRUEFALSE
yes
Evaluation Count:42264
yes
Evaluation Count:40858
40858-42264
591 if (isEmpty())
partially evaluated: isEmpty()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:42264
0-42264
592 break;
never executed: break;
0
593 -
594 QTimerInfo *currentTimerInfo = first();
executed (the execution status of this line is deduced): QTimerInfo *currentTimerInfo = first();
-
595 if (currentTime < currentTimerInfo->timeout)
evaluated: currentTime < currentTimerInfo->timeout
TRUEFALSE
yes
Evaluation Count:125
yes
Evaluation Count:42139
125-42139
596 break; // no timer has expired
executed: break;
Execution Count:125
125
597 -
598 if (!firstTimerInfo) {
evaluated: !firstTimerInfo
TRUEFALSE
yes
Evaluation Count:42110
yes
Evaluation Count:29
29-42110
599 firstTimerInfo = currentTimerInfo;
executed (the execution status of this line is deduced): firstTimerInfo = currentTimerInfo;
-
600 } else if (firstTimerInfo == currentTimerInfo) {
executed: }
Execution Count:42110
partially evaluated: firstTimerInfo == currentTimerInfo
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:29
0-42110
601 // avoid sending the same timer multiple times -
602 break;
never executed: break;
0
603 } else if (currentTimerInfo->interval < firstTimerInfo->interval
evaluated: currentTimerInfo->interval < firstTimerInfo->interval
TRUEFALSE
yes
Evaluation Count:12
yes
Evaluation Count:17
12-17
604 || currentTimerInfo->interval == firstTimerInfo->interval) {
evaluated: currentTimerInfo->interval == firstTimerInfo->interval
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:16
1-16
605 firstTimerInfo = currentTimerInfo;
executed (the execution status of this line is deduced): firstTimerInfo = currentTimerInfo;
-
606 }
executed: }
Execution Count:13
13
607 -
608 // remove from list -
609 removeFirst();
executed (the execution status of this line is deduced): removeFirst();
-
610 -
611#ifdef QTIMERINFO_DEBUG -
612 float diff; -
613 if (currentTime < currentTimerInfo->expected) { -
614 // early -
615 timeval early = currentTimerInfo->expected - currentTime; -
616 diff = -(early.tv_sec + early.tv_usec / 1000000.0); -
617 } else { -
618 timeval late = currentTime - currentTimerInfo->expected; -
619 diff = late.tv_sec + late.tv_usec / 1000000.0; -
620 } -
621 currentTimerInfo->cumulativeError += diff; -
622 ++currentTimerInfo->count; -
623 if (currentTimerInfo->timerType != Qt::PreciseTimer) -
624 qDebug() << "timer" << currentTimerInfo->timerType << hex << currentTimerInfo->id << dec << "interval" -
625 << currentTimerInfo->interval << "firing at" << currentTime -
626 << "(orig" << currentTimerInfo->expected << "scheduled at" << currentTimerInfo->timeout -
627 << ") off by" << diff << "activation" << currentTimerInfo->count -
628 << "avg error" << (currentTimerInfo->cumulativeError / currentTimerInfo->count); -
629#endif -
630 -
631 // determine next timeout time -
632 calculateNextTimeout(currentTimerInfo, currentTime);
executed (the execution status of this line is deduced): calculateNextTimeout(currentTimerInfo, currentTime);
-
633 -
634 // reinsert timer -
635 timerInsert(currentTimerInfo);
executed (the execution status of this line is deduced): timerInsert(currentTimerInfo);
-
636 if (currentTimerInfo->interval > 0)
evaluated: currentTimerInfo->interval > 0
TRUEFALSE
yes
Evaluation Count:5389
yes
Evaluation Count:36750
5389-36750
637 n_act++;
executed: n_act++;
Execution Count:5389
5389
638 -
639 if (!currentTimerInfo->activateRef) {
evaluated: !currentTimerInfo->activateRef
TRUEFALSE
yes
Evaluation Count:42121
yes
Evaluation Count:18
18-42121
640 // send event, but don't allow it to recurse -
641 currentTimerInfo->activateRef = &currentTimerInfo;
executed (the execution status of this line is deduced): currentTimerInfo->activateRef = &currentTimerInfo;
-
642 -
643 QTimerEvent e(currentTimerInfo->id);
executed (the execution status of this line is deduced): QTimerEvent e(currentTimerInfo->id);
-
644 QCoreApplication::sendEvent(currentTimerInfo->obj, &e);
executed (the execution status of this line is deduced): QCoreApplication::sendEvent(currentTimerInfo->obj, &e);
-
645 -
646 if (currentTimerInfo)
evaluated: currentTimerInfo
TRUEFALSE
yes
Evaluation Count:29839
yes
Evaluation Count:12282
12282-29839
647 currentTimerInfo->activateRef = 0;
executed: currentTimerInfo->activateRef = 0;
Execution Count:29839
29839
648 }
executed: }
Execution Count:42121
42121
649 }
executed: }
Execution Count:42139
42139
650 -
651 firstTimerInfo = 0;
executed (the execution status of this line is deduced): firstTimerInfo = 0;
-
652 // qDebug() << "Thread" << QThread::currentThreadId() << "activated" << n_act << "timers"; -
653 return n_act;
executed: return n_act;
Execution Count:40983
40983
654} -
655 -
656QT_END_NAMESPACE -
657 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial