Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/thread/qreadwritelock.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
5 | ** | - | ||||||||||||
6 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||
7 | ** | - | ||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
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 http://www.qt.io/terms-conditions. For further | - | ||||||||||||
15 | ** information use the contact form at http://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 2.1 or version 3 as published by the Free | - | ||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
25 | ** | - | ||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
29 | ** | - | ||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||
31 | ** | - | ||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||
33 | - | |||||||||||||
34 | #include "qplatformdefs.h" | - | ||||||||||||
35 | #include "qreadwritelock.h" | - | ||||||||||||
36 | - | |||||||||||||
37 | #ifndef QT_NO_THREAD | - | ||||||||||||
38 | #include "qmutex.h" | - | ||||||||||||
39 | #include "qthread.h" | - | ||||||||||||
40 | #include "qwaitcondition.h" | - | ||||||||||||
41 | - | |||||||||||||
42 | #include "qreadwritelock_p.h" | - | ||||||||||||
43 | - | |||||||||||||
44 | QT_BEGIN_NAMESPACE | - | ||||||||||||
45 | - | |||||||||||||
46 | /*! \class QReadWriteLock | - | ||||||||||||
47 | \inmodule QtCore | - | ||||||||||||
48 | \brief The QReadWriteLock class provides read-write locking. | - | ||||||||||||
49 | - | |||||||||||||
50 | \threadsafe | - | ||||||||||||
51 | - | |||||||||||||
52 | \ingroup thread | - | ||||||||||||
53 | - | |||||||||||||
54 | A read-write lock is a synchronization tool for protecting | - | ||||||||||||
55 | resources that can be accessed for reading and writing. This type | - | ||||||||||||
56 | of lock is useful if you want to allow multiple threads to have | - | ||||||||||||
57 | simultaneous read-only access, but as soon as one thread wants to | - | ||||||||||||
58 | write to the resource, all other threads must be blocked until | - | ||||||||||||
59 | the writing is complete. | - | ||||||||||||
60 | - | |||||||||||||
61 | In many cases, QReadWriteLock is a direct competitor to QMutex. | - | ||||||||||||
62 | QReadWriteLock is a good choice if there are many concurrent | - | ||||||||||||
63 | reads and writing occurs infrequently. | - | ||||||||||||
64 | - | |||||||||||||
65 | Example: | - | ||||||||||||
66 | - | |||||||||||||
67 | \snippet code/src_corelib_thread_qreadwritelock.cpp 0 | - | ||||||||||||
68 | - | |||||||||||||
69 | To ensure that writers aren't blocked forever by readers, readers | - | ||||||||||||
70 | attempting to obtain a lock will not succeed if there is a blocked | - | ||||||||||||
71 | writer waiting for access, even if the lock is currently only | - | ||||||||||||
72 | accessed by other readers. Also, if the lock is accessed by a | - | ||||||||||||
73 | writer and another writer comes in, that writer will have | - | ||||||||||||
74 | priority over any readers that might also be waiting. | - | ||||||||||||
75 | - | |||||||||||||
76 | Like QMutex, a QReadWriteLock can be recursively locked by the | - | ||||||||||||
77 | same thread when constructed with \l{QReadWriteLock::Recursive} as | - | ||||||||||||
78 | \l{QReadWriteLock::RecursionMode}. In such cases, | - | ||||||||||||
79 | unlock() must be called the same number of times lockForWrite() or | - | ||||||||||||
80 | lockForRead() was called. Note that the lock type cannot be | - | ||||||||||||
81 | changed when trying to lock recursively, i.e. it is not possible | - | ||||||||||||
82 | to lock for reading in a thread that already has locked for | - | ||||||||||||
83 | writing (and vice versa). | - | ||||||||||||
84 | - | |||||||||||||
85 | \sa QReadLocker, QWriteLocker, QMutex, QSemaphore | - | ||||||||||||
86 | */ | - | ||||||||||||
87 | - | |||||||||||||
88 | /*! | - | ||||||||||||
89 | \enum QReadWriteLock::RecursionMode | - | ||||||||||||
90 | \since 4.4 | - | ||||||||||||
91 | - | |||||||||||||
92 | \value Recursive In this mode, a thread can lock the same | - | ||||||||||||
93 | QReadWriteLock multiple times. The QReadWriteLock won't be unlocked | - | ||||||||||||
94 | until a corresponding number of unlock() calls have been made. | - | ||||||||||||
95 | - | |||||||||||||
96 | \value NonRecursive In this mode, a thread may only lock a | - | ||||||||||||
97 | QReadWriteLock once. | - | ||||||||||||
98 | - | |||||||||||||
99 | \sa QReadWriteLock() | - | ||||||||||||
100 | */ | - | ||||||||||||
101 | - | |||||||||||||
102 | /*! | - | ||||||||||||
103 | \since 4.4 | - | ||||||||||||
104 | - | |||||||||||||
105 | Constructs a QReadWriteLock object in the given \a recursionMode. | - | ||||||||||||
106 | - | |||||||||||||
107 | The default recursion mode is NonRecursive. | - | ||||||||||||
108 | - | |||||||||||||
109 | \sa lockForRead(), lockForWrite(), RecursionMode | - | ||||||||||||
110 | */ | - | ||||||||||||
111 | QReadWriteLock::QReadWriteLock(RecursionMode recursionMode) | - | ||||||||||||
112 | : d(new QReadWriteLockPrivate(recursionMode)) | - | ||||||||||||
113 | { executed 39003 times by 297 tests: }end of block Executed by:
executed 39003 times by 297 tests: end of block Executed by:
| 39003 | ||||||||||||
114 | - | |||||||||||||
115 | /*! | - | ||||||||||||
116 | Destroys the QReadWriteLock object. | - | ||||||||||||
117 | - | |||||||||||||
118 | \warning Destroying a read-write lock that is in use may result | - | ||||||||||||
119 | in undefined behavior. | - | ||||||||||||
120 | */ | - | ||||||||||||
121 | QReadWriteLock::~QReadWriteLock() | - | ||||||||||||
122 | { | - | ||||||||||||
123 | delete d; | - | ||||||||||||
124 | } executed 39976 times by 552 tests: end of block Executed by:
| 39976 | ||||||||||||
125 | - | |||||||||||||
126 | /*! | - | ||||||||||||
127 | Locks the lock for reading. This function will block the current | - | ||||||||||||
128 | thread if another thread has locked for writing. | - | ||||||||||||
129 | - | |||||||||||||
130 | It is not possible to lock for read if the thread already has | - | ||||||||||||
131 | locked for write. | - | ||||||||||||
132 | - | |||||||||||||
133 | \sa unlock(), lockForWrite(), tryLockForRead() | - | ||||||||||||
134 | */ | - | ||||||||||||
135 | void QReadWriteLock::lockForRead() | - | ||||||||||||
136 | { | - | ||||||||||||
137 | QMutexLocker lock(&d->mutex); | - | ||||||||||||
138 | - | |||||||||||||
139 | Qt::HANDLE self = 0; | - | ||||||||||||
140 | if (d->recursive) {
| 90-1685730 | ||||||||||||
141 | self = QThread::currentThreadId(); | - | ||||||||||||
142 | - | |||||||||||||
143 | QHash<Qt::HANDLE, int>::iterator it = d->currentReaders.find(self); | - | ||||||||||||
144 | if (it != d->currentReaders.end()) {
| 8-82 | ||||||||||||
145 | ++it.value(); | - | ||||||||||||
146 | ++d->accessCount; | - | ||||||||||||
147 | Q_ASSERT_X(d->accessCount > 0, "QReadWriteLock::lockForRead()", | - | ||||||||||||
148 | "Overflow in lock counter"); | - | ||||||||||||
149 | return; executed 8 times by 2 tests: return; Executed by:
| 8 | ||||||||||||
150 | } | - | ||||||||||||
151 | } executed 82 times by 5 tests: end of block Executed by:
| 82 | ||||||||||||
152 | - | |||||||||||||
153 | while (d->accessCount < 0 || d->waitingWriters) {
| 61-1686319 | ||||||||||||
154 | ++d->waitingReaders; | - | ||||||||||||
155 | d->readerWait.wait(&d->mutex); | - | ||||||||||||
156 | --d->waitingReaders; | - | ||||||||||||
157 | } executed 568 times by 3 tests: end of block Executed by:
| 568 | ||||||||||||
158 | if (d->recursive)
| 82-1685730 | ||||||||||||
159 | d->currentReaders.insert(self, 1); executed 82 times by 5 tests: d->currentReaders.insert(self, 1); Executed by:
| 82 | ||||||||||||
160 | - | |||||||||||||
161 | ++d->accessCount; | - | ||||||||||||
162 | Q_ASSERT_X(d->accessCount > 0, "QReadWriteLock::lockForRead()", "Overflow in lock counter"); | - | ||||||||||||
163 | } executed 1685812 times by 357 tests: end of block Executed by:
| 1685812 | ||||||||||||
164 | - | |||||||||||||
165 | /*! | - | ||||||||||||
166 | Attempts to lock for reading. If the lock was obtained, this | - | ||||||||||||
167 | function returns \c true, otherwise it returns \c false instead of | - | ||||||||||||
168 | waiting for the lock to become available, i.e. it does not block. | - | ||||||||||||
169 | - | |||||||||||||
170 | The lock attempt will fail if another thread has locked for | - | ||||||||||||
171 | writing. | - | ||||||||||||
172 | - | |||||||||||||
173 | If the lock was obtained, the lock must be unlocked with unlock() | - | ||||||||||||
174 | before another thread can successfully lock it for writing. | - | ||||||||||||
175 | - | |||||||||||||
176 | It is not possible to lock for read if the thread already has | - | ||||||||||||
177 | locked for write. | - | ||||||||||||
178 | - | |||||||||||||
179 | \sa unlock(), lockForRead() | - | ||||||||||||
180 | */ | - | ||||||||||||
181 | bool QReadWriteLock::tryLockForRead() | - | ||||||||||||
182 | { | - | ||||||||||||
183 | QMutexLocker lock(&d->mutex); | - | ||||||||||||
184 | - | |||||||||||||
185 | Qt::HANDLE self = 0; | - | ||||||||||||
186 | if (d->recursive) {
| 7-36 | ||||||||||||
187 | self = QThread::currentThreadId(); | - | ||||||||||||
188 | - | |||||||||||||
189 | QHash<Qt::HANDLE, int>::iterator it = d->currentReaders.find(self); | - | ||||||||||||
190 | if (it != d->currentReaders.end()) {
| 11-25 | ||||||||||||
191 | ++it.value(); | - | ||||||||||||
192 | ++d->accessCount; | - | ||||||||||||
193 | Q_ASSERT_X(d->accessCount > 0, "QReadWriteLock::tryLockForRead()", | - | ||||||||||||
194 | "Overflow in lock counter"); | - | ||||||||||||
195 | return true; executed 11 times by 2 tests: return true; Executed by:
| 11 | ||||||||||||
196 | } | - | ||||||||||||
197 | } executed 25 times by 2 tests: end of block Executed by:
| 25 | ||||||||||||
198 | - | |||||||||||||
199 | if (d->accessCount < 0)
| 9-23 | ||||||||||||
200 | return false; executed 23 times by 2 tests: return false; Executed by:
| 23 | ||||||||||||
201 | if (d->recursive)
| 4-5 | ||||||||||||
202 | d->currentReaders.insert(self, 1); executed 4 times by 2 tests: d->currentReaders.insert(self, 1); Executed by:
| 4 | ||||||||||||
203 | - | |||||||||||||
204 | ++d->accessCount; | - | ||||||||||||
205 | Q_ASSERT_X(d->accessCount > 0, "QReadWriteLock::tryLockForRead()", "Overflow in lock counter"); | - | ||||||||||||
206 | - | |||||||||||||
207 | return true; executed 9 times by 2 tests: return true; Executed by:
| 9 | ||||||||||||
208 | } | - | ||||||||||||
209 | - | |||||||||||||
210 | /*! \overload | - | ||||||||||||
211 | - | |||||||||||||
212 | Attempts to lock for reading. This function returns \c true if the | - | ||||||||||||
213 | lock was obtained; otherwise it returns \c false. If another thread | - | ||||||||||||
214 | has locked for writing, this function will wait for at most \a | - | ||||||||||||
215 | timeout milliseconds for the lock to become available. | - | ||||||||||||
216 | - | |||||||||||||
217 | Note: Passing a negative number as the \a timeout is equivalent to | - | ||||||||||||
218 | calling lockForRead(), i.e. this function will wait forever until | - | ||||||||||||
219 | lock can be locked for reading when \a timeout is negative. | - | ||||||||||||
220 | - | |||||||||||||
221 | If the lock was obtained, the lock must be unlocked with unlock() | - | ||||||||||||
222 | before another thread can successfully lock it for writing. | - | ||||||||||||
223 | - | |||||||||||||
224 | It is not possible to lock for read if the thread already has | - | ||||||||||||
225 | locked for write. | - | ||||||||||||
226 | - | |||||||||||||
227 | \sa unlock(), lockForRead() | - | ||||||||||||
228 | */ | - | ||||||||||||
229 | bool QReadWriteLock::tryLockForRead(int timeout) | - | ||||||||||||
230 | { | - | ||||||||||||
231 | QMutexLocker lock(&d->mutex); | - | ||||||||||||
232 | - | |||||||||||||
233 | Qt::HANDLE self = 0; | - | ||||||||||||
234 | if (d->recursive) {
| 0-3 | ||||||||||||
235 | self = QThread::currentThreadId(); | - | ||||||||||||
236 | - | |||||||||||||
237 | QHash<Qt::HANDLE, int>::iterator it = d->currentReaders.find(self); | - | ||||||||||||
238 | if (it != d->currentReaders.end()) {
| 0 | ||||||||||||
239 | ++it.value(); | - | ||||||||||||
240 | ++d->accessCount; | - | ||||||||||||
241 | Q_ASSERT_X(d->accessCount > 0, "QReadWriteLock::tryLockForRead()", | - | ||||||||||||
242 | "Overflow in lock counter"); | - | ||||||||||||
243 | return true; never executed: return true; | 0 | ||||||||||||
244 | } | - | ||||||||||||
245 | } never executed: end of block | 0 | ||||||||||||
246 | - | |||||||||||||
247 | while (d->accessCount < 0 || d->waitingWriters) {
| 0-2 | ||||||||||||
248 | ++d->waitingReaders; | - | ||||||||||||
249 | bool success = d->readerWait.wait(&d->mutex, timeout < 0 ? ULONG_MAX : ulong(timeout)); | - | ||||||||||||
250 | --d->waitingReaders; | - | ||||||||||||
251 | if (!success)
| 0-1 | ||||||||||||
252 | return false; executed 1 time by 1 test: return false; Executed by:
| 1 | ||||||||||||
253 | } never executed: end of block | 0 | ||||||||||||
254 | if (d->recursive)
| 0-2 | ||||||||||||
255 | d->currentReaders.insert(self, 1); never executed: d->currentReaders.insert(self, 1); | 0 | ||||||||||||
256 | - | |||||||||||||
257 | ++d->accessCount; | - | ||||||||||||
258 | Q_ASSERT_X(d->accessCount > 0, "QReadWriteLock::tryLockForRead()", "Overflow in lock counter"); | - | ||||||||||||
259 | - | |||||||||||||
260 | return true; executed 2 times by 1 test: return true; Executed by:
| 2 | ||||||||||||
261 | } | - | ||||||||||||
262 | - | |||||||||||||
263 | /*! | - | ||||||||||||
264 | Locks the lock for writing. This function will block the current | - | ||||||||||||
265 | thread if another thread (including the current) has locked for | - | ||||||||||||
266 | reading or writing (unless the lock has been created using the | - | ||||||||||||
267 | \l{QReadWriteLock::Recursive} mode). | - | ||||||||||||
268 | - | |||||||||||||
269 | It is not possible to lock for write if the thread already has | - | ||||||||||||
270 | locked for read. | - | ||||||||||||
271 | - | |||||||||||||
272 | \sa unlock(), lockForRead(), tryLockForWrite() | - | ||||||||||||
273 | */ | - | ||||||||||||
274 | void QReadWriteLock::lockForWrite() | - | ||||||||||||
275 | { | - | ||||||||||||
276 | QMutexLocker lock(&d->mutex); | - | ||||||||||||
277 | - | |||||||||||||
278 | Qt::HANDLE self = 0; | - | ||||||||||||
279 | if (d->recursive) {
| 37-330454 | ||||||||||||
280 | self = QThread::currentThreadId(); | - | ||||||||||||
281 | - | |||||||||||||
282 | if (d->currentWriter == self) {
| 0-37 | ||||||||||||
283 | --d->accessCount; | - | ||||||||||||
284 | Q_ASSERT_X(d->accessCount < 0, "QReadWriteLock::lockForWrite()", | - | ||||||||||||
285 | "Overflow in lock counter"); | - | ||||||||||||
286 | return; never executed: return; | 0 | ||||||||||||
287 | } | - | ||||||||||||
288 | } executed 37 times by 8 tests: end of block Executed by:
| 37 | ||||||||||||
289 | - | |||||||||||||
290 | while (d->accessCount != 0) {
| 261-330491 | ||||||||||||
291 | ++d->waitingWriters; | - | ||||||||||||
292 | d->writerWait.wait(&d->mutex); | - | ||||||||||||
293 | --d->waitingWriters; | - | ||||||||||||
294 | } executed 261 times by 5 tests: end of block Executed by:
| 261 | ||||||||||||
295 | if (d->recursive)
| 37-330454 | ||||||||||||
296 | d->currentWriter = self; executed 37 times by 8 tests: d->currentWriter = self; Executed by:
| 37 | ||||||||||||
297 | - | |||||||||||||
298 | --d->accessCount; | - | ||||||||||||
299 | Q_ASSERT_X(d->accessCount < 0, "QReadWriteLock::lockForWrite()", "Overflow in lock counter"); | - | ||||||||||||
300 | } executed 330491 times by 563 tests: end of block Executed by:
| 330491 | ||||||||||||
301 | - | |||||||||||||
302 | /*! | - | ||||||||||||
303 | Attempts to lock for writing. If the lock was obtained, this | - | ||||||||||||
304 | function returns \c true; otherwise, it returns \c false immediately. | - | ||||||||||||
305 | - | |||||||||||||
306 | The lock attempt will fail if another thread has locked for | - | ||||||||||||
307 | reading or writing. | - | ||||||||||||
308 | - | |||||||||||||
309 | If the lock was obtained, the lock must be unlocked with unlock() | - | ||||||||||||
310 | before another thread can successfully lock it. | - | ||||||||||||
311 | - | |||||||||||||
312 | It is not possible to lock for write if the thread already has | - | ||||||||||||
313 | locked for read. | - | ||||||||||||
314 | - | |||||||||||||
315 | \sa unlock(), lockForWrite() | - | ||||||||||||
316 | */ | - | ||||||||||||
317 | bool QReadWriteLock::tryLockForWrite() | - | ||||||||||||
318 | { | - | ||||||||||||
319 | QMutexLocker lock(&d->mutex); | - | ||||||||||||
320 | - | |||||||||||||
321 | Qt::HANDLE self = 0; | - | ||||||||||||
322 | if (d->recursive) {
| 23-39 | ||||||||||||
323 | self = QThread::currentThreadId(); | - | ||||||||||||
324 | - | |||||||||||||
325 | if (d->currentWriter == self) {
| 12-27 | ||||||||||||
326 | --d->accessCount; | - | ||||||||||||
327 | Q_ASSERT_X(d->accessCount < 0, "QReadWriteLock::lockForWrite()", | - | ||||||||||||
328 | "Overflow in lock counter"); | - | ||||||||||||
329 | return true; executed 12 times by 2 tests: return true; Executed by:
| 12 | ||||||||||||
330 | } | - | ||||||||||||
331 | } executed 27 times by 2 tests: end of block Executed by:
| 27 | ||||||||||||
332 | - | |||||||||||||
333 | if (d->accessCount != 0)
| 18-32 | ||||||||||||
334 | return false; executed 32 times by 4 tests: return false; Executed by:
| 32 | ||||||||||||
335 | if (d->recursive)
| 5-13 | ||||||||||||
336 | d->currentWriter = self; executed 5 times by 2 tests: d->currentWriter = self; Executed by:
| 5 | ||||||||||||
337 | - | |||||||||||||
338 | --d->accessCount; | - | ||||||||||||
339 | Q_ASSERT_X(d->accessCount < 0, "QReadWriteLock::tryLockForWrite()", | - | ||||||||||||
340 | "Overflow in lock counter"); | - | ||||||||||||
341 | - | |||||||||||||
342 | return true; executed 18 times by 4 tests: return true; Executed by:
| 18 | ||||||||||||
343 | } | - | ||||||||||||
344 | - | |||||||||||||
345 | /*! \overload | - | ||||||||||||
346 | - | |||||||||||||
347 | Attempts to lock for writing. This function returns \c true if the | - | ||||||||||||
348 | lock was obtained; otherwise it returns \c false. If another thread | - | ||||||||||||
349 | has locked for reading or writing, this function will wait for at | - | ||||||||||||
350 | most \a timeout milliseconds for the lock to become available. | - | ||||||||||||
351 | - | |||||||||||||
352 | Note: Passing a negative number as the \a timeout is equivalent to | - | ||||||||||||
353 | calling lockForWrite(), i.e. this function will wait forever until | - | ||||||||||||
354 | lock can be locked for writing when \a timeout is negative. | - | ||||||||||||
355 | - | |||||||||||||
356 | If the lock was obtained, the lock must be unlocked with unlock() | - | ||||||||||||
357 | before another thread can successfully lock it. | - | ||||||||||||
358 | - | |||||||||||||
359 | It is not possible to lock for write if the thread already has | - | ||||||||||||
360 | locked for read. | - | ||||||||||||
361 | - | |||||||||||||
362 | \sa unlock(), lockForWrite() | - | ||||||||||||
363 | */ | - | ||||||||||||
364 | bool QReadWriteLock::tryLockForWrite(int timeout) | - | ||||||||||||
365 | { | - | ||||||||||||
366 | QMutexLocker lock(&d->mutex); | - | ||||||||||||
367 | - | |||||||||||||
368 | Qt::HANDLE self = 0; | - | ||||||||||||
369 | if (d->recursive) {
| 0-2 | ||||||||||||
370 | self = QThread::currentThreadId(); | - | ||||||||||||
371 | - | |||||||||||||
372 | if (d->currentWriter == self) {
| 0 | ||||||||||||
373 | --d->accessCount; | - | ||||||||||||
374 | Q_ASSERT_X(d->accessCount < 0, "QReadWriteLock::lockForWrite()", | - | ||||||||||||
375 | "Overflow in lock counter"); | - | ||||||||||||
376 | return true; never executed: return true; | 0 | ||||||||||||
377 | } | - | ||||||||||||
378 | } never executed: end of block | 0 | ||||||||||||
379 | - | |||||||||||||
380 | while (d->accessCount != 0) {
| 1 | ||||||||||||
381 | ++d->waitingWriters; | - | ||||||||||||
382 | bool success = d->writerWait.wait(&d->mutex, timeout < 0 ? ULONG_MAX : ulong(timeout)); | - | ||||||||||||
383 | --d->waitingWriters; | - | ||||||||||||
384 | - | |||||||||||||
385 | if (!success)
| 0-1 | ||||||||||||
386 | return false; executed 1 time by 1 test: return false; Executed by:
| 1 | ||||||||||||
387 | } never executed: end of block | 0 | ||||||||||||
388 | if (d->recursive)
| 0-1 | ||||||||||||
389 | d->currentWriter = self; never executed: d->currentWriter = self; | 0 | ||||||||||||
390 | - | |||||||||||||
391 | --d->accessCount; | - | ||||||||||||
392 | Q_ASSERT_X(d->accessCount < 0, "QReadWriteLock::tryLockForWrite()", | - | ||||||||||||
393 | "Overflow in lock counter"); | - | ||||||||||||
394 | - | |||||||||||||
395 | return true; executed 1 time by 1 test: return true; Executed by:
| 1 | ||||||||||||
396 | } | - | ||||||||||||
397 | - | |||||||||||||
398 | /*! | - | ||||||||||||
399 | Unlocks the lock. | - | ||||||||||||
400 | - | |||||||||||||
401 | Attempting to unlock a lock that is not locked is an error, and will result | - | ||||||||||||
402 | in program termination. | - | ||||||||||||
403 | - | |||||||||||||
404 | \sa lockForRead(), lockForWrite(), tryLockForRead(), tryLockForWrite() | - | ||||||||||||
405 | */ | - | ||||||||||||
406 | void QReadWriteLock::unlock() | - | ||||||||||||
407 | { | - | ||||||||||||
408 | QMutexLocker lock(&d->mutex); | - | ||||||||||||
409 | - | |||||||||||||
410 | Q_ASSERT_X(d->accessCount != 0, "QReadWriteLock::unlock()", "Cannot unlock an unlocked lock"); | - | ||||||||||||
411 | - | |||||||||||||
412 | bool unlocked = false; | - | ||||||||||||
413 | if (d->accessCount > 0) {
| 330522-1685842 | ||||||||||||
414 | // releasing a read lock | - | ||||||||||||
415 | if (d->recursive) {
| 105-1685737 | ||||||||||||
416 | Qt::HANDLE self = QThread::currentThreadId(); | - | ||||||||||||
417 | QHash<Qt::HANDLE, int>::iterator it = d->currentReaders.find(self); | - | ||||||||||||
418 | if (it != d->currentReaders.end()) {
| 0-105 | ||||||||||||
419 | if (--it.value() <= 0)
| 19-86 | ||||||||||||
420 | d->currentReaders.erase(it); executed 86 times by 5 tests: d->currentReaders.erase(it); Executed by:
| 86 | ||||||||||||
421 | } executed 105 times by 5 tests: end of block Executed by:
| 105 | ||||||||||||
422 | } executed 105 times by 5 tests: end of block Executed by:
| 105 | ||||||||||||
423 | - | |||||||||||||
424 | unlocked = --d->accessCount == 0; | - | ||||||||||||
425 | } else if (d->accessCount < 0 && ++d->accessCount == 0) { executed 1685842 times by 357 tests: end of block Executed by:
| 0-1685842 | ||||||||||||
426 | // released a write lock | - | ||||||||||||
427 | unlocked = true; | - | ||||||||||||
428 | d->currentWriter = 0; | - | ||||||||||||
429 | } executed 330510 times by 564 tests: end of block Executed by:
| 330510 | ||||||||||||
430 | - | |||||||||||||
431 | if (unlocked) {
| 143932-1872432 | ||||||||||||
432 | if (d->waitingWriters) {
| 6953-1865479 | ||||||||||||
433 | d->writerWait.wakeOne(); | - | ||||||||||||
434 | } else if (d->waitingReaders) { executed 6953 times by 5 tests: end of block Executed by:
| 217-1865262 | ||||||||||||
435 | d->readerWait.wakeAll(); | - | ||||||||||||
436 | } executed 217 times by 3 tests: end of block Executed by:
| 217 | ||||||||||||
437 | } executed 1872432 times by 570 tests: end of block Executed by:
| 1872432 | ||||||||||||
438 | } executed 2016364 times by 570 tests: end of block Executed by:
| 2016364 | ||||||||||||
439 | - | |||||||||||||
440 | /*! | - | ||||||||||||
441 | \class QReadLocker | - | ||||||||||||
442 | \inmodule QtCore | - | ||||||||||||
443 | \brief The QReadLocker class is a convenience class that | - | ||||||||||||
444 | simplifies locking and unlocking read-write locks for read access. | - | ||||||||||||
445 | - | |||||||||||||
446 | \threadsafe | - | ||||||||||||
447 | - | |||||||||||||
448 | \ingroup thread | - | ||||||||||||
449 | - | |||||||||||||
450 | The purpose of QReadLocker (and QWriteLocker) is to simplify | - | ||||||||||||
451 | QReadWriteLock locking and unlocking. Locking and unlocking | - | ||||||||||||
452 | statements or in exception handling code is error-prone and | - | ||||||||||||
453 | difficult to debug. QReadLocker can be used in such situations | - | ||||||||||||
454 | to ensure that the state of the lock is always well-defined. | - | ||||||||||||
455 | - | |||||||||||||
456 | Here's an example that uses QReadLocker to lock and unlock a | - | ||||||||||||
457 | read-write lock for reading: | - | ||||||||||||
458 | - | |||||||||||||
459 | \snippet code/src_corelib_thread_qreadwritelock.cpp 1 | - | ||||||||||||
460 | - | |||||||||||||
461 | It is equivalent to the following code: | - | ||||||||||||
462 | - | |||||||||||||
463 | \snippet code/src_corelib_thread_qreadwritelock.cpp 2 | - | ||||||||||||
464 | - | |||||||||||||
465 | The QMutexLocker documentation shows examples where the use of a | - | ||||||||||||
466 | locker object greatly simplifies programming. | - | ||||||||||||
467 | - | |||||||||||||
468 | \sa QWriteLocker, QReadWriteLock | - | ||||||||||||
469 | */ | - | ||||||||||||
470 | - | |||||||||||||
471 | /*! | - | ||||||||||||
472 | \fn QReadLocker::QReadLocker(QReadWriteLock *lock) | - | ||||||||||||
473 | - | |||||||||||||
474 | Constructs a QReadLocker and locks \a lock for reading. The lock | - | ||||||||||||
475 | will be unlocked when the QReadLocker is destroyed. If \c lock is | - | ||||||||||||
476 | zero, QReadLocker does nothing. | - | ||||||||||||
477 | - | |||||||||||||
478 | \sa QReadWriteLock::lockForRead() | - | ||||||||||||
479 | */ | - | ||||||||||||
480 | - | |||||||||||||
481 | /*! | - | ||||||||||||
482 | \fn QReadLocker::~QReadLocker() | - | ||||||||||||
483 | - | |||||||||||||
484 | Destroys the QReadLocker and unlocks the lock that was passed to | - | ||||||||||||
485 | the constructor. | - | ||||||||||||
486 | - | |||||||||||||
487 | \sa QReadWriteLock::unlock() | - | ||||||||||||
488 | */ | - | ||||||||||||
489 | - | |||||||||||||
490 | /*! | - | ||||||||||||
491 | \fn void QReadLocker::unlock() | - | ||||||||||||
492 | - | |||||||||||||
493 | Unlocks the lock associated with this locker. | - | ||||||||||||
494 | - | |||||||||||||
495 | \sa QReadWriteLock::unlock() | - | ||||||||||||
496 | */ | - | ||||||||||||
497 | - | |||||||||||||
498 | /*! | - | ||||||||||||
499 | \fn void QReadLocker::relock() | - | ||||||||||||
500 | - | |||||||||||||
501 | Relocks an unlocked lock. | - | ||||||||||||
502 | - | |||||||||||||
503 | \sa unlock() | - | ||||||||||||
504 | */ | - | ||||||||||||
505 | - | |||||||||||||
506 | /*! | - | ||||||||||||
507 | \fn QReadWriteLock *QReadLocker::readWriteLock() const | - | ||||||||||||
508 | - | |||||||||||||
509 | Returns a pointer to the read-write lock that was passed | - | ||||||||||||
510 | to the constructor. | - | ||||||||||||
511 | */ | - | ||||||||||||
512 | - | |||||||||||||
513 | /*! | - | ||||||||||||
514 | \class QWriteLocker | - | ||||||||||||
515 | \inmodule QtCore | - | ||||||||||||
516 | \brief The QWriteLocker class is a convenience class that | - | ||||||||||||
517 | simplifies locking and unlocking read-write locks for write access. | - | ||||||||||||
518 | - | |||||||||||||
519 | \threadsafe | - | ||||||||||||
520 | - | |||||||||||||
521 | \ingroup thread | - | ||||||||||||
522 | - | |||||||||||||
523 | The purpose of QWriteLocker (and QReadLocker) is to simplify | - | ||||||||||||
524 | QReadWriteLock locking and unlocking. Locking and unlocking | - | ||||||||||||
525 | statements or in exception handling code is error-prone and | - | ||||||||||||
526 | difficult to debug. QWriteLocker can be used in such situations | - | ||||||||||||
527 | to ensure that the state of the lock is always well-defined. | - | ||||||||||||
528 | - | |||||||||||||
529 | Here's an example that uses QWriteLocker to lock and unlock a | - | ||||||||||||
530 | read-write lock for writing: | - | ||||||||||||
531 | - | |||||||||||||
532 | \snippet code/src_corelib_thread_qreadwritelock.cpp 3 | - | ||||||||||||
533 | - | |||||||||||||
534 | It is equivalent to the following code: | - | ||||||||||||
535 | - | |||||||||||||
536 | \snippet code/src_corelib_thread_qreadwritelock.cpp 4 | - | ||||||||||||
537 | - | |||||||||||||
538 | The QMutexLocker documentation shows examples where the use of a | - | ||||||||||||
539 | locker object greatly simplifies programming. | - | ||||||||||||
540 | - | |||||||||||||
541 | \sa QReadLocker, QReadWriteLock | - | ||||||||||||
542 | */ | - | ||||||||||||
543 | - | |||||||||||||
544 | /*! | - | ||||||||||||
545 | \fn QWriteLocker::QWriteLocker(QReadWriteLock *lock) | - | ||||||||||||
546 | - | |||||||||||||
547 | Constructs a QWriteLocker and locks \a lock for writing. The lock | - | ||||||||||||
548 | will be unlocked when the QWriteLocker is destroyed. If \c lock is | - | ||||||||||||
549 | zero, QWriteLocker does nothing. | - | ||||||||||||
550 | - | |||||||||||||
551 | \sa QReadWriteLock::lockForWrite() | - | ||||||||||||
552 | */ | - | ||||||||||||
553 | - | |||||||||||||
554 | /*! | - | ||||||||||||
555 | \fn QWriteLocker::~QWriteLocker() | - | ||||||||||||
556 | - | |||||||||||||
557 | Destroys the QWriteLocker and unlocks the lock that was passed to | - | ||||||||||||
558 | the constructor. | - | ||||||||||||
559 | - | |||||||||||||
560 | \sa QReadWriteLock::unlock() | - | ||||||||||||
561 | */ | - | ||||||||||||
562 | - | |||||||||||||
563 | /*! | - | ||||||||||||
564 | \fn void QWriteLocker::unlock() | - | ||||||||||||
565 | - | |||||||||||||
566 | Unlocks the lock associated with this locker. | - | ||||||||||||
567 | - | |||||||||||||
568 | \sa QReadWriteLock::unlock() | - | ||||||||||||
569 | */ | - | ||||||||||||
570 | - | |||||||||||||
571 | /*! | - | ||||||||||||
572 | \fn void QWriteLocker::relock() | - | ||||||||||||
573 | - | |||||||||||||
574 | Relocks an unlocked lock. | - | ||||||||||||
575 | - | |||||||||||||
576 | \sa unlock() | - | ||||||||||||
577 | */ | - | ||||||||||||
578 | - | |||||||||||||
579 | /*! | - | ||||||||||||
580 | \fn QReadWriteLock *QWriteLocker::readWriteLock() const | - | ||||||||||||
581 | - | |||||||||||||
582 | Returns a pointer to the read-write lock that was passed | - | ||||||||||||
583 | to the constructor. | - | ||||||||||||
584 | */ | - | ||||||||||||
585 | - | |||||||||||||
586 | QT_END_NAMESPACE | - | ||||||||||||
587 | - | |||||||||||||
588 | #endif // QT_NO_THREAD | - | ||||||||||||
Source code | Switch to Preprocessed file |