diff options
Diffstat (limited to 'src/corelib/thread/qatomic.cpp')
-rw-r--r-- | src/corelib/thread/qatomic.cpp | 128 |
1 files changed, 110 insertions, 18 deletions
diff --git a/src/corelib/thread/qatomic.cpp b/src/corelib/thread/qatomic.cpp index 90441eb0f3..a437eb3319 100644 --- a/src/corelib/thread/qatomic.cpp +++ b/src/corelib/thread/qatomic.cpp @@ -31,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 @@ -397,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 ¤tValue} 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] + */ /*! @@ -412,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 ¤tValue} 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] */ /*! @@ -428,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 ¤tValue} 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] */ /*! @@ -444,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 ¤tValue} 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 ¤tValue) + \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 ¤tValue) + \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 ¤tValue) + \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 ¤tValue) + \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 */ /*! @@ -950,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 */ /*! @@ -1270,7 +1359,7 @@ \endlist - \sa QAtomicInteger + \sa QAtomicInteger, qYieldCpu() */ /*! @@ -1653,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 @@ -1671,22 +1766,19 @@ static_assert(sizeof(QAtomicInteger<quintptr>)); static_assert(sizeof(QAtomicInteger<qptrdiff>)); static_assert(sizeof(QAtomicInteger<char32_t>)); -#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 static_assert(sizeof(QAtomicInteger<char16_t>)); -#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 |