diff --git a/src/corelib/thread/qmutex.cpp b/src/corelib/thread/qmutex.cpp index ba97ef8a42..dec0a95c47 100644 --- a/src/corelib/thread/qmutex.cpp +++ b/src/corelib/thread/qmutex.cpp @@ -494,6 +494,14 @@ void QRecursiveMutex::unlock() noexcept \sa QMutex::unlock() */ +/*! + \fn template bool QMutexLocker::isLocked() const noexcept + \since 6.4 + + Returns true if this QMutexLocker is currently locking its associated + mutex, or false otherwise. +*/ + /*! \fn template void QMutexLocker::unlock() noexcept diff --git a/src/corelib/thread/qmutex.h b/src/corelib/thread/qmutex.h index fea3657150..f57a95de22 100644 --- a/src/corelib/thread/qmutex.h +++ b/src/corelib/thread/qmutex.h @@ -252,6 +252,11 @@ public: unlock(); } + inline bool isLocked() const noexcept + { + return m_isLocked; + } + inline void unlock() noexcept { if (!m_isLocked) diff --git a/tests/auto/corelib/thread/qmutexlocker/tst_qmutexlocker.cpp b/tests/auto/corelib/thread/qmutexlocker/tst_qmutexlocker.cpp index d873291fd6..29c8b92222 100644 --- a/tests/auto/corelib/thread/qmutexlocker/tst_qmutexlocker.cpp +++ b/tests/auto/corelib/thread/qmutexlocker/tst_qmutexlocker.cpp @@ -80,6 +80,7 @@ void tst_QMutexLocker::scopeTest() { QMutexLocker locker(&mutex); + QVERIFY(locker.isLocked()); waitForTest(); } @@ -122,16 +123,23 @@ void tst_QMutexLocker::unlockAndRelockTest() void run() override { QMutexLocker locker(&mutex); + QVERIFY(locker.isLocked()); waitForTest(); + QVERIFY(locker.isLocked()); locker.unlock(); + QVERIFY(!locker.isLocked()); waitForTest(); + QVERIFY(!locker.isLocked()); locker.relock(); + QVERIFY(locker.isLocked()); waitForTest(); + + QVERIFY(locker.isLocked()); } }; @@ -169,10 +177,16 @@ void tst_QMutexLocker::lockerStateTest() { { QMutexLocker locker(&mutex); + QVERIFY(locker.isLocked()); + locker.relock(); + QVERIFY(locker.isLocked()); + locker.unlock(); + QVERIFY(!locker.isLocked()); waitForTest(); + QVERIFY(!locker.isLocked()); } waitForTest();