166 }; |
166 }; |
167 |
167 |
168 class Exit_Thread : public Simple_Thread |
168 class Exit_Thread : public Simple_Thread |
169 { |
169 { |
170 public: |
170 public: |
|
171 Exit_Object *object; |
171 int code; |
172 int code; |
172 int result; |
173 int result; |
173 |
174 |
174 void run() |
175 void run() |
175 { |
176 { |
176 Simple_Thread::run(); |
177 Simple_Thread::run(); |
177 Exit_Object o; |
178 if (object) { |
178 o.thread = this; |
179 object->thread = this; |
179 o.code = code; |
180 object->code = code; |
180 QTimer::singleShot(100, &o, SLOT(slot())); |
181 QTimer::singleShot(100, object, SLOT(slot())); |
|
182 } |
181 result = exec(); |
183 result = exec(); |
182 } |
184 } |
183 }; |
185 }; |
184 |
186 |
185 class Terminate_Thread : public Simple_Thread |
187 class Terminate_Thread : public Simple_Thread |
209 }; |
211 }; |
210 |
212 |
211 class Quit_Thread : public Simple_Thread |
213 class Quit_Thread : public Simple_Thread |
212 { |
214 { |
213 public: |
215 public: |
|
216 Quit_Object *object; |
214 int result; |
217 int result; |
215 |
218 |
216 void run() |
219 void run() |
217 { |
220 { |
218 { |
221 Simple_Thread::run(); |
219 QMutexLocker locker(&mutex); |
222 if (object) { |
220 cond.wakeOne(); |
223 object->thread = this; |
|
224 QTimer::singleShot(100, object, SLOT(slot())); |
221 } |
225 } |
222 Quit_Object o; |
|
223 o.thread = this; |
|
224 QTimer::singleShot(100, &o, SLOT(slot())); |
|
225 result = exec(); |
226 result = exec(); |
226 } |
227 } |
227 }; |
228 }; |
228 |
229 |
229 class Sleep_Thread : public Simple_Thread |
230 class Sleep_Thread : public Simple_Thread |
431 thread.cond.wait(locker.mutex()); |
434 thread.cond.wait(locker.mutex()); |
432 QVERIFY(thread.wait(five_minutes)); |
435 QVERIFY(thread.wait(five_minutes)); |
433 QVERIFY(thread.isFinished()); |
436 QVERIFY(thread.isFinished()); |
434 QVERIFY(!thread.isRunning()); |
437 QVERIFY(!thread.isRunning()); |
435 QCOMPARE(thread.result, thread.code); |
438 QCOMPARE(thread.result, thread.code); |
|
439 delete thread.object; |
|
440 |
|
441 Exit_Thread thread2; |
|
442 thread2.object = 0; |
|
443 thread2.code = 53; |
|
444 thread2.result = 0; |
|
445 QMutexLocker locker2(&thread2.mutex); |
|
446 thread2.start(); |
|
447 thread2.exit(thread2.code); |
|
448 thread2.cond.wait(locker2.mutex()); |
|
449 QVERIFY(thread2.wait(five_minutes)); |
|
450 QCOMPARE(thread2.result, thread2.code); |
436 } |
451 } |
437 |
452 |
438 void tst_QThread::start() |
453 void tst_QThread::start() |
439 { |
454 { |
440 QThread::Priority priorities[] = { |
455 QThread::Priority priorities[] = { |
441 QThread::IdlePriority, |
456 QThread::IdlePriority, |
442 QThread::LowestPriority, |
457 QThread::LowestPriority, |
443 QThread::LowPriority, |
458 QThread::LowPriority, |
444 QThread::NormalPriority, |
459 QThread::NormalPriority, |
445 QThread::HighPriority, |
460 QThread::HighPriority, |
446 QThread::HighestPriority, |
461 QThread::HighestPriority, |
447 QThread::TimeCriticalPriority, |
462 QThread::TimeCriticalPriority, |
448 QThread::InheritPriority |
463 QThread::InheritPriority |
449 }; |
464 }; |
450 const int prio_count = sizeof(priorities) / sizeof(QThread::Priority); |
465 const int prio_count = sizeof(priorities) / sizeof(QThread::Priority); |
451 |
466 |
452 for (int i = 0; i < prio_count; ++i) { |
467 for (int i = 0; i < prio_count; ++i) { |
453 Simple_Thread thread; |
468 Simple_Thread thread; |
478 } |
493 } |
479 |
494 |
480 void tst_QThread::quit() |
495 void tst_QThread::quit() |
481 { |
496 { |
482 Quit_Thread thread; |
497 Quit_Thread thread; |
|
498 thread.object = new Quit_Object; |
|
499 thread.object->moveToThread(&thread); |
|
500 thread.result = -1; |
483 QVERIFY(!thread.isFinished()); |
501 QVERIFY(!thread.isFinished()); |
484 QVERIFY(!thread.isRunning()); |
502 QVERIFY(!thread.isRunning()); |
485 QMutexLocker locker(&thread.mutex); |
503 QMutexLocker locker(&thread.mutex); |
486 thread.start(); |
504 thread.start(); |
487 QVERIFY(thread.isRunning()); |
505 QVERIFY(thread.isRunning()); |
489 thread.cond.wait(locker.mutex()); |
507 thread.cond.wait(locker.mutex()); |
490 QVERIFY(thread.wait(five_minutes)); |
508 QVERIFY(thread.wait(five_minutes)); |
491 QVERIFY(thread.isFinished()); |
509 QVERIFY(thread.isFinished()); |
492 QVERIFY(!thread.isRunning()); |
510 QVERIFY(!thread.isRunning()); |
493 QCOMPARE(thread.result, 0); |
511 QCOMPARE(thread.result, 0); |
|
512 delete thread.object; |
|
513 |
|
514 Quit_Thread thread2; |
|
515 thread2.object = 0; |
|
516 thread2.result = -1; |
|
517 QMutexLocker locker2(&thread2.mutex); |
|
518 thread2.start(); |
|
519 thread2.quit(); |
|
520 thread2.cond.wait(locker2.mutex()); |
|
521 QVERIFY(thread2.wait(five_minutes)); |
|
522 QCOMPARE(thread2.result, 0); |
494 } |
523 } |
495 |
524 |
496 void tst_QThread::wait() |
525 void tst_QThread::wait() |
497 { |
526 { |
498 DEPENDS_ON("isRunning"); |
527 DEPENDS_ON("isRunning"); |
665 this->data = data; |
694 this->data = data; |
666 #ifdef Q_OS_UNIX |
695 #ifdef Q_OS_UNIX |
667 const int state = pthread_create(&nativeThreadHandle, 0, NativeThreadWrapper::runUnix, this); |
696 const int state = pthread_create(&nativeThreadHandle, 0, NativeThreadWrapper::runUnix, this); |
668 Q_UNUSED(state); |
697 Q_UNUSED(state); |
669 #elif defined(Q_OS_WINCE) |
698 #elif defined(Q_OS_WINCE) |
670 nativeThreadHandle = CreateThread(NULL, 0 , (LPTHREAD_START_ROUTINE)NativeThreadWrapper::runWin , this, 0, NULL); |
699 nativeThreadHandle = CreateThread(NULL, 0 , (LPTHREAD_START_ROUTINE)NativeThreadWrapper::runWin , this, 0, NULL); |
671 #elif defined Q_OS_WIN |
700 #elif defined Q_OS_WIN |
672 unsigned thrdid = 0; |
701 unsigned thrdid = 0; |
673 nativeThreadHandle = (Qt::HANDLE) _beginthreadex(NULL, 0, NativeThreadWrapper::runWin, this, 0, &thrdid); |
702 nativeThreadHandle = (Qt::HANDLE) _beginthreadex(NULL, 0, NativeThreadWrapper::runWin, this, 0, &thrdid); |
674 #endif |
703 #endif |
675 } |
704 } |