QReadWriteLock: extract method fastTryLock
This removes a little bit of duplication, and reduces the temptation to remove the first part of tryLockForX as duplicated (it should remain there for performance reasons). Change-Id: I7ce102ceeb03b5ab4fe026b56fbba245b4c2f310 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
677ee0cf7c
commit
22d4f117bd
@ -164,6 +164,14 @@ void QReadWriteLock::destroyRecursive(QReadWriteLockPrivate *d)
|
|||||||
\sa unlock(), lockForRead()
|
\sa unlock(), lockForRead()
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
static Q_ALWAYS_INLINE bool fastTryLock(QAtomicPointer<QReadWriteLockPrivate> &d_ptr,
|
||||||
|
QReadWriteLockPrivate *dummyValue,
|
||||||
|
QReadWriteLockPrivate *&d)
|
||||||
|
{
|
||||||
|
// Succeed fast if not contended
|
||||||
|
return d == nullptr && d_ptr.testAndSetAcquire(nullptr, dummyValue, d);
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\overload
|
\overload
|
||||||
\since 6.6
|
\since 6.6
|
||||||
@ -183,9 +191,8 @@ void QReadWriteLock::destroyRecursive(QReadWriteLockPrivate *d)
|
|||||||
*/
|
*/
|
||||||
bool QReadWriteLock::tryLockForRead(QDeadlineTimer timeout)
|
bool QReadWriteLock::tryLockForRead(QDeadlineTimer timeout)
|
||||||
{
|
{
|
||||||
// Fast case: non contended:
|
|
||||||
QReadWriteLockPrivate *d = d_ptr.loadRelaxed();
|
QReadWriteLockPrivate *d = d_ptr.loadRelaxed();
|
||||||
if (d == nullptr && d_ptr.testAndSetAcquire(nullptr, dummyLockedForRead, d))
|
if (fastTryLock(d_ptr, dummyLockedForRead, d))
|
||||||
return true;
|
return true;
|
||||||
return contendedTryLockForRead(d_ptr, timeout, d);
|
return contendedTryLockForRead(d_ptr, timeout, d);
|
||||||
}
|
}
|
||||||
@ -195,9 +202,9 @@ Q_NEVER_INLINE static bool contendedTryLockForRead(QAtomicPointer<QReadWriteLock
|
|||||||
{
|
{
|
||||||
while (true) {
|
while (true) {
|
||||||
if (d == nullptr) {
|
if (d == nullptr) {
|
||||||
if (!d_ptr.testAndSetAcquire(nullptr, dummyLockedForRead, d))
|
if (fastTryLock(d_ptr, dummyLockedForRead, d))
|
||||||
continue;
|
return true;
|
||||||
return true;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((quintptr(d) & StateMask) == StateLockedForRead) {
|
if ((quintptr(d) & StateMask) == StateLockedForRead) {
|
||||||
@ -301,9 +308,8 @@ Q_NEVER_INLINE static bool contendedTryLockForRead(QAtomicPointer<QReadWriteLock
|
|||||||
*/
|
*/
|
||||||
bool QReadWriteLock::tryLockForWrite(QDeadlineTimer timeout)
|
bool QReadWriteLock::tryLockForWrite(QDeadlineTimer timeout)
|
||||||
{
|
{
|
||||||
// Fast case: non contended:
|
|
||||||
QReadWriteLockPrivate *d = d_ptr.loadRelaxed();
|
QReadWriteLockPrivate *d = d_ptr.loadRelaxed();
|
||||||
if (d == nullptr && d_ptr.testAndSetAcquire(nullptr, dummyLockedForWrite, d))
|
if (fastTryLock(d_ptr, dummyLockedForWrite, d))
|
||||||
return true;
|
return true;
|
||||||
return contendedTryLockForWrite(d_ptr, timeout, d);
|
return contendedTryLockForWrite(d_ptr, timeout, d);
|
||||||
}
|
}
|
||||||
@ -313,9 +319,9 @@ Q_NEVER_INLINE static bool contendedTryLockForWrite(QAtomicPointer<QReadWriteLoc
|
|||||||
{
|
{
|
||||||
while (true) {
|
while (true) {
|
||||||
if (d == nullptr) {
|
if (d == nullptr) {
|
||||||
if (!d_ptr.testAndSetAcquire(d, dummyLockedForWrite, d))
|
if (fastTryLock(d_ptr, dummyLockedForWrite, d))
|
||||||
continue;
|
return true;
|
||||||
return true;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isUncontendedLocked(d)) {
|
if (isUncontendedLocked(d)) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user