Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/kernel/qbasictimer.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||
2 | ** | - | ||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||
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 The Qt Company. For licensing terms | - | ||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||
15 | ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software | - | ||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||
24 | ** | - | ||||||
25 | ** GNU General Public License Usage | - | ||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||
35 | ** | - | ||||||
36 | ** $QT_END_LICENSE$ | - | ||||||
37 | ** | - | ||||||
38 | ****************************************************************************/ | - | ||||||
39 | - | |||||||
40 | #include "qbasictimer.h" | - | ||||||
41 | #include "qabstracteventdispatcher.h" | - | ||||||
42 | #include "qabstracteventdispatcher_p.h" | - | ||||||
43 | - | |||||||
44 | QT_BEGIN_NAMESPACE | - | ||||||
45 | - | |||||||
46 | /*! | - | ||||||
47 | \class QBasicTimer | - | ||||||
48 | \inmodule QtCore | - | ||||||
49 | \brief The QBasicTimer class provides timer events for objects. | - | ||||||
50 | - | |||||||
51 | \ingroup events | - | ||||||
52 | - | |||||||
53 | This is a fast, lightweight, and low-level class used by Qt | - | ||||||
54 | internally. We recommend using the higher-level QTimer class | - | ||||||
55 | rather than this class if you want to use timers in your | - | ||||||
56 | applications. Note that this timer is a repeating timer that | - | ||||||
57 | will send subsequent timer events unless the stop() function is called. | - | ||||||
58 | - | |||||||
59 | To use this class, create a QBasicTimer, and call its start() | - | ||||||
60 | function with a timeout interval and with a pointer to a QObject | - | ||||||
61 | subclass. When the timer times out it will send a timer event to | - | ||||||
62 | the QObject subclass. The timer can be stopped at any time using | - | ||||||
63 | stop(). isActive() returns \c true for a timer that is running; | - | ||||||
64 | i.e. it has been started, has not reached the timeout time, and | - | ||||||
65 | has not been stopped. The timer's ID can be retrieved using | - | ||||||
66 | timerId(). | - | ||||||
67 | - | |||||||
68 | The \l{widgets/wiggly}{Wiggly} example uses QBasicTimer to repaint | - | ||||||
69 | a widget at regular intervals. | - | ||||||
70 | - | |||||||
71 | \sa QTimer, QTimerEvent, QObject::timerEvent(), Timers, {Wiggly Example} | - | ||||||
72 | */ | - | ||||||
73 | - | |||||||
74 | - | |||||||
75 | /*! | - | ||||||
76 | \fn QBasicTimer::QBasicTimer() | - | ||||||
77 | - | |||||||
78 | Contructs a basic timer. | - | ||||||
79 | - | |||||||
80 | \sa start() | - | ||||||
81 | */ | - | ||||||
82 | /*! | - | ||||||
83 | \fn QBasicTimer::~QBasicTimer() | - | ||||||
84 | - | |||||||
85 | Destroys the basic timer. | - | ||||||
86 | */ | - | ||||||
87 | - | |||||||
88 | /*! | - | ||||||
89 | \fn bool QBasicTimer::isActive() const | - | ||||||
90 | - | |||||||
91 | Returns \c true if the timer is running and has not been stopped; otherwise | - | ||||||
92 | returns \c false. | - | ||||||
93 | - | |||||||
94 | \sa start(), stop() | - | ||||||
95 | */ | - | ||||||
96 | - | |||||||
97 | /*! | - | ||||||
98 | \fn int QBasicTimer::timerId() const | - | ||||||
99 | - | |||||||
100 | Returns the timer's ID. | - | ||||||
101 | - | |||||||
102 | \sa QTimerEvent::timerId() | - | ||||||
103 | */ | - | ||||||
104 | - | |||||||
105 | /*! | - | ||||||
106 | \fn void QBasicTimer::start(int msec, QObject *object) | - | ||||||
107 | - | |||||||
108 | Starts (or restarts) the timer with a \a msec milliseconds timeout. The | - | ||||||
109 | timer will be a Qt::CoarseTimer. See Qt::TimerType for information on the | - | ||||||
110 | different timer types. | - | ||||||
111 | - | |||||||
112 | The given \a object will receive timer events. | - | ||||||
113 | - | |||||||
114 | \sa stop(), isActive(), QObject::timerEvent(), Qt::CoarseTimer | - | ||||||
115 | */ | - | ||||||
116 | void QBasicTimer::start(int msec, QObject *obj) | - | ||||||
117 | { | - | ||||||
118 | QAbstractEventDispatcher *eventDispatcher = QAbstractEventDispatcher::instance(); | - | ||||||
119 | if (Q_UNLIKELY(!eventDispatcher)) {
| 0-124026 | ||||||
120 | qWarning("QBasicTimer::start: QBasicTimer can only be used with threads started with QThread"); | - | ||||||
121 | return; never executed: return; | 0 | ||||||
122 | } | - | ||||||
123 | if (Q_UNLIKELY(obj && obj->thread() != eventDispatcher->thread())) {
| 0-124026 | ||||||
124 | qWarning("QBasicTimer::start: Timers cannot be started from another thread"); | - | ||||||
125 | return; never executed: return; | 0 | ||||||
126 | } | - | ||||||
127 | if (id) {
| 19345-104681 | ||||||
128 | if (Q_LIKELY(eventDispatcher->unregisterTimer(id)))
| 0-104681 | ||||||
129 | QAbstractEventDispatcherPrivate::releaseTimerId(id); executed 104681 times by 51 tests: QAbstractEventDispatcherPrivate::releaseTimerId(id); Executed by:
| 104681 | ||||||
130 | else | - | ||||||
131 | qWarning("QBasicTimer::start: Stopping previous timer failed. Possibly trying to stop from a different thread"); never executed: QMessageLogger(__FILE__, 131, __PRETTY_FUNCTION__).warning("QBasicTimer::start: Stopping previous timer failed. Possibly trying to stop from a different thread"); | 0 | ||||||
132 | } | - | ||||||
133 | id = 0; | - | ||||||
134 | if (obj)
| 236-123790 | ||||||
135 | id = eventDispatcher->registerTimer(msec, Qt::CoarseTimer, obj); executed 123790 times by 85 tests: id = eventDispatcher->registerTimer(msec, Qt::CoarseTimer, obj); Executed by:
| 123790 | ||||||
136 | } executed 124026 times by 85 tests: end of block Executed by:
| 124026 | ||||||
137 | - | |||||||
138 | /*! | - | ||||||
139 | \overload | - | ||||||
140 | - | |||||||
141 | Starts (or restarts) the timer with a \a msec milliseconds timeout and the | - | ||||||
142 | given \a timerType. See Qt::TimerType for information on the different | - | ||||||
143 | timer types. | - | ||||||
144 | - | |||||||
145 | \a obj will receive timer events. | - | ||||||
146 | - | |||||||
147 | \sa stop(), isActive(), QObject::timerEvent(), Qt::TimerType | - | ||||||
148 | */ | - | ||||||
149 | void QBasicTimer::start(int msec, Qt::TimerType timerType, QObject *obj) | - | ||||||
150 | { | - | ||||||
151 | QAbstractEventDispatcher *eventDispatcher = QAbstractEventDispatcher::instance(); | - | ||||||
152 | if (Q_UNLIKELY(msec < 0)) {
| 0-155 | ||||||
153 | qWarning("QBasicTimer::start: Timers cannot have negative timeouts"); | - | ||||||
154 | return; never executed: return; | 0 | ||||||
155 | } | - | ||||||
156 | if (Q_UNLIKELY(!eventDispatcher)) {
| 0-155 | ||||||
157 | qWarning("QBasicTimer::start: QBasicTimer can only be used with threads started with QThread"); | - | ||||||
158 | return; never executed: return; | 0 | ||||||
159 | } | - | ||||||
160 | if (Q_UNLIKELY(obj && obj->thread() != eventDispatcher->thread())) {
| 0-155 | ||||||
161 | qWarning("QBasicTimer::start: Timers cannot be started from another thread"); | - | ||||||
162 | return; never executed: return; | 0 | ||||||
163 | } | - | ||||||
164 | if (id) {
| 20-135 | ||||||
165 | if (Q_LIKELY(eventDispatcher->unregisterTimer(id)))
| 0-20 | ||||||
166 | QAbstractEventDispatcherPrivate::releaseTimerId(id); executed 20 times by 2 tests: QAbstractEventDispatcherPrivate::releaseTimerId(id); Executed by:
| 20 | ||||||
167 | else | - | ||||||
168 | qWarning("QBasicTimer::start: Stopping previous timer failed. Possibly trying to stop from a different thread"); never executed: QMessageLogger(__FILE__, 168, __PRETTY_FUNCTION__).warning("QBasicTimer::start: Stopping previous timer failed. Possibly trying to stop from a different thread"); | 0 | ||||||
169 | } | - | ||||||
170 | id = 0; | - | ||||||
171 | if (obj)
| 0-155 | ||||||
172 | id = eventDispatcher->registerTimer(msec, timerType, obj); executed 155 times by 12 tests: id = eventDispatcher->registerTimer(msec, timerType, obj); Executed by:
| 155 | ||||||
173 | } executed 155 times by 12 tests: end of block Executed by:
| 155 | ||||||
174 | - | |||||||
175 | /*! | - | ||||||
176 | Stops the timer. | - | ||||||
177 | - | |||||||
178 | \sa start(), isActive() | - | ||||||
179 | */ | - | ||||||
180 | void QBasicTimer::stop() | - | ||||||
181 | { | - | ||||||
182 | if (id) {
| 17772-167911 | ||||||
183 | QAbstractEventDispatcher *eventDispatcher = QAbstractEventDispatcher::instance(); | - | ||||||
184 | if (eventDispatcher) {
| 3-17769 | ||||||
185 | if (Q_UNLIKELY(!eventDispatcher->unregisterTimer(id))) {
| 0-17769 | ||||||
186 | qWarning("QBasicTimer::stop: Failed. Possibly trying to stop from a different thread"); | - | ||||||
187 | return; never executed: return; | 0 | ||||||
188 | } | - | ||||||
189 | QAbstractEventDispatcherPrivate::releaseTimerId(id); | - | ||||||
190 | } executed 17769 times by 96 tests: end of block Executed by:
| 17769 | ||||||
191 | } executed 17772 times by 99 tests: end of block Executed by:
| 17772 | ||||||
192 | id = 0; | - | ||||||
193 | } executed 185683 times by 298 tests: end of block Executed by:
| 185683 | ||||||
194 | - | |||||||
195 | QT_END_NAMESPACE | - | ||||||
Source code | Switch to Preprocessed file |