Make QVector instances counter thread safe

Change-Id: I7c7aa1eb0f8e91c43023882a3734e908be4ba4fe
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Jędrzej Nowacki 2013-02-12 12:32:29 +01:00 committed by The Qt Project
parent 3fdfde294f
commit d3a4230757

View File

@ -40,6 +40,7 @@
****************************************************************************/
#include <QtTest/QtTest>
#include <QAtomicInt>
#include <qvector.h>
struct Movable {
@ -47,21 +48,21 @@ struct Movable {
: i(input)
, state(Constructed)
{
++counter;
counter.fetchAndAddRelaxed(1);
}
Movable(const Movable &other)
: i(other.i)
, state(Constructed)
{
check(other.state, Constructed);
++counter;
counter.fetchAndAddRelaxed(1);
}
~Movable()
{
check(state, Constructed);
i = 0;
--counter;
counter.fetchAndAddRelaxed(-1);
state = Destructed;
}
@ -80,7 +81,7 @@ struct Movable {
return *this;
}
char i;
static int counter;
static QAtomicInt counter;
private:
enum State { Constructed = 106, Destructed = 110 };
State state;
@ -91,7 +92,7 @@ private:
}
};
int Movable::counter = 0;
QAtomicInt Movable::counter = 0;
QT_BEGIN_NAMESPACE
Q_DECLARE_TYPEINFO(Movable, Q_MOVABLE_TYPE);
QT_END_NAMESPACE
@ -103,21 +104,21 @@ struct Custom {
, that(this)
, state(Constructed)
{
++counter;
counter.fetchAndAddRelaxed(1);
}
Custom(const Custom &other)
: that(this)
, state(Constructed)
{
check(&other);
++counter;
counter.fetchAndAddRelaxed(1);
this->i = other.i;
}
~Custom()
{
check(this);
i = 0;
--counter;
counter.fetchAndAddRelaxed(-1);
state = Destructed;
}
@ -135,7 +136,7 @@ struct Custom {
i = other.i;
return *this;
}
static int counter;
static QAtomicInt counter;
char i; // used to identify orgin of an instance
private:
@ -151,7 +152,7 @@ private:
QCOMPARE(c->state, Constructed);
}
};
int Custom::counter = 0;
QAtomicInt Custom::counter = 0;
Q_DECLARE_METATYPE(Custom);
@ -415,16 +416,16 @@ void tst_QVector::copyConstructorInt() const
void tst_QVector::copyConstructorMovable() const
{
const int instancesCount = Movable::counter;
const int instancesCount = Movable::counter.loadAcquire();
copyConstructor<Movable>();
QCOMPARE(instancesCount, Movable::counter);
QCOMPARE(instancesCount, Movable::counter.loadAcquire());
}
void tst_QVector::copyConstructorCustom() const
{
const int instancesCount = Custom::counter;
const int instancesCount = Custom::counter.loadAcquire();
copyConstructor<Custom>();
QCOMPARE(instancesCount, Custom::counter);
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
}
template<typename T>
@ -465,16 +466,16 @@ void tst_QVector::addInt() const
void tst_QVector::addMovable() const
{
const int instancesCount = Movable::counter;
const int instancesCount = Movable::counter.loadAcquire();
add<Movable>();
QCOMPARE(instancesCount, Movable::counter);
QCOMPARE(instancesCount, Movable::counter.loadAcquire());
}
void tst_QVector::addCustom() const
{
const int instancesCount = Custom::counter;
const int instancesCount = Custom::counter.loadAcquire();
add<Custom>();
QCOMPARE(instancesCount, Custom::counter);
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
}
template<typename T>
@ -523,16 +524,16 @@ void tst_QVector::appendInt() const
void tst_QVector::appendMovable() const
{
const int instancesCount = Movable::counter;
const int instancesCount = Movable::counter.loadAcquire();
append<Movable>();
QCOMPARE(instancesCount, Movable::counter);
QCOMPARE(instancesCount, Movable::counter.loadAcquire());
}
void tst_QVector::appendCustom() const
{
const int instancesCount = Custom::counter;
const int instancesCount = Custom::counter.loadAcquire();
append<Custom>();
QCOMPARE(instancesCount, Custom::counter);
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
}
void tst_QVector::at() const
@ -602,16 +603,16 @@ void tst_QVector::capacityInt() const
void tst_QVector::capacityMovable() const
{
const int instancesCount = Movable::counter;
const int instancesCount = Movable::counter.loadAcquire();
capacity<Movable>();
QCOMPARE(instancesCount, Movable::counter);
QCOMPARE(instancesCount, Movable::counter.loadAcquire());
}
void tst_QVector::capacityCustom() const
{
const int instancesCount = Custom::counter;
const int instancesCount = Custom::counter.loadAcquire();
capacity<Custom>();
QCOMPARE(instancesCount, Custom::counter);
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
}
template<typename T>
@ -633,16 +634,16 @@ void tst_QVector::clearInt() const
void tst_QVector::clearMovable() const
{
const int instancesCount = Movable::counter;
const int instancesCount = Movable::counter.loadAcquire();
clear<Movable>();
QCOMPARE(instancesCount, Movable::counter);
QCOMPARE(instancesCount, Movable::counter.loadAcquire());
}
void tst_QVector::clearCustom() const
{
const int instancesCount = Custom::counter;
const int instancesCount = Custom::counter.loadAcquire();
clear<Custom>();
QCOMPARE(instancesCount, Custom::counter);
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
}
void tst_QVector::constData() const
@ -717,16 +718,16 @@ void tst_QVector::countInt() const
void tst_QVector::countMovable() const
{
const int instancesCount = Movable::counter;
const int instancesCount = Movable::counter.loadAcquire();
count<Movable>();
QCOMPARE(instancesCount, Movable::counter);
QCOMPARE(instancesCount, Movable::counter.loadAcquire());
}
void tst_QVector::countCustom() const
{
const int instancesCount = Custom::counter;
const int instancesCount = Custom::counter.loadAcquire();
count<Custom>();
QCOMPARE(instancesCount, Custom::counter);
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
}
void tst_QVector::data() const
@ -771,16 +772,16 @@ void tst_QVector::emptyInt() const
void tst_QVector::emptyMovable() const
{
const int instancesCount = Movable::counter;
const int instancesCount = Movable::counter.loadAcquire();
empty<Movable>();
QCOMPARE(instancesCount, Movable::counter);
QCOMPARE(instancesCount, Movable::counter.loadAcquire());
}
void tst_QVector::emptyCustom() const
{
const int instancesCount = Custom::counter;
const int instancesCount = Custom::counter.loadAcquire();
empty<Custom>();
QCOMPARE(instancesCount, Custom::counter);
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
}
void tst_QVector::endsWith() const
@ -826,16 +827,16 @@ void tst_QVector::eraseEmptyInt() const
void tst_QVector::eraseEmptyMovable() const
{
const int instancesCount = Movable::counter;
const int instancesCount = Movable::counter.loadAcquire();
eraseEmpty<Movable>();
QCOMPARE(instancesCount, Movable::counter);
QCOMPARE(instancesCount, Movable::counter.loadAcquire());
}
void tst_QVector::eraseEmptyCustom() const
{
const int instancesCount = Custom::counter;
const int instancesCount = Custom::counter.loadAcquire();
eraseEmpty<Custom>();
QCOMPARE(instancesCount, Custom::counter);
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
}
template<typename T>
@ -863,16 +864,16 @@ void tst_QVector::eraseEmptyReservedInt() const
void tst_QVector::eraseEmptyReservedMovable() const
{
const int instancesCount = Movable::counter;
const int instancesCount = Movable::counter.loadAcquire();
eraseEmptyReserved<Movable>();
QCOMPARE(instancesCount, Movable::counter);
QCOMPARE(instancesCount, Movable::counter.loadAcquire());
}
void tst_QVector::eraseEmptyReservedCustom() const
{
const int instancesCount = Custom::counter;
const int instancesCount = Custom::counter.loadAcquire();
eraseEmptyReserved<Custom>();
QCOMPARE(instancesCount, Custom::counter);
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
}
template<typename T>
@ -986,30 +987,30 @@ void tst_QVector::eraseIntShared() const
void tst_QVector::eraseMovable() const
{
const int instancesCount = Movable::counter;
const int instancesCount = Movable::counter.loadAcquire();
erase<Movable>(false);
QCOMPARE(instancesCount, Movable::counter);
QCOMPARE(instancesCount, Movable::counter.loadAcquire());
}
void tst_QVector::eraseMovableShared() const
{
const int instancesCount = Movable::counter;
const int instancesCount = Movable::counter.loadAcquire();
erase<Movable>(true);
QCOMPARE(instancesCount, Movable::counter);
QCOMPARE(instancesCount, Movable::counter.loadAcquire());
}
void tst_QVector::eraseCustom() const
{
const int instancesCount = Custom::counter;
const int instancesCount = Custom::counter.loadAcquire();
erase<Custom>(false);
QCOMPARE(instancesCount, Custom::counter);
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
}
void tst_QVector::eraseCustomShared() const
{
const int instancesCount = Custom::counter;
const int instancesCount = Custom::counter.loadAcquire();
erase<Custom>(true);
QCOMPARE(instancesCount, Custom::counter);
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
}
template<typename T> void tst_QVector::eraseReserved() const
@ -1062,16 +1063,16 @@ void tst_QVector::eraseReservedInt() const
void tst_QVector::eraseReservedMovable() const
{
const int instancesCount = Movable::counter;
const int instancesCount = Movable::counter.loadAcquire();
eraseReserved<Movable>();
QCOMPARE(instancesCount, Movable::counter);
QCOMPARE(instancesCount, Movable::counter.loadAcquire());
}
void tst_QVector::eraseReservedCustom() const
{
const int instancesCount = Custom::counter;
const int instancesCount = Custom::counter.loadAcquire();
eraseReserved<Custom>();
QCOMPARE(instancesCount, Custom::counter);
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
}
template<typename T>
@ -1102,16 +1103,16 @@ void tst_QVector::fillInt() const
void tst_QVector::fillMovable() const
{
const int instancesCount = Movable::counter;
const int instancesCount = Movable::counter.loadAcquire();
fill<Movable>();
QCOMPARE(instancesCount, Movable::counter);
QCOMPARE(instancesCount, Movable::counter.loadAcquire());
}
void tst_QVector::fillCustom() const
{
const int instancesCount = Custom::counter;
const int instancesCount = Custom::counter.loadAcquire();
fill<Custom>();
QCOMPARE(instancesCount, Custom::counter);
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
}
void tst_QVector::first() const
@ -1152,16 +1153,16 @@ void tst_QVector::fromListInt() const
void tst_QVector::fromListMovable() const
{
const int instancesCount = Movable::counter;
const int instancesCount = Movable::counter.loadAcquire();
fromList<Movable>();
QCOMPARE(instancesCount, Movable::counter);
QCOMPARE(instancesCount, Movable::counter.loadAcquire());
}
void tst_QVector::fromListCustom() const
{
const int instancesCount = Custom::counter;
const int instancesCount = Custom::counter.loadAcquire();
fromList<Custom>();
QCOMPARE(instancesCount, Custom::counter);
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
}
void tst_QVector::fromStdVector() const
@ -1338,16 +1339,16 @@ void tst_QVector::prependInt() const
void tst_QVector::prependMovable() const
{
const int instancesCount = Movable::counter;
const int instancesCount = Movable::counter.loadAcquire();
prepend<Movable>();
QCOMPARE(instancesCount, Movable::counter);
QCOMPARE(instancesCount, Movable::counter.loadAcquire());
}
void tst_QVector::prependCustom() const
{
const int instancesCount = Custom::counter;
const int instancesCount = Custom::counter.loadAcquire();
prepend<Custom>();
QCOMPARE(instancesCount, Custom::counter);
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
}
template<typename T>
@ -1374,16 +1375,16 @@ void tst_QVector::removeInt() const
void tst_QVector::removeMovable() const
{
const int instancesCount = Movable::counter;
const int instancesCount = Movable::counter.loadAcquire();
remove<Movable>();
QCOMPARE(instancesCount, Movable::counter);
QCOMPARE(instancesCount, Movable::counter.loadAcquire());
}
void tst_QVector::removeCustom() const
{
const int instancesCount = Custom::counter;
const int instancesCount = Custom::counter.loadAcquire();
remove<Custom>();
QCOMPARE(instancesCount, Custom::counter);
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
}
void tst_QVector::resizePOD_data() const
@ -1510,7 +1511,7 @@ void tst_QVector::resizeComplexMovable_data() const
void tst_QVector::resizeComplexMovable() const
{
const int items = Movable::counter;
const int items = Movable::counter.loadAcquire();
{
QFETCH(QVector<Movable>, vector);
QFETCH(int, size);
@ -1529,7 +1530,7 @@ void tst_QVector::resizeComplexMovable() const
QCOMPARE(vector.size(), 0);
QVERIFY(vector.capacity() <= capacity);
}
QCOMPARE(items, Movable::counter);
QCOMPARE(items, Movable::counter.loadAcquire());
}
void tst_QVector::resizeComplex_data() const
@ -1585,7 +1586,7 @@ void tst_QVector::resizeComplex_data() const
void tst_QVector::resizeComplex() const
{
const int items = Custom::counter;
const int items = Custom::counter.loadAcquire();
{
QFETCH(QVector<Custom>, vector);
QFETCH(int, size);
@ -1604,12 +1605,12 @@ void tst_QVector::resizeComplex() const
QVERIFY(vector.isEmpty());
QVERIFY(vector.capacity() <= capacity);
}
QCOMPARE(Custom::counter, items);
QCOMPARE(Custom::counter.loadAcquire(), items);
}
void tst_QVector::resizeCtorAndDtor() const
{
const int items = Custom::counter;
const int items = Custom::counter.loadAcquire();
{
QVector<Custom> null;
QVector<Custom> empty(0, '0');
@ -1631,7 +1632,7 @@ void tst_QVector::resizeCtorAndDtor() const
nonEmpty.resize(0);
nonEmptyReserved.resize(2);
}
QCOMPARE(Custom::counter, items);
QCOMPARE(Custom::counter.loadAcquire(), items);
}
template<typename T>
@ -1661,16 +1662,16 @@ void tst_QVector::sizeInt() const
void tst_QVector::sizeMovable() const
{
const int instancesCount = Movable::counter;
const int instancesCount = Movable::counter.loadAcquire();
size<Movable>();
QCOMPARE(instancesCount, Movable::counter);
QCOMPARE(instancesCount, Movable::counter.loadAcquire());
}
void tst_QVector::sizeCustom() const
{
const int instancesCount = Custom::counter;
const int instancesCount = Custom::counter.loadAcquire();
size<Custom>();
QCOMPARE(instancesCount, Custom::counter);
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
}
// ::squeeze() is tested in ::capacity().
@ -1720,16 +1721,16 @@ void tst_QVector::swapInt() const
void tst_QVector::swapMovable() const
{
const int instancesCount = Movable::counter;
const int instancesCount = Movable::counter.loadAcquire();
swap<Movable>();
QCOMPARE(instancesCount, Movable::counter);
QCOMPARE(instancesCount, Movable::counter.loadAcquire());
}
void tst_QVector::swapCustom() const
{
const int instancesCount = Custom::counter;
const int instancesCount = Custom::counter.loadAcquire();
swap<Custom>();
QCOMPARE(instancesCount, Custom::counter);
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
}
void tst_QVector::toList() const
@ -1947,16 +1948,16 @@ void tst_QVector::initializeListInt()
void tst_QVector::initializeListMovable()
{
const int instancesCount = Movable::counter;
const int instancesCount = Movable::counter.loadAcquire();
initializeList<Movable>();
QCOMPARE(instancesCount, Movable::counter);
QCOMPARE(instancesCount, Movable::counter.loadAcquire());
}
void tst_QVector::initializeListCustom()
{
const int instancesCount = Custom::counter;
const int instancesCount = Custom::counter.loadAcquire();
initializeList<Custom>();
QCOMPARE(instancesCount, Custom::counter);
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
}
void tst_QVector::const_shared_null()
@ -2084,16 +2085,16 @@ void tst_QVector::setSharableInt()
void tst_QVector::setSharableMovable()
{
const int instancesCount = Movable::counter;
const int instancesCount = Movable::counter.loadAcquire();
setSharable<Movable>();
QCOMPARE(instancesCount, Movable::counter);
QCOMPARE(instancesCount, Movable::counter.loadAcquire());
}
void tst_QVector::setSharableCustom()
{
const int instancesCount = Custom::counter;
const int instancesCount = Custom::counter.loadAcquire();
setSharable<Custom>();
QCOMPARE(instancesCount, Custom::counter);
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
}
template<typename T>
@ -2200,16 +2201,16 @@ void tst_QVector::detachInt() const
void tst_QVector::detachMovable() const
{
const int instancesCount = Movable::counter;
const int instancesCount = Movable::counter.loadAcquire();
detach<Movable>();
QCOMPARE(instancesCount, Movable::counter);
QCOMPARE(instancesCount, Movable::counter.loadAcquire());
}
void tst_QVector::detachCustom() const
{
const int instancesCount = Custom::counter;
const int instancesCount = Custom::counter.loadAcquire();
detach<Custom>();
QCOMPARE(instancesCount, Custom::counter);
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
}
QTEST_APPLESS_MAIN(tst_QVector)