summaryrefslogtreecommitdiffstats
path: root/src/corelib/thread/qatomic.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/thread/qatomic.cpp')
-rw-r--r--src/corelib/thread/qatomic.cpp226
1 files changed, 114 insertions, 112 deletions
diff --git a/src/corelib/thread/qatomic.cpp b/src/corelib/thread/qatomic.cpp
index d302da72eb..a437eb3319 100644
--- a/src/corelib/thread/qatomic.cpp
+++ b/src/corelib/thread/qatomic.cpp
@@ -1,42 +1,6 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Copyright (C) 2016 Intel Corporation.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtCore module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2016 The Qt Company Ltd.
+// Copyright (C) 2016 Intel Corporation.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qatomic.h"
@@ -67,19 +31,19 @@
The template parameter \c T must be a C++ integer type:
\list
- \li 8-bit: char, signed char, unsigned char, qint8, quint8
- \li 16-bit: short, unsigned short, qint16, quint16, char16_t (C++11)
- \li 32-bit: int, unsigned int, qint32, quint32, char32_t (C++11)
+ \li 8-bit: bool, char, signed char, unsigned char, qint8, quint8, char8_t (C++20)
+ \li 16-bit: short, unsigned short, qint16, quint16, char16_t
+ \li 32-bit: int, unsigned int, qint32, quint32, char32_t
\li 64-bit: long long, unsigned long long, qint64, quint64
\li platform-specific size: long, unsigned long
\li pointer size: qintptr, quintptr, qptrdiff
\endlist
- Of the list above, only the 32-bit- and pointer-sized instantiations are guaranteed to
- work on all platforms. Support for other sizes depends on the compiler and
- processor architecture the code is being compiled for. To test whether the
- other types are supported, check the macro \c Q_ATOMIC_INT\e{nn}_IS_SUPPORTED,
- where \c{\e{nn}} is the number of bits desired.
+ Of the list above, only the 8-bit, 16-bit, 32-bit- and pointer-sized
+ instantiations are guaranteed to work on all platforms. Support for other
+ sizes depends on the compiler and processor architecture the code is being
+ compiled for. To test whether the 64-bit types are supported on 32-bit
+ platforms, check the macro \c Q_ATOMIC_INT64_IS_SUPPORTED.
\section1 The Atomic API
@@ -261,19 +225,6 @@
/*!
- \fn template <typename T> T QAtomicInteger<T>::load() const
- \obsolete
-
- Use loadRelaxed() instead.
-
- Atomically loads the value of this QAtomicInteger using relaxed memory
- ordering. The value is not modified in any way, but note that there's no
- guarantee that it remains so.
-
- \sa storeRelaxed(), loadAcquire()
-*/
-
-/*!
\fn template <typename T> T QAtomicInteger<T>::loadRelaxed() const
\since 5.14
@@ -295,18 +246,6 @@
*/
/*!
- \fn template <typename T> void QAtomicInteger<T>::store(T newValue)
- \obsolete
-
- Use storeRelaxed() instead.
-
- Atomically stores the \a newValue value into this atomic type, using
- relaxed memory ordering.
-
- \sa storeRelease(), loadRelaxed()
-*/
-
-/*!
\fn template <typename T> void QAtomicInteger<T>::storeRelaxed(T newValue)
\since 5.14
@@ -322,7 +261,7 @@
Atomically stores the \a newValue value into this atomic type, using
the "Release" memory ordering.
- \sa store(), loadAcquire()
+ \sa storeRelaxed(), loadAcquire()
*/
/*!
@@ -458,14 +397,21 @@
Atomic test-and-set.
+ \note If you use this function in a loop, consider using the overload with the
+ additional \c{T &currentValue} argument instead, which avoids the extra load() on
+ failure.
+
If the current value of this QAtomicInteger is the \a expectedValue,
the test-and-set functions assign the \a newValue to this
QAtomicInteger and return true. If the values are \e not the same,
this function does nothing and returns \c false.
+//![memory-order-relaxed]
This function uses \e relaxed \l {QAtomicInteger#Memory
ordering}{memory ordering} semantics, leaving the compiler and
processor to freely reorder memory accesses.
+//![memory-order-relaxed]
+
*/
/*!
@@ -473,15 +419,21 @@
Atomic test-and-set.
+ \note If you use this function in a loop, consider using the overload with the
+ additional \c{T &currentValue} argument instead, which avoids the extra load() on
+ failure.
+
If the current value of this QAtomicInteger is the \a expectedValue,
the test-and-set functions assign the \a newValue to this
QAtomicInteger and return true. If the values are \e not the same,
this function does nothing and returns \c false.
+//![memory-order-acquire]
This function uses \e acquire \l {QAtomicInteger#Memory
ordering}{memory ordering} semantics, which ensures that memory
access following the atomic operation (in program order) may not
be re-ordered before the atomic operation.
+//![memory-order-acquire]
*/
/*!
@@ -489,15 +441,21 @@
Atomic test-and-set.
+ \note If you use this function in a loop, consider using the overload with the
+ additional \c{T &currentValue} argument instead, which avoids the extra load() on
+ failure.
+
If the current value of this QAtomicInteger is the \a expectedValue,
the test-and-set functions assign the \a newValue to this
QAtomicInteger and return true. If the values are \e not the same,
this function does nothing and returns \c false.
+//![memory-order-release]
This function uses \e release \l {QAtomicInteger#Memory
ordering}{memory ordering} semantics, which ensures that memory
access before the atomic operation (in program order) may not be
re-ordered after the atomic operation.
+//![memory-order-release]
*/
/*!
@@ -505,15 +463,78 @@
Atomic test-and-set.
+ \note If you use this function in a loop, consider using the overload with the
+ additional \c{T &currentValue} argument instead, which avoids the extra load() on
+ failure.
+
If the current value of this QAtomicInteger is the \a expectedValue,
the test-and-set functions assign the \a newValue to this
QAtomicInteger and return true. If the values are \e not the same,
this function does nothing and returns \c false.
+//![memory-order-ordered]
This function uses \e ordered \l {QAtomicInteger#Memory
ordering}{memory ordering} semantics, which ensures that memory
access before and after the atomic operation (in program order)
may not be re-ordered.
+//![memory-order-ordered]
+
+*/
+
+/*!
+ \fn template <typename T> bool QAtomicInteger<T>::testAndSetRelaxed(T expectedValue, T newValue, T &currentValue)
+ \since 5.3
+
+ Atomic test-and-set.
+
+ If the current value of this QAtomicInteger is the \a expectedValue, the
+ test-and-set functions assign the \a newValue to this QAtomicInteger and
+ return \c true. If the values are \e not the same, the functions load the
+ current value of this QAtomicInteger into \a currentValue and return \c false.
+
+ \include qatomic.cpp memory-order-relaxed
+*/
+
+/*!
+ \fn template <typename T> bool QAtomicInteger<T>::testAndSetAcquire(T expectedValue, T newValue, T &currentValue)
+ \since 5.3
+
+ Atomic test-and-set.
+
+ If the current value of this QAtomicInteger is the \a expectedValue, the
+ test-and-set functions assign the \a newValue to this QAtomicInteger and
+ return \c true. If the values are \e not the same, the functions load the
+ current value of this QAtomicInteger into \a currentValue and return \c false.
+
+ \include qatomic.cpp memory-order-acquire
+*/
+
+/*!
+ \fn template <typename T> bool QAtomicInteger<T>::testAndSetRelease(T expectedValue, T newValue, T &currentValue)
+ \since 5.3
+
+ Atomic test-and-set.
+
+ If the current value of this QAtomicInteger is the \a expectedValue, the
+ test-and-set functions assign the \a newValue to this QAtomicInteger and
+ return \c true. If the values are \e not the same, the functions loads the
+ current value of this QAtomicInteger into \a currentValue and return \c false.
+
+ \include qatomic.cpp memory-order-release
+*/
+
+/*!
+ \fn template <typename T> bool QAtomicInteger<T>::testAndSetOrdered(T expectedValue, T newValue, T &currentValue)
+ \since 5.3
+
+ Atomic test-and-set.
+
+ If the current value of this QAtomicInteger is the \a expectedValue, the
+ test-and-set functions assign the \a newValue to this QAtomicInteger and
+ return \c true. If the values are \e not the same, it loads the current
+ value of this QAtomicInteger into \a currentValue and return \c false.
+
+ \include qatomic.cpp memory-order-ordered
*/
/*!
@@ -1011,9 +1032,16 @@
This macro is defined if atomic integers of size \e{nn} (in bits) are
supported in this compiler / architecture combination.
- Q_ATOMIC_INT32_IS_SUPPORTED is always defined.
\e{nn} is the size of the integer, in bits (8, 16, 32 or 64).
+
+ The following macros always defined:
+
+ \list
+ \li Q_ATOMIC_INT8_IS_SUPPORTED
+ \li Q_ATOMIC_INT16_IS_SUPPORTED
+ \li Q_ATOMIC_INT32_IS_SUPPORTED
+ \endlist
*/
/*!
@@ -1331,7 +1359,7 @@
\endlist
- \sa QAtomicInteger
+ \sa QAtomicInteger, qYieldCpu()
*/
/*!
@@ -1354,19 +1382,6 @@
*/
/*!
- \fn template <typename T> T *QAtomicPointer<T>::load() const
- \obsolete
-
- Use loadRelaxed() instead.
-
- Atomically loads the value of this QAtomicPointer using relaxed memory
- ordering. The value is not modified in any way, but note that there's no
- guarantee that it remains so.
-
- \sa storeRelaxed(), loadAcquire()
-*/
-
-/*!
\fn template <typename T> T *QAtomicPointer<T>::loadRelaxed() const
\since 5.14
@@ -1389,18 +1404,6 @@
*/
/*!
- \fn template <typename T> void QAtomicPointer<T>::store(T *newValue)
- \obsolete
-
- Use storeRelaxed() instead.
-
- Atomically stores the \a newValue value into this atomic type, using
- relaxed memory ordering.
-
- \sa storeRelease(), loadRelaxed()
-*/
-
-/*!
\fn template <typename T> void QAtomicPointer<T>::storeRelaxed(T *newValue)
\since 5.14
@@ -1739,6 +1742,12 @@
*/
// static checks
+#ifndef Q_ATOMIC_INT8_IS_SUPPORTED
+# error "Q_ATOMIC_INT8_IS_SUPPORTED must be defined"
+#endif
+#ifndef Q_ATOMIC_INT16_IS_SUPPORTED
+# error "Q_ATOMIC_INT16_IS_SUPPORTED must be defined"
+#endif
#ifndef Q_ATOMIC_INT32_IS_SUPPORTED
# error "Q_ATOMIC_INT32_IS_SUPPORTED must be defined"
#endif
@@ -1755,28 +1764,21 @@ static_assert(sizeof(QAtomicInteger<long>));
static_assert(sizeof(QAtomicInteger<unsigned long>));
static_assert(sizeof(QAtomicInteger<quintptr>));
static_assert(sizeof(QAtomicInteger<qptrdiff>));
-#ifdef Q_COMPILER_UNICODE_STRINGS
static_assert(sizeof(QAtomicInteger<char32_t>));
-#endif
-#ifdef Q_ATOMIC_INT16_IS_SUPPORTED
static_assert(sizeof(QAtomicInteger<short>));
static_assert(sizeof(QAtomicInteger<unsigned short>));
-# if WCHAR_MAX < 0x10000
static_assert(sizeof(QAtomicInteger<wchar_t>));
-# endif
-# ifdef Q_COMPILER_UNICODE_STRINGS
static_assert(sizeof(QAtomicInteger<char16_t>));
-# endif
-#endif
+
+static_assert(sizeof(QAtomicInteger<char>));
+static_assert(sizeof(QAtomicInteger<unsigned char>));
+static_assert(sizeof(QAtomicInteger<signed char>));
+static_assert(sizeof(QAtomicInteger<bool>));
#ifdef Q_ATOMIC_INT64_IS_SUPPORTED
static_assert(sizeof(QAtomicInteger<qint64>));
static_assert(sizeof(QAtomicInteger<quint64>));
#endif
-#if WCHAR_MAX == INT_MAX
-static_assert(sizeof(QAtomicInteger<wchar_t>));
-#endif
-
QT_END_NAMESPACE