1 | /** @file
|
---|
2 | *
|
---|
3 | * Automatic locks, implementation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2009 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include "AutoLock.h"
|
---|
23 |
|
---|
24 | #include "Logging.h"
|
---|
25 |
|
---|
26 | #include <iprt/cdefs.h>
|
---|
27 | #include <iprt/critsect.h>
|
---|
28 | #include <iprt/thread.h>
|
---|
29 | #include <iprt/semaphore.h>
|
---|
30 |
|
---|
31 | #include <iprt/err.h>
|
---|
32 | #include <iprt/assert.h>
|
---|
33 |
|
---|
34 | #if defined(DEBUG)
|
---|
35 | # include <iprt/asm.h> // for ASMReturnAddress
|
---|
36 | #endif
|
---|
37 |
|
---|
38 | #include <iprt/string.h>
|
---|
39 | #include <iprt/path.h>
|
---|
40 | #include <iprt/stream.h>
|
---|
41 |
|
---|
42 | #include <VBox/com/string.h>
|
---|
43 |
|
---|
44 | #include <vector>
|
---|
45 | #include <list>
|
---|
46 |
|
---|
47 | namespace util
|
---|
48 | {
|
---|
49 |
|
---|
50 | ////////////////////////////////////////////////////////////////////////////////
|
---|
51 | //
|
---|
52 | // RuntimeLockClass
|
---|
53 | //
|
---|
54 | ////////////////////////////////////////////////////////////////////////////////
|
---|
55 |
|
---|
56 | class RuntimeLockClass
|
---|
57 | {
|
---|
58 | };
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Called from initterm.cpp on process initialization (on the main thread)
|
---|
62 | * to give us a chance to initialize lock validation runtime data.
|
---|
63 | */
|
---|
64 | void InitAutoLockSystem()
|
---|
65 | {
|
---|
66 | RTPrintf("InitAutoLockSystem\n");
|
---|
67 | }
|
---|
68 |
|
---|
69 | ////////////////////////////////////////////////////////////////////////////////
|
---|
70 | //
|
---|
71 | // RWLockHandle
|
---|
72 | //
|
---|
73 | ////////////////////////////////////////////////////////////////////////////////
|
---|
74 |
|
---|
75 | struct RWLockHandle::Data
|
---|
76 | {
|
---|
77 | Data()
|
---|
78 | { }
|
---|
79 |
|
---|
80 | RTSEMRW sem;
|
---|
81 | MainLockValidationClasses lockClass;
|
---|
82 |
|
---|
83 | #ifdef RT_LOCK_STRICT
|
---|
84 | com::Utf8Str strDescription;
|
---|
85 | #endif
|
---|
86 | };
|
---|
87 |
|
---|
88 | RWLockHandle::RWLockHandle(MainLockValidationClasses lockClass)
|
---|
89 | {
|
---|
90 | m = new Data();
|
---|
91 |
|
---|
92 | m->lockClass = lockClass;
|
---|
93 |
|
---|
94 | int vrc = RTSemRWCreateEx(&m->sem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
|
---|
95 | AssertRC(vrc);
|
---|
96 |
|
---|
97 | #ifdef RT_LOCK_STRICT
|
---|
98 | m->strDescription = com::Utf8StrFmt("r/w %RCv", this);
|
---|
99 | #endif
|
---|
100 | }
|
---|
101 |
|
---|
102 | /*virtual*/ RWLockHandle::~RWLockHandle()
|
---|
103 | {
|
---|
104 | RTSemRWDestroy(m->sem);
|
---|
105 | delete m;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /*virtual*/ bool RWLockHandle::isWriteLockOnCurrentThread() const
|
---|
109 | {
|
---|
110 | return RTSemRWIsWriteOwner(m->sem);
|
---|
111 | }
|
---|
112 |
|
---|
113 | /*virtual*/ void RWLockHandle::lockWrite(LOCKVAL_SRC_POS_DECL)
|
---|
114 | {
|
---|
115 | #if defined(RT_LOCK_STRICT)
|
---|
116 | int vrc = RTSemRWRequestWriteDebug(m->sem, RT_INDEFINITE_WAIT, (uintptr_t)ASMReturnAddress(), RT_SRC_POS_ARGS);
|
---|
117 | #else
|
---|
118 | int vrc = RTSemRWRequestWrite(m->sem, RT_INDEFINITE_WAIT);
|
---|
119 | #endif
|
---|
120 | AssertRC(vrc);
|
---|
121 | }
|
---|
122 |
|
---|
123 | /*virtual*/ void RWLockHandle::unlockWrite()
|
---|
124 | {
|
---|
125 | int vrc = RTSemRWReleaseWrite(m->sem);
|
---|
126 | AssertRC(vrc);
|
---|
127 |
|
---|
128 | }
|
---|
129 |
|
---|
130 | /*virtual*/ void RWLockHandle::lockRead(LOCKVAL_SRC_POS_DECL)
|
---|
131 | {
|
---|
132 | #if defined(RT_LOCK_STRICT)
|
---|
133 | int vrc = RTSemRWRequestReadDebug(m->sem, RT_INDEFINITE_WAIT, (uintptr_t)ASMReturnAddress(), RT_SRC_POS_ARGS);
|
---|
134 | #else
|
---|
135 | int vrc = RTSemRWRequestRead(m->sem, RT_INDEFINITE_WAIT);
|
---|
136 | #endif
|
---|
137 | AssertRC(vrc);
|
---|
138 | }
|
---|
139 |
|
---|
140 | /*virtual*/ void RWLockHandle::unlockRead()
|
---|
141 | {
|
---|
142 | int vrc = RTSemRWReleaseRead(m->sem);
|
---|
143 | AssertRC(vrc);
|
---|
144 | }
|
---|
145 |
|
---|
146 | /*virtual*/ uint32_t RWLockHandle::writeLockLevel() const
|
---|
147 | {
|
---|
148 | /* Note! This does not include read recursions done by the writer! */
|
---|
149 | return RTSemRWGetWriteRecursion(m->sem);
|
---|
150 | }
|
---|
151 |
|
---|
152 | #ifdef RT_LOCK_STRICT
|
---|
153 | /*virtual*/ const char* RWLockHandle::describe() const
|
---|
154 | {
|
---|
155 | return m->strDescription.c_str();
|
---|
156 | }
|
---|
157 | #endif
|
---|
158 |
|
---|
159 | ////////////////////////////////////////////////////////////////////////////////
|
---|
160 | //
|
---|
161 | // WriteLockHandle
|
---|
162 | //
|
---|
163 | ////////////////////////////////////////////////////////////////////////////////
|
---|
164 |
|
---|
165 | struct WriteLockHandle::Data
|
---|
166 | {
|
---|
167 | Data()
|
---|
168 | { }
|
---|
169 |
|
---|
170 | mutable RTCRITSECT sem;
|
---|
171 | MainLockValidationClasses lockClass;
|
---|
172 |
|
---|
173 | #ifdef RT_LOCK_STRICT
|
---|
174 | com::Utf8Str strDescription;
|
---|
175 | #endif
|
---|
176 | };
|
---|
177 |
|
---|
178 | WriteLockHandle::WriteLockHandle(MainLockValidationClasses lockClass)
|
---|
179 | {
|
---|
180 | m = new Data;
|
---|
181 |
|
---|
182 | m->lockClass = lockClass;
|
---|
183 |
|
---|
184 | int vrc = RTCritSectInitEx(&m->sem, 0/*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
|
---|
185 | AssertRC(vrc);
|
---|
186 |
|
---|
187 | #ifdef RT_LOCK_STRICT
|
---|
188 | m->strDescription = com::Utf8StrFmt("crit %RCv", this);
|
---|
189 | #endif
|
---|
190 | }
|
---|
191 |
|
---|
192 | WriteLockHandle::~WriteLockHandle()
|
---|
193 | {
|
---|
194 | RTCritSectDelete(&m->sem);
|
---|
195 | delete m;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /*virtual*/ bool WriteLockHandle::isWriteLockOnCurrentThread() const
|
---|
199 | {
|
---|
200 | return RTCritSectIsOwner(&m->sem);
|
---|
201 | }
|
---|
202 |
|
---|
203 | /*virtual*/ void WriteLockHandle::lockWrite(LOCKVAL_SRC_POS_DECL)
|
---|
204 | {
|
---|
205 | #if defined(RT_LOCK_STRICT)
|
---|
206 | RTCritSectEnterDebug(&m->sem, (uintptr_t)ASMReturnAddress(), RT_SRC_POS_ARGS);
|
---|
207 | #else
|
---|
208 | RTCritSectEnter(&m->sem);
|
---|
209 | #endif
|
---|
210 | }
|
---|
211 |
|
---|
212 | /*virtual*/ void WriteLockHandle::unlockWrite()
|
---|
213 | {
|
---|
214 | RTCritSectLeave(&m->sem);
|
---|
215 | }
|
---|
216 |
|
---|
217 | /*virtual*/ void WriteLockHandle::lockRead(LOCKVAL_SRC_POS_DECL)
|
---|
218 | {
|
---|
219 | lockWrite(LOCKVAL_SRC_POS_ARGS);
|
---|
220 | }
|
---|
221 |
|
---|
222 | /*virtual*/ void WriteLockHandle::unlockRead()
|
---|
223 | {
|
---|
224 | unlockWrite();
|
---|
225 | }
|
---|
226 |
|
---|
227 | /*virtual*/ uint32_t WriteLockHandle::writeLockLevel() const
|
---|
228 | {
|
---|
229 | return RTCritSectGetRecursion(&m->sem);
|
---|
230 | }
|
---|
231 |
|
---|
232 | #ifdef RT_LOCK_STRICT
|
---|
233 | /*virtual*/ const char* WriteLockHandle::describe() const
|
---|
234 | {
|
---|
235 | return m->strDescription.c_str();
|
---|
236 | }
|
---|
237 | #endif
|
---|
238 |
|
---|
239 | ////////////////////////////////////////////////////////////////////////////////
|
---|
240 | //
|
---|
241 | // AutoLockBase
|
---|
242 | //
|
---|
243 | ////////////////////////////////////////////////////////////////////////////////
|
---|
244 |
|
---|
245 | typedef std::vector<LockHandle*> HandlesVector;
|
---|
246 | typedef std::vector<uint32_t> CountsVector;
|
---|
247 |
|
---|
248 | struct AutoLockBase::Data
|
---|
249 | {
|
---|
250 | Data(size_t cHandles
|
---|
251 | #ifdef RT_LOCK_STRICT
|
---|
252 | , const char *pcszFile_,
|
---|
253 | unsigned uLine_,
|
---|
254 | const char *pcszFunction_
|
---|
255 | #endif
|
---|
256 | )
|
---|
257 | : fIsLocked(false),
|
---|
258 | aHandles(cHandles), // size of array
|
---|
259 | acUnlockedInLeave(cHandles)
|
---|
260 | #ifdef RT_LOCK_STRICT
|
---|
261 | , pcszFile(pcszFile_),
|
---|
262 | uLine(uLine_),
|
---|
263 | pcszFunction(pcszFunction_)
|
---|
264 | #endif
|
---|
265 | {
|
---|
266 | for (uint32_t i = 0; i < cHandles; ++i)
|
---|
267 | {
|
---|
268 | acUnlockedInLeave[i] = 0;
|
---|
269 | aHandles[i] = NULL;
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | bool fIsLocked; // if true, then all items in aHandles are locked by this AutoLock and
|
---|
274 | // need to be unlocked in the destructor
|
---|
275 | HandlesVector aHandles; // array (vector) of LockHandle instances; in the case of AutoWriteLock
|
---|
276 | // and AutoReadLock, there will only be one item on the list; with the
|
---|
277 | // AutoMulti* derivatives, there will be multiple
|
---|
278 | CountsVector acUnlockedInLeave; // for each lock handle, how many times the handle was unlocked in leave(); otherwise 0
|
---|
279 |
|
---|
280 | #ifdef RT_LOCK_STRICT
|
---|
281 | // information about where the lock occured (passed down from the AutoLock classes)
|
---|
282 | const char *pcszFile;
|
---|
283 | unsigned uLine;
|
---|
284 | const char *pcszFunction;
|
---|
285 | #endif
|
---|
286 | };
|
---|
287 |
|
---|
288 | AutoLockBase::AutoLockBase(uint32_t cHandles
|
---|
289 | COMMA_LOCKVAL_SRC_POS_DECL)
|
---|
290 | {
|
---|
291 | m = new Data(cHandles
|
---|
292 | COMMA_LOCKVAL_SRC_POS_ARGS);
|
---|
293 | }
|
---|
294 |
|
---|
295 | AutoLockBase::AutoLockBase(uint32_t cHandles,
|
---|
296 | LockHandle *pHandle
|
---|
297 | COMMA_LOCKVAL_SRC_POS_DECL)
|
---|
298 | {
|
---|
299 | Assert(cHandles == 1);
|
---|
300 | m = new Data(1
|
---|
301 | COMMA_LOCKVAL_SRC_POS_ARGS);
|
---|
302 | m->aHandles[0] = pHandle;
|
---|
303 | }
|
---|
304 |
|
---|
305 | AutoLockBase::~AutoLockBase()
|
---|
306 | {
|
---|
307 | delete m;
|
---|
308 | }
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * Requests ownership of all contained lock handles by calling
|
---|
312 | * the pure virtual callLockImpl() function on each of them,
|
---|
313 | * which must be implemented by the descendant class; in the
|
---|
314 | * implementation, AutoWriteLock will request a write lock
|
---|
315 | * whereas AutoReadLock will request a read lock.
|
---|
316 | *
|
---|
317 | * Does *not* modify the lock counts in the member variables.
|
---|
318 | */
|
---|
319 | void AutoLockBase::callLockOnAllHandles()
|
---|
320 | {
|
---|
321 | for (HandlesVector::iterator it = m->aHandles.begin();
|
---|
322 | it != m->aHandles.end();
|
---|
323 | ++it)
|
---|
324 | {
|
---|
325 | LockHandle *pHandle = *it;
|
---|
326 | if (pHandle)
|
---|
327 | // call virtual function implemented in AutoWriteLock or AutoReadLock
|
---|
328 | this->callLockImpl(*pHandle);
|
---|
329 | }
|
---|
330 | }
|
---|
331 |
|
---|
332 | /**
|
---|
333 | * Releases ownership of all contained lock handles by calling
|
---|
334 | * the pure virtual callUnlockImpl() function on each of them,
|
---|
335 | * which must be implemented by the descendant class; in the
|
---|
336 | * implementation, AutoWriteLock will release a write lock
|
---|
337 | * whereas AutoReadLock will release a read lock.
|
---|
338 | *
|
---|
339 | * Does *not* modify the lock counts in the member variables.
|
---|
340 | */
|
---|
341 | void AutoLockBase::callUnlockOnAllHandles()
|
---|
342 | {
|
---|
343 | // unlock in reverse order!
|
---|
344 | for (HandlesVector::reverse_iterator it = m->aHandles.rbegin();
|
---|
345 | it != m->aHandles.rend();
|
---|
346 | ++it)
|
---|
347 | {
|
---|
348 | LockHandle *pHandle = *it;
|
---|
349 | if (pHandle)
|
---|
350 | // call virtual function implemented in AutoWriteLock or AutoReadLock
|
---|
351 | this->callUnlockImpl(*pHandle);
|
---|
352 | }
|
---|
353 | }
|
---|
354 |
|
---|
355 | /**
|
---|
356 | * Destructor implementation that can also be called explicitly, if required.
|
---|
357 | * Restores the exact state before the AutoLock was created; that is, unlocks
|
---|
358 | * all contained semaphores and might actually lock them again if leave()
|
---|
359 | * was called during the AutoLock's lifetime.
|
---|
360 | */
|
---|
361 | void AutoLockBase::cleanup()
|
---|
362 | {
|
---|
363 | bool fAnyUnlockedInLeave = false;
|
---|
364 |
|
---|
365 | uint32_t i = 0;
|
---|
366 | for (HandlesVector::iterator it = m->aHandles.begin();
|
---|
367 | it != m->aHandles.end();
|
---|
368 | ++it)
|
---|
369 | {
|
---|
370 | LockHandle *pHandle = *it;
|
---|
371 | if (pHandle)
|
---|
372 | {
|
---|
373 | if (m->acUnlockedInLeave[i])
|
---|
374 | {
|
---|
375 | // there was a leave() before the destruction: then restore the
|
---|
376 | // lock level that might have been set by locks other than our own
|
---|
377 | if (m->fIsLocked)
|
---|
378 | {
|
---|
379 | --m->acUnlockedInLeave[i];
|
---|
380 | fAnyUnlockedInLeave = true;
|
---|
381 | }
|
---|
382 | for (; m->acUnlockedInLeave[i]; --m->acUnlockedInLeave[i])
|
---|
383 | callLockImpl(*pHandle);
|
---|
384 | }
|
---|
385 | }
|
---|
386 | ++i;
|
---|
387 | }
|
---|
388 |
|
---|
389 | if (m->fIsLocked && !fAnyUnlockedInLeave)
|
---|
390 | callUnlockOnAllHandles();
|
---|
391 | }
|
---|
392 |
|
---|
393 | /**
|
---|
394 | * Requests ownership of all contained semaphores. Public method that can
|
---|
395 | * only be called once and that also gets called by the AutoLock constructors.
|
---|
396 | */
|
---|
397 | void AutoLockBase::acquire()
|
---|
398 | {
|
---|
399 | AssertMsg(!m->fIsLocked, ("m->fIsLocked is true, attempting to lock twice!"));
|
---|
400 | callLockOnAllHandles();
|
---|
401 | m->fIsLocked = true;
|
---|
402 | }
|
---|
403 |
|
---|
404 | /**
|
---|
405 | * Releases ownership of all contained semaphores. Public method.
|
---|
406 | */
|
---|
407 | void AutoLockBase::release()
|
---|
408 | {
|
---|
409 | AssertMsg(m->fIsLocked, ("m->fIsLocked is false, cannot release!"));
|
---|
410 | callUnlockOnAllHandles();
|
---|
411 | m->fIsLocked = false;
|
---|
412 | }
|
---|
413 |
|
---|
414 | ////////////////////////////////////////////////////////////////////////////////
|
---|
415 | //
|
---|
416 | // AutoReadLock
|
---|
417 | //
|
---|
418 | ////////////////////////////////////////////////////////////////////////////////
|
---|
419 |
|
---|
420 | /**
|
---|
421 | * Release all read locks acquired by this instance through the #lock()
|
---|
422 | * call and destroys the instance.
|
---|
423 | *
|
---|
424 | * Note that if there there are nested #lock() calls without the
|
---|
425 | * corresponding number of #unlock() calls when the destructor is called, it
|
---|
426 | * will assert. This is because having an unbalanced number of nested locks
|
---|
427 | * is a program logic error which must be fixed.
|
---|
428 | */
|
---|
429 | /*virtual*/ AutoReadLock::~AutoReadLock()
|
---|
430 | {
|
---|
431 | LockHandle *pHandle = m->aHandles[0];
|
---|
432 |
|
---|
433 | if (pHandle)
|
---|
434 | {
|
---|
435 | if (m->fIsLocked)
|
---|
436 | callUnlockImpl(*pHandle);
|
---|
437 | }
|
---|
438 | }
|
---|
439 |
|
---|
440 | /**
|
---|
441 | * Implementation of the pure virtual declared in AutoLockBase.
|
---|
442 | * This gets called by AutoLockBase.acquire() to actually request
|
---|
443 | * the semaphore; in the AutoReadLock implementation, we request
|
---|
444 | * the semaphore in read mode.
|
---|
445 | */
|
---|
446 | /*virtual*/ void AutoReadLock::callLockImpl(LockHandle &l)
|
---|
447 | {
|
---|
448 | #ifdef RT_LOCK_STRICT
|
---|
449 | l.lockRead(m->pcszFile, m->uLine, m->pcszFunction);
|
---|
450 | #else
|
---|
451 | l.lockRead();
|
---|
452 | #endif
|
---|
453 | }
|
---|
454 |
|
---|
455 | /**
|
---|
456 | * Implementation of the pure virtual declared in AutoLockBase.
|
---|
457 | * This gets called by AutoLockBase.release() to actually release
|
---|
458 | * the semaphore; in the AutoReadLock implementation, we release
|
---|
459 | * the semaphore in read mode.
|
---|
460 | */
|
---|
461 | /*virtual*/ void AutoReadLock::callUnlockImpl(LockHandle &l)
|
---|
462 | {
|
---|
463 | l.unlockRead();
|
---|
464 | }
|
---|
465 |
|
---|
466 | ////////////////////////////////////////////////////////////////////////////////
|
---|
467 | //
|
---|
468 | // AutoWriteLockBase
|
---|
469 | //
|
---|
470 | ////////////////////////////////////////////////////////////////////////////////
|
---|
471 |
|
---|
472 | /**
|
---|
473 | * Implementation of the pure virtual declared in AutoLockBase.
|
---|
474 | * This gets called by AutoLockBase.acquire() to actually request
|
---|
475 | * the semaphore; in the AutoWriteLock implementation, we request
|
---|
476 | * the semaphore in write mode.
|
---|
477 | */
|
---|
478 | /*virtual*/ void AutoWriteLockBase::callLockImpl(LockHandle &l)
|
---|
479 | {
|
---|
480 | #ifdef RT_LOCK_STRICT
|
---|
481 | l.lockWrite(m->pcszFile, m->uLine, m->pcszFunction);
|
---|
482 | #else
|
---|
483 | l.lockWrite();
|
---|
484 | #endif
|
---|
485 | }
|
---|
486 |
|
---|
487 | /**
|
---|
488 | * Implementation of the pure virtual declared in AutoLockBase.
|
---|
489 | * This gets called by AutoLockBase.release() to actually release
|
---|
490 | * the semaphore; in the AutoWriteLock implementation, we release
|
---|
491 | * the semaphore in write mode.
|
---|
492 | */
|
---|
493 | /*virtual*/ void AutoWriteLockBase::callUnlockImpl(LockHandle &l)
|
---|
494 | {
|
---|
495 | l.unlockWrite();
|
---|
496 | }
|
---|
497 |
|
---|
498 | /**
|
---|
499 | * Causes the current thread to completely release the write lock to make
|
---|
500 | * the managed semaphore immediately available for locking by other threads.
|
---|
501 | *
|
---|
502 | * This implies that all nested write locks on the semaphore will be
|
---|
503 | * released, even those that were acquired through the calls to #lock()
|
---|
504 | * methods of all other AutoWriteLock/AutoReadLock instances managing the
|
---|
505 | * <b>same</b> read/write semaphore.
|
---|
506 | *
|
---|
507 | * After calling this method, the only method you are allowed to call is
|
---|
508 | * #enter(). It will acquire the write lock again and restore the same
|
---|
509 | * level of nesting as it had before calling #leave().
|
---|
510 | *
|
---|
511 | * If this instance is destroyed without calling #enter(), the destructor
|
---|
512 | * will try to restore the write lock level that existed when #leave() was
|
---|
513 | * called minus the number of nested #lock() calls made on this instance
|
---|
514 | * itself. This is done to preserve lock levels of other
|
---|
515 | * AutoWriteLock/AutoReadLock instances managing the same semaphore (if
|
---|
516 | * any). Tiis also means that the destructor may indefinitely block if a
|
---|
517 | * write or a read lock is owned by some other thread by that time.
|
---|
518 | */
|
---|
519 | void AutoWriteLockBase::leave()
|
---|
520 | {
|
---|
521 | AssertMsg(m->fIsLocked, ("m->fIsLocked is false, cannot leave()!"));
|
---|
522 |
|
---|
523 | // unlock in reverse order!
|
---|
524 | uint32_t i = m->aHandles.size();
|
---|
525 | for (HandlesVector::reverse_iterator it = m->aHandles.rbegin();
|
---|
526 | it != m->aHandles.rend();
|
---|
527 | ++it)
|
---|
528 | {
|
---|
529 | --i; // array index is zero based, decrement with every loop since we iterate backwards
|
---|
530 | LockHandle *pHandle = *it;
|
---|
531 | if (pHandle)
|
---|
532 | {
|
---|
533 | AssertMsg(m->acUnlockedInLeave[i] == 0, ("m->cUnlockedInLeave[%d] is %d, must be 0! Called leave() twice?", i, m->acUnlockedInLeave[i]));
|
---|
534 | m->acUnlockedInLeave[i] = pHandle->writeLockLevel();
|
---|
535 | AssertMsg(m->acUnlockedInLeave[i] >= 1, ("m->cUnlockedInLeave[%d] is %d, must be >=1!", i, m->acUnlockedInLeave[i]));
|
---|
536 |
|
---|
537 | #ifdef RT_LOCK_STRICT
|
---|
538 | LogFlowFunc(("LOCKVAL: will unlock handle %d [%s] %d times\n", i, pHandle->describe(), m->acUnlockedInLeave[i]));
|
---|
539 | #endif
|
---|
540 |
|
---|
541 | for (uint32_t left = m->acUnlockedInLeave[i];
|
---|
542 | left;
|
---|
543 | --left)
|
---|
544 | callUnlockImpl(*pHandle);
|
---|
545 | }
|
---|
546 | }
|
---|
547 | }
|
---|
548 |
|
---|
549 | /**
|
---|
550 | * Causes the current thread to restore the write lock level after the
|
---|
551 | * #leave() call. This call will indefinitely block if another thread has
|
---|
552 | * successfully acquired a write or a read lock on the same semaphore in
|
---|
553 | * between.
|
---|
554 | */
|
---|
555 | void AutoWriteLockBase::enter()
|
---|
556 | {
|
---|
557 | AssertMsg(m->fIsLocked, ("m->fIsLocked is false, cannot enter()!"));
|
---|
558 |
|
---|
559 | uint32_t i = 0;
|
---|
560 | for (HandlesVector::iterator it = m->aHandles.begin();
|
---|
561 | it != m->aHandles.end();
|
---|
562 | ++it)
|
---|
563 | {
|
---|
564 | LockHandle *pHandle = *it;
|
---|
565 | if (pHandle)
|
---|
566 | {
|
---|
567 | AssertMsg(m->acUnlockedInLeave[i] != 0, ("m->cUnlockedInLeave[%d] is 0! enter() without leave()?", i));
|
---|
568 |
|
---|
569 | #ifdef RT_LOCK_STRICT
|
---|
570 | LogFlowFunc(("LOCKVAL: will lock handle %d [%s] %d times\n", i, pHandle->describe(), m->acUnlockedInLeave[i]));
|
---|
571 | #endif
|
---|
572 |
|
---|
573 | for (; m->acUnlockedInLeave[i]; --m->acUnlockedInLeave[i])
|
---|
574 | callLockImpl(*pHandle);
|
---|
575 | }
|
---|
576 | ++i;
|
---|
577 | }
|
---|
578 | }
|
---|
579 |
|
---|
580 | /**
|
---|
581 | * Same as #leave() but checks if the current thread actally owns the lock
|
---|
582 | * and only proceeds in this case. As a result, as opposed to #leave(),
|
---|
583 | * doesn't assert when called with no lock being held.
|
---|
584 | */
|
---|
585 | void AutoWriteLockBase::maybeLeave()
|
---|
586 | {
|
---|
587 | // unlock in reverse order!
|
---|
588 | uint32_t i = m->aHandles.size();
|
---|
589 | for (HandlesVector::reverse_iterator it = m->aHandles.rbegin();
|
---|
590 | it != m->aHandles.rend();
|
---|
591 | ++it)
|
---|
592 | {
|
---|
593 | --i; // array index is zero based, decrement with every loop since we iterate backwards
|
---|
594 | LockHandle *pHandle = *it;
|
---|
595 | if (pHandle)
|
---|
596 | {
|
---|
597 | if (pHandle->isWriteLockOnCurrentThread())
|
---|
598 | {
|
---|
599 | m->acUnlockedInLeave[i] = pHandle->writeLockLevel();
|
---|
600 | AssertMsg(m->acUnlockedInLeave[i] >= 1, ("m->cUnlockedInLeave[%d] is %d, must be >=1!", i, m->acUnlockedInLeave[i]));
|
---|
601 |
|
---|
602 | #ifdef RT_LOCK_STRICT
|
---|
603 | LogFlowFunc(("LOCKVAL: will unlock handle %d [%s] %d times\n", i, pHandle->describe(), m->acUnlockedInLeave[i]));
|
---|
604 | #endif
|
---|
605 |
|
---|
606 | for (uint32_t left = m->acUnlockedInLeave[i];
|
---|
607 | left;
|
---|
608 | --left)
|
---|
609 | callUnlockImpl(*pHandle);
|
---|
610 | }
|
---|
611 | }
|
---|
612 | ++i;
|
---|
613 | }
|
---|
614 | }
|
---|
615 |
|
---|
616 | /**
|
---|
617 | * Same as #enter() but checks if the current thread actally owns the lock
|
---|
618 | * and only proceeds if not. As a result, as opposed to #enter(), doesn't
|
---|
619 | * assert when called with the lock already being held.
|
---|
620 | */
|
---|
621 | void AutoWriteLockBase::maybeEnter()
|
---|
622 | {
|
---|
623 | uint32_t i = 0;
|
---|
624 | for (HandlesVector::iterator it = m->aHandles.begin();
|
---|
625 | it != m->aHandles.end();
|
---|
626 | ++it)
|
---|
627 | {
|
---|
628 | LockHandle *pHandle = *it;
|
---|
629 | if (pHandle)
|
---|
630 | {
|
---|
631 | if (!pHandle->isWriteLockOnCurrentThread())
|
---|
632 | {
|
---|
633 | #ifdef RT_LOCK_STRICT
|
---|
634 | LogFlowFunc(("LOCKVAL: will lock handle %d [%s] %d times\n", i, pHandle->describe(), m->acUnlockedInLeave[i]));
|
---|
635 | #endif
|
---|
636 |
|
---|
637 | for (; m->acUnlockedInLeave[i]; --m->acUnlockedInLeave[i])
|
---|
638 | callLockImpl(*pHandle);
|
---|
639 | }
|
---|
640 | }
|
---|
641 | ++i;
|
---|
642 | }
|
---|
643 | }
|
---|
644 |
|
---|
645 | ////////////////////////////////////////////////////////////////////////////////
|
---|
646 | //
|
---|
647 | // AutoWriteLock
|
---|
648 | //
|
---|
649 | ////////////////////////////////////////////////////////////////////////////////
|
---|
650 |
|
---|
651 | /**
|
---|
652 | * Attaches another handle to this auto lock instance.
|
---|
653 | *
|
---|
654 | * The previous object's lock is completely released before the new one is
|
---|
655 | * acquired. The lock level of the new handle will be the same. This
|
---|
656 | * also means that if the lock was not acquired at all before #attach(), it
|
---|
657 | * will not be acquired on the new handle too.
|
---|
658 | *
|
---|
659 | * @param aHandle New handle to attach.
|
---|
660 | */
|
---|
661 | void AutoWriteLock::attach(LockHandle *aHandle)
|
---|
662 | {
|
---|
663 | LockHandle *pHandle = m->aHandles[0];
|
---|
664 |
|
---|
665 | /* detect simple self-reattachment */
|
---|
666 | if (pHandle != aHandle)
|
---|
667 | {
|
---|
668 | bool fWasLocked = m->fIsLocked;
|
---|
669 |
|
---|
670 | cleanup();
|
---|
671 |
|
---|
672 | m->aHandles[0] = aHandle;
|
---|
673 | m->fIsLocked = fWasLocked;
|
---|
674 |
|
---|
675 | if (aHandle)
|
---|
676 | if (fWasLocked)
|
---|
677 | callLockImpl(*aHandle);
|
---|
678 | }
|
---|
679 | }
|
---|
680 |
|
---|
681 | /**
|
---|
682 | * Returns @c true if the current thread holds a write lock on the managed
|
---|
683 | * read/write semaphore. Returns @c false if the managed semaphore is @c
|
---|
684 | * NULL.
|
---|
685 | *
|
---|
686 | * @note Intended for debugging only.
|
---|
687 | */
|
---|
688 | bool AutoWriteLock::isWriteLockOnCurrentThread() const
|
---|
689 | {
|
---|
690 | return m->aHandles[0] ? m->aHandles[0]->isWriteLockOnCurrentThread() : false;
|
---|
691 | }
|
---|
692 |
|
---|
693 | /**
|
---|
694 | * Returns the current write lock level of the managed smaphore. The lock
|
---|
695 | * level determines the number of nested #lock() calls on the given
|
---|
696 | * semaphore handle. Returns @c 0 if the managed semaphore is @c
|
---|
697 | * NULL.
|
---|
698 | *
|
---|
699 | * Note that this call is valid only when the current thread owns a write
|
---|
700 | * lock on the given semaphore handle and will assert otherwise.
|
---|
701 | *
|
---|
702 | * @note Intended for debugging only.
|
---|
703 | */
|
---|
704 | uint32_t AutoWriteLock::writeLockLevel() const
|
---|
705 | {
|
---|
706 | return m->aHandles[0] ? m->aHandles[0]->writeLockLevel() : 0;
|
---|
707 | }
|
---|
708 |
|
---|
709 | ////////////////////////////////////////////////////////////////////////////////
|
---|
710 | //
|
---|
711 | // AutoMultiWriteLock*
|
---|
712 | //
|
---|
713 | ////////////////////////////////////////////////////////////////////////////////
|
---|
714 |
|
---|
715 | AutoMultiWriteLock2::AutoMultiWriteLock2(Lockable *pl1,
|
---|
716 | Lockable *pl2
|
---|
717 | COMMA_LOCKVAL_SRC_POS_DECL)
|
---|
718 | : AutoWriteLockBase(2
|
---|
719 | COMMA_LOCKVAL_SRC_POS_ARGS)
|
---|
720 | {
|
---|
721 | if (pl1)
|
---|
722 | m->aHandles[0] = pl1->lockHandle();
|
---|
723 | if (pl2)
|
---|
724 | m->aHandles[1] = pl2->lockHandle();
|
---|
725 | acquire();
|
---|
726 | }
|
---|
727 |
|
---|
728 | AutoMultiWriteLock2::AutoMultiWriteLock2(LockHandle *pl1,
|
---|
729 | LockHandle *pl2
|
---|
730 | COMMA_LOCKVAL_SRC_POS_DECL)
|
---|
731 | : AutoWriteLockBase(2
|
---|
732 | COMMA_LOCKVAL_SRC_POS_ARGS)
|
---|
733 | {
|
---|
734 | m->aHandles[0] = pl1;
|
---|
735 | m->aHandles[1] = pl2;
|
---|
736 | acquire();
|
---|
737 | }
|
---|
738 |
|
---|
739 | AutoMultiWriteLock3::AutoMultiWriteLock3(Lockable *pl1,
|
---|
740 | Lockable *pl2,
|
---|
741 | Lockable *pl3
|
---|
742 | COMMA_LOCKVAL_SRC_POS_DECL)
|
---|
743 | : AutoWriteLockBase(3
|
---|
744 | COMMA_LOCKVAL_SRC_POS_ARGS)
|
---|
745 | {
|
---|
746 | if (pl1)
|
---|
747 | m->aHandles[0] = pl1->lockHandle();
|
---|
748 | if (pl2)
|
---|
749 | m->aHandles[1] = pl2->lockHandle();
|
---|
750 | if (pl3)
|
---|
751 | m->aHandles[2] = pl3->lockHandle();
|
---|
752 | acquire();
|
---|
753 | }
|
---|
754 |
|
---|
755 | AutoMultiWriteLock3::AutoMultiWriteLock3(LockHandle *pl1,
|
---|
756 | LockHandle *pl2,
|
---|
757 | LockHandle *pl3
|
---|
758 | COMMA_LOCKVAL_SRC_POS_DECL)
|
---|
759 | : AutoWriteLockBase(3
|
---|
760 | COMMA_LOCKVAL_SRC_POS_ARGS)
|
---|
761 | {
|
---|
762 | m->aHandles[0] = pl1;
|
---|
763 | m->aHandles[1] = pl2;
|
---|
764 | m->aHandles[2] = pl3;
|
---|
765 | acquire();
|
---|
766 | }
|
---|
767 |
|
---|
768 | } /* namespace util */
|
---|
769 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|