summaryrefslogtreecommitdiffstats
path: root/src/corelib/thread
diff options
context:
space:
mode:
authorSami Lempinen <sami.lempinen@nokia.com>2011-04-28 08:10:57 (GMT)
committerSami Lempinen <sami.lempinen@nokia.com>2011-04-28 08:10:57 (GMT)
commit95de3f34d9dba4cd95f1f3d32b35c4a4d97e70d9 (patch)
tree7be35a9028b5c83b792190fb954127e9f558baf5 /src/corelib/thread
parent9d6530b9774de482b0b3a29720f7f756e986f5c7 (diff)
parent8e615d9b07f6146b5cb6b56c4cd2e32376a8b429 (diff)
downloadQt-95de3f34d9dba4cd95f1f3d32b35c4a4d97e70d9.zip
Qt-95de3f34d9dba4cd95f1f3d32b35c4a4d97e70d9.tar.gz
Qt-95de3f34d9dba4cd95f1f3d32b35c4a4d97e70d9.tar.bz2
Merge remote-tracking branch 'qt/4.8'
Diffstat (limited to 'src/corelib/thread')
-rw-r--r--src/corelib/thread/qmutexpool.cpp22
-rw-r--r--src/corelib/thread/qmutexpool_p.h10
-rw-r--r--src/corelib/thread/qthread.cpp9
-rw-r--r--src/corelib/thread/qthread_p.h3
-rw-r--r--src/corelib/thread/qthread_symbian.cpp10
-rw-r--r--src/corelib/thread/qthread_unix.cpp24
-rw-r--r--src/corelib/thread/qthread_win.cpp15
7 files changed, 72 insertions, 21 deletions
diff --git a/src/corelib/thread/qmutexpool.cpp b/src/corelib/thread/qmutexpool.cpp
index 13e29c3..144fa35 100644
--- a/src/corelib/thread/qmutexpool.cpp
+++ b/src/corelib/thread/qmutexpool.cpp
@@ -123,22 +123,20 @@ QMutexPool *QMutexPool::instance()
return globalMutexPool();
}
-/*!
+/*! \fn QMutexPool::get(void *address)
Returns a QMutex from the pool. QMutexPool uses the value \a address
to determine which mutex is returned from the pool.
*/
-QMutex *QMutexPool::get(const void *address)
-{
- Q_ASSERT_X(address != 0, "QMutexPool::get()", "'address' argument cannot be zero");
- int index = int((quintptr(address) >> (sizeof(address) >> 1)) % mutexes.count());
-
- if (!mutexes[index]) {
- // mutex not created, create one
- QMutex *newMutex = new QMutex(recursionMode);
- if (!mutexes[index].testAndSetOrdered(0, newMutex))
- delete newMutex;
- }
+/*! \internal
+ create the mutex for the given index
+ */
+QMutex *QMutexPool::createMutex(int index)
+{
+ // mutex not created, create one
+ QMutex *newMutex = new QMutex(recursionMode);
+ if (!mutexes[index].testAndSetOrdered(0, newMutex))
+ delete newMutex;
return mutexes[index];
}
diff --git a/src/corelib/thread/qmutexpool_p.h b/src/corelib/thread/qmutexpool_p.h
index 1a45ba9..b2cd210 100644
--- a/src/corelib/thread/qmutexpool_p.h
+++ b/src/corelib/thread/qmutexpool_p.h
@@ -67,11 +67,19 @@ public:
explicit QMutexPool(QMutex::RecursionMode recursionMode = QMutex::NonRecursive, int size = 131);
~QMutexPool();
- QMutex *get(const void *address);
+ inline QMutex *get(const void *address) {
+ int index = uint(quintptr(address)) % mutexes.count();
+ QMutex *m = mutexes[index];
+ if (m)
+ return m;
+ else
+ return createMutex(index);
+ }
static QMutexPool *instance();
static QMutex *globalInstanceGet(const void *address);
private:
+ QMutex *createMutex(int index);
QVarLengthArray<QAtomicPointer<QMutex>, 131> mutexes;
QMutex::RecursionMode recursionMode;
};
diff --git a/src/corelib/thread/qthread.cpp b/src/corelib/thread/qthread.cpp
index 326f494..aaa1e6d 100644
--- a/src/corelib/thread/qthread.cpp
+++ b/src/corelib/thread/qthread.cpp
@@ -77,8 +77,8 @@ QT_BEGIN_NAMESPACE
*/
QThreadData::QThreadData(int initialRefCount)
- : _ref(initialRefCount), thread(0),
- quitNow(false), loopLevel(0), eventDispatcher(0), canWait(true)
+ : _ref(initialRefCount), thread(0), threadId(0),
+ quitNow(false), loopLevel(0), eventDispatcher(0), canWait(true), isAdopted(false)
{
// fprintf(stderr, "QThreadData %p created\n", this);
}
@@ -149,9 +149,6 @@ QAdoptedThread::QAdoptedThread(QThreadData *data)
QAdoptedThread::~QAdoptedThread()
{
-#ifndef QT_NO_THREAD
- QThreadPrivate::finish(this);
-#endif
// fprintf(stderr, "~QAdoptedThread = %p\n", this);
}
@@ -409,7 +406,7 @@ QThread::~QThread()
wait();
locker.relock();
}
- if (d->running && !d->finished)
+ if (d->running && !d->finished && !d->data->isAdopted)
qWarning("QThread: Destroyed while thread is still running");
d->data->thread = 0;
diff --git a/src/corelib/thread/qthread_p.h b/src/corelib/thread/qthread_p.h
index 36e07c0..b43a456 100644
--- a/src/corelib/thread/qthread_p.h
+++ b/src/corelib/thread/qthread_p.h
@@ -146,6 +146,7 @@ public:
#else
static void finish(void *);
#endif
+
#endif // Q_OS_UNIX
#if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
@@ -201,6 +202,7 @@ public:
void deref();
QThread *thread;
+ Qt::HANDLE threadId;
bool quitNow;
int loopLevel;
QAbstractEventDispatcher *eventDispatcher;
@@ -208,6 +210,7 @@ public:
QPostEventList postEventList;
bool canWait;
QVector<void *> tls;
+ bool isAdopted;
# ifdef Q_OS_SYMBIAN
RThread symbian_thread_handle;
diff --git a/src/corelib/thread/qthread_symbian.cpp b/src/corelib/thread/qthread_symbian.cpp
index 436c105..665aadd 100644
--- a/src/corelib/thread/qthread_symbian.cpp
+++ b/src/corelib/thread/qthread_symbian.cpp
@@ -113,6 +113,8 @@ QThreadData *QThreadData::current()
}
data->deref();
}
+ data->isAdopted = true;
+ data->threadId = QThread::currentThreadId();
if (!QCoreApplicationPrivate::theMainThread)
QCoreApplicationPrivate::theMainThread = data->thread;
}
@@ -256,6 +258,13 @@ QCAddAdoptedThread* QCAddAdoptedThread::adoptedThreadAdder = 0;
void QCAdoptedThreadMonitor::RunL()
{
+ if (data->isAdopted) {
+ QThread *thread = data->thread;
+ Q_ASSERT(thread);
+ QThreadPrivate *thread_p = static_cast<QThreadPrivate *>(QObjectPrivate::get(thread));
+ Q_ASSERT(!thread_p->finished);
+ thread_p->finish(thread);
+ }
data->deref();
QCAddAdoptedThread::threadDied();
delete this;
@@ -312,6 +321,7 @@ void *QThreadPrivate::start(void *arg)
// attribute of the thread again once the app gains control in run()
User::SetCritical(User::EProcessCritical);
+ data->threadId = QThread::currentThreadId();
set_thread_data(data);
{
diff --git a/src/corelib/thread/qthread_unix.cpp b/src/corelib/thread/qthread_unix.cpp
index 835378a..044eb05 100644
--- a/src/corelib/thread/qthread_unix.cpp
+++ b/src/corelib/thread/qthread_unix.cpp
@@ -133,7 +133,16 @@ static void destroy_current_thread_data(void *p)
// this destructor function, so we need to set it back to the
// right value...
pthread_setspecific(current_thread_data_key, p);
- reinterpret_cast<QThreadData *>(p)->deref();
+ QThreadData *data = static_cast<QThreadData *>(p);
+ if (data->isAdopted) {
+ QThread *thread = data->thread;
+ Q_ASSERT(thread);
+ QThreadPrivate *thread_p = static_cast<QThreadPrivate *>(QObjectPrivate::get(thread));
+ Q_ASSERT(!thread_p->finished);
+ thread_p->finish(thread);
+ }
+ data->deref();
+
// ... but we must reset it to zero before returning so we aren't
// called again (POSIX allows implementations to call destructor
// functions repeatedly until all values are zero)
@@ -161,18 +170,28 @@ Q_DESTRUCTOR_FUNCTION(destroy_current_thread_data_key)
// Utility functions for getting, setting and clearing thread specific data.
static QThreadData *get_thread_data()
{
+#ifdef HAVE_TLS
+ return currentThreadData;
+#else
pthread_once(&current_thread_data_once, create_current_thread_data_key);
return reinterpret_cast<QThreadData *>(pthread_getspecific(current_thread_data_key));
+#endif
}
static void set_thread_data(QThreadData *data)
{
+#ifdef HAVE_TLS
+ currentThreadData = data;
+#endif
pthread_once(&current_thread_data_once, create_current_thread_data_key);
pthread_setspecific(current_thread_data_key, data);
}
static void clear_thread_data()
{
+#ifdef HAVE_TLS
+ currentThreadData = 0;
+#endif
pthread_setspecific(current_thread_data_key, 0);
}
@@ -202,6 +221,8 @@ QThreadData *QThreadData::current()
}
data->deref();
}
+ data->isAdopted = true;
+ data->threadId = (Qt::HANDLE)pthread_self();
if (!QCoreApplicationPrivate::theMainThread)
QCoreApplicationPrivate::theMainThread = data->thread;
}
@@ -259,6 +280,7 @@ void *QThreadPrivate::start(void *arg)
thr->setPriority(QThread::Priority(thr->d_func()->priority & ~ThreadPriorityResetFlag));
}
+ data->threadId = (Qt::HANDLE)pthread_self();
set_thread_data(data);
data->ref();
diff --git a/src/corelib/thread/qthread_win.cpp b/src/corelib/thread/qthread_win.cpp
index 6b7932b..bab6cf8 100644
--- a/src/corelib/thread/qthread_win.cpp
+++ b/src/corelib/thread/qthread_win.cpp
@@ -124,6 +124,8 @@ QThreadData *QThreadData::current()
}
threadData->deref();
}
+ threadData->isAdopted = true;
+ threadData->threadId = (Qt::HANDLE)GetCurrentThreadId();
if (!QCoreApplicationPrivate::theMainThread) {
QCoreApplicationPrivate::theMainThread = threadData->thread;
@@ -231,7 +233,17 @@ void qt_adopted_thread_watcher_function(void *)
} else {
// printf("(qt) - qt_adopted_thread_watcher_function... called\n");
const int qthreadIndex = handleIndex - 1;
- QThreadData::get2(qt_adopted_qthreads.at(qthreadIndex))->deref();
+
+ QThreadData *data = QThreadData::get2(qt_adopted_qthreads.at(qthreadIndex));
+ if (data->isAdopted) {
+ QThread *thread = data->thread;
+ Q_ASSERT(thread);
+ QThreadPrivate *thread_p = static_cast<QThreadPrivate *>(QObjectPrivate::get(thread));
+ Q_ASSERT(!thread_p->finished);
+ thread_p->finish(thread);
+ }
+ data->deref();
+
#if !defined(Q_OS_WINCE) || (defined(_WIN32_WCE) && (_WIN32_WCE>=0x600))
CloseHandle(qt_adopted_thread_handles.at(handleIndex));
#endif
@@ -295,6 +307,7 @@ unsigned int __stdcall QThreadPrivate::start(void *arg)
qt_create_tls();
TlsSetValue(qt_current_thread_data_tls_index, data);
+ data->threadId = (Qt::HANDLE)GetCurrentThreadId();
QThread::setTerminationEnabled(false);