diff options
author | Robert Griebl <rgriebl@trolltech.com> | 2009-06-10 11:46:23 (GMT) |
---|---|---|
committer | Robert Griebl <rgriebl@trolltech.com> | 2009-06-10 11:46:23 (GMT) |
commit | 7604f8087f88171ef933d8ae08f501467e647338 (patch) | |
tree | 51d071f462ed48d0b25884d9f62b8ba11c5dff13 /src/corelib/thread | |
parent | 8c265860b41214daade7c8a28237c1e07ea71a3c (diff) | |
download | Qt-7604f8087f88171ef933d8ae08f501467e647338.zip Qt-7604f8087f88171ef933d8ae08f501467e647338.tar.gz Qt-7604f8087f88171ef933d8ae08f501467e647338.tar.bz2 |
Make Qt exception safer.
Squashed commit of the branch haralds-haralds-qt-s60-topics/topic/exceptions,
which also contains the full history.
Rev-By: Harald Fernengel
Rev-By: Ralf Engels
Diffstat (limited to 'src/corelib/thread')
-rw-r--r-- | src/corelib/thread/qthread.cpp | 15 | ||||
-rw-r--r-- | src/corelib/thread/qthread_unix.cpp | 9 | ||||
-rw-r--r-- | src/corelib/thread/qthread_win.cpp | 9 | ||||
-rw-r--r-- | src/corelib/thread/qthreadstorage.cpp | 14 |
4 files changed, 32 insertions, 15 deletions
diff --git a/src/corelib/thread/qthread.cpp b/src/corelib/thread/qthread.cpp index cb44a25..f1bdc64 100644 --- a/src/corelib/thread/qthread.cpp +++ b/src/corelib/thread/qthread.cpp @@ -163,9 +163,9 @@ QAdoptedThread::~QAdoptedThread() QThread *QAdoptedThread::createThreadForAdoption() { - QThread *t = new QAdoptedThread(0); - t->moveToThread(t); - return t; + QScopedPointer<QThread> t(new QAdoptedThread(0)); + t->moveToThread(t.data()); + return t.take(); } void QAdoptedThread::run() @@ -483,10 +483,10 @@ uint QThread::stackSize() const int QThread::exec() { Q_D(QThread); - d->mutex.lock(); + QMutexLocker locker(&d->mutex); d->data->quitNow = false; QEventLoop eventLoop; - d->mutex.unlock(); + locker.unlock(); int returnCode = eventLoop.exec(); return returnCode; } @@ -734,8 +734,9 @@ QThreadData* QThreadData::current() { static QThreadData *data = 0; // reinterpret_cast<QThreadData *>(pthread_getspecific(current_thread_data_key)); if (!data) { - data = new QThreadData; - data->thread = new QAdoptedThread(data); + QScopedPointer<QThreadData> newdata(new QThreadData); + newdata->thread = new QAdoptedThread(newdata.data()); + data = newdata.take(); data->deref(); } return data; diff --git a/src/corelib/thread/qthread_unix.cpp b/src/corelib/thread/qthread_unix.cpp index 567c936..d2a824a 100644 --- a/src/corelib/thread/qthread_unix.cpp +++ b/src/corelib/thread/qthread_unix.cpp @@ -124,7 +124,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) diff --git a/src/corelib/thread/qthread_win.cpp b/src/corelib/thread/qthread_win.cpp index 7094e3d..76e551e 100644 --- a/src/corelib/thread/qthread_win.cpp +++ b/src/corelib/thread/qthread_win.cpp @@ -112,7 +112,14 @@ QThreadData *QThreadData::current() // This needs to be called prior to new AdoptedThread() to // avoid recursion. TlsSetValue(qt_current_thread_data_tls_index, threadData); - threadData->thread = new QAdoptedThread(threadData); + QT_TRY { + threadData->thread = new QAdoptedThread(threadData); + } QT_CATCH(...) { + TlsSetValue(qt_current_thread_data_tls_index, 0); + threadData->deref(); + threadData = 0; + QT_RETHROW; + } threadData->deref(); } diff --git a/src/corelib/thread/qthreadstorage.cpp b/src/corelib/thread/qthreadstorage.cpp index 35c55c1..4657009 100644 --- a/src/corelib/thread/qthreadstorage.cpp +++ b/src/corelib/thread/qthreadstorage.cpp @@ -101,12 +101,14 @@ void **QThreadStorageData::get() const qWarning("QThreadStorage::get: QThreadStorage can only be used with threads started with QThread"); return 0; } - QMap<int, void *>::iterator it = data->tls.find(id); + QMap<int, void *>::const_iterator it = data->tls.constFind(id); DEBUG_MSG("QThreadStorageData: Returning storage %d, data %p, for thread %p", id, it != data->tls.end() ? it.value() : 0, data->thread); - return it != data->tls.end() && it.value() != 0 ? &it.value() : 0; + // const_cast below is a bit evil - but we have to make sure not to detach here + // otherwise we'll go bonkers in oom situations + return it != data->tls.constEnd() && it.value() != 0 ? const_cast<void **>(&it.value()) : 0; } void **QThreadStorageData::set(void *p) @@ -129,9 +131,9 @@ void **QThreadStorageData::set(void *p) void *q = it.value(); it.value() = 0; - mutex()->lock(); + QMutexLocker locker(mutex()); void (*destructor)(void *) = destructors()->value(id); - mutex()->unlock(); + locker.unlock(); destructor(q); } @@ -167,9 +169,9 @@ void QThreadStorageData::finish(void **p) continue; } - mutex()->lock(); + QMutexLocker locker(mutex()); void (*destructor)(void *) = destructors()->value(id); - mutex()->unlock(); + locker.unlock(); if (!destructor) { if (QThread::currentThread()) |