1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtCore module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #ifndef QREADWRITELOCK_H
|
---|
43 | #define QREADWRITELOCK_H
|
---|
44 |
|
---|
45 | #include <QtCore/qglobal.h>
|
---|
46 | #include <limits.h> // ### Qt 5: remove
|
---|
47 |
|
---|
48 | QT_BEGIN_HEADER
|
---|
49 |
|
---|
50 | QT_BEGIN_NAMESPACE
|
---|
51 |
|
---|
52 | QT_MODULE(Core)
|
---|
53 |
|
---|
54 | #ifndef QT_NO_THREAD
|
---|
55 |
|
---|
56 | struct QReadWriteLockPrivate;
|
---|
57 |
|
---|
58 | class Q_CORE_EXPORT QReadWriteLock
|
---|
59 | {
|
---|
60 | public:
|
---|
61 | enum RecursionMode { NonRecursive, Recursive };
|
---|
62 |
|
---|
63 | QReadWriteLock(); // ### Qt 5: merge with below
|
---|
64 | QReadWriteLock(RecursionMode recursionMode);
|
---|
65 | ~QReadWriteLock();
|
---|
66 |
|
---|
67 | void lockForRead();
|
---|
68 | bool tryLockForRead();
|
---|
69 | bool tryLockForRead(int timeout);
|
---|
70 |
|
---|
71 | void lockForWrite();
|
---|
72 | bool tryLockForWrite();
|
---|
73 | bool tryLockForWrite(int timeout);
|
---|
74 |
|
---|
75 | void unlock();
|
---|
76 |
|
---|
77 | private:
|
---|
78 | Q_DISABLE_COPY(QReadWriteLock)
|
---|
79 | QReadWriteLockPrivate *d;
|
---|
80 |
|
---|
81 | friend class QWaitCondition;
|
---|
82 | };
|
---|
83 |
|
---|
84 | #if defined(Q_CC_MSVC)
|
---|
85 | #pragma warning( push )
|
---|
86 | #pragma warning( disable : 4312 ) // ignoring the warning from /Wp64
|
---|
87 | #endif
|
---|
88 |
|
---|
89 | class Q_CORE_EXPORT QReadLocker
|
---|
90 | {
|
---|
91 | public:
|
---|
92 | inline QReadLocker(QReadWriteLock *readWriteLock);
|
---|
93 |
|
---|
94 | inline ~QReadLocker()
|
---|
95 | { unlock(); }
|
---|
96 |
|
---|
97 | inline void unlock()
|
---|
98 | {
|
---|
99 | if (q_lock) {
|
---|
100 | if ((q_val & quintptr(1u)) == quintptr(1u)) {
|
---|
101 | q_val &= ~quintptr(1u);
|
---|
102 | q_lock->unlock();
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | inline void relock()
|
---|
108 | {
|
---|
109 | if (q_lock) {
|
---|
110 | if ((q_val & quintptr(1u)) == quintptr(0u)) {
|
---|
111 | q_lock->lockForRead();
|
---|
112 | q_val |= quintptr(1u);
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | inline QReadWriteLock *readWriteLock() const
|
---|
118 | { return reinterpret_cast<QReadWriteLock *>(q_val & ~quintptr(1u)); }
|
---|
119 |
|
---|
120 | private:
|
---|
121 | Q_DISABLE_COPY(QReadLocker)
|
---|
122 | union {
|
---|
123 | QReadWriteLock *q_lock;
|
---|
124 | quintptr q_val;
|
---|
125 | };
|
---|
126 | };
|
---|
127 |
|
---|
128 | inline QReadLocker::QReadLocker(QReadWriteLock *areadWriteLock)
|
---|
129 | : q_lock(areadWriteLock)
|
---|
130 | {
|
---|
131 | Q_ASSERT_X((q_val & quintptr(1u)) == quintptr(0),
|
---|
132 | "QReadLocker", "QReadWriteLock pointer is misaligned");
|
---|
133 | relock();
|
---|
134 | }
|
---|
135 |
|
---|
136 | class Q_CORE_EXPORT QWriteLocker
|
---|
137 | {
|
---|
138 | public:
|
---|
139 | inline QWriteLocker(QReadWriteLock *readWriteLock);
|
---|
140 |
|
---|
141 | inline ~QWriteLocker()
|
---|
142 | { unlock(); }
|
---|
143 |
|
---|
144 | inline void unlock()
|
---|
145 | {
|
---|
146 | if (q_lock) {
|
---|
147 | if ((q_val & quintptr(1u)) == quintptr(1u)) {
|
---|
148 | q_val &= ~quintptr(1u);
|
---|
149 | q_lock->unlock();
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | inline void relock()
|
---|
155 | {
|
---|
156 | if (q_lock) {
|
---|
157 | if ((q_val & quintptr(1u)) == quintptr(0u)) {
|
---|
158 | q_lock->lockForWrite();
|
---|
159 | q_val |= quintptr(1u);
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | inline QReadWriteLock *readWriteLock() const
|
---|
165 | { return reinterpret_cast<QReadWriteLock *>(q_val & ~quintptr(1u)); }
|
---|
166 |
|
---|
167 |
|
---|
168 | private:
|
---|
169 | Q_DISABLE_COPY(QWriteLocker)
|
---|
170 | union{
|
---|
171 | QReadWriteLock *q_lock;
|
---|
172 | quintptr q_val;
|
---|
173 | };
|
---|
174 | };
|
---|
175 |
|
---|
176 | inline QWriteLocker::QWriteLocker(QReadWriteLock *areadWriteLock)
|
---|
177 | : q_lock(areadWriteLock)
|
---|
178 | {
|
---|
179 | Q_ASSERT_X((q_val & quintptr(1u)) == quintptr(0),
|
---|
180 | "QWriteLocker", "QReadWriteLock pointer is misaligned");
|
---|
181 | relock();
|
---|
182 | }
|
---|
183 |
|
---|
184 | #if defined(Q_CC_MSVC)
|
---|
185 | #pragma warning( pop )
|
---|
186 | #endif
|
---|
187 |
|
---|
188 | #else // QT_NO_THREAD
|
---|
189 |
|
---|
190 | class Q_CORE_EXPORT QReadWriteLock
|
---|
191 | {
|
---|
192 | public:
|
---|
193 | inline explicit QReadWriteLock() { }
|
---|
194 | inline ~QReadWriteLock() { }
|
---|
195 |
|
---|
196 | static inline void lockForRead() { }
|
---|
197 | static inline bool tryLockForRead() { return true; }
|
---|
198 | static inline bool tryLockForRead(int timeout) { Q_UNUSED(timeout); return true; }
|
---|
199 |
|
---|
200 | static inline void lockForWrite() { }
|
---|
201 | static inline bool tryLockForWrite() { return true; }
|
---|
202 | static inline bool tryLockForWrite(int timeout) { Q_UNUSED(timeout); return true; }
|
---|
203 |
|
---|
204 | static inline void unlock() { }
|
---|
205 |
|
---|
206 | private:
|
---|
207 | Q_DISABLE_COPY(QReadWriteLock)
|
---|
208 | };
|
---|
209 |
|
---|
210 | class Q_CORE_EXPORT QReadLocker
|
---|
211 | {
|
---|
212 | public:
|
---|
213 | inline QReadLocker(QReadWriteLock *) { }
|
---|
214 | inline ~QReadLocker() { }
|
---|
215 |
|
---|
216 | static inline void unlock() { }
|
---|
217 | static inline void relock() { }
|
---|
218 | static inline QReadWriteLock *readWriteLock() { return 0; }
|
---|
219 |
|
---|
220 | private:
|
---|
221 | Q_DISABLE_COPY(QReadLocker)
|
---|
222 | };
|
---|
223 |
|
---|
224 | class Q_CORE_EXPORT QWriteLocker
|
---|
225 | {
|
---|
226 | public:
|
---|
227 | inline explicit QWriteLocker(QReadWriteLock *) { }
|
---|
228 | inline ~QWriteLocker() { }
|
---|
229 |
|
---|
230 | static inline void unlock() { }
|
---|
231 | static inline void relock() { }
|
---|
232 | static inline QReadWriteLock *readWriteLock() { return 0; }
|
---|
233 |
|
---|
234 | private:
|
---|
235 | Q_DISABLE_COPY(QWriteLocker)
|
---|
236 | };
|
---|
237 |
|
---|
238 | #endif // QT_NO_THREAD
|
---|
239 |
|
---|
240 | QT_END_NAMESPACE
|
---|
241 |
|
---|
242 | QT_END_HEADER
|
---|
243 |
|
---|
244 | #endif // QREADWRITELOCK_H
|
---|