diff options
author | David Boddie <dboddie@trolltech.com> | 2009-08-26 12:35:45 (GMT) |
---|---|---|
committer | David Boddie <dboddie@trolltech.com> | 2009-08-26 12:35:45 (GMT) |
commit | 7e30c92186878beb300a13093c3668ae8a10f2be (patch) | |
tree | 313aa7f7a8345e65b7803bae1cb68ad866bdadbc /src/corelib/thread/qthread_unix.cpp | |
parent | b98042831f4f6f6c5844965731b2dd64f27c076e (diff) | |
parent | aeb4251c76560a070ce22320be00e860950b4668 (diff) | |
download | Qt-7e30c92186878beb300a13093c3668ae8a10f2be.zip Qt-7e30c92186878beb300a13093c3668ae8a10f2be.tar.gz Qt-7e30c92186878beb300a13093c3668ae8a10f2be.tar.bz2 |
Merge branch 'master' of git@scm.dev.nokia.troll.no:qt/qt
Diffstat (limited to 'src/corelib/thread/qthread_unix.cpp')
-rw-r--r-- | src/corelib/thread/qthread_unix.cpp | 120 |
1 files changed, 115 insertions, 5 deletions
diff --git a/src/corelib/thread/qthread_unix.cpp b/src/corelib/thread/qthread_unix.cpp index 6308f2c..30914f4 100644 --- a/src/corelib/thread/qthread_unix.cpp +++ b/src/corelib/thread/qthread_unix.cpp @@ -47,7 +47,12 @@ #if !defined(QT_NO_GLIB) # include "../kernel/qeventdispatcher_glib_p.h" #endif + +#ifdef Q_OS_SYMBIAN +#include <private/qeventdispatcher_symbian_p.h> +#else #include <private/qeventdispatcher_unix_p.h> +#endif #include "qthreadstorage.h" @@ -138,7 +143,14 @@ QThreadData *QThreadData::current() } else { data = new QThreadData; pthread_setspecific(current_thread_data_key, data); - data->thread = new QAdoptedThread(data); + QT_TRY { + data->thread = new QAdoptedThread(data); + } QT_CATCH(...) { + pthread_setspecific(current_thread_data_key, 0); + data->deref(); + data = 0; + QT_RETHROW; + } data->deref(); } if (!QCoreApplicationPrivate::theMainThread) @@ -150,7 +162,13 @@ QThreadData *QThreadData::current() void QAdoptedThread::init() { - d_func()->thread_id = pthread_self(); + Q_D(QThread); + d->thread_id = pthread_self(); +#ifdef Q_OS_SYMBIAN + d->data->symbian_thread_handle = RThread(); + TThreadId threadId = d->data->symbian_thread_handle.Id(); + d->data->symbian_thread_handle.Open(threadId); +#endif } /* @@ -178,7 +196,11 @@ void QThreadPrivate::createEventDispatcher(QThreadData *data) data->eventDispatcher = new QEventDispatcherGlib; else #endif +#ifdef Q_OS_SYMBIAN + data->eventDispatcher = new QEventDispatcherSymbian; +#else data->eventDispatcher = new QEventDispatcherUNIX; +#endif data->eventDispatcher->startingUp(); } @@ -186,12 +208,25 @@ void QThreadPrivate::createEventDispatcher(QThreadData *data) void *QThreadPrivate::start(void *arg) { + // Symbian Open C supports neither thread cancellation nor cleanup_push. +#ifndef Q_OS_SYMBIAN pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); pthread_cleanup_push(QThreadPrivate::finish, arg); +#endif QThread *thr = reinterpret_cast<QThread *>(arg); QThreadData *data = QThreadData::get2(thr); +#ifdef Q_OS_SYMBIAN + // Because Symbian Open C does not provide a way to convert between + // RThread and pthread_t, we must delay initialization of the RThread + // handle when creating a thread, until we are running in the new thread. + // Here, we pick up the current thread and assign that to the handle. + data->symbian_thread_handle = RThread(); + TThreadId threadId = data->symbian_thread_handle.Id(); + data->symbian_thread_handle.Open(threadId); +#endif + pthread_once(¤t_thread_data_once, create_current_thread_data_key); pthread_setspecific(current_thread_data_key, data); @@ -202,19 +237,33 @@ void *QThreadPrivate::start(void *arg) createEventDispatcher(data); emit thr->started(); +#ifndef Q_OS_SYMBIAN pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); pthread_testcancel(); +#endif thr->run(); +#ifdef Q_OS_SYMBIAN + QThreadPrivate::finish(arg); +#else pthread_cleanup_pop(1); +#endif + return 0; } +#ifdef Q_OS_SYMBIAN +void QThreadPrivate::finish(void *arg, bool lockAnyway, bool closeNativeHandle) +#else void QThreadPrivate::finish(void *arg) +#endif { QThread *thr = reinterpret_cast<QThread *>(arg); QThreadPrivate *d = thr->d_func(); - QMutexLocker locker(&d->mutex); +#ifdef Q_OS_SYMBIAN + if (lockAnyway) +#endif + d->mutex.lock(); d->priority = QThread::InheritPriority; d->running = false; @@ -235,7 +284,15 @@ void QThreadPrivate::finish(void *arg) QThreadStorageData::finish((void **)data); d->thread_id = 0; +#ifdef Q_OS_SYMBIAN + if (closeNativeHandle) + d->data->symbian_thread_handle.Close(); +#endif d->thread_done.wakeAll(); +#ifdef Q_OS_SYMBIAN + if (lockAnyway) +#endif + d->mutex.unlock(); } @@ -288,6 +345,9 @@ int QThread::idealThreadCount() #elif defined(Q_OS_INTEGRITY) // as of aug 2008 Integrity only supports one single core CPU cores = 1; +#elif defined(Q_OS_SYMBIAN) + // ### TODO - Get the number of cores from HAL? when multicore architectures (SMP) are supported + cores = 1; #elif defined(Q_OS_VXWORKS) // VxWorks # if defined(QT_VXWORKS_HAS_CPUSET) @@ -389,7 +449,8 @@ void QThread::start(Priority priority) d->priority = priority; -#if defined(Q_OS_DARWIN) || !defined(Q_OS_OPENBSD) && defined(_POSIX_THREAD_PRIORITY_SCHEDULING) && (_POSIX_THREAD_PRIORITY_SCHEDULING-0 >= 0) +#if defined(Q_OS_DARWIN) || !defined(Q_OS_OPENBSD) && !defined(Q_OS_SYMBIAN) && defined(_POSIX_THREAD_PRIORITY_SCHEDULING) && (_POSIX_THREAD_PRIORITY_SCHEDULING-0 >= 0) +// ### Need to implement thread sheduling and priorities for symbian os. Implementation removed for now switch (priority) { case InheritPriority: { @@ -447,6 +508,13 @@ void QThread::start(Priority priority) } #endif // _POSIX_THREAD_PRIORITY_SCHEDULING +#ifdef Q_OS_SYMBIAN + if (d->stackSize == 0) + // The default stack size on Symbian is very small, making even basic + // operations like file I/O fail, so we increase it by default. + d->stackSize = 0x14000; // Maximum stack size on Symbian. +#endif + if (d->stackSize > 0) { #if defined(_POSIX_THREAD_ATTR_STACKSIZE) && (_POSIX_THREAD_ATTR_STACKSIZE-0 > 0) int code = pthread_attr_setstacksize(&attr, d->stackSize); @@ -471,7 +539,9 @@ void QThread::start(Priority priority) if (code == EPERM) { // caller does not have permission to set the scheduling // parameters/policy +#ifndef Q_OS_SYMBIAN pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED); +#endif code = pthread_create(&d->thread_id, &attr, QThreadPrivate::start, this); } @@ -484,6 +554,9 @@ void QThread::start(Priority priority) d->running = false; d->finished = false; d->thread_id = 0; +#ifdef Q_OS_SYMBIAN + d->data->symbian_thread_handle.Close(); +#endif } } @@ -495,6 +568,7 @@ void QThread::terminate() if (!d->thread_id) return; +#ifndef Q_OS_SYMBIAN int code = pthread_cancel(d->thread_id); if (code) { qWarning("QThread::start: Thread termination error: %s", @@ -502,6 +576,26 @@ void QThread::terminate() } else { d->terminated = true; } +#else + if (!d->running) + return; + if (!d->terminationEnabled) { + d->terminatePending = true; + return; + } + + d->terminated = true; + // "false, false" meaning: + // 1. lockAnyway = false. Don't lock the mutex because it's already locked + // (see above). + // 2. closeNativeSymbianHandle = false. We don't want to close the thread handle, + // because we need it here to terminate the thread. + QThreadPrivate::finish(this, false, false); + d->data->symbian_thread_handle.Terminate(KErrNone); + d->data->symbian_thread_handle.Close(); +#endif + + } bool QThread::wait(unsigned long time) @@ -526,11 +620,27 @@ bool QThread::wait(unsigned long time) void QThread::setTerminationEnabled(bool enabled) { - Q_ASSERT_X(currentThread() != 0, "QThread::setTerminationEnabled()", + QThread *thr = currentThread(); + Q_ASSERT_X(thr != 0, "QThread::setTerminationEnabled()", "Current thread was not started with QThread."); +#ifndef Q_OS_SYMBIAN pthread_setcancelstate(enabled ? PTHREAD_CANCEL_ENABLE : PTHREAD_CANCEL_DISABLE, NULL); if (enabled) pthread_testcancel(); +#else + QThreadPrivate *d = thr->d_func(); + QMutexLocker locker(&d->mutex); + d->terminationEnabled = enabled; + if (enabled && d->terminatePending) { + d->terminated = true; + // "false" meaning: + // - lockAnyway = false. Don't lock the mutex because it's already locked + // (see above). + QThreadPrivate::finish(thr, false); + locker.unlock(); // don't leave the mutex locked! + pthread_exit(NULL); + } +#endif } void QThread::setPriority(Priority priority) |