diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-11-20 13:51:16 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-06-17 09:27:47 +0000 |
commit | e41c100460126d8cb044f11db73b52d6b80f9309 (patch) | |
tree | be80d2ba9c18a3560539148d83a6ccc87c7b1667 /tests/auto/other/qobjectrace/tst_qobjectrace.cpp | |
parent | b6323f0c7ba24a4ad5daa3535fcc9d6b285cfe28 (diff) |
Fix crash in concurrent disconnect
This does not fix all data races that we have in the system yet.
One major issue is the virtual disconnectNotify(), that can be
called from any thread and thus is inherently problematic, as it
can collide with the object getting destroyed at the same time
in another thread.
Task-number: QTBUG-88248
Change-Id: I9d841eb363b7e4f0de1657aeb8f5340d0fd55190
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 71b4d4f150bc3c904a5aceec37513ddc3cd1c150)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'tests/auto/other/qobjectrace/tst_qobjectrace.cpp')
-rw-r--r-- | tests/auto/other/qobjectrace/tst_qobjectrace.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/tests/auto/other/qobjectrace/tst_qobjectrace.cpp b/tests/auto/other/qobjectrace/tst_qobjectrace.cpp index ac33fa3ec3..093b4d2476 100644 --- a/tests/auto/other/qobjectrace/tst_qobjectrace.cpp +++ b/tests/auto/other/qobjectrace/tst_qobjectrace.cpp @@ -49,6 +49,7 @@ private slots: void destroyRace(); void blockingQueuedDestroyRace(); void disconnectRace(); + void disconnectRace2(); }; class RaceObject : public QObject @@ -562,5 +563,53 @@ void tst_QObjectRace::disconnectRace() QCOMPARE(countedStructObjectsCount.loadRelaxed(), 0u); } +void tst_QObjectRace::disconnectRace2() +{ + enum { IterationCount = 100, ConnectionCount = 100, YieldCount = 100 }; + + QAtomicPointer<MyObject> ptr; + QSemaphore createSemaphore(0); + QSemaphore proceedSemaphore(0); + + std::unique_ptr<QThread> t1(QThread::create([&]() { + for (int i = 0; i < IterationCount; ++i) { + MyObject sender; + ptr.storeRelease(&sender); + createSemaphore.release(); + proceedSemaphore.acquire(); + ptr.storeRelaxed(nullptr); + for (int i = 0; i < YieldCount; ++i) + QThread::yieldCurrentThread(); + } + })); + t1->start(); + + + std::unique_ptr<QThread> t2(QThread::create([&]() { + auto connections = std::make_unique<QMetaObject::Connection[]>(ConnectionCount); + for (int i = 0; i < IterationCount; ++i) { + MyObject receiver; + MyObject *sender = nullptr; + + createSemaphore.acquire(); + + while (!(sender = ptr.loadAcquire())) + ; + + for (int i = 0; i < ConnectionCount; ++i) + connections[i] = QObject::connect(sender, &MyObject::signal1, &receiver, &MyObject::slot1); + + proceedSemaphore.release(); + + for (int i = 0; i < ConnectionCount; ++i) + QObject::disconnect(connections[i]); + } + })); + t2->start(); + + t1->wait(); + t2->wait(); +} + QTEST_MAIN(tst_QObjectRace) #include "tst_qobjectrace.moc" |