1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QREADWRITELOCK_H
41#define QREADWRITELOCK_H
42
43#include <QtCore/qglobal.h>
44
45QT_BEGIN_NAMESPACE
46
47
48#if QT_CONFIG(thread)
49
50class QReadWriteLockPrivate;
51
52class Q_CORE_EXPORT QReadWriteLock
53{
54public:
55 enum RecursionMode { NonRecursive, Recursive };
56
57 explicit QReadWriteLock(RecursionMode recursionMode = NonRecursive);
58 ~QReadWriteLock();
59
60 void lockForRead();
61 bool tryLockForRead();
62 bool tryLockForRead(int timeout);
63
64 void lockForWrite();
65 bool tryLockForWrite();
66 bool tryLockForWrite(int timeout);
67
68 void unlock();
69
70private:
71 Q_DISABLE_COPY(QReadWriteLock)
72 QAtomicPointer<QReadWriteLockPrivate> d_ptr;
73
74 enum StateForWaitCondition { LockedForRead, LockedForWrite, Unlocked, RecursivelyLocked };
75 StateForWaitCondition stateForWaitCondition() const;
76 friend class QWaitCondition;
77};
78
79#if defined(Q_CC_MSVC)
80#pragma warning( push )
81#pragma warning( disable : 4312 ) // ignoring the warning from /Wp64
82#endif
83
84class Q_CORE_EXPORT QReadLocker
85{
86public:
87 inline QReadLocker(QReadWriteLock *readWriteLock);
88
89 inline ~QReadLocker()
90 { unlock(); }
91
92 inline void unlock()
93 {
94 if (q_val) {
95 if ((q_val & quintptr(1u)) == quintptr(1u)) {
96 q_val &= ~quintptr(1u);
97 readWriteLock()->unlock();
98 }
99 }
100 }
101
102 inline void relock()
103 {
104 if (q_val) {
105 if ((q_val & quintptr(1u)) == quintptr(0u)) {
106 readWriteLock()->lockForRead();
107 q_val |= quintptr(1u);
108 }
109 }
110 }
111
112 inline QReadWriteLock *readWriteLock() const
113 { return reinterpret_cast<QReadWriteLock *>(q_val & ~quintptr(1u)); }
114
115private:
116 Q_DISABLE_COPY(QReadLocker)
117 quintptr q_val;
118};
119
120inline QReadLocker::QReadLocker(QReadWriteLock *areadWriteLock)
121 : q_val(reinterpret_cast<quintptr>(areadWriteLock))
122{
123 Q_ASSERT_X((q_val & quintptr(1u)) == quintptr(0),
124 "QReadLocker", "QReadWriteLock pointer is misaligned");
125 relock();
126}
127
128class Q_CORE_EXPORT QWriteLocker
129{
130public:
131 inline QWriteLocker(QReadWriteLock *readWriteLock);
132
133 inline ~QWriteLocker()
134 { unlock(); }
135
136 inline void unlock()
137 {
138 if (q_val) {
139 if ((q_val & quintptr(1u)) == quintptr(1u)) {
140 q_val &= ~quintptr(1u);
141 readWriteLock()->unlock();
142 }
143 }
144 }
145
146 inline void relock()
147 {
148 if (q_val) {
149 if ((q_val & quintptr(1u)) == quintptr(0u)) {
150 readWriteLock()->lockForWrite();
151 q_val |= quintptr(1u);
152 }
153 }
154 }
155
156 inline QReadWriteLock *readWriteLock() const
157 { return reinterpret_cast<QReadWriteLock *>(q_val & ~quintptr(1u)); }
158
159
160private:
161 Q_DISABLE_COPY(QWriteLocker)
162 quintptr q_val;
163};
164
165inline QWriteLocker::QWriteLocker(QReadWriteLock *areadWriteLock)
166 : q_val(reinterpret_cast<quintptr>(areadWriteLock))
167{
168 Q_ASSERT_X((q_val & quintptr(1u)) == quintptr(0),
169 "QWriteLocker", "QReadWriteLock pointer is misaligned");
170 relock();
171}
172
173#if defined(Q_CC_MSVC)
174#pragma warning( pop )
175#endif
176
177#else // QT_CONFIG(thread)
178
179class Q_CORE_EXPORT QReadWriteLock
180{
181public:
182 enum RecursionMode { NonRecursive, Recursive };
183 inline explicit QReadWriteLock(RecursionMode = NonRecursive) Q_DECL_NOTHROW { }
184 inline ~QReadWriteLock() { }
185
186 static inline void lockForRead() Q_DECL_NOTHROW { }
187 static inline bool tryLockForRead() Q_DECL_NOTHROW { return true; }
188 static inline bool tryLockForRead(int timeout) Q_DECL_NOTHROW { Q_UNUSED(timeout); return true; }
189
190 static inline void lockForWrite() Q_DECL_NOTHROW { }
191 static inline bool tryLockForWrite() Q_DECL_NOTHROW { return true; }
192 static inline bool tryLockForWrite(int timeout) Q_DECL_NOTHROW { Q_UNUSED(timeout); return true; }
193
194 static inline void unlock() Q_DECL_NOTHROW { }
195
196private:
197 Q_DISABLE_COPY(QReadWriteLock)
198};
199
200class Q_CORE_EXPORT QReadLocker
201{
202public:
203 inline QReadLocker(QReadWriteLock *) Q_DECL_NOTHROW { }
204 inline ~QReadLocker() Q_DECL_NOTHROW { }
205
206 static inline void unlock() Q_DECL_NOTHROW { }
207 static inline void relock() Q_DECL_NOTHROW { }
208 static inline QReadWriteLock *readWriteLock() Q_DECL_NOTHROW { return nullptr; }
209
210private:
211 Q_DISABLE_COPY(QReadLocker)
212};
213
214class Q_CORE_EXPORT QWriteLocker
215{
216public:
217 inline explicit QWriteLocker(QReadWriteLock *) Q_DECL_NOTHROW { }
218 inline ~QWriteLocker() Q_DECL_NOTHROW { }
219
220 static inline void unlock() Q_DECL_NOTHROW { }
221 static inline void relock() Q_DECL_NOTHROW { }
222 static inline QReadWriteLock *readWriteLock() Q_DECL_NOTHROW { return nullptr; }
223
224private:
225 Q_DISABLE_COPY(QWriteLocker)
226};
227
228#endif // QT_CONFIG(thread)
229
230QT_END_NAMESPACE
231
232#endif // QREADWRITELOCK_H
233