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));