From 4f00e6c8b9aa1f9ce7223b09b61e041cf6ad034b Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 1 May 2023 15:19:31 -0700 Subject: [PATCH] QReadWriteLock: inline the constructor and destructor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ieab617d69f3b4b54ab30fffd175b2500dd860431 Reviewed-by: MÃ¥rten Nordheim --- src/corelib/thread/qreadwritelock.cpp | 12 +++++++----- src/corelib/thread/qreadwritelock.h | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/corelib/thread/qreadwritelock.cpp b/src/corelib/thread/qreadwritelock.cpp index acbb62432f..3a9fb9d4c8 100644 --- a/src/corelib/thread/qreadwritelock.cpp +++ b/src/corelib/thread/qreadwritelock.cpp @@ -101,6 +101,7 @@ static bool contendedTryLockForWrite(QAtomicPointer &d_pt */ /*! + \fn QReadWriteLock::QReadWriteLock(RecursionMode recursionMode) \since 4.4 Constructs a QReadWriteLock object in the given \a recursionMode. @@ -109,21 +110,22 @@ static bool contendedTryLockForWrite(QAtomicPointer &d_pt \sa lockForRead(), lockForWrite(), RecursionMode */ -QReadWriteLock::QReadWriteLock(RecursionMode recursionMode) - : d_ptr(recursionMode == Recursive ? new QReadWriteLockPrivate(true) : nullptr) +QReadWriteLockPrivate *QReadWriteLock::initRecursive() { - Q_ASSERT_X(!(quintptr(d_ptr.loadRelaxed()) & StateMask), "QReadWriteLock::QReadWriteLock", "bad d_ptr alignment"); + auto d = new QReadWriteLockPrivate(true); + Q_ASSERT_X(!(quintptr(d) & StateMask), "QReadWriteLock::QReadWriteLock", "bad d_ptr alignment"); + return d; } /*! + \fn QReadWriteLock::~QReadWriteLock() Destroys the QReadWriteLock object. \warning Destroying a read-write lock that is in use may result in undefined behavior. */ -QReadWriteLock::~QReadWriteLock() +void QReadWriteLock::destroyRecursive(QReadWriteLockPrivate *d) { - auto d = d_ptr.loadAcquire(); if (isUncontendedLocked(d)) { qWarning("QReadWriteLock: destroying locked QReadWriteLock"); return; diff --git a/src/corelib/thread/qreadwritelock.h b/src/corelib/thread/qreadwritelock.h index 0494088bee..352aaab2cf 100644 --- a/src/corelib/thread/qreadwritelock.h +++ b/src/corelib/thread/qreadwritelock.h @@ -18,7 +18,9 @@ class Q_CORE_EXPORT QReadWriteLock public: enum RecursionMode { NonRecursive, Recursive }; + QT_CORE_INLINE_SINCE(6, 6) explicit QReadWriteLock(RecursionMode recursionMode = NonRecursive); + QT_CORE_INLINE_SINCE(6, 6) ~QReadWriteLock(); QT_CORE_INLINE_SINCE(6, 6) @@ -54,9 +56,22 @@ private: Q_DISABLE_COPY(QReadWriteLock) QAtomicPointer d_ptr; friend class QReadWriteLockPrivate; + static QReadWriteLockPrivate *initRecursive(); + static void destroyRecursive(QReadWriteLockPrivate *); }; #if QT_CORE_INLINE_IMPL_SINCE(6, 6) +QReadWriteLock::QReadWriteLock(RecursionMode recursionMode) + : d_ptr(recursionMode == Recursive ? initRecursive() : nullptr) +{ +} + +QReadWriteLock::~QReadWriteLock() +{ + if (auto d = d_ptr.loadAcquire()) + destroyRecursive(d); +} + void QReadWriteLock::lockForRead() { tryLockForRead(QDeadlineTimer(QDeadlineTimer::Forever));