summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/thread/qatomicint/tst_qatomicint.cpp
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2012-08-06 19:57:59 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-02-16 05:12:26 +0100
commit634f82f1f1fda7983abf70b58e43c580b1f01df0 (patch)
tree0d3b67615c35f6c8938b0d40c2ab59097a4001ea /tests/auto/corelib/thread/qatomicint/tst_qatomicint.cpp
parentfb30a3dd7c2e449235ac5896877bbc0ad41a3835 (diff)
Add a testAndSet overload to the atomics that returns the current value
This is extremely useful, since the most common action after a failed compare-and-swap is to loop around, trying again with the current value as found in memory. Code currently written as: do { Type value = atomic.load(); ... } while (!atomic.testAndSetRelaxed(value, desired)); Becomes: Type value = atomic.load(); do { ... } while (!atomic.testAndSetRelaxed(value, desired, value)); In most CPU architectures, the value that was found in memory is known to the compare-and-swap code, so this is more efficient than the previous code. In architectures where the value is not known, the new code is no worse than before. The implementation sometimes modified an existing function, sometimes it added a new one, depending on whether more registers were needed in the assembly (like ARMv6-7), the code became more complex (ARMv5), the optimizer failed (C++11), or it was just plain equivalent (MIPS). Change-Id: I7d6d200ea9746ec8978a0c1e1969dbc3580b9285 Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@digia.com> Reviewed-by: Lars Knoll <lars.knoll@digia.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/corelib/thread/qatomicint/tst_qatomicint.cpp')
-rw-r--r--tests/auto/corelib/thread/qatomicint/tst_qatomicint.cpp91
1 files changed, 91 insertions, 0 deletions
diff --git a/tests/auto/corelib/thread/qatomicint/tst_qatomicint.cpp b/tests/auto/corelib/thread/qatomicint/tst_qatomicint.cpp
index 42b3a52531..f0d817d37d 100644
--- a/tests/auto/corelib/thread/qatomicint/tst_qatomicint.cpp
+++ b/tests/auto/corelib/thread/qatomicint/tst_qatomicint.cpp
@@ -84,6 +84,8 @@ private slots:
void fetchAndAdd_data();
void fetchAndAdd();
+ void operators();
+
// stress tests
void testAndSet_loop();
void fetchAndAdd_loop();
@@ -540,6 +542,43 @@ void tst_QAtomicInt::testAndSet()
QAtomicInt atomic = value;
QTEST(atomic.testAndSetOrdered(expected, newval) ? 1 : 0, "result");
}
+
+#ifdef Q_ATOMIC_INT32_IS_SUPPORTED
+ QFETCH(int, result);
+ // the new implementation has the version that loads the current value
+
+ {
+ QAtomicInt atomic = value;
+ int currentval = 0xdeadbeef;
+ QCOMPARE(atomic.testAndSetRelaxed(expected, newval, currentval), result);
+ if (!result)
+ QCOMPARE(currentval, value);
+ }
+
+ {
+ QAtomicInt atomic = value;
+ int currentval = 0xdeadbeef;
+ QCOMPARE(atomic.testAndSetAcquire(expected, newval, currentval), result);
+ if (!result)
+ QCOMPARE(currentval, value);
+ }
+
+ {
+ QAtomicInt atomic = value;
+ int currentval = 0xdeadbeef;
+ QCOMPARE(atomic.testAndSetRelease(expected, newval, currentval), result);
+ if (!result)
+ QCOMPARE(currentval, value);
+ }
+
+ {
+ QAtomicInt atomic = value;
+ int currentval = 0xdeadbeef;
+ QCOMPARE(atomic.testAndSetOrdered(expected, newval, currentval), result);
+ if (!result)
+ QCOMPARE(currentval, value);
+ }
+#endif
}
void tst_QAtomicInt::isFetchAndStoreNative()
@@ -770,6 +809,58 @@ void tst_QAtomicInt::fetchAndAdd()
}
}
+void tst_QAtomicInt::operators()
+{
+ {
+ // Test that QBasicAtomicInt also has operator= and cast operators
+ // We've been using them for QAtomicInt elsewhere
+ QBasicAtomicInt atomic = Q_BASIC_ATOMIC_INITIALIZER(0);
+ atomic = 1;
+ QCOMPARE(int(atomic), 1);
+ }
+
+ QAtomicInt atomic = 0;
+ int x = ++atomic;
+ QCOMPARE(int(atomic), x);
+ QCOMPARE(int(atomic), 1);
+
+ x = atomic++;
+ QCOMPARE(int(atomic), x + 1);
+ QCOMPARE(int(atomic), 2);
+
+ x = atomic--;
+ QCOMPARE(int(atomic), x - 1);
+ QCOMPARE(int(atomic), 1);
+
+ x = --atomic;
+ QCOMPARE(int(atomic), x);
+ QCOMPARE(int(atomic), 0);
+
+ x = (atomic += 1);
+ QCOMPARE(int(atomic), x);
+ QCOMPARE(int(atomic), 1);
+
+ x = (atomic -= 1);
+ QCOMPARE(int(atomic), x);
+ QCOMPARE(int(atomic), 0);
+
+ x = (atomic |= 0xf);
+ QCOMPARE(int(atomic), x);
+ QCOMPARE(int(atomic), 0xf);
+
+ x = (atomic &= 0x17);
+ QCOMPARE(int(atomic), x);
+ QCOMPARE(int(atomic), 7);
+
+ x = (atomic ^= 0x14);
+ QCOMPARE(int(atomic), x);
+ QCOMPARE(int(atomic), 0x13);
+
+ x = (atomic ^= atomic);
+ QCOMPARE(int(atomic), x);
+ QCOMPARE(int(atomic), 0);
+}
+
void tst_QAtomicInt::testAndSet_loop()
{
QTime stopWatch;