From 6476ac738ca029af95932f53b53f0705808eb80e Mon Sep 17 00:00:00 2001 From: "Bradley T. Hughes" Date: Thu, 29 Sep 2011 11:50:08 +0200 Subject: Replace implicit QAtomic* casts with explicit load()/store() Change-Id: Ia7ef1a8e01001f203e409c710c977d6f4686342e Reviewed-by: Thiago Macieira --- src/corelib/concurrent/qfutureinterface.cpp | 2 +- src/corelib/concurrent/qfuturewatcher.cpp | 2 +- src/corelib/concurrent/qtconcurrentiteratekernel.h | 8 ++++---- .../concurrent/qtconcurrentthreadengine.cpp | 10 ++++----- src/corelib/io/qprocess_p.h | 2 +- src/corelib/io/qurl.cpp | 2 +- src/corelib/kernel/qabstractitemmodel.cpp | 2 +- src/corelib/kernel/qobject.cpp | 12 +++++------ src/corelib/kernel/qvariant.cpp | 2 +- src/corelib/kernel/qvariant.h | 2 +- src/corelib/thread/qatomic.h | 2 +- src/corelib/thread/qmutex.cpp | 24 +++++++++++----------- src/corelib/thread/qmutex_p.h | 10 ++++----- src/corelib/thread/qmutexpool.cpp | 12 +++++------ src/corelib/thread/qmutexpool_p.h | 2 +- src/corelib/thread/qthread.cpp | 4 ++-- src/corelib/tools/qregexp.cpp | 2 +- src/corelib/tools/qsharedpointer.cpp | 11 +++++----- 18 files changed, 55 insertions(+), 56 deletions(-) (limited to 'src/corelib') diff --git a/src/corelib/concurrent/qfutureinterface.cpp b/src/corelib/concurrent/qfutureinterface.cpp index 74c3af0ce0..f54b335403 100644 --- a/src/corelib/concurrent/qfutureinterface.cpp +++ b/src/corelib/concurrent/qfutureinterface.cpp @@ -416,7 +416,7 @@ QFutureInterfaceBase &QFutureInterfaceBase::operator=(const QFutureInterfaceBase bool QFutureInterfaceBase::referenceCountIsOne() const { - return d->refCount == 1; + return d->refCount.load() == 1; } QFutureInterfaceBasePrivate::QFutureInterfaceBasePrivate(QFutureInterfaceBase::State initialState) diff --git a/src/corelib/concurrent/qfuturewatcher.cpp b/src/corelib/concurrent/qfuturewatcher.cpp index 5c5d65dc71..c9a16a8bbf 100644 --- a/src/corelib/concurrent/qfuturewatcher.cpp +++ b/src/corelib/concurrent/qfuturewatcher.cpp @@ -464,7 +464,7 @@ void QFutureWatcherBasePrivate::sendCallOutEvent(QFutureCallOutEvent *event) emit q->resultsReadyAt(beginIndex, endIndex); - if (int(resultAtConnected) <= 0) + if (resultAtConnected.load() <= 0) break; for (int i = beginIndex; i < endIndex; ++i) diff --git a/src/corelib/concurrent/qtconcurrentiteratekernel.h b/src/corelib/concurrent/qtconcurrentiteratekernel.h index c6fcb973ab..49c053caf7 100644 --- a/src/corelib/concurrent/qtconcurrentiteratekernel.h +++ b/src/corelib/concurrent/qtconcurrentiteratekernel.h @@ -214,9 +214,9 @@ public: bool shouldStartThread() { if (forIteration) - return (currentIndex < iterationCount) && !this->shouldThrottleThread(); + return (currentIndex.load() < iterationCount) && !this->shouldThrottleThread(); else // whileIteration - return (iteratorThreads == 0); + return (iteratorThreads.load() == 0); } ThreadFunctionResult threadFunction() @@ -238,7 +238,7 @@ public: const int currentBlockSize = blockSizeManager.blockSize(); - if (currentIndex >= iterationCount) + if (currentIndex.load() >= iterationCount) break; // Atomically reserve a block of iterationCount for this thread. @@ -269,7 +269,7 @@ public: // Report progress if progress reporting enabled. if (progressReportingEnabled) { completed.fetchAndAddAcquire(finalBlockSize); - this->setProgressValue(this->completed); + this->setProgressValue(this->completed.load()); } if (this->shouldThrottleThread()) diff --git a/src/corelib/concurrent/qtconcurrentthreadengine.cpp b/src/corelib/concurrent/qtconcurrentthreadengine.cpp index bb9b0800c9..71a47164d2 100644 --- a/src/corelib/concurrent/qtconcurrentthreadengine.cpp +++ b/src/corelib/concurrent/qtconcurrentthreadengine.cpp @@ -53,7 +53,7 @@ ThreadEngineBarrier::ThreadEngineBarrier() void ThreadEngineBarrier::acquire() { forever { - int localCount = int(count); + int localCount = count.load(); if (localCount < 0) { if (count.testAndSetOrdered(localCount, localCount -1)) return; @@ -67,7 +67,7 @@ void ThreadEngineBarrier::acquire() int ThreadEngineBarrier::release() { forever { - int localCount = int(count); + int localCount = count.load(); if (localCount == -1) { if (count.testAndSetOrdered(-1, 0)) { semaphore.release(); @@ -87,7 +87,7 @@ int ThreadEngineBarrier::release() void ThreadEngineBarrier::wait() { forever { - int localCount = int(count); + int localCount = count.load(); if (localCount == 0) return; @@ -101,7 +101,7 @@ void ThreadEngineBarrier::wait() int ThreadEngineBarrier::currentCount() { - return int(count); + return count.load(); } // releases a thread, unless this is the last thread. @@ -109,7 +109,7 @@ int ThreadEngineBarrier::currentCount() bool ThreadEngineBarrier::releaseUnlessLast() { forever { - int localCount = int(count); + int localCount = count.load(); if (qAbs(localCount) == 1) { return false; } else if (localCount < 0) { diff --git a/src/corelib/io/qprocess_p.h b/src/corelib/io/qprocess_p.h index 9adb331a13..5bebff089e 100644 --- a/src/corelib/io/qprocess_p.h +++ b/src/corelib/io/qprocess_p.h @@ -184,7 +184,7 @@ public: template<> Q_INLINE_TEMPLATE void QSharedDataPointer::detach() { - if (d && d->ref == 1) + if (d && d->ref.load() == 1) return; QProcessEnvironmentPrivate *x = (d ? new QProcessEnvironmentPrivate(*d) : new QProcessEnvironmentPrivate); diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp index 6dca036e05..b960528fb8 100644 --- a/src/corelib/io/qurl.cpp +++ b/src/corelib/io/qurl.cpp @@ -6102,7 +6102,7 @@ void QUrl::detach() */ bool QUrl::isDetached() const { - return !d || d->ref == 1; + return !d || d->ref.load() == 1; } diff --git a/src/corelib/kernel/qabstractitemmodel.cpp b/src/corelib/kernel/qabstractitemmodel.cpp index b7ac6aa594..bda25c301e 100644 --- a/src/corelib/kernel/qabstractitemmodel.cpp +++ b/src/corelib/kernel/qabstractitemmodel.cpp @@ -74,7 +74,7 @@ QPersistentModelIndexData *QPersistentModelIndexData::create(const QModelIndex & void QPersistentModelIndexData::destroy(QPersistentModelIndexData *data) { Q_ASSERT(data); - Q_ASSERT(data->ref == 0); + Q_ASSERT(data->ref.load() == 0); QAbstractItemModel *model = const_cast(data->model); // a valid persistent model index with a null model pointer can only happen if the model was destroyed if (model) { diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index 4c969c8f4a..555bfa674d 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -807,19 +807,19 @@ QObject::~QObject() QObjectPrivate::clearGuards(this); } - if (d->sharedRefcount) { - if (d->sharedRefcount->strongref.load() > 0) { + QtSharedPointer::ExternalRefCountData *sharedRefcount = d->sharedRefcount.load(); + if (sharedRefcount) { + if (sharedRefcount->strongref.load() > 0) { qWarning("QObject: shared QObject was deleted directly. The program is malformed and may crash."); // but continue deleting, it's too late to stop anyway } // indicate to all QWeakPointers that this QObject has now been deleted - d->sharedRefcount->strongref.store(0); - if (!d->sharedRefcount->weakref.deref()) - delete d->sharedRefcount; + sharedRefcount->strongref.store(0); + if (!sharedRefcount->weakref.deref()) + delete sharedRefcount; } - if (d->isSignalConnected(0)) { QT_TRY { emit destroyed(this); diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp index 76957f106a..470be1c0b0 100644 --- a/src/corelib/kernel/qvariant.cpp +++ b/src/corelib/kernel/qvariant.cpp @@ -1823,7 +1823,7 @@ QVariant& QVariant::operator=(const QVariant &variant) void QVariant::detach() { - if (!d.is_shared || d.data.shared->ref == 1) + if (!d.is_shared || d.data.shared->ref.load() == 1) return; Private dd; diff --git a/src/corelib/kernel/qvariant.h b/src/corelib/kernel/qvariant.h index e77fc90b92..61dc48ad94 100644 --- a/src/corelib/kernel/qvariant.h +++ b/src/corelib/kernel/qvariant.h @@ -448,7 +448,7 @@ Q_CORE_EXPORT QDataStream& operator<< (QDataStream& s, const QVariant::Type p); #endif inline bool QVariant::isDetached() const -{ return !d.is_shared || d.data.shared->ref == 1; } +{ return !d.is_shared || d.data.shared->ref.load() == 1; } #ifdef qdoc diff --git a/src/corelib/thread/qatomic.h b/src/corelib/thread/qatomic.h index 6fe4d65832..0b72ce9adf 100644 --- a/src/corelib/thread/qatomic.h +++ b/src/corelib/thread/qatomic.h @@ -259,7 +259,7 @@ inline void qAtomicAssign(T *&d, T *x) template inline void qAtomicDetach(T *&d) { - if (d->ref == 1) + if (d->ref.load() == 1) return; T *x = d; d = new T(*d); diff --git a/src/corelib/thread/qmutex.cpp b/src/corelib/thread/qmutex.cpp index e2da0adf17..959d1f958a 100644 --- a/src/corelib/thread/qmutex.cpp +++ b/src/corelib/thread/qmutex.cpp @@ -154,7 +154,7 @@ QMutex::~QMutex() delete static_cast(d.load()); else if (d.load()) { #ifndef Q_OS_LINUX - if (d.load()->possiblyUnlocked && tryLock()) { unlock(); return; } + if (d.load()->possiblyUnlocked.load() && tryLock()) { unlock(); return; } #endif qWarning("QMutex: destroying locked mutex"); } @@ -361,7 +361,7 @@ bool QBasicMutex::lockInternal(int timeout) return static_cast(d)->lock(timeout); } - if (timeout == 0 && !d->possiblyUnlocked) + if (timeout == 0 && !d->possiblyUnlocked.load()) return false; if (!d->ref()) @@ -375,7 +375,7 @@ bool QBasicMutex::lockInternal(int timeout) int old_waiters; do { - old_waiters = d->waiters; + old_waiters = d->waiters.load(); if (old_waiters == -QMutexPrivate::BigNumber) { // we are unlocking, and the thread that unlocks is about to change d to 0 // we try to aquire the mutex by changing to dummyLocked() @@ -407,7 +407,7 @@ bool QBasicMutex::lockInternal(int timeout) } if (d->wait(timeout)) { - if (d->possiblyUnlocked && d->possiblyUnlocked.testAndSetRelaxed(true, false)) + if (d->possiblyUnlocked.load() && d->possiblyUnlocked.testAndSetRelaxed(true, false)) d->deref(); d->derefWaiters(1); //we got the lock. (do not deref) @@ -445,7 +445,7 @@ void QBasicMutex::unlockInternal() if (d->waiters.fetchAndAddRelease(-QMutexPrivate::BigNumber) == 0) { //there is no one waiting on this mutex anymore, set the mutex as unlocked (d = 0) if (this->d.testAndSetRelease(d, 0)) { - if (d->possiblyUnlocked && d->possiblyUnlocked.testAndSetRelaxed(true, false)) + if (d->possiblyUnlocked.load() && d->possiblyUnlocked.testAndSetRelaxed(true, false)) d->deref(); } d->derefWaiters(0); @@ -479,10 +479,10 @@ QMutexPrivate *QMutexPrivate::allocate() int i = freelist()->next(); QMutexPrivate *d = &(*freelist())[i]; d->id = i; - Q_ASSERT(d->refCount == 0); + Q_ASSERT(d->refCount.load() == 0); Q_ASSERT(!d->recursive); - Q_ASSERT(!d->possiblyUnlocked); - Q_ASSERT(d->waiters == 0); + Q_ASSERT(!d->possiblyUnlocked.load()); + Q_ASSERT(d->waiters.load() == 0); d->refCount = 1; return d; } @@ -490,9 +490,9 @@ QMutexPrivate *QMutexPrivate::allocate() void QMutexPrivate::release() { Q_ASSERT(!recursive); - Q_ASSERT(refCount == 0); - Q_ASSERT(!possiblyUnlocked); - Q_ASSERT(waiters == 0); + Q_ASSERT(refCount.load() == 0); + Q_ASSERT(!possiblyUnlocked.load()); + Q_ASSERT(waiters.load() == 0); freelist()->release(id); } @@ -502,7 +502,7 @@ void QMutexPrivate::derefWaiters(int value) int old_waiters; int new_waiters; do { - old_waiters = waiters; + old_waiters = waiters.load(); new_waiters = old_waiters; if (new_waiters < 0) { new_waiters += QMutexPrivate::BigNumber; diff --git a/src/corelib/thread/qmutex_p.h b/src/corelib/thread/qmutex_p.h index 00f071ebef..00bda48e5b 100644 --- a/src/corelib/thread/qmutex_p.h +++ b/src/corelib/thread/qmutex_p.h @@ -79,21 +79,21 @@ public: int id; bool ref() { - Q_ASSERT(refCount >= 0); + Q_ASSERT(refCount.load() >= 0); int c; do { - c = refCount; + c = refCount.load(); if (c == 0) return false; } while (!refCount.testAndSetRelaxed(c, c + 1)); - Q_ASSERT(refCount >= 0); + Q_ASSERT(refCount.load() >= 0); return true; } void deref() { - Q_ASSERT(refCount >=0); + Q_ASSERT(refCount.load() >= 0); if (!refCount.deref()) release(); - Q_ASSERT(refCount >=0); + Q_ASSERT(refCount.load() >= 0); } void release(); static QMutexPrivate *allocate(); diff --git a/src/corelib/thread/qmutexpool.cpp b/src/corelib/thread/qmutexpool.cpp index 49fb46b0f4..ef4e9560fb 100644 --- a/src/corelib/thread/qmutexpool.cpp +++ b/src/corelib/thread/qmutexpool.cpp @@ -99,7 +99,7 @@ QMutexPool::QMutexPool(QMutex::RecursionMode recursionMode, int size) : mutexes(size), recursionMode(recursionMode) { for (int index = 0; index < mutexes.count(); ++index) { - mutexes[index] = 0; + mutexes[index].store(0); } } @@ -109,10 +109,8 @@ QMutexPool::QMutexPool(QMutex::RecursionMode recursionMode, int size) */ QMutexPool::~QMutexPool() { - for (int index = 0; index < mutexes.count(); ++index) { - delete mutexes[index]; - mutexes[index] = 0; - } + for (int index = 0; index < mutexes.count(); ++index) + delete mutexes[index].load(); } /*! @@ -136,9 +134,9 @@ QMutex *QMutexPool::createMutex(int index) { // mutex not created, create one QMutex *newMutex = new QMutex(recursionMode); - if (!mutexes[index].testAndSetOrdered(0, newMutex)) + if (!mutexes[index].testAndSetRelease(0, newMutex)) delete newMutex; - return mutexes[index]; + return mutexes[index].load(); } /*! diff --git a/src/corelib/thread/qmutexpool_p.h b/src/corelib/thread/qmutexpool_p.h index fa9a734065..20449c24d4 100644 --- a/src/corelib/thread/qmutexpool_p.h +++ b/src/corelib/thread/qmutexpool_p.h @@ -69,7 +69,7 @@ public: inline QMutex *get(const void *address) { int index = uint(quintptr(address)) % mutexes.count(); - QMutex *m = mutexes[index]; + QMutex *m = mutexes[index].load(); if (m) return m; else diff --git a/src/corelib/thread/qthread.cpp b/src/corelib/thread/qthread.cpp index 40cb258e7e..31332cd521 100644 --- a/src/corelib/thread/qthread.cpp +++ b/src/corelib/thread/qthread.cpp @@ -85,7 +85,7 @@ QThreadData::QThreadData(int initialRefCount) QThreadData::~QThreadData() { - Q_ASSERT(_ref == 0); + Q_ASSERT(_ref.load() == 0); // In the odd case that Qt is running on a secondary thread, the main // thread instance will have been dereffed asunder because of the deref in @@ -117,7 +117,7 @@ void QThreadData::ref() { #ifndef QT_NO_THREAD (void) _ref.ref(); - Q_ASSERT(_ref != 0); + Q_ASSERT(_ref.load() != 0); #endif } diff --git a/src/corelib/tools/qregexp.cpp b/src/corelib/tools/qregexp.cpp index 59a54f3eb7..433939fe3e 100644 --- a/src/corelib/tools/qregexp.cpp +++ b/src/corelib/tools/qregexp.cpp @@ -1701,7 +1701,7 @@ void QRegExpEngine::dump() const void QRegExpEngine::setup() { - ref = 1; + ref.store(1); #ifndef QT_NO_REGEXP_CAPTURE f.resize(32); nf = 0; diff --git a/src/corelib/tools/qsharedpointer.cpp b/src/corelib/tools/qsharedpointer.cpp index e9ae3cb4da..34060926c7 100644 --- a/src/corelib/tools/qsharedpointer.cpp +++ b/src/corelib/tools/qsharedpointer.cpp @@ -1238,9 +1238,9 @@ void QtSharedPointer::ExternalRefCountData::setQObjectShared(const QObject *obj, Q_ASSERT(obj); QObjectPrivate *d = QObjectPrivate::get(const_cast(obj)); - if (d->sharedRefcount) + if (d->sharedRefcount.load() != 0) qFatal("QSharedPointer: pointer %p already has reference counting", obj); - d->sharedRefcount = this; + d->sharedRefcount.store(this); // QObject decreases the refcount too, so increase it up weakref.ref(); @@ -1252,7 +1252,7 @@ QtSharedPointer::ExternalRefCountData *QtSharedPointer::ExternalRefCountData::ge QObjectPrivate *d = QObjectPrivate::get(const_cast(obj)); Q_ASSERT_X(!d->wasDeleted, "QWeakPointer", "Detected QWeakPointer creation in a QObject being deleted"); - ExternalRefCountData *that = d->sharedRefcount; + ExternalRefCountData *that = d->sharedRefcount.load(); if (that) { that->weakref.ref(); return that; @@ -1264,9 +1264,10 @@ QtSharedPointer::ExternalRefCountData *QtSharedPointer::ExternalRefCountData::ge x->weakref.store(2); // the QWeakPointer that called us plus the QObject itself if (!d->sharedRefcount.testAndSetRelease(0, x)) { delete x; - d->sharedRefcount->weakref.ref(); + x = d->sharedRefcount.loadAcquire(); + x->weakref.ref(); } - return d->sharedRefcount.loadAcquire(); + return x; } QT_END_NAMESPACE -- cgit v1.2.3