diff --git a/src/corelib/thread/qmutex.h b/src/corelib/thread/qmutex.h index 407473c799..be62dc01d6 100644 --- a/src/corelib/thread/qmutex.h +++ b/src/corelib/thread/qmutex.h @@ -102,7 +102,10 @@ public: bool try_lock() noexcept { return tryLock(); } private: - inline bool fastTryLock() noexcept { + inline bool fastTryLock() noexcept + { + if (d_ptr.loadRelaxed() != nullptr) + return false; return d_ptr.testAndSetAcquire(nullptr, dummyLocked()); } inline bool fastTryUnlock() noexcept { diff --git a/src/corelib/thread/qreadwritelock.cpp b/src/corelib/thread/qreadwritelock.cpp index 274d0ba7f8..32302b1e17 100644 --- a/src/corelib/thread/qreadwritelock.cpp +++ b/src/corelib/thread/qreadwritelock.cpp @@ -191,8 +191,8 @@ bool QReadWriteLock::tryLockForRead() bool QReadWriteLock::tryLockForRead(int timeout) { // Fast case: non contended: - QReadWriteLockPrivate *d; - if (d_ptr.testAndSetAcquire(nullptr, dummyLockedForRead, d)) + QReadWriteLockPrivate *d = d_ptr.loadRelaxed(); + if (d == nullptr && d_ptr.testAndSetAcquire(nullptr, dummyLockedForRead, d)) return true; while (true) { @@ -305,8 +305,8 @@ bool QReadWriteLock::tryLockForWrite() bool QReadWriteLock::tryLockForWrite(int timeout) { // Fast case: non contended: - QReadWriteLockPrivate *d; - if (d_ptr.testAndSetAcquire(nullptr, dummyLockedForWrite, d)) + QReadWriteLockPrivate *d = d_ptr.loadRelaxed(); + if (d == nullptr && d_ptr.testAndSetAcquire(nullptr, dummyLockedForWrite, d)) return true; while (true) {