Changeset View
Changeset View
Standalone View
Standalone View
src/qca_safetimer.cpp
Show First 20 Lines • Show All 78 Lines • ▼ Show 20 Line(s) | 75 | { | |||
---|---|---|---|---|---|
79 | fixerTimerId = startTimer(msec); | 79 | fixerTimerId = startTimer(msec); | ||
80 | #ifdef SAFETIMER_DEBUG | 80 | #ifdef SAFETIMER_DEBUG | ||
81 | qDebug() << "START FIXTIMER: id =" << fixerTimerId << ", thread =" << thread() << ", interval =" << msec << parent(); | 81 | qDebug() << "START FIXTIMER: id =" << fixerTimerId << ", thread =" << thread() << ", interval =" << msec << parent(); | ||
82 | #endif | 82 | #endif | ||
83 | } | 83 | } | ||
84 | 84 | | |||
85 | bool SafeTimer::Private::event(QEvent *event) | 85 | bool SafeTimer::Private::event(QEvent *event) | ||
86 | { | 86 | { | ||
87 | if (event->type() == QEvent::ThreadChange && fixerTimerId /* timer is actived */) | 87 | if (event->type() == QEvent::ThreadChange && fixerTimerId /* timer is actived */) { | ||
88 | { | | |||
89 | // Timer dies when an object changes owner thread. This trick | 88 | // Timer dies when an object changes owner thread. This trick | ||
90 | // used to ressurect old timer in the new thread. | 89 | // used to ressurect old timer in the new thread. | ||
91 | // Signal is emited in the old thread but will be gotten in the new one. | 90 | // Signal is emited in the old thread but will be gotten in the new one. | ||
92 | #ifdef SAFETIMER_DEBUG | 91 | #ifdef SAFETIMER_DEBUG | ||
93 | qDebug() << "STOP FIXTIMER ON CHANGE THREAD: id =" << fixerTimerId << ", thread =" << thread() << parent(); | 92 | qDebug() << "STOP FIXTIMER ON CHANGE THREAD: id =" << fixerTimerId << ", thread =" << thread() << parent(); | ||
94 | #endif | 93 | #endif | ||
95 | killTimer(fixerTimerId); | 94 | killTimer(fixerTimerId); | ||
96 | fixerTimerId = 0; | 95 | fixerTimerId = 0; | ||
97 | emit needFix(); | 96 | emit needFix(); | ||
98 | } | 97 | } | ||
99 | 98 | | |||
100 | return QObject::event(event); | 99 | return QObject::event(event); | ||
101 | } | 100 | } | ||
102 | 101 | | |||
103 | void SafeTimer::Private::timerEvent(QTimerEvent *event) | 102 | void SafeTimer::Private::timerEvent(QTimerEvent *event) | ||
104 | { | 103 | { | ||
105 | if (event->timerId() == fixerTimerId) | 104 | if (event->timerId() == fixerTimerId) { | ||
106 | { | | |||
107 | #ifdef SAFETIMER_DEBUG | 105 | #ifdef SAFETIMER_DEBUG | ||
108 | qDebug() << "STOP FIXTIMER ON TIMEOUT: id =" << fixerTimerId << ", thread =" << thread() << parent(); | 106 | qDebug() << "STOP FIXTIMER ON TIMEOUT: id =" << fixerTimerId << ", thread =" << thread() << parent(); | ||
109 | #endif | 107 | #endif | ||
110 | killTimer(fixerTimerId); | 108 | killTimer(fixerTimerId); | ||
111 | fixerTimerId = 0; | 109 | fixerTimerId = 0; | ||
112 | 110 | | |||
113 | SafeTimer *safeTimer = qobject_cast<SafeTimer*>(parent()); | 111 | SafeTimer *safeTimer = qobject_cast<SafeTimer *>(parent()); | ||
114 | // Emulate timeout signal of not yet ressurected timer | 112 | // Emulate timeout signal of not yet ressurected timer | ||
115 | emit safeTimer->timeout(); | 113 | emit safeTimer->timeout(); | ||
116 | // Ressurect timer here if not a singleshot | 114 | // Ressurect timer here if not a singleshot | ||
117 | if (!isSingleShot) | 115 | if (!isSingleShot) { | ||
118 | safeTimer->start(); | 116 | safeTimer->start(); | ||
119 | else | 117 | } else { | ||
120 | isActive = false; | 118 | isActive = false; | ||
121 | } | 119 | } | ||
122 | else | 120 | } else { | ||
123 | { | | |||
124 | #ifdef SAFETIMER_DEBUG | 121 | #ifdef SAFETIMER_DEBUG | ||
125 | qDebug() << "BAD PRIVATE TIME EVENT: id =" << timerId << ", thread =" << thread() << this << ", badId =" << event->timerId() << parent(); | 122 | qDebug() << "BAD PRIVATE TIME EVENT: id =" << timerId << ", thread =" << thread() << this << ", badId =" << event->timerId() << parent(); | ||
126 | #endif | 123 | #endif | ||
127 | } | 124 | } | ||
128 | } | 125 | } | ||
129 | 126 | | |||
130 | SafeTimer::SafeTimer(QObject *parent) | 127 | SafeTimer::SafeTimer(QObject *parent) | ||
131 | : QObject() | 128 | : QObject() | ||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Line(s) | |||||
182 | 179 | | |||
183 | #ifdef SAFETIMER_DEBUG | 180 | #ifdef SAFETIMER_DEBUG | ||
184 | qDebug() << "START TIMER: id =" << d->timerId << ", thread =" << thread() << ", interval =" << d->interval << this; | 181 | qDebug() << "START TIMER: id =" << d->timerId << ", thread =" << thread() << ", interval =" << d->interval << this; | ||
185 | #endif | 182 | #endif | ||
186 | } | 183 | } | ||
187 | 184 | | |||
188 | void SafeTimer::stop() | 185 | void SafeTimer::stop() | ||
189 | { | 186 | { | ||
190 | if (d->timerId) | 187 | if (d->timerId) { | ||
191 | { | | |||
192 | QObject::killTimer(d->timerId); | 188 | QObject::killTimer(d->timerId); | ||
193 | #ifdef SAFETIMER_DEBUG | 189 | #ifdef SAFETIMER_DEBUG | ||
194 | qDebug() << "STOP TIMER: id =" << d->timerId << ", thread =" << thread() << this; | 190 | qDebug() << "STOP TIMER: id =" << d->timerId << ", thread =" << thread() << this; | ||
195 | #endif | 191 | #endif | ||
196 | d->timerId = 0; | 192 | d->timerId = 0; | ||
197 | } | 193 | } | ||
198 | 194 | | |||
199 | if (d->fixerTimerId) | 195 | if (d->fixerTimerId) { | ||
200 | { | | |||
201 | #ifdef SAFETIMER_DEBUG | 196 | #ifdef SAFETIMER_DEBUG | ||
202 | qDebug() << "STOP FIXER TIMER: id =" << d->fixerTimerId << ", thread =" << thread() << this; | 197 | qDebug() << "STOP FIXER TIMER: id =" << d->fixerTimerId << ", thread =" << thread() << this; | ||
203 | #endif | 198 | #endif | ||
204 | d->killTimer(d->fixerTimerId); | 199 | d->killTimer(d->fixerTimerId); | ||
205 | d->fixerTimerId = 0; | 200 | d->fixerTimerId = 0; | ||
206 | } | 201 | } | ||
207 | d->isActive = false; | 202 | d->isActive = false; | ||
208 | } | 203 | } | ||
209 | 204 | | |||
210 | bool SafeTimer::event(QEvent *event) | 205 | bool SafeTimer::event(QEvent *event) | ||
211 | { | 206 | { | ||
212 | if (event->type() == QEvent::ThreadChange && d->timerId /* timer is actived */) | 207 | if (event->type() == QEvent::ThreadChange && d->timerId /* timer is actived */) { | ||
213 | { | | |||
214 | // Timer dies when an object changes owner thread. This trick | 208 | // Timer dies when an object changes owner thread. This trick | ||
215 | // used to ressurect old timer in the new thread. | 209 | // used to ressurect old timer in the new thread. | ||
216 | // Signal is emited in the old thread but will be gotten in the new one. | 210 | // Signal is emited in the old thread but will be gotten in the new one. | ||
217 | #ifdef SAFETIMER_DEBUG | 211 | #ifdef SAFETIMER_DEBUG | ||
218 | qDebug() << "CHANGE THREAD: id =" << d->timerId << ", thread =" << thread() << this; | 212 | qDebug() << "CHANGE THREAD: id =" << d->timerId << ", thread =" << thread() << this; | ||
219 | #endif | 213 | #endif | ||
220 | killTimer(d->timerId); | 214 | killTimer(d->timerId); | ||
221 | d->timerId = 0; | 215 | d->timerId = 0; | ||
222 | emit d->needFix(); | 216 | emit d->needFix(); | ||
223 | } | 217 | } | ||
224 | 218 | | |||
225 | return QObject::event(event); | 219 | return QObject::event(event); | ||
226 | } | 220 | } | ||
227 | 221 | | |||
228 | void SafeTimer::timerEvent(QTimerEvent *event) | 222 | void SafeTimer::timerEvent(QTimerEvent *event) | ||
229 | { | 223 | { | ||
230 | if (event->timerId() == d->timerId) | 224 | if (event->timerId() == d->timerId) { | ||
231 | { | 225 | if (d->isSingleShot) { | ||
232 | if (d->isSingleShot) | | |||
233 | stop(); | 226 | stop(); | ||
234 | emit timeout(); | | |||
235 | } | 227 | } | ||
236 | else | 228 | emit timeout(); | ||
237 | { | 229 | } else { | ||
238 | #ifdef SAFETIMER_DEBUG | 230 | #ifdef SAFETIMER_DEBUG | ||
239 | qDebug() << "BAD TIME EVENT: id =" << d->timerId << ", thread =" << thread() << this << ", badId =" << event->timerId() << this; | 231 | qDebug() << "BAD TIME EVENT: id =" << d->timerId << ", thread =" << thread() << this << ", badId =" << event->timerId() << this; | ||
240 | #endif | 232 | #endif | ||
241 | } | 233 | } | ||
242 | } | 234 | } | ||
243 | 235 | | |||
244 | } // end namespace QCA | 236 | } // end namespace QCA | ||
245 | 237 | | |||
246 | #include "qca_safetimer.moc" | 238 | #include "qca_safetimer.moc" |