summaryrefslogtreecommitdiffstats
path: root/src/corelib/arch/qatomic_armv5.h
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 /src/corelib/arch/qatomic_armv5.h
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 'src/corelib/arch/qatomic_armv5.h')
-rw-r--r--src/corelib/arch/qatomic_armv5.h7
1 files changed, 5 insertions, 2 deletions
diff --git a/src/corelib/arch/qatomic_armv5.h b/src/corelib/arch/qatomic_armv5.h
index 237ff5f254..6939650c54 100644
--- a/src/corelib/arch/qatomic_armv5.h
+++ b/src/corelib/arch/qatomic_armv5.h
@@ -133,13 +133,16 @@ bool QBasicAtomicOps<4>::deref(T &_q_value) Q_DECL_NOTHROW
}
template<> template <typename T> inline
-bool QBasicAtomicOps<4>::testAndSetRelaxed(T &_q_value, T expectedValue, T newValue) Q_DECL_NOTHROW
+bool QBasicAtomicOps<4>::testAndSetRelaxed(T &_q_value, T expectedValue, T newValue, T *currentValue) Q_DECL_NOTHROW
{
T originalValue;
do {
originalValue = _q_value;
- if (originalValue != expectedValue)
+ if (originalValue != expectedValue) {
+ if (currentValue)
+ *currentValue = originalValue;
return false;
+ }
} while (_q_cmpxchg(expectedValue, newValue, &_q_value) != 0);
return true;
}