Warning, file /include/QtCore/qreadwritelock.h was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004 #ifndef QREADWRITELOCK_H
0005 #define QREADWRITELOCK_H
0006
0007 #include <QtCore/qglobal.h>
0008 #include <QtCore/qdeadlinetimer.h>
0009
0010 QT_BEGIN_NAMESPACE
0011
0012 #if QT_CONFIG(thread)
0013
0014 class QReadWriteLockPrivate;
0015
0016 class Q_CORE_EXPORT QReadWriteLock
0017 {
0018 public:
0019 enum RecursionMode { NonRecursive, Recursive };
0020
0021 QT_CORE_INLINE_SINCE(6, 6)
0022 explicit QReadWriteLock(RecursionMode recursionMode = NonRecursive);
0023 QT_CORE_INLINE_SINCE(6, 6)
0024 ~QReadWriteLock();
0025
0026 QT_CORE_INLINE_SINCE(6, 6)
0027 void lockForRead();
0028 #if QT_CORE_REMOVED_SINCE(6, 6)
0029 bool tryLockForRead();
0030 #endif
0031 QT_CORE_INLINE_SINCE(6, 6)
0032 bool tryLockForRead(int timeout);
0033 bool tryLockForRead(QDeadlineTimer timeout = {});
0034
0035 QT_CORE_INLINE_SINCE(6, 6)
0036 void lockForWrite();
0037 #if QT_CORE_REMOVED_SINCE(6, 6)
0038 bool tryLockForWrite();
0039 #endif
0040 QT_CORE_INLINE_SINCE(6, 6)
0041 bool tryLockForWrite(int timeout);
0042 bool tryLockForWrite(QDeadlineTimer timeout = {});
0043
0044 void unlock();
0045
0046 private:
0047 Q_DISABLE_COPY(QReadWriteLock)
0048 QAtomicPointer<QReadWriteLockPrivate> d_ptr;
0049 friend class QReadWriteLockPrivate;
0050 static QReadWriteLockPrivate *initRecursive();
0051 static void destroyRecursive(QReadWriteLockPrivate *);
0052 };
0053
0054 #if QT_CORE_INLINE_IMPL_SINCE(6, 6)
0055 QReadWriteLock::QReadWriteLock(RecursionMode recursionMode)
0056 : d_ptr(recursionMode == Recursive ? initRecursive() : nullptr)
0057 {
0058 }
0059
0060 QReadWriteLock::~QReadWriteLock()
0061 {
0062 if (auto d = d_ptr.loadAcquire())
0063 destroyRecursive(d);
0064 }
0065
0066 void QReadWriteLock::lockForRead()
0067 {
0068 tryLockForRead(QDeadlineTimer(QDeadlineTimer::Forever));
0069 }
0070
0071 bool QReadWriteLock::tryLockForRead(int timeout)
0072 {
0073 return tryLockForRead(QDeadlineTimer(timeout));
0074 }
0075
0076 void QReadWriteLock::lockForWrite()
0077 {
0078 tryLockForWrite(QDeadlineTimer(QDeadlineTimer::Forever));
0079 }
0080
0081 bool QReadWriteLock::tryLockForWrite(int timeout)
0082 {
0083 return tryLockForWrite(QDeadlineTimer(timeout));
0084 }
0085 #endif
0086
0087 #if defined(Q_CC_MSVC)
0088 #pragma warning( push )
0089 #pragma warning( disable : 4312 )
0090 #endif
0091
0092 class QT6_ONLY(Q_CORE_EXPORT) QReadLocker
0093 {
0094 public:
0095 Q_NODISCARD_CTOR
0096 inline QReadLocker(QReadWriteLock *readWriteLock);
0097
0098 inline ~QReadLocker()
0099 { unlock(); }
0100
0101 inline void unlock()
0102 {
0103 if (q_val) {
0104 if ((q_val & quintptr(1u)) == quintptr(1u)) {
0105 q_val &= ~quintptr(1u);
0106 readWriteLock()->unlock();
0107 }
0108 }
0109 }
0110
0111 inline void relock()
0112 {
0113 if (q_val) {
0114 if ((q_val & quintptr(1u)) == quintptr(0u)) {
0115 readWriteLock()->lockForRead();
0116 q_val |= quintptr(1u);
0117 }
0118 }
0119 }
0120
0121 inline QReadWriteLock *readWriteLock() const
0122 { return reinterpret_cast<QReadWriteLock *>(q_val & ~quintptr(1u)); }
0123
0124 private:
0125 Q_DISABLE_COPY(QReadLocker)
0126 quintptr q_val;
0127 };
0128
0129 inline QReadLocker::QReadLocker(QReadWriteLock *areadWriteLock)
0130 : q_val(reinterpret_cast<quintptr>(areadWriteLock))
0131 {
0132 Q_ASSERT_X((q_val & quintptr(1u)) == quintptr(0),
0133 "QReadLocker", "QReadWriteLock pointer is misaligned");
0134 relock();
0135 }
0136
0137 class QT6_ONLY(Q_CORE_EXPORT) QWriteLocker
0138 {
0139 public:
0140 Q_NODISCARD_CTOR
0141 inline QWriteLocker(QReadWriteLock *readWriteLock);
0142
0143 inline ~QWriteLocker()
0144 { unlock(); }
0145
0146 inline void unlock()
0147 {
0148 if (q_val) {
0149 if ((q_val & quintptr(1u)) == quintptr(1u)) {
0150 q_val &= ~quintptr(1u);
0151 readWriteLock()->unlock();
0152 }
0153 }
0154 }
0155
0156 inline void relock()
0157 {
0158 if (q_val) {
0159 if ((q_val & quintptr(1u)) == quintptr(0u)) {
0160 readWriteLock()->lockForWrite();
0161 q_val |= quintptr(1u);
0162 }
0163 }
0164 }
0165
0166 inline QReadWriteLock *readWriteLock() const
0167 { return reinterpret_cast<QReadWriteLock *>(q_val & ~quintptr(1u)); }
0168
0169
0170 private:
0171 Q_DISABLE_COPY(QWriteLocker)
0172 quintptr q_val;
0173 };
0174
0175 inline QWriteLocker::QWriteLocker(QReadWriteLock *areadWriteLock)
0176 : q_val(reinterpret_cast<quintptr>(areadWriteLock))
0177 {
0178 Q_ASSERT_X((q_val & quintptr(1u)) == quintptr(0),
0179 "QWriteLocker", "QReadWriteLock pointer is misaligned");
0180 relock();
0181 }
0182
0183 #if defined(Q_CC_MSVC)
0184 #pragma warning( pop )
0185 #endif
0186
0187 #else
0188
0189 class QT6_ONLY(Q_CORE_EXPORT) QReadWriteLock
0190 {
0191 public:
0192 enum RecursionMode { NonRecursive, Recursive };
0193 inline explicit QReadWriteLock(RecursionMode = NonRecursive) noexcept { }
0194 inline ~QReadWriteLock() { }
0195
0196 void lockForRead() noexcept { }
0197 bool tryLockForRead() noexcept { return true; }
0198 bool tryLockForRead(QDeadlineTimer) noexcept { return true; }
0199 bool tryLockForRead(int timeout) noexcept { Q_UNUSED(timeout); return true; }
0200
0201 void lockForWrite() noexcept { }
0202 bool tryLockForWrite() noexcept { return true; }
0203 bool tryLockForWrite(QDeadlineTimer) noexcept { return true; }
0204 bool tryLockForWrite(int timeout) noexcept { Q_UNUSED(timeout); return true; }
0205
0206 void unlock() noexcept { }
0207
0208 private:
0209 Q_DISABLE_COPY(QReadWriteLock)
0210 };
0211
0212 class QT6_ONLY(Q_CORE_EXPORT) QReadLocker
0213 {
0214 public:
0215 Q_NODISCARD_CTOR
0216 inline explicit QReadLocker(QReadWriteLock *) noexcept { }
0217 inline ~QReadLocker() noexcept { }
0218
0219 void unlock() noexcept { }
0220 void relock() noexcept { }
0221 QReadWriteLock *readWriteLock() noexcept { return nullptr; }
0222
0223 private:
0224 Q_DISABLE_COPY(QReadLocker)
0225 };
0226
0227 class QT6_ONLY(Q_CORE_EXPORT) QWriteLocker
0228 {
0229 public:
0230 Q_NODISCARD_CTOR
0231 inline explicit QWriteLocker(QReadWriteLock *) noexcept { }
0232 inline ~QWriteLocker() noexcept { }
0233
0234 void unlock() noexcept { }
0235 void relock() noexcept { }
0236 QReadWriteLock *readWriteLock() noexcept { return nullptr; }
0237
0238 private:
0239 Q_DISABLE_COPY(QWriteLocker)
0240 };
0241
0242 #endif
0243
0244 QT_END_NAMESPACE
0245
0246 #endif