summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMurray Read <ext-murray.2.read@nokia.com>2012-02-06 11:33:07 +0000
committerQt by Nokia <qt-info@nokia.com>2012-02-08 01:52:00 +0100
commit4490850b9286d9b01fab9bc8900deb1598398f2e (patch)
tree24868d540c24316f2c1d91d0bc9f797236aa6817
parentf343fc7c0844fc33886f3e25d49e043062c06e7a (diff)
Handling adopted thread death with active timers on Symbian
Timers are thread local objects on Symbian. When the adopted thread monitor tries to tidy up a thread, any existing qt timers will be cleaned up. But that has to be done very carefully without touching the underlying symbian timer objects. Checks are added to make sure the Symbian timers are only touched in the correct thread. Task-number: ou1cimx1#960478 Change-Id: If16750b455aa576aa9f2d93657a3627d18060272 Reviewed-by: Shane Kearns <ext-shane.2.kearns@nokia.com>
-rw-r--r--src/corelib/kernel/qeventdispatcher_symbian.cpp28
-rw-r--r--src/corelib/kernel/qeventdispatcher_symbian_p.h1
-rw-r--r--tests/auto/qthread/tst_qthread.cpp27
3 files changed, 50 insertions, 6 deletions
diff --git a/src/corelib/kernel/qeventdispatcher_symbian.cpp b/src/corelib/kernel/qeventdispatcher_symbian.cpp
index 637408e8d3..4930a167e9 100644
--- a/src/corelib/kernel/qeventdispatcher_symbian.cpp
+++ b/src/corelib/kernel/qeventdispatcher_symbian.cpp
@@ -157,6 +157,7 @@ private:
QActiveObject::QActiveObject(TInt priority, QEventDispatcherSymbian *dispatcher)
: CActive(priority),
m_dispatcher(dispatcher),
+ m_threadData(QThreadData::current()),
m_hasAlreadyRun(false),
m_hasRunAgain(false),
m_iterationCount(1)
@@ -258,18 +259,32 @@ QTimerActiveObject::QTimerActiveObject(QEventDispatcherSymbian *dispatcher, Symb
QTimerActiveObject::~QTimerActiveObject()
{
Cancel();
- m_rTimer.Close(); //close of null handle is safe
+ // deletion in the wrong thread (eg adoptedThreadMonitor thread) must avoid using the RTimer, which is local
+ // to the thread it was created in.
+ if (QThreadData::current() == m_threadData)
+ m_rTimer.Close(); //close of null handle is safe
}
void QTimerActiveObject::DoCancel()
{
- if (m_timerInfo->interval > 0) {
- m_rTimer.Cancel();
+ // RTimer is thread local and cannot be cancelled outside of the thread it was created in
+ if (QThreadData::current() == m_threadData) {
+ if (m_timerInfo->interval > 0) {
+ m_rTimer.Cancel();
+ } else {
+ if (iStatus.Int() == KRequestPending) {
+ TRequestStatus *status = &iStatus;
+ QEventDispatcherSymbian::RequestComplete(status, KErrNone);
+ }
+ }
} else {
- if (iStatus.Int() == KRequestPending) {
- TRequestStatus *status = &iStatus;
- QEventDispatcherSymbian::RequestComplete(status, KErrNone);
+ // Cancel requires a signal to continue, we're in the wrong thread to use the RTimer
+ if (m_threadData->symbian_thread_handle.ExitType() == EExitPending) {
+ // owner thread is still running, it will receive a stray event if the timer fires now.
+ qFatal("QTimerActiveObject cancelled from wrong thread");
}
+ TRequestStatus *status = &iStatus;
+ User::RequestComplete(status, KErrCancel);
}
}
@@ -358,6 +373,7 @@ void QTimerActiveObject::Start()
if (m_timerInfo->interval > 0) {
if (!m_rTimer.Handle()) {
qt_symbian_throwIfError(m_rTimer.CreateLocal());
+ m_threadData = QThreadData::current();
}
m_timeoutTimer.start();
m_expectedTimeSinceLastEvent = 0;
diff --git a/src/corelib/kernel/qeventdispatcher_symbian_p.h b/src/corelib/kernel/qeventdispatcher_symbian_p.h
index 4173862d7d..c520d12b3d 100644
--- a/src/corelib/kernel/qeventdispatcher_symbian_p.h
+++ b/src/corelib/kernel/qeventdispatcher_symbian_p.h
@@ -88,6 +88,7 @@ public:
void reactivateAndComplete();
protected:
QEventDispatcherSymbian *m_dispatcher;
+ QThreadData *m_threadData;
private:
bool m_hasAlreadyRun : 1;
diff --git a/tests/auto/qthread/tst_qthread.cpp b/tests/auto/qthread/tst_qthread.cpp
index c1b2a95ef6..4a8baa0a98 100644
--- a/tests/auto/qthread/tst_qthread.cpp
+++ b/tests/auto/qthread/tst_qthread.cpp
@@ -102,6 +102,7 @@ private slots:
void adoptedThreadSetPriority();
void adoptedThreadExit();
void adoptedThreadExec();
+ void adoptThreadExitWithActiveTimer();
void adoptedThreadFinished();
void adoptedThreadExecFinished();
void adoptMultipleThreads();
@@ -1010,6 +1011,32 @@ void tst_QThread::adoptMultipleThreadsOverlap()
QVERIFY(!QTestEventLoop::instance().timeout());
QCOMPARE(int(recorder.activationCount), numThreads);
}
+
+void adoptedThreadActiveTimerFunction(void *)
+{
+ QThread * const adoptedThread = QThread::currentThread();
+ QEventLoop eventLoop(adoptedThread);
+
+ const int code = 1;
+ Exit_Object o;
+ o.thread = adoptedThread;
+ o.code = code;
+ QTimer::singleShot(100, &o, SLOT(slot()));
+
+ // create a timer that will still be active when the thread finishes
+ QTimer::singleShot(3000, &o, SLOT(slot()));
+
+ const int result = eventLoop.exec();
+ QCOMPARE(result, code);
+}
+
+void tst_QThread::adoptThreadExitWithActiveTimer()
+{
+ NativeThreadWrapper nativeThread;
+ nativeThread.start(adoptedThreadActiveTimerFunction);
+ nativeThread.join();
+}
+
void tst_QThread::stressTest()
{
#if defined(Q_OS_WINCE)