Changeset View
Changeset View
Standalone View
Standalone View
src/support/synchronizer.cpp
Show All 29 Lines | |||||
30 | #include <QWaitCondition> | 30 | #include <QWaitCondition> | ||
31 | 31 | | |||
32 | //#define TIMERFIXER_DEBUG | 32 | //#define TIMERFIXER_DEBUG | ||
33 | 33 | | |||
34 | #ifdef TIMERFIXER_DEBUG | 34 | #ifdef TIMERFIXER_DEBUG | ||
35 | #include <stdio.h> | 35 | #include <stdio.h> | ||
36 | #endif | 36 | #endif | ||
37 | 37 | | |||
38 | namespace QCA { | 38 | namespace QCA | ||
39 | { | ||||
39 | 40 | | |||
40 | //---------------------------------------------------------------------------- | 41 | //---------------------------------------------------------------------------- | ||
41 | // TimerFixer | 42 | // TimerFixer | ||
42 | //---------------------------------------------------------------------------- | 43 | //---------------------------------------------------------------------------- | ||
43 | class TimerFixer : public QObject | 44 | class TimerFixer : public QObject | ||
44 | { | 45 | { | ||
45 | Q_OBJECT | 46 | Q_OBJECT | ||
46 | public: | 47 | public: | ||
47 | struct TimerInfo | 48 | struct TimerInfo { | ||
48 | { | | |||
49 | int id; | 49 | int id; | ||
50 | int interval; | 50 | int interval; | ||
51 | QTime time; | 51 | QTime time; | ||
52 | bool fixInterval; | 52 | bool fixInterval; | ||
53 | 53 | | |||
54 | TimerInfo() : fixInterval(false) {} | 54 | TimerInfo() : fixInterval(false) {} | ||
55 | }; | 55 | }; | ||
56 | 56 | | |||
57 | TimerFixer *fixerParent; | 57 | TimerFixer *fixerParent; | ||
58 | QList<TimerFixer*> fixerChildren; | 58 | QList<TimerFixer *> fixerChildren; | ||
59 | 59 | | |||
60 | QObject *target; | 60 | QObject *target; | ||
61 | QAbstractEventDispatcher *ed; | 61 | QAbstractEventDispatcher *ed; | ||
62 | QList<TimerInfo> timers; | 62 | QList<TimerInfo> timers; | ||
63 | 63 | | |||
64 | static bool haveFixer(QObject *obj) | 64 | static bool haveFixer(QObject *obj) | ||
65 | { | 65 | { | ||
66 | return obj->findChild<TimerFixer *>() ? true : false; | 66 | return obj->findChild<TimerFixer *>() ? true : false; | ||
67 | } | 67 | } | ||
68 | 68 | | |||
69 | TimerFixer(QObject *_target, TimerFixer *_fp = 0) : QObject(_target) | 69 | TimerFixer(QObject *_target, TimerFixer *_fp = 0) : QObject(_target) | ||
70 | { | 70 | { | ||
71 | ed = 0; | 71 | ed = 0; | ||
72 | 72 | | |||
73 | target = _target; | 73 | target = _target; | ||
74 | fixerParent = _fp; | 74 | fixerParent = _fp; | ||
75 | if(fixerParent) | 75 | if (fixerParent) { | ||
76 | fixerParent->fixerChildren.append(this); | 76 | fixerParent->fixerChildren.append(this); | ||
77 | } | ||||
77 | 78 | | |||
78 | #ifdef TIMERFIXER_DEBUG | 79 | #ifdef TIMERFIXER_DEBUG | ||
79 | printf("TimerFixer[%p] pairing with %p (%s)\n", this, target, target->metaObject()->className()); | 80 | printf("TimerFixer[%p] pairing with %p (%s)\n", this, target, target->metaObject()->className()); | ||
80 | #endif | 81 | #endif | ||
81 | edlink(); | 82 | edlink(); | ||
82 | target->installEventFilter(this); | 83 | target->installEventFilter(this); | ||
83 | 84 | | |||
84 | QObjectList list = target->children(); | 85 | QObjectList list = target->children(); | ||
85 | for(int n = 0; n < list.count(); ++n) | 86 | for (int n = 0; n < list.count(); ++n) { | ||
86 | hook(list[n]); | 87 | hook(list[n]); | ||
87 | } | 88 | } | ||
89 | } | ||||
88 | 90 | | |||
89 | ~TimerFixer() | 91 | ~TimerFixer() | ||
90 | { | 92 | { | ||
91 | if(fixerParent) | 93 | if (fixerParent) { | ||
92 | fixerParent->fixerChildren.removeAll(this); | 94 | fixerParent->fixerChildren.removeAll(this); | ||
95 | } | ||||
93 | 96 | | |||
94 | QList<TimerFixer*> list = fixerChildren; | 97 | QList<TimerFixer *> list = fixerChildren; | ||
95 | for(int n = 0; n < list.count(); ++n) | 98 | for (int n = 0; n < list.count(); ++n) { | ||
96 | delete list[n]; | 99 | delete list[n]; | ||
100 | } | ||||
97 | list.clear(); | 101 | list.clear(); | ||
98 | 102 | | |||
99 | updateTimerList(); // do this just to trip debug output | 103 | updateTimerList(); // do this just to trip debug output | ||
100 | 104 | | |||
101 | target->removeEventFilter(this); | 105 | target->removeEventFilter(this); | ||
102 | edunlink(); | 106 | edunlink(); | ||
103 | #ifdef TIMERFIXER_DEBUG | 107 | #ifdef TIMERFIXER_DEBUG | ||
104 | printf("TimerFixer[%p] unpaired with %p (%s)\n", this, target, target->metaObject()->className()); | 108 | printf("TimerFixer[%p] unpaired with %p (%s)\n", this, target, target->metaObject()->className()); | ||
105 | #endif | 109 | #endif | ||
106 | } | 110 | } | ||
107 | 111 | | |||
108 | virtual bool event(QEvent *e) | 112 | virtual bool event(QEvent *e) | ||
109 | { | 113 | { | ||
110 | switch(e->type()) | 114 | switch (e->type()) { | ||
111 | { | | |||
112 | case QEvent::ThreadChange: // this happens second | 115 | case QEvent::ThreadChange: // this happens second | ||
113 | //printf("TimerFixer[%p] self changing threads\n", this); | 116 | //printf("TimerFixer[%p] self changing threads\n", this); | ||
114 | edunlink(); | 117 | edunlink(); | ||
115 | QMetaObject::invokeMethod(this, "fixTimers", Qt::QueuedConnection); | 118 | QMetaObject::invokeMethod(this, "fixTimers", Qt::QueuedConnection); | ||
116 | break; | 119 | break; | ||
117 | default: | 120 | default: | ||
118 | break; | 121 | break; | ||
119 | } | 122 | } | ||
120 | 123 | | |||
121 | return QObject::event(e); | 124 | return QObject::event(e); | ||
122 | } | 125 | } | ||
123 | 126 | | |||
124 | virtual bool eventFilter(QObject *, QEvent *e) | 127 | virtual bool eventFilter(QObject *, QEvent *e) | ||
125 | { | 128 | { | ||
126 | switch(e->type()) | 129 | switch (e->type()) { | ||
127 | { | | |||
128 | case QEvent::ChildAdded: | 130 | case QEvent::ChildAdded: | ||
129 | hook(((QChildEvent *)e)->child()); | 131 | hook(((QChildEvent *)e)->child()); | ||
130 | break; | 132 | break; | ||
131 | case QEvent::ChildRemoved: | 133 | case QEvent::ChildRemoved: | ||
132 | unhook(((QChildEvent *)e)->child()); | 134 | unhook(((QChildEvent *)e)->child()); | ||
133 | break; | 135 | break; | ||
134 | case QEvent::Timer: | 136 | case QEvent::Timer: | ||
135 | handleTimerEvent(((QTimerEvent *)e)->timerId()); | 137 | handleTimerEvent(((QTimerEvent *)e)->timerId()); | ||
Show All 16 Lines | 153 | { | |||
152 | ed = QAbstractEventDispatcher::instance(); | 154 | ed = QAbstractEventDispatcher::instance(); | ||
153 | //printf("TimerFixer[%p] linking to dispatcher %p\n", this, ed); | 155 | //printf("TimerFixer[%p] linking to dispatcher %p\n", this, ed); | ||
154 | connect(ed, SIGNAL(aboutToBlock()), SLOT(ed_aboutToBlock())); | 156 | connect(ed, SIGNAL(aboutToBlock()), SLOT(ed_aboutToBlock())); | ||
155 | } | 157 | } | ||
156 | 158 | | |||
157 | void edunlink() | 159 | void edunlink() | ||
158 | { | 160 | { | ||
159 | //printf("TimerFixer[%p] unlinking from dispatcher %p\n", this, ed); | 161 | //printf("TimerFixer[%p] unlinking from dispatcher %p\n", this, ed); | ||
160 | if(ed) | 162 | if (ed) { | ||
161 | { | | |||
162 | disconnect(ed, SIGNAL(aboutToBlock()), this, SLOT(ed_aboutToBlock())); | 163 | disconnect(ed, SIGNAL(aboutToBlock()), this, SLOT(ed_aboutToBlock())); | ||
163 | ed = 0; | 164 | ed = 0; | ||
164 | } | 165 | } | ||
165 | } | 166 | } | ||
166 | 167 | | |||
167 | void ed_aboutToBlock() | 168 | void ed_aboutToBlock() | ||
168 | { | 169 | { | ||
169 | //printf("TimerFixer[%p] aboutToBlock\n", this); | 170 | //printf("TimerFixer[%p] aboutToBlock\n", this); | ||
170 | updateTimerList(); | 171 | updateTimerList(); | ||
171 | } | 172 | } | ||
172 | 173 | | |||
173 | void fixTimers() | 174 | void fixTimers() | ||
174 | { | 175 | { | ||
175 | updateTimerList(); | 176 | updateTimerList(); | ||
176 | edlink(); | 177 | edlink(); | ||
177 | 178 | | |||
178 | for(int n = 0; n < timers.count(); ++n) | 179 | for (int n = 0; n < timers.count(); ++n) { | ||
179 | { | | |||
180 | TimerInfo &info = timers[n]; | 180 | TimerInfo &info = timers[n]; | ||
181 | 181 | | |||
182 | QThread *objectThread = target->thread(); | 182 | QThread *objectThread = target->thread(); | ||
183 | QAbstractEventDispatcher *ed = QAbstractEventDispatcher::instance(objectThread); | 183 | QAbstractEventDispatcher *ed = QAbstractEventDispatcher::instance(objectThread); | ||
184 | 184 | | |||
185 | int timeLeft = qMax(info.interval - info.time.elapsed(), 0); | 185 | int timeLeft = qMax(info.interval - info.time.elapsed(), 0); | ||
186 | info.fixInterval = true; | 186 | info.fixInterval = true; | ||
187 | ed->unregisterTimer(info.id); | 187 | ed->unregisterTimer(info.id); | ||
Show All 9 Lines | 196 | #endif | |||
197 | } | 197 | } | ||
198 | } | 198 | } | ||
199 | 199 | | |||
200 | private: | 200 | private: | ||
201 | void hook(QObject *obj) | 201 | void hook(QObject *obj) | ||
202 | { | 202 | { | ||
203 | // don't watch a fixer or any object that already has one | 203 | // don't watch a fixer or any object that already has one | ||
204 | // SafeTimer has own method to fix timers, skip it too | 204 | // SafeTimer has own method to fix timers, skip it too | ||
205 | if(obj == this || qobject_cast<TimerFixer *>(obj) || haveFixer(obj) || qobject_cast<SafeTimer*>(obj)) | 205 | if (obj == this || qobject_cast<TimerFixer *>(obj) || haveFixer(obj) || qobject_cast<SafeTimer *>(obj)) { | ||
206 | return; | 206 | return; | ||
207 | } | ||||
207 | 208 | | |||
208 | new TimerFixer(obj, this); | 209 | new TimerFixer(obj, this); | ||
209 | } | 210 | } | ||
210 | 211 | | |||
211 | void unhook(QObject *obj) | 212 | void unhook(QObject *obj) | ||
212 | { | 213 | { | ||
213 | TimerFixer *t = 0; | 214 | TimerFixer *t = 0; | ||
214 | for(int n = 0; n < fixerChildren.count(); ++n) | 215 | for (int n = 0; n < fixerChildren.count(); ++n) { | ||
215 | { | 216 | if (fixerChildren[n]->target == obj) { | ||
216 | if(fixerChildren[n]->target == obj) | | |||
217 | t = fixerChildren[n]; | 217 | t = fixerChildren[n]; | ||
218 | } | 218 | } | ||
219 | } | ||||
219 | delete t; | 220 | delete t; | ||
220 | } | 221 | } | ||
221 | 222 | | |||
222 | void handleTimerEvent(int id) | 223 | void handleTimerEvent(int id) | ||
223 | { | 224 | { | ||
224 | bool found = false; | 225 | bool found = false; | ||
225 | int n; | 226 | int n; | ||
226 | for(n = 0; n < timers.count(); ++n) | 227 | for (n = 0; n < timers.count(); ++n) { | ||
227 | { | 228 | if (timers[n].id == id) { | ||
228 | if(timers[n].id == id) | | |||
229 | { | | |||
230 | found = true; | 229 | found = true; | ||
231 | break; | 230 | break; | ||
232 | } | 231 | } | ||
233 | } | 232 | } | ||
234 | if(!found) | 233 | if (!found) { | ||
235 | { | | |||
236 | //printf("*** unrecognized timer [%d] activated ***\n", id); | 234 | //printf("*** unrecognized timer [%d] activated ***\n", id); | ||
237 | return; | 235 | return; | ||
238 | } | 236 | } | ||
239 | 237 | | |||
240 | TimerInfo &info = timers[n]; | 238 | TimerInfo &info = timers[n]; | ||
241 | #ifdef TIMERFIXER_DEBUG | 239 | #ifdef TIMERFIXER_DEBUG | ||
242 | printf("TimerFixer[%p] timer [%d] activated!\n", this, info.id); | 240 | printf("TimerFixer[%p] timer [%d] activated!\n", this, info.id); | ||
243 | #endif | 241 | #endif | ||
244 | 242 | | |||
245 | if(info.fixInterval) | 243 | if (info.fixInterval) { | ||
246 | { | | |||
247 | #ifdef TIMERFIXER_DEBUG | 244 | #ifdef TIMERFIXER_DEBUG | ||
248 | printf("restoring correct interval (%d)\n", info.interval); | 245 | printf("restoring correct interval (%d)\n", info.interval); | ||
249 | #endif | 246 | #endif | ||
250 | info.fixInterval = false; | 247 | info.fixInterval = false; | ||
251 | ed->unregisterTimer(info.id); | 248 | ed->unregisterTimer(info.id); | ||
252 | #if QT_VERSION >= 0x050000 | 249 | #if QT_VERSION >= 0x050000 | ||
253 | info.id = ed->registerTimer(info.interval, Qt::CoarseTimer, target); | 250 | info.id = ed->registerTimer(info.interval, Qt::CoarseTimer, target); | ||
254 | #else | 251 | #else | ||
255 | info.id = ed->registerTimer(info.interval, target); | 252 | info.id = ed->registerTimer(info.interval, target); | ||
256 | #endif | 253 | #endif | ||
257 | } | 254 | } | ||
258 | 255 | | |||
259 | info.time.start(); | 256 | info.time.start(); | ||
260 | } | 257 | } | ||
261 | 258 | | |||
262 | void updateTimerList() | 259 | void updateTimerList() | ||
263 | { | 260 | { | ||
264 | QList<QAbstractEventDispatcher::TimerInfo> edtimers; | 261 | QList<QAbstractEventDispatcher::TimerInfo> edtimers; | ||
265 | if(ed) | 262 | if (ed) { | ||
266 | edtimers = ed->registeredTimers(target); | 263 | edtimers = ed->registeredTimers(target); | ||
264 | } | ||||
267 | 265 | | |||
268 | // removed? | 266 | // removed? | ||
269 | for(int n = 0; n < timers.count(); ++n) | 267 | for (int n = 0; n < timers.count(); ++n) { | ||
270 | { | | |||
271 | bool found = false; | 268 | bool found = false; | ||
272 | int id = timers[n].id; | 269 | int id = timers[n].id; | ||
273 | for(int i = 0; i < edtimers.count(); ++i) | 270 | for (int i = 0; i < edtimers.count(); ++i) { | ||
274 | { | | |||
275 | #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) | 271 | #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) | ||
276 | if(edtimers[i].timerId == id) | 272 | if (edtimers[i].timerId == id) | ||
277 | #else | 273 | #else | ||
278 | if(edtimers[i].first == id) | 274 | if (edtimers[i].first == id) | ||
279 | #endif | 275 | #endif | ||
280 | { | 276 | { | ||
281 | found = true; | 277 | found = true; | ||
282 | break; | 278 | break; | ||
283 | } | 279 | } | ||
284 | } | 280 | } | ||
285 | 281 | | |||
286 | if(!found) | 282 | if (!found) { | ||
287 | { | | |||
288 | timers.removeAt(n); | 283 | timers.removeAt(n); | ||
289 | --n; | 284 | --n; | ||
290 | #ifdef TIMERFIXER_DEBUG | 285 | #ifdef TIMERFIXER_DEBUG | ||
291 | printf("TimerFixer[%p] timer [%d] removed\n", this, id); | 286 | printf("TimerFixer[%p] timer [%d] removed\n", this, id); | ||
292 | #endif | 287 | #endif | ||
293 | } | 288 | } | ||
294 | } | 289 | } | ||
295 | 290 | | |||
296 | // added? | 291 | // added? | ||
297 | for(int n = 0; n < edtimers.count(); ++n) | 292 | for (int n = 0; n < edtimers.count(); ++n) { | ||
298 | { | | |||
299 | #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) | 293 | #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) | ||
300 | int id = edtimers[n].timerId; | 294 | int id = edtimers[n].timerId; | ||
301 | #else | 295 | #else | ||
302 | int id = edtimers[n].first; | 296 | int id = edtimers[n].first; | ||
303 | #endif | 297 | #endif | ||
304 | bool found = false; | 298 | bool found = false; | ||
305 | for(int i = 0; i < timers.count(); ++i) | 299 | for (int i = 0; i < timers.count(); ++i) { | ||
306 | { | 300 | if (timers[i].id == id) { | ||
307 | if(timers[i].id == id) | | |||
308 | { | | |||
309 | found = true; | 301 | found = true; | ||
310 | break; | 302 | break; | ||
311 | } | 303 | } | ||
312 | } | 304 | } | ||
313 | 305 | | |||
314 | if(!found) | 306 | if (!found) { | ||
315 | { | | |||
316 | TimerInfo info; | 307 | TimerInfo info; | ||
317 | info.id = id; | 308 | info.id = id; | ||
318 | #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) | 309 | #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) | ||
319 | info.interval = edtimers[n].interval; | 310 | info.interval = edtimers[n].interval; | ||
320 | #else | 311 | #else | ||
321 | info.interval = edtimers[n].second; | 312 | info.interval = edtimers[n].second; | ||
322 | #endif | 313 | #endif | ||
323 | info.time.start(); | 314 | info.time.start(); | ||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Line(s) | 358 | Private(QObject *_obj, Synchronizer *_q) | |||
374 | , loop(0) | 365 | , loop(0) | ||
375 | , agent(0) | 366 | , agent(0) | ||
376 | , fixer(0) | 367 | , fixer(0) | ||
377 | , m(QMutex::NonRecursive) | 368 | , m(QMutex::NonRecursive) | ||
378 | , w() | 369 | , w() | ||
379 | , orig_thread(0) | 370 | , orig_thread(0) | ||
380 | { | 371 | { | ||
381 | // SafeTimer has own method to fix timers, skip it too | 372 | // SafeTimer has own method to fix timers, skip it too | ||
382 | if (!qobject_cast<SafeTimer*>(obj)) | 373 | if (!qobject_cast<SafeTimer *>(obj)) { | ||
383 | fixer = new TimerFixer(obj); | 374 | fixer = new TimerFixer(obj); | ||
384 | } | 375 | } | ||
376 | } | ||||
385 | 377 | | |||
386 | ~Private() | 378 | ~Private() | ||
387 | { | 379 | { | ||
388 | stop(); | 380 | stop(); | ||
389 | delete fixer; | 381 | delete fixer; | ||
390 | } | 382 | } | ||
391 | 383 | | |||
392 | void start() | 384 | void start() | ||
393 | { | 385 | { | ||
394 | if(active) | 386 | if (active) { | ||
395 | return; | 387 | return; | ||
388 | } | ||||
396 | 389 | | |||
397 | m.lock(); | 390 | m.lock(); | ||
398 | active = true; | 391 | active = true; | ||
399 | do_quit = false; | 392 | do_quit = false; | ||
400 | QThread::start(); | 393 | QThread::start(); | ||
401 | w.wait(&m); | 394 | w.wait(&m); | ||
402 | m.unlock(); | 395 | m.unlock(); | ||
403 | } | 396 | } | ||
404 | 397 | | |||
405 | void stop() | 398 | void stop() | ||
406 | { | 399 | { | ||
407 | if(!active) | 400 | if (!active) { | ||
408 | return; | 401 | return; | ||
402 | } | ||||
409 | 403 | | |||
410 | m.lock(); | 404 | m.lock(); | ||
411 | do_quit = true; | 405 | do_quit = true; | ||
412 | w.wakeOne(); | 406 | w.wakeOne(); | ||
413 | m.unlock(); | 407 | m.unlock(); | ||
414 | wait(); | 408 | wait(); | ||
415 | active = false; | 409 | active = false; | ||
416 | } | 410 | } | ||
417 | 411 | | |||
418 | bool waitForCondition(int msecs) | 412 | bool waitForCondition(int msecs) | ||
419 | { | 413 | { | ||
420 | unsigned long time = ULONG_MAX; | 414 | unsigned long time = ULONG_MAX; | ||
421 | if(msecs != -1) | 415 | if (msecs != -1) { | ||
422 | time = msecs; | 416 | time = msecs; | ||
417 | } | ||||
423 | 418 | | |||
424 | // move object to the worker thread | 419 | // move object to the worker thread | ||
425 | cond_met = false; | 420 | cond_met = false; | ||
426 | orig_thread = QThread::currentThread(); | 421 | orig_thread = QThread::currentThread(); | ||
427 | q->setParent(0); // don't follow the object | 422 | q->setParent(0); // don't follow the object | ||
428 | QObject *orig_parent = obj->parent(); | 423 | QObject *orig_parent = obj->parent(); | ||
429 | obj->setParent(0); // unparent the target or the move will fail | 424 | obj->setParent(0); // unparent the target or the move will fail | ||
430 | obj->moveToThread(this); | 425 | obj->moveToThread(this); | ||
431 | 426 | | |||
432 | // tell the worker thread to start, wait for completion | 427 | // tell the worker thread to start, wait for completion | ||
433 | m.lock(); | 428 | m.lock(); | ||
434 | w.wakeOne(); | 429 | w.wakeOne(); | ||
435 | if(!w.wait(&m, time)) | 430 | if (!w.wait(&m, time)) { | ||
436 | { | 431 | if (loop) { | ||
437 | if(loop) | | |||
438 | { | | |||
439 | // if we timed out, tell the worker to quit | 432 | // if we timed out, tell the worker to quit | ||
440 | QMetaObject::invokeMethod(loop, "quit"); | 433 | QMetaObject::invokeMethod(loop, "quit"); | ||
441 | w.wait(&m); | 434 | w.wait(&m); | ||
442 | } | 435 | } | ||
443 | } | 436 | } | ||
444 | 437 | | |||
445 | // at this point the worker is done. cleanup and return | 438 | // at this point the worker is done. cleanup and return | ||
446 | m.unlock(); | 439 | m.unlock(); | ||
447 | 440 | | |||
448 | // restore parents | 441 | // restore parents | ||
449 | obj->setParent(orig_parent); | 442 | obj->setParent(orig_parent); | ||
450 | q->setParent(obj); | 443 | q->setParent(obj); | ||
451 | 444 | | |||
452 | return cond_met; | 445 | return cond_met; | ||
453 | } | 446 | } | ||
454 | 447 | | |||
455 | void conditionMet() | 448 | void conditionMet() | ||
456 | { | 449 | { | ||
457 | if(!loop) | 450 | if (!loop) { | ||
458 | return; | 451 | return; | ||
452 | } | ||||
459 | loop->quit(); | 453 | loop->quit(); | ||
460 | cond_met = true; | 454 | cond_met = true; | ||
461 | } | 455 | } | ||
462 | 456 | | |||
463 | protected: | 457 | protected: | ||
464 | virtual void run() | 458 | virtual void run() | ||
465 | { | 459 | { | ||
466 | m.lock(); | 460 | m.lock(); | ||
467 | QEventLoop eventLoop; | 461 | QEventLoop eventLoop; | ||
468 | 462 | | |||
469 | while(1) | 463 | while (1) { | ||
470 | { | | |||
471 | // thread now sleeps, waiting for work | 464 | // thread now sleeps, waiting for work | ||
472 | w.wakeOne(); | 465 | w.wakeOne(); | ||
473 | w.wait(&m); | 466 | w.wait(&m); | ||
474 | if(do_quit) | 467 | if (do_quit) { | ||
475 | { | | |||
476 | m.unlock(); | 468 | m.unlock(); | ||
477 | break; | 469 | break; | ||
478 | } | 470 | } | ||
479 | 471 | | |||
480 | loop = &eventLoop; | 472 | loop = &eventLoop; | ||
481 | agent = new SynchronizerAgent; | 473 | agent = new SynchronizerAgent; | ||
482 | connect(agent, SIGNAL(started()), SLOT(agent_started()), Qt::DirectConnection); | 474 | connect(agent, SIGNAL(started()), SLOT(agent_started()), Qt::DirectConnection); | ||
483 | 475 | | |||
Show All 19 Lines | |||||
503 | private slots: | 495 | private slots: | ||
504 | void agent_started() | 496 | void agent_started() | ||
505 | { | 497 | { | ||
506 | m.unlock(); | 498 | m.unlock(); | ||
507 | } | 499 | } | ||
508 | }; | 500 | }; | ||
509 | 501 | | |||
510 | Synchronizer::Synchronizer(QObject *parent) | 502 | Synchronizer::Synchronizer(QObject *parent) | ||
511 | :QObject(parent) | 503 | : QObject(parent) | ||
512 | { | 504 | { | ||
513 | d = new Private(parent, this); | 505 | d = new Private(parent, this); | ||
514 | } | 506 | } | ||
515 | 507 | | |||
516 | Synchronizer::~Synchronizer() | 508 | Synchronizer::~Synchronizer() | ||
517 | { | 509 | { | ||
518 | delete d; | 510 | delete d; | ||
519 | } | 511 | } | ||
Show All 15 Lines |