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