Make all uses of QBasicAtomicInt and Pointer use load() and store()
authorThiago Macieira <thiago@kde.org>
Tue, 5 Jul 2011 21:46:19 +0000 (23:46 +0200)
committerQt by Nokia <qt-info@nokia.com>
Mon, 3 Oct 2011 05:57:41 +0000 (07:57 +0200)
Most of these changes are search-and-replace of d->ref ==, d->ref !=
and d->ref =.

The QBasicAtomicPointer in QObjectPrivate::Connection didn't need to
be basic, so I made it QAtomicPointer.

Change-Id: Ie3271abd1728af599f9ab17c6f4868e475f17bb6
Reviewed-on: http://codereview.qt-project.org/5030
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
25 files changed:
src/corelib/animation/qvariantanimation.cpp
src/corelib/global/qglobal.h
src/corelib/kernel/qmetatype.h
src/corelib/kernel/qobject.cpp
src/corelib/kernel/qobject_p.h
src/corelib/thread/qmutex.cpp
src/corelib/thread/qmutex.h
src/corelib/thread/qmutex_linux.cpp
src/corelib/tools/qcontiguouscache.h
src/corelib/tools/qrefcount.h
src/corelib/tools/qshareddata.h
src/corelib/tools/qsharedpointer.cpp
src/corelib/tools/qsharedpointer_impl.h
src/corelib/tools/qsimd.cpp
src/gui/painting/qregion.cpp
src/opengl/qglcolormap.cpp
src/opengl/qglcolormap.h
src/testlib/qtestlog.cpp
tests/auto/corelib/thread/qatomicpointer/tst_qatomicpointer.cpp
tests/auto/corelib/thread/qmutex/tst_qmutex.cpp
tests/auto/corelib/thread/qthreadonce/qthreadonce.cpp
tests/auto/corelib/thread/qthreadonce/tst_qthreadonce.cpp
tests/auto/corelib/thread/qthreadstorage/tst_qthreadstorage.cpp
tests/auto/corelib/tools/qcontiguouscache/tst_qcontiguouscache.cpp
tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp

index 4bfcd1d..7dfb4bd 100644 (file)
@@ -292,11 +292,11 @@ void QVariantAnimationPrivate::setCurrentValueForProgress(const qreal progress)
     qSwap(currentValue, ret);
     q->updateCurrentValue(currentValue);
     static QBasicAtomicInt changedSignalIndex = Q_BASIC_ATOMIC_INITIALIZER(0);
-    if (!changedSignalIndex) {
+    if (!changedSignalIndex.load()) {
         //we keep the mask so that we emit valueChanged only when needed (for performance reasons)
         changedSignalIndex.testAndSetRelaxed(0, signalIndex("valueChanged(QVariant)"));
     }
-    if (isSignalConnected(changedSignalIndex) && currentValue != ret) {
+    if (isSignalConnected(changedSignalIndex.load()) && currentValue != ret) {
         //the value has changed
         emit q->valueChanged(currentValue);
     }
index 2d2db24..228c9a3 100644 (file)
@@ -1909,8 +1909,8 @@ public:
 
     inline ~QGlobalStaticDeleter()
     {
-        delete globalStatic.pointer;
-        globalStatic.pointer = 0;
+        delete globalStatic.pointer.load();
+        globalStatic.pointer.store(0);
         globalStatic.destroyed = true;
     }
 };
@@ -1920,14 +1920,14 @@ public:
     {                                                                         \
         static QGlobalStatic<TYPE > thisGlobalStatic                          \
                             = { Q_BASIC_ATOMIC_INITIALIZER(0), false };       \
-        if (!thisGlobalStatic.pointer && !thisGlobalStatic.destroyed) {       \
+        if (!thisGlobalStatic.pointer.load() && !thisGlobalStatic.destroyed) { \
             TYPE *x = new TYPE;                                               \
             if (!thisGlobalStatic.pointer.testAndSetOrdered(0, x))            \
                 delete x;                                                     \
             else                                                              \
                 static QGlobalStaticDeleter<TYPE > cleanup(thisGlobalStatic); \
         }                                                                     \
-        return thisGlobalStatic.pointer;                                      \
+        return thisGlobalStatic.pointer.load();                               \
     }
 
 #define Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS)                           \
@@ -1935,14 +1935,14 @@ public:
     {                                                                         \
         static QGlobalStatic<TYPE > thisGlobalStatic                          \
                             = { Q_BASIC_ATOMIC_INITIALIZER(0), false };       \
-        if (!thisGlobalStatic.pointer && !thisGlobalStatic.destroyed) {       \
+        if (!thisGlobalStatic.pointer.load() && !thisGlobalStatic.destroyed) {       \
             TYPE *x = new TYPE ARGS;                                          \
             if (!thisGlobalStatic.pointer.testAndSetOrdered(0, x))            \
                 delete x;                                                     \
             else                                                              \
                 static QGlobalStaticDeleter<TYPE > cleanup(thisGlobalStatic); \
         }                                                                     \
-        return thisGlobalStatic.pointer;                                      \
+        return thisGlobalStatic.pointer.load();                               \
     }
 
 #define Q_GLOBAL_STATIC_WITH_INITIALIZER(TYPE, NAME, INITIALIZER)             \
@@ -1950,7 +1950,7 @@ public:
     {                                                                         \
         static QGlobalStatic<TYPE > thisGlobalStatic                          \
                             = { Q_BASIC_ATOMIC_INITIALIZER(0), false };       \
-        if (!thisGlobalStatic.pointer && !thisGlobalStatic.destroyed) {       \
+        if (!thisGlobalStatic.pointer.load() && !thisGlobalStatic.destroyed) {       \
             QScopedPointer<TYPE > x(new TYPE);                                \
             INITIALIZER;                                                      \
             if (thisGlobalStatic.pointer.testAndSetOrdered(0, x.data())) {    \
@@ -1958,7 +1958,7 @@ public:
                 x.take();                                                     \
             }                                                                 \
         }                                                                     \
-        return thisGlobalStatic.pointer;                                      \
+        return thisGlobalStatic.pointer.load();                               \
     }
 
 #endif
index 5df7658..f7473e3 100644 (file)
@@ -273,10 +273,10 @@ inline int qRegisterMetaTypeStreamOperators()
         static int qt_metatype_id()                                     \
             {                                                           \
                 static QBasicAtomicInt metatype_id = Q_BASIC_ATOMIC_INITIALIZER(0); \
-                if (!metatype_id)                                       \
-                    metatype_id = qRegisterMetaType< TYPE >(#TYPE,      \
-                               reinterpret_cast< TYPE *>(quintptr(-1))); \
-                return metatype_id;                                     \
+                if (!metatype_id.load())                                \
+                    metatype_id.storeRelease(qRegisterMetaType< TYPE >(#TYPE, \
+                               reinterpret_cast< TYPE *>(quintptr(-1)))); \
+                return metatype_id.loadAcquire();                       \
             }                                                           \
     };                                                                  \
     QT_END_NAMESPACE
index 0d7b840..6e539d1 100644 (file)
@@ -808,13 +808,13 @@ QObject::~QObject()
     }
 
     if (d->sharedRefcount) {
-        if (d->sharedRefcount->strongref > 0) {
+        if (d->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 = 0;
+        d->sharedRefcount->strongref.store(0);
         if (!d->sharedRefcount->weakref.deref())
             delete d->sharedRefcount;
     }
index e7233c5..b4c30bd 100644 (file)
@@ -120,7 +120,7 @@ public:
         //senders linked list
         Connection *next;
         Connection **prev;
-        QBasicAtomicPointer<int> argumentTypes;
+        QAtomicPointer<int> argumentTypes;
         ushort method_offset;
         ushort method_relative;
         ushort connectionType : 3; // 0 == auto, 1 == direct, 2 == queued, 4 == blocking
index c90b44b..b2f5250 100644 (file)
@@ -140,10 +140,7 @@ QT_BEGIN_NAMESPACE
 */
 QMutex::QMutex(RecursionMode mode)
 {
-    if (mode == Recursive)
-        d = new QRecursiveMutexPrivate;
-    else
-        d = 0;
+    d.store(mode == Recursive ? new QRecursiveMutexPrivate : 0);
 }
 
 /*!
@@ -154,10 +151,10 @@ QMutex::QMutex(RecursionMode mode)
 QMutex::~QMutex()
 {
     if (isRecursive())
-        delete static_cast<QRecursiveMutexPrivate *>(d._q_value);
-    else if (d) {
+        delete static_cast<QRecursiveMutexPrivate *>(d.load());
+    else if (d.load()) {
 #ifndef Q_OS_LINUX
-        if (d->possiblyUnlocked && tryLock()) { unlock(); return; }
+        if (d.load()->possiblyUnlocked && tryLock()) { unlock(); return; }
 #endif
         qWarning("QMutex: destroying locked mutex");
     }
@@ -236,7 +233,7 @@ QMutex::~QMutex()
 
 */
 bool QBasicMutex::isRecursive() {
-    QMutexPrivate *d = this->d;
+    QMutexPrivate *d = this->d.load();
     if (quintptr(d) <= 0x3)
         return false;
     return d->recursive;
@@ -345,7 +342,7 @@ bool QBasicMutex::isRecursive() {
 bool QBasicMutex::lockInternal(int timeout)
 {
     while (!fastTryLock()) {
-        QMutexPrivate *d = this->d;
+        QMutexPrivate *d = this->d.loadAcquire();
         if (!d) // if d is 0, the mutex is unlocked
             continue;
 
@@ -370,7 +367,7 @@ bool QBasicMutex::lockInternal(int timeout)
         if (!d->ref())
             continue; //that QMutexPrivate was already released
 
-        if (d != this->d) {
+        if (d != this->d.loadAcquire()) {
             //Either the mutex is already unlocked, or relocked with another mutex
             d->deref();
             continue;
@@ -389,7 +386,7 @@ bool QBasicMutex::lockInternal(int timeout)
                     d->deref();
                     return true;
                 } else {
-                    Q_ASSERT(d != this->d); //else testAndSetAcquire should have succeeded
+                    Q_ASSERT(d != this->d.load()); //else testAndSetAcquire should have succeeded
                     // Mutex is likely to bo 0, we should continue the outer-loop,
                     //  set old_waiters to the magic value of BigNumber
                     old_waiters = QMutexPrivate::BigNumber;
@@ -398,7 +395,7 @@ bool QBasicMutex::lockInternal(int timeout)
             }
         } while (!d->waiters.testAndSetRelaxed(old_waiters, old_waiters + 1));
 
-        if (d != this->d) {
+        if (d != this->d.loadAcquire()) {
             // Mutex was unlocked.
             if (old_waiters != QMutexPrivate::BigNumber) {
                 //we did not break the previous loop
@@ -436,7 +433,7 @@ bool QBasicMutex::lockInternal(int timeout)
 */
 void QBasicMutex::unlockInternal()
 {
-    QMutexPrivate *d = this->d;
+    QMutexPrivate *d = this->d.loadAcquire();
     Q_ASSERT(d); //we must be locked
     Q_ASSERT(d != dummyLocked()); // testAndSetRelease(dummyLocked(), 0) failed
 
index a49b981..4bc2c87 100644 (file)
@@ -65,7 +65,7 @@ public:
     }
 
     inline void unlock() {
-        Q_ASSERT(d); //mutex must be locked
+        Q_ASSERT(d.load()); //mutex must be locked
         if (!d.testAndSetRelease(dummyLocked(), 0))
             unlockInternal();
     }
index 17015b8..a3ca150 100644 (file)
@@ -54,7 +54,7 @@
 
 QT_BEGIN_NAMESPACE
 
-static inline int _q_futex(QMutexPrivate *volatile *addr, int op, int val, const struct timespec *timeout)
+static inline int _q_futex(void *addr, int op, int val, const struct timespec *timeout)
 {
     volatile int *int_addr = reinterpret_cast<volatile int *>(addr);
 #if Q_BYTE_ORDER == Q_BIG_ENDIAN && QT_POINTER_SIZE == 8
@@ -82,7 +82,7 @@ bool QBasicMutex::lockInternal(int timeout)
         elapsedTimer.start();
 
     while (!fastTryLock()) {
-        QMutexPrivate *d = this->d;
+        QMutexPrivate *d = this->d.load();
         if (!d) // if d is 0, the mutex is unlocked
             continue;
 
@@ -103,7 +103,7 @@ bool QBasicMutex::lockInternal(int timeout)
                     ts.tv_nsec = xtimeout % (Q_INT64_C(1000) * 1000 * 1000);
                     pts = &ts;
                 }
-                int r = _q_futex(&this->d._q_value, FUTEX_WAIT, quintptr(dummyFutexValue()), pts);
+                int r = _q_futex(&this->d, FUTEX_WAIT, quintptr(dummyFutexValue()), pts);
                 if (r != 0 && errno == ETIMEDOUT)
                     return false;
             }
@@ -112,19 +112,19 @@ bool QBasicMutex::lockInternal(int timeout)
         Q_ASSERT(d->recursive);
         return static_cast<QRecursiveMutexPrivate *>(d)->lock(timeout);
     }
-    Q_ASSERT(this->d);
+    Q_ASSERT(this->d.load());
     return true;
 }
 
 void QBasicMutex::unlockInternal()
 {
-    QMutexPrivate *d = this->d;
+    QMutexPrivate *d = this->d.load();
     Q_ASSERT(d); //we must be locked
     Q_ASSERT(d != dummyLocked()); // testAndSetRelease(dummyLocked(), 0) failed
 
     if (d == dummyFutexValue()) {
         this->d.fetchAndStoreRelease(0);
-        _q_futex(&this->d._q_value, FUTEX_WAKE, 1, 0);
+        _q_futex(&this->d, FUTEX_WAKE, 1, 0);
         return;
     }
 
index 526d8fb..e192edf 100644 (file)
@@ -105,8 +105,8 @@ public:
 
     inline ~QContiguousCache() { if (!d) return; if (!d->ref.deref()) free(p); }
 
-    inline void detach() { if (d->ref != 1) detach_helper(); }
-    inline bool isDetached() const { return d->ref == 1; }
+    inline void detach() { if (d->ref.load() != 1) detach_helper(); }
+    inline bool isDetached() const { return d->ref.load() == 1; }
     inline void setSharable(bool sharable) { if (!sharable) detach(); d->sharable = sharable; }
 
     QContiguousCache<T> &operator=(const QContiguousCache<T> &other);
@@ -185,7 +185,7 @@ void QContiguousCache<T>::detach_helper()
     union { QContiguousCacheData *d; QContiguousCacheTypedData<T> *p; } x;
 
     x.d = malloc(d->alloc);
-    x.d->ref = 1;
+    x.d->ref.store(1);
     x.d->count = d->count;
     x.d->start = d->start;
     x.d->offset = d->offset;
@@ -258,7 +258,7 @@ void QContiguousCache<T>::setCapacity(int asize)
 template <typename T>
 void QContiguousCache<T>::clear()
 {
-    if (d->ref == 1) {
+    if (d->ref.load() == 1) {
         if (QTypeInfo<T>::isComplex) {
             int oldcount = d->count;
             T * i = p->array + d->start;
@@ -274,7 +274,7 @@ void QContiguousCache<T>::clear()
     } else {
         union { QContiguousCacheData *d; QContiguousCacheTypedData<T> *p; } x;
         x.d = malloc(d->alloc);
-        x.d->ref = 1;
+        x.d->ref.store(1);
         x.d->alloc = d->alloc;
         x.d->count = x.d->start = x.d->offset = 0;
         x.d->sharable = true;
@@ -293,7 +293,7 @@ template <typename T>
 QContiguousCache<T>::QContiguousCache(int cap)
 {
     d = malloc(cap);
-    d->ref = 1;
+    d->ref.store(1);
     d->alloc = cap;
     d->count = d->start = d->offset = 0;
     d->sharable = true;
index e72ddf6..619f61e 100644 (file)
@@ -56,27 +56,29 @@ namespace QtPrivate
 class RefCount
 {
 public:
-   inline void ref() {
-        if (atomic > 0)
+    inline void ref() {
+        if (atomic.load() > 0)
             atomic.ref();
     }
 
     inline bool deref() {
-        if (atomic <= 0)
+        if (atomic.load() <= 0)
             return true;
         return atomic.deref();
     }
 
     inline bool operator==(int value) const
-    { return atomic.operator ==(value); }
+    { return atomic.load() == value; }
     inline bool operator!=(int value) const
-    { return atomic.operator !=(value); }
+    { return atomic.load() != value; }
     inline bool operator!() const
-    { return atomic.operator !(); }
+    { return !atomic.load(); }
     inline operator int() const
-    { return atomic.operator int(); }
+    { return atomic.load(); }
     inline RefCount &operator=(int value)
-    { atomic = value; return *this; }
+    { atomic.store(value); return *this; }
+    inline RefCount &operator=(const RefCount &other)
+    { atomic.store(other.atomic.load()); return *this; }
 
     QBasicAtomicInt atomic;
 };
index 811b186..6de6a19 100644 (file)
@@ -72,7 +72,7 @@ public:
     typedef T Type;
     typedef T *pointer;
 
-    inline void detach() { if (d && d->ref != 1) detach_helper(); }
+    inline void detach() { if (d && d->ref.load() != 1) detach_helper(); }
     inline T &operator*() { detach(); return *d; }
     inline const T &operator*() const { return *d; }
     inline T *operator->() { detach(); return d; }
@@ -145,7 +145,7 @@ public:
     inline T *data() const { return d; }
     inline const T *constData() const { return d; }
 
-    inline void detach() { if (d && d->ref != 1) detach_helper(); }
+    inline void detach() { if (d && d->ref.load() != 1) detach_helper(); }
 
     inline void reset()
     {
index 873a2bf..e9ae3cb 100644 (file)
@@ -1260,13 +1260,13 @@ QtSharedPointer::ExternalRefCountData *QtSharedPointer::ExternalRefCountData::ge
 
     // we can create the refcount data because it doesn't exist
     ExternalRefCountData *x = new ExternalRefCountData(Qt::Uninitialized);
-    x->strongref = -1;
-    x->weakref = 2;  // the QWeakPointer that called us plus the QObject itself
+    x->strongref.store(-1);
+    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();
     }
-    return d->sharedRefcount;
+    return d->sharedRefcount.loadAcquire();
 }
 
 QT_END_NAMESPACE
index 47ad21a..f038891 100644 (file)
@@ -186,11 +186,11 @@ namespace QtSharedPointer {
 
         inline ExternalRefCountData()
         {
-            strongref = 1;
-            weakref = 1;
+            strongref.store(1);
+            weakref.store(1);
         }
         inline ExternalRefCountData(Qt::Initialization) { }
-        virtual inline ~ExternalRefCountData() { Q_ASSERT(!weakref); Q_ASSERT(strongref <= 0); }
+        virtual inline ~ExternalRefCountData() { Q_ASSERT(!weakref.load()); Q_ASSERT(strongref.load() <= 0); }
 
         // overridden by derived classes
         // returns false to indicate caller should delete the pointer
@@ -432,12 +432,12 @@ namespace QtSharedPointer {
             if (o) {
                 // increase the strongref, but never up from zero
                 // or less (-1 is used by QWeakPointer on untracked QObject)
-                register int tmp = o->strongref;
+                register int tmp = o->strongref.load();
                 while (tmp > 0) {
                     // try to increment from "tmp" to "tmp + 1"
                     if (o->strongref.testAndSetRelaxed(tmp, tmp + 1))
                         break;   // succeeded
-                    tmp = o->strongref;  // failed, try again
+                    tmp = o->strongref.load();  // failed, try again
                 }
 
                 if (tmp > 0)
@@ -448,7 +448,7 @@ namespace QtSharedPointer {
 
             qSwap(d, o);
             qSwap(this->value, actual);
-            if (!d || d->strongref == 0)
+            if (!d || d->strongref.load() == 0)
                 this->value = 0;
 
             // dereference saved data
@@ -577,14 +577,14 @@ public:
     typedef const value_type &const_reference;
     typedef qptrdiff difference_type;
 
-    inline bool isNull() const { return d == 0 || d->strongref == 0 || value == 0; }
+    inline bool isNull() const { return d == 0 || d->strongref.load() == 0 || value == 0; }
 #ifndef Q_CC_NOKIAX86
     inline operator RestrictedBool() const { return isNull() ? 0 : &QWeakPointer::value; }
 #else
     inline operator bool() const { return isNull() ? 0 : &QWeakPointer::value; }
 #endif
     inline bool operator !() const { return isNull(); }
-    inline T *data() const { return d == 0 || d->strongref == 0 ? 0 : value; }
+    inline T *data() const { return d == 0 || d->strongref.load() == 0 ? 0 : value; }
 
     inline QWeakPointer() : d(0), value(0) { }
     inline ~QWeakPointer() { if (d && !d->weakref.deref()) delete d; }
index 9bfb956..3dfe38a 100644 (file)
@@ -382,8 +382,8 @@ const int features_count = (sizeof features_indices - 1) / (sizeof features_indi
 uint qDetectCPUFeatures()
 {
     static QBasicAtomicInt features = Q_BASIC_ATOMIC_INITIALIZER(-1);
-    if (features != -1)
-        return features;
+    if (features.load() != -1)
+        return features.load();
 
     uint f = detectProcessorFeatures();
     QByteArray disable = qgetenv("QT_NO_CPU_FEATURE");
@@ -395,8 +395,8 @@ uint qDetectCPUFeatures()
         }
     }
 
-    features = f;
-    return features;
+    features.store(f);
+    return f;
 }
 
 void qDumpCPUFeatures()
index f343fa9..3f8e80c 100644 (file)
@@ -261,7 +261,7 @@ QRegion::QRegion(int x, int y, int w, int h, RegionType t)
 
 void QRegion::detach()
 {
-    if (d->ref != 1)
+    if (d->ref.load() != 1)
         *this = copy();
 #if defined(Q_WS_X11)
     else if (d->xrectangles) {
@@ -3828,7 +3828,7 @@ QRegion::QRegion(const QRect &r, RegionType t)
         d->ref.ref();
     } else {
         d = new QRegionData;
-        d->ref = 1;
+        d->ref.store(1);
 #if defined(Q_WS_X11)
         d->rgn = 0;
         d->xrectangles = 0;
@@ -3853,7 +3853,7 @@ QRegion::QRegion(const QPolygon &a, Qt::FillRule fillRule)
                                                fillRule == Qt::WindingFill ? WindingRule : EvenOddRule);
         if (qt_rgn) {
             d =  new QRegionData;
-            d->ref = 1;
+            d->ref.store(1);
 #if defined(Q_WS_X11)
             d->rgn = 0;
             d->xrectangles = 0;
@@ -3885,7 +3885,7 @@ QRegion::QRegion(const QBitmap &bm)
         d->ref.ref();
     } else {
         d = new QRegionData;
-        d->ref = 1;
+        d->ref.store(1);
 #if defined(Q_WS_X11)
         d->rgn = 0;
         d->xrectangles = 0;
@@ -3935,7 +3935,7 @@ QRegion QRegion::copy() const
 {
     QRegion r;
     QScopedPointer<QRegionData> x(new QRegionData);
-    x->ref = 1;
+    x->ref.store(1);
 #if defined(Q_WS_X11)
     x->rgn = 0;
     x->xrectangles = 0;
index 492154f..8ff2f9f 100644 (file)
@@ -154,7 +154,7 @@ QGLColormap & QGLColormap::operator=(const QGLColormap &map)
 void QGLColormap::detach_helper()
 {
     QGLColormapData *x = new QGLColormapData;
-    x->ref = 1;
+    x->ref.store(1);
     x->cmapHandle = 0;
     x->cells = 0;
     if (d->cells) {
index d9e6ff9..cc85191 100644 (file)
@@ -94,7 +94,7 @@ private:
 
 inline void QGLColormap::detach()
 {
-    if (d->ref != 1)
+    if (d->ref.load() != 1)
         detach_helper();
 }
 
index acd1a49..c812f6c 100644 (file)
@@ -221,7 +221,7 @@ namespace QTest {
             return;
 
         if (type != QtFatalMsg) {
-            if (counter <= 0)
+            if (counter.load() <= 0)
                 return;
 
             if (!counter.deref()) {
index 42e744a..903f6de 100644 (file)
@@ -102,7 +102,7 @@ void tst_QAtomicPointer::warningFreeHelper()
 
     QBasicAtomicPointer<WFHC> p = Q_BASIC_ATOMIC_INITIALIZER(0);
 
-    p->bar();
+    p.load()->bar();
 
     WFHC *expectedValue = 0;
     WFHC *newValue = 0;
index 7ad6a98..71a310b 100644 (file)
@@ -486,7 +486,7 @@ void tst_QMutex::stressTest()
     for (int i = 1; i < threadCount; ++i)
         QVERIFY(threads[i].wait(10000));
     QCOMPARE(StressTestThread::errorCount, 0);
-    qDebug("locked %d times", int(StressTestThread::lockCount));
+    qDebug("locked %d times", int(StressTestThread::lockCount.load()));
 }
 
 class TryLockRaceThread : public QThread
index b23e11b..abd3d6b 100644 (file)
@@ -75,7 +75,7 @@ QOnceControl::QOnceControl(QBasicAtomicInt *control)
     d = 0;
     gv = control;
     // check if code has already run once
-    if (*gv == 2) {
+    if (gv->loadAcquire() == 2) {
         // uncontended case: it has already initialized
         // no waiting
         return;
index 235829b..041f801 100644 (file)
@@ -70,7 +70,7 @@ class SingletonObject: public QObject
     Q_OBJECT
 public:
     static int runCount;
-    SingletonObject() { val = 42; ++runCount; }
+    SingletonObject() { val.store(42); ++runCount; }
     ~SingletonObject() { }
 
     QBasicAtomicInt val;
@@ -126,7 +126,7 @@ void tst_QThreadOnce::sameThread()
     QCOMPARE(controlVariable, 1);
 
     static QSingleton<SingletonObject> s;
-    QTEST((int)s->val, "expectedValue");
+    QTEST((int)s->val.load(), "expectedValue");
     s->val.ref();
 
     QCOMPARE(SingletonObject::runCount, 1);
@@ -148,7 +148,7 @@ void tst_QThreadOnce::multipleThreads()
 
     QCOMPARE(controlVariable, 0); // nothing must have set them yet
     SingletonObject::runCount = 0;
-    IncrementThread::runCount = 0;
+    IncrementThread::runCount.store(0);
 
     // wait for all of them to be ready
     sem2.acquire(NumberOfThreads);
@@ -159,7 +159,7 @@ void tst_QThreadOnce::multipleThreads()
     delete parent;
 
     QCOMPARE(controlVariable, 1);
-    QCOMPARE((int)IncrementThread::runCount, NumberOfThreads);
+    QCOMPARE((int)IncrementThread::runCount.load(), NumberOfThreads);
     QCOMPARE(SingletonObject::runCount, 1);
 }
 
index e123fc4..4fc3981 100644 (file)
@@ -358,7 +358,7 @@ void tst_QThreadStorage::QTBUG14579_leakInDestructor()
             QVERIFY(tls.hasLocalData());
         }
     };
-    int c = SPointer::count;
+    int c = SPointer::count.load();
 
     QThreadStorage<QTBUG14579_class *> tls;
 
@@ -382,7 +382,7 @@ void tst_QThreadStorage::QTBUG14579_leakInDestructor()
     QVERIFY(t3.wait());
 
     //check all the constructed things have been destructed
-    QCOMPARE(int(SPointer::count), c);
+    QCOMPARE(int(SPointer::count.load()), c);
 }
 
 class QTBUG14579_reset {
@@ -410,7 +410,7 @@ void tst_QThreadStorage::QTBUG14579_resetInDestructor()
             QVERIFY(QTBUG14579_resetTls()->hasLocalData());
         }
     };
-    int c = SPointer::count;
+    int c = SPointer::count.load();
 
     Thread t1;
     Thread t2;
@@ -423,7 +423,7 @@ void tst_QThreadStorage::QTBUG14579_resetInDestructor()
     QVERIFY(t3.wait());
 
     //check all the constructed things have been destructed
-    QCOMPARE(int(SPointer::count), c);
+    QCOMPARE(int(SPointer::count.load()), c);
 }
 
 
@@ -474,7 +474,7 @@ void tst_QThreadStorage::valueBased()
     QThreadStorage<QString> tlsString;
     QThreadStorage<int> tlsInt;
 
-    int c = SPointer::count;
+    int c = SPointer::count.load();
 
     Thread t1(tlsSPointer, tlsString, tlsInt);
     Thread t2(tlsSPointer, tlsString, tlsInt);
@@ -494,7 +494,7 @@ void tst_QThreadStorage::valueBased()
     QVERIFY(t2.wait());
     QVERIFY(t3.wait());
 
-    QCOMPARE(c, int(SPointer::count));
+    QCOMPARE(c, int(SPointer::count.load()));
 
 }
 
index 0a0c999..076b6ca 100644 (file)
@@ -351,7 +351,7 @@ public:
         return *this;
     }
 
-    int refCount() const { return d->ref; }
+    int refCount() const { return d->ref.load(); }
 private:
     RefCountingClassData *d;
 };
index 65709e3..1c98186 100644 (file)
@@ -207,8 +207,8 @@ void tst_QSharedPointer::basics()
         QVERIFY(! (ptr == otherData));
         QVERIFY(! (otherData == ptr));
     }
-    QVERIFY(!refCountData(ptr) || refCountData(ptr)->weakref == 1);
-    QVERIFY(!refCountData(ptr) || refCountData(ptr)->strongref == 1);
+    QVERIFY(!refCountData(ptr) || refCountData(ptr)->weakref.load() == 1);
+    QVERIFY(!refCountData(ptr) || refCountData(ptr)->strongref.load() == 1);
 
     {
         // create another object:
@@ -220,8 +220,8 @@ void tst_QSharedPointer::basics()
 
         // otherData is deleted here
     }
-    QVERIFY(!refCountData(ptr) || refCountData(ptr)->weakref == 1);
-    QVERIFY(!refCountData(ptr) || refCountData(ptr)->strongref == 1);
+    QVERIFY(!refCountData(ptr) || refCountData(ptr)->weakref.load() == 1);
+    QVERIFY(!refCountData(ptr) || refCountData(ptr)->strongref.load() == 1);
 
     {
         // create a copy:
@@ -237,8 +237,8 @@ void tst_QSharedPointer::basics()
         QCOMPARE(copy.data(), aData);
         QVERIFY(copy == aData);
     }
-    QVERIFY(!refCountData(ptr) || refCountData(ptr)->weakref == 1);
-    QVERIFY(!refCountData(ptr) || refCountData(ptr)->strongref == 1);
+    QVERIFY(!refCountData(ptr) || refCountData(ptr)->weakref.load() == 1);
+    QVERIFY(!refCountData(ptr) || refCountData(ptr)->strongref.load() == 1);
 
     {
         // create a weak reference:
@@ -269,8 +269,8 @@ void tst_QSharedPointer::basics()
         QVERIFY(strong == ptr);
         QCOMPARE(strong.data(), aData);
     }
-    QVERIFY(!refCountData(ptr) || refCountData(ptr)->weakref == 1);
-    QVERIFY(!refCountData(ptr) || refCountData(ptr)->strongref == 1);
+    QVERIFY(!refCountData(ptr) || refCountData(ptr)->weakref.load() == 1);
+    QVERIFY(!refCountData(ptr) || refCountData(ptr)->strongref.load() == 1);
 
     // aData is deleted here
 }
@@ -555,15 +555,15 @@ void tst_QSharedPointer::upCast()
         QVERIFY(baseptr == derivedptr);
         QCOMPARE(static_cast<Data *>(derivedptr.data()), baseptr.data());
     }
-    QCOMPARE(int(refCountData(baseptr)->weakref), 1);
-    QCOMPARE(int(refCountData(baseptr)->strongref), 1);
+    QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1);
+    QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1);
 
     {
         QWeakPointer<DerivedData> derivedptr = qWeakPointerCast<DerivedData>(baseptr);
         QVERIFY(baseptr == derivedptr);
     }
-    QCOMPARE(int(refCountData(baseptr)->weakref), 1);
-    QCOMPARE(int(refCountData(baseptr)->strongref), 1);
+    QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1);
+    QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1);
 
     {
         QWeakPointer<Data> weakptr = baseptr;
@@ -571,16 +571,16 @@ void tst_QSharedPointer::upCast()
         QVERIFY(baseptr == derivedptr);
         QCOMPARE(static_cast<Data *>(derivedptr.data()), baseptr.data());
     }
-    QCOMPARE(int(refCountData(baseptr)->weakref), 1);
-    QCOMPARE(int(refCountData(baseptr)->strongref), 1);
+    QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1);
+    QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1);
 
     {
         QSharedPointer<DerivedData> derivedptr = baseptr.staticCast<DerivedData>();
         QVERIFY(baseptr == derivedptr);
         QCOMPARE(static_cast<Data *>(derivedptr.data()), baseptr.data());
     }
-    QCOMPARE(int(refCountData(baseptr)->weakref), 1);
-    QCOMPARE(int(refCountData(baseptr)->strongref), 1);
+    QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1);
+    QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1);
 }
 
 class OtherObject: public QObject
@@ -955,8 +955,8 @@ void tst_QSharedPointer::dynamicCast()
         QCOMPARE(derivedptr.data(), aData);
         QCOMPARE(static_cast<Data *>(derivedptr.data()), baseptr.data());
     }
-    QCOMPARE(int(refCountData(baseptr)->weakref), 1);
-    QCOMPARE(int(refCountData(baseptr)->strongref), 1);
+    QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1);
+    QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1);
 
     {
         QWeakPointer<Data> weakptr = baseptr;
@@ -965,8 +965,8 @@ void tst_QSharedPointer::dynamicCast()
         QCOMPARE(derivedptr.data(), aData);
         QCOMPARE(static_cast<Data *>(derivedptr.data()), baseptr.data());
     }
-    QCOMPARE(int(refCountData(baseptr)->weakref), 1);
-    QCOMPARE(int(refCountData(baseptr)->strongref), 1);
+    QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1);
+    QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1);
 
     {
         QSharedPointer<DerivedData> derivedptr = baseptr.dynamicCast<DerivedData>();
@@ -974,8 +974,8 @@ void tst_QSharedPointer::dynamicCast()
         QCOMPARE(derivedptr.data(), aData);
         QCOMPARE(static_cast<Data *>(derivedptr.data()), baseptr.data());
     }
-    QCOMPARE(int(refCountData(baseptr)->weakref), 1);
-    QCOMPARE(int(refCountData(baseptr)->strongref), 1);
+    QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1);
+    QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1);
 }
 
 void tst_QSharedPointer::dynamicCastDifferentPointers()
@@ -990,8 +990,8 @@ void tst_QSharedPointer::dynamicCastDifferentPointers()
         QCOMPARE(derivedptr.data(), aData);
         QCOMPARE(static_cast<Data *>(derivedptr.data()), baseptr.data());
     }
-    QCOMPARE(int(refCountData(baseptr)->weakref), 1);
-    QCOMPARE(int(refCountData(baseptr)->strongref), 1);
+    QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1);
+    QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1);
 
     {
         QWeakPointer<Data> weakptr = baseptr;
@@ -1000,8 +1000,8 @@ void tst_QSharedPointer::dynamicCastDifferentPointers()
         QCOMPARE(derivedptr.data(), aData);
         QCOMPARE(static_cast<Data *>(derivedptr.data()), baseptr.data());
     }
-    QCOMPARE(int(refCountData(baseptr)->weakref), 1);
-    QCOMPARE(int(refCountData(baseptr)->strongref), 1);
+    QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1);
+    QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1);
 
     {
         QSharedPointer<DiffPtrDerivedData> derivedptr = baseptr.dynamicCast<DiffPtrDerivedData>();
@@ -1009,8 +1009,8 @@ void tst_QSharedPointer::dynamicCastDifferentPointers()
         QCOMPARE(derivedptr.data(), aData);
         QCOMPARE(static_cast<Data *>(derivedptr.data()), baseptr.data());
     }
-    QCOMPARE(int(refCountData(baseptr)->weakref), 1);
-    QCOMPARE(int(refCountData(baseptr)->strongref), 1);
+    QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1);
+    QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1);
 
     {
         Stuffing *nakedptr = dynamic_cast<Stuffing *>(baseptr.data());
@@ -1035,8 +1035,8 @@ void tst_QSharedPointer::dynamicCastVirtualBase()
         QCOMPARE(derivedptr.data(), aData);
         QCOMPARE(static_cast<Data *>(derivedptr.data()), baseptr.data());
     }
-    QCOMPARE(int(refCountData(baseptr)->weakref), 1);
-    QCOMPARE(int(refCountData(baseptr)->strongref), 1);
+    QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1);
+    QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1);
 
     {
         QWeakPointer<Data> weakptr = baseptr;
@@ -1045,8 +1045,8 @@ void tst_QSharedPointer::dynamicCastVirtualBase()
         QCOMPARE(derivedptr.data(), aData);
         QCOMPARE(static_cast<Data *>(derivedptr.data()), baseptr.data());
     }
-    QCOMPARE(int(refCountData(baseptr)->weakref), 1);
-    QCOMPARE(int(refCountData(baseptr)->strongref), 1);
+    QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1);
+    QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1);
 
     {
         QSharedPointer<VirtualDerived> derivedptr = baseptr.dynamicCast<VirtualDerived>();
@@ -1054,8 +1054,8 @@ void tst_QSharedPointer::dynamicCastVirtualBase()
         QCOMPARE(derivedptr.data(), aData);
         QCOMPARE(static_cast<Data *>(derivedptr.data()), baseptr.data());
     }
-    QCOMPARE(int(refCountData(baseptr)->weakref), 1);
-    QCOMPARE(int(refCountData(baseptr)->strongref), 1);
+    QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1);
+    QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1);
 }
 
 void tst_QSharedPointer::dynamicCastFailure()
@@ -1067,15 +1067,15 @@ void tst_QSharedPointer::dynamicCastFailure()
         QSharedPointer<DerivedData> derivedptr = qSharedPointerDynamicCast<DerivedData>(baseptr);
         QVERIFY(derivedptr.isNull());
     }
-    QCOMPARE(int(refCountData(baseptr)->weakref), 1);
-    QCOMPARE(int(refCountData(baseptr)->strongref), 1);
+    QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1);
+    QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1);
 
     {
         QSharedPointer<DerivedData> derivedptr = baseptr.dynamicCast<DerivedData>();
         QVERIFY(derivedptr.isNull());
     }
-    QCOMPARE(int(refCountData(baseptr)->weakref), 1);
-    QCOMPARE(int(refCountData(baseptr)->strongref), 1);
+    QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1);
+    QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1);
 }
 #endif
 
@@ -1371,8 +1371,8 @@ void tst_QSharedPointer::creating()
         QCOMPARE(Data::destructorCounter, 1);
 
         // valgrind will complain here if something happened to the pointer
-        QVERIFY(d->weakref == 1);
-        QVERIFY(d->strongref == 0);
+        QVERIFY(d->weakref.load() == 1);
+        QVERIFY(d->strongref.load() == 0);
     }
     check();