Revert "QAtomic: remove the copy ctor and assignment operator"

This reverts commit 6a93ec2435.

Reason for revert: Breaks qtdeclarative build, submodules need
to be clean before we deprecate or remove APIs.

Change-Id: Id0726b9bfad6072065b380b44b6ff6dffda79e45
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
This commit is contained in:
Volker Hilsheimer 2023-10-25 07:53:45 +00:00
parent 1af821825a
commit 342b37f388
6 changed files with 157 additions and 7 deletions

View File

@ -210,6 +210,20 @@
Constructs a QAtomicInteger with the given \a value.
*/
/*!
\fn template <typename T> QAtomicInteger<T>::QAtomicInteger(const QAtomicInteger &other)
Constructs a copy of \a other.
*/
/*!
\fn template <typename T> QAtomicInteger &QAtomicInteger<T>::operator=(const QAtomicInteger &other)
Assigns \a other to this QAtomicInteger and returns a reference to
this QAtomicInteger.
*/
/*!
\fn template <typename T> T QAtomicInteger<T>::loadRelaxed() const
\since 5.14
@ -1354,6 +1368,19 @@
Constructs a QAtomicPointer with the given \a value.
*/
/*!
\fn template <typename T> QAtomicPointer<T>::QAtomicPointer(const QAtomicPointer<T> &other)
Constructs a copy of \a other.
*/
/*!
\fn template <typename T> QAtomicPointer &QAtomicPointer<T>::operator=(const QAtomicPointer &other)
Assigns \a other to this QAtomicPointer and returns a reference to
this QAtomicPointer.
*/
/*!
\fn template <typename T> T *QAtomicPointer<T>::loadRelaxed() const
\since 5.14

View File

@ -17,8 +17,20 @@ template <typename T>
class QAtomicInteger : public QBasicAtomicInteger<T>
{
public:
// Non-atomic API
constexpr QAtomicInteger(T value = 0) noexcept : QBasicAtomicInteger<T>(value) {}
using QBasicAtomicInteger<T>::operator=;
inline QAtomicInteger(const QAtomicInteger &other) noexcept
: QBasicAtomicInteger<T>()
{
this->storeRelease(other.loadAcquire());
}
inline QAtomicInteger &operator=(const QAtomicInteger &other) noexcept
{
this->storeRelease(other.loadAcquire());
return *this;
}
#ifdef Q_QDOC
T loadRelaxed() const;
@ -96,12 +108,13 @@ public:
#endif
};
// ### Qt 7: make QAtomicInt a typedef
class QAtomicInt : public QAtomicInteger<int>
{
public:
using QAtomicInteger<int>::QAtomicInteger;
using QAtomicInteger<int>::operator=;
// Non-atomic API
// We could use QT_COMPILER_INHERITING_CONSTRUCTORS, but we need only one;
// the implicit definition for all the others is fine.
constexpr QAtomicInt(int value = 0) noexcept : QAtomicInteger<int>(value) {}
};
// High-level atomic pointer operations
@ -110,7 +123,18 @@ class QAtomicPointer : public QBasicAtomicPointer<T>
{
public:
constexpr QAtomicPointer(T *value = nullptr) noexcept : QBasicAtomicPointer<T>(value) {}
using QBasicAtomicPointer<T>::operator=;
inline QAtomicPointer(const QAtomicPointer<T> &other) noexcept
: QBasicAtomicPointer<T>()
{
this->storeRelease(other.loadAcquire());
}
inline QAtomicPointer<T> &operator=(const QAtomicPointer<T> &other) noexcept
{
this->storeRelease(other.loadAcquire());
return *this;
}
#ifdef Q_QDOC
T *loadAcquire() const;

View File

@ -39,7 +39,7 @@ public:
T loadAcquire() const noexcept { return Ops::loadAcquire(_q_value); }
void storeRelease(T newValue) noexcept { Ops::storeRelease(_q_value, newValue); }
operator T() const noexcept { return loadAcquire(); }
QBasicAtomicInteger &operator=(T newValue) noexcept { storeRelease(newValue); return *this; }
T operator=(T newValue) noexcept { storeRelease(newValue); return newValue; }
static constexpr bool isReferenceCountingNative() noexcept { return Ops::isReferenceCountingNative(); }
static constexpr bool isReferenceCountingWaitFree() noexcept { return Ops::isReferenceCountingWaitFree(); }
@ -171,7 +171,7 @@ public:
void storeRelaxed(Type newValue) noexcept { Ops::storeRelaxed(_q_value, newValue); }
operator Type() const noexcept { return loadAcquire(); }
QBasicAtomicPointer &operator=(Type newValue) noexcept { storeRelease(newValue); return *this; }
Type operator=(Type newValue) noexcept { storeRelease(newValue); return newValue; }
// Atomic API, implemented in qatomic_XXX.h
Type loadAcquire() const noexcept { return Ops::loadAcquire(_q_value); }

View File

@ -21,6 +21,8 @@ private slots:
// QAtomicInt members
void constructor_data();
void constructor();
void copy_constructor_data();
void copy_constructor();
void assignment_operator_data();
void assignment_operator();
@ -234,6 +236,25 @@ void tst_QAtomicInt::constructor()
QCOMPARE(atomic2.loadRelaxed(), value);
}
void tst_QAtomicInt::copy_constructor_data()
{ constructor_data(); }
void tst_QAtomicInt::copy_constructor()
{
QFETCH(int, value);
QAtomicInt atomic1(value);
QCOMPARE(atomic1.loadRelaxed(), value);
QAtomicInt atomic2(atomic1);
QCOMPARE(atomic2.loadRelaxed(), value);
QAtomicInt atomic3 = atomic1;
QCOMPARE(atomic3.loadRelaxed(), value);
QAtomicInt atomic4(atomic2);
QCOMPARE(atomic4.loadRelaxed(), value);
QAtomicInt atomic5 = atomic2;
QCOMPARE(atomic5.loadRelaxed(), value);
}
void tst_QAtomicInt::assignment_operator_data()
{
QTest::addColumn<int>("value");
@ -258,6 +279,10 @@ void tst_QAtomicInt::assignment_operator()
QCOMPARE(atomic1.loadRelaxed(), newval);
atomic1 = value;
QCOMPARE(atomic1.loadRelaxed(), value);
QAtomicInt atomic2 = newval;
atomic1 = atomic2;
QCOMPARE(atomic1.loadRelaxed(), atomic2.loadRelaxed());
}
}

View File

@ -90,6 +90,12 @@ private Q_SLOTS:
void constructor_data() { addData(); }
void constructor();
void copy_data() { addData(); }
void copy();
void assign_data() { addData(); }
void assign();
void operatorInteger_data() { addData(); }
void operatorInteger();
@ -213,6 +219,52 @@ void tst_QAtomicIntegerXX::constructor()
QVERIFY(atomic.loadRelaxed() <= std::numeric_limits<T>::max());
}
void tst_QAtomicIntegerXX::copy()
{
QFETCH(LargeInt, value);
QAtomicInteger<T> atomic(value);
QAtomicInteger<T> copy(atomic);
QCOMPARE(copy.loadRelaxed(), atomic.loadRelaxed());
QAtomicInteger<T> copy2 = atomic;
QCOMPARE(copy2.loadRelaxed(), atomic.loadRelaxed());
// move
QAtomicInteger<T> copy3(std::move(copy));
QCOMPARE(copy3.loadRelaxed(), atomic.loadRelaxed());
QAtomicInteger<T> copy4 = std::move(copy2);
QCOMPARE(copy4.loadRelaxed(), atomic.loadRelaxed());
}
void tst_QAtomicIntegerXX::assign()
{
QFETCH(LargeInt, value);
QAtomicInteger<T> atomic(value);
QAtomicInteger<T> copy;
copy = atomic;
QCOMPARE(copy.loadRelaxed(), atomic.loadRelaxed());
QAtomicInteger<T> copy2;
copy2 = atomic; // operator=(const QAtomicInteger &)
QCOMPARE(copy2.loadRelaxed(), atomic.loadRelaxed());
QAtomicInteger<T> copy2bis;
copy2bis = atomic.loadRelaxed(); // operator=(T)
QCOMPARE(copy2bis.loadRelaxed(), atomic.loadRelaxed());
// move
QAtomicInteger<T> copy3;
copy3 = std::move(copy);
QCOMPARE(copy3.loadRelaxed(), atomic.loadRelaxed());
QAtomicInteger<T> copy4;
copy4 = std::move(copy2);
QCOMPARE(copy4.loadRelaxed(), atomic.loadRelaxed());
}
void tst_QAtomicIntegerXX::operatorInteger()
{
QFETCH(LargeInt, value);

View File

@ -15,6 +15,7 @@ private slots:
void alignment();
void constructor();
void copy_constructor();
void assignment_operator();
void isTestAndSetNative();
@ -100,6 +101,27 @@ void tst_QAtomicPointer::constructor()
QCOMPARE(atomic3.loadRelaxed(), three);
}
void tst_QAtomicPointer::copy_constructor()
{
void *one = this;
QAtomicPointer<void> atomic1 = one;
QAtomicPointer<void> atomic1_copy = atomic1;
QCOMPARE(atomic1_copy.loadRelaxed(), one);
QCOMPARE(atomic1_copy.loadRelaxed(), atomic1.loadRelaxed());
void *two = &one;
QAtomicPointer<void> atomic2 = two;
QAtomicPointer<void> atomic2_copy = atomic2;
QCOMPARE(atomic2_copy.loadRelaxed(), two);
QCOMPARE(atomic2_copy.loadRelaxed(), atomic2.loadRelaxed());
void *three = &two;
QAtomicPointer<void> atomic3 = three;
QAtomicPointer<void> atomic3_copy = atomic3;
QCOMPARE(atomic3_copy.loadRelaxed(), three);
QCOMPARE(atomic3_copy.loadRelaxed(), atomic3.loadRelaxed());
}
void tst_QAtomicPointer::assignment_operator()
{
void *one = this;