diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2023-04-17 16:14:37 -0700 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2023-05-11 12:31:56 -0700 |
commit | 834c755977bbfe66e041aba6d2dfa69954eb3808 (patch) | |
tree | b517dfe37346c0ed87b9e3d6ff6561c3f89e3a77 /src/corelib/thread/qreadwritelock.cpp | |
parent | 0c0778fb36641efe73caa8776ee0a2ffdc98f4ea (diff) |
QReadWriteLock: force the loop to exist in a separate function
This allows the fast, uncontended case to exist in a function that does
much less work and therefore needs to save less state in its
prologue. This is interesting too for LTO because the compiler can then
inline the fast, uncontended path where the locks were used.
Pick-to: 6.5
Change-Id: I3d728c4197df49169066fffd1756dc04d8a5f04a
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/corelib/thread/qreadwritelock.cpp')
-rw-r--r-- | src/corelib/thread/qreadwritelock.cpp | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/src/corelib/thread/qreadwritelock.cpp b/src/corelib/thread/qreadwritelock.cpp index 4aaf076d9f..fd60fb0934 100644 --- a/src/corelib/thread/qreadwritelock.cpp +++ b/src/corelib/thread/qreadwritelock.cpp @@ -39,6 +39,11 @@ inline bool isUncontendedLocked(const QReadWriteLockPrivate *d) { return quintptr(d) & StateMask; } } +static bool contendedTryLockForRead(QAtomicPointer<QReadWriteLockPrivate> &d_ptr, + int timeout, QReadWriteLockPrivate *d); +static bool contendedTryLockForWrite(QAtomicPointer<QReadWriteLockPrivate> &d_ptr, + int timeout, QReadWriteLockPrivate *d); + /*! \class QReadWriteLock \inmodule QtCore \brief The QReadWriteLock class provides read-write locking. @@ -137,8 +142,6 @@ QReadWriteLock::~QReadWriteLock() */ void QReadWriteLock::lockForRead() { - if (d_ptr.testAndSetAcquire(nullptr, dummyLockedForRead)) - return; tryLockForRead(-1); } @@ -188,7 +191,12 @@ bool QReadWriteLock::tryLockForRead(int timeout) QReadWriteLockPrivate *d = d_ptr.loadRelaxed(); if (d == nullptr && d_ptr.testAndSetAcquire(nullptr, dummyLockedForRead, d)) return true; + return contendedTryLockForRead(d_ptr, timeout, d); +} +Q_NEVER_INLINE static bool contendedTryLockForRead(QAtomicPointer<QReadWriteLockPrivate> &d_ptr, + int timeout, QReadWriteLockPrivate *d) +{ while (true) { if (d == nullptr) { if (!d_ptr.testAndSetAcquire(nullptr, dummyLockedForRead, d)) @@ -302,7 +310,12 @@ bool QReadWriteLock::tryLockForWrite(int timeout) QReadWriteLockPrivate *d = d_ptr.loadRelaxed(); if (d == nullptr && d_ptr.testAndSetAcquire(nullptr, dummyLockedForWrite, d)) return true; + return contendedTryLockForWrite(d_ptr, timeout, d); +} +Q_NEVER_INLINE static bool contendedTryLockForWrite(QAtomicPointer<QReadWriteLockPrivate> &d_ptr, + int timeout, QReadWriteLockPrivate *d) +{ while (true) { if (d == nullptr) { if (!d_ptr.testAndSetAcquire(d, dummyLockedForWrite, d)) |