VirtualBox

source: vbox/trunk/src/VBox/Main/glue/AutoLock.cpp@ 25842

Last change on this file since 25842 was 25842, checked in by vboxsync, 15 years ago

Main: lock validation adjustments

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette