AutoLock.cpp revision da3229f1051324ee1137c8907b5a647fd76fe17f
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync/** @file
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync *
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * Automatic locks, implementation
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync */
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync/*
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Copyright (C) 2006-2009 Sun Microsystems, Inc.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync *
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * available from http://www.virtualbox.org. This file is free software;
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * you can redistribute it and/or modify it under the terms of the GNU
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * General Public License (GPL) as published by the Free Software
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync *
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * additional information or have any questions.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync */
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync#include <iprt/cdefs.h>
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync#include <iprt/critsect.h>
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync#include <iprt/thread.h>
f84cd77241a1c4b9106a92280611c659243e10d1vboxsync#include <iprt/semaphore.h>
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync#include <iprt/err.h>
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync#include <iprt/assert.h>
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync#if defined(RT_LOCK_STRICT)
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync# include <iprt/asm.h> // for ASMReturnAddress
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync#endif
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#include <iprt/string.h>
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#include <iprt/path.h>
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync#include <iprt/stream.h>
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#include "VBox/com/AutoLock.h"
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#include <VBox/com/string.h>
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync#include <vector>
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#include <list>
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#include <map>
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsyncnamespace util
289060a0c3cb1d509f2cb01fca060796212376f6vboxsync{
289060a0c3cb1d509f2cb01fca060796212376f6vboxsync
289060a0c3cb1d509f2cb01fca060796212376f6vboxsync////////////////////////////////////////////////////////////////////////////////
289060a0c3cb1d509f2cb01fca060796212376f6vboxsync//
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync// RuntimeLockClass
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync//
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync////////////////////////////////////////////////////////////////////////////////
6420f75ffc86ab6494eb5e95418f0c95e71e8068vboxsync
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync#ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
6420f75ffc86ab6494eb5e95418f0c95e71e8068vboxsynctypedef std::map<VBoxLockingClass, RTLOCKVALCLASS> LockValidationClassesMap;
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsyncLockValidationClassesMap g_mapLockValidationClasses;
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync#endif
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync
6420f75ffc86ab6494eb5e95418f0c95e71e8068vboxsync/**
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * Called from initterm.cpp on process initialization (on the main thread)
6420f75ffc86ab6494eb5e95418f0c95e71e8068vboxsync * to give us a chance to initialize lock validation runtime data.
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync */
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsyncvoid InitAutoLockSystem()
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync{
9ad5e3912962c3dbccc1afc4e7d62890fe906814vboxsync#ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync struct
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync {
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync VBoxLockingClass cls;
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync const char *pcszDescription;
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync } aClasses[] =
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync {
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync { LOCKCLASS_VIRTUALBOXOBJECT, "1-VIRTUALBOXOBJECT" },
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync { LOCKCLASS_USBPROXYSERVICE, "2-USBPROXYSERVICE" },
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync { LOCKCLASS_HOSTOBJECT, "3-HOSTOBJECT" },
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync { LOCKCLASS_LISTOFMACHINES, "4-LISTOFMACHINES" },
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync { LOCKCLASS_MACHINEOBJECT, "5-MACHINEOBJECT" },
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync { LOCKCLASS_LISTOFSNAPSHOTS, "6-LISTOFSNAPSHOTS" },
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync { LOCKCLASS_SNAPSHOTOBJECT, "7-SNAPSHOTOBJECT" },
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync { LOCKCLASS_LISTOFMEDIA, "8-LISTOFMEDIA" },
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync { LOCKCLASS_LISTOFOTHEROBJECTS, "9-LISTOFOTHEROBJECTS" },
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync { LOCKCLASS_OTHEROBJECT, "10-OTHEROBJECT" },
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync { LOCKCLASS_USBLIST, "11-USBLIST" },
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync { LOCKCLASS_PROGRESSLIST, "12-PROGRESSLIST" },
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync { LOCKCLASS_OBJECTSTATE, "13-OBJECTSTATE" }
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync };
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync RTLOCKVALCLASS hClass;
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync int vrc;
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync for (unsigned i = 0; i < RT_ELEMENTS(aClasses); ++i)
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync {
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync vrc = RTLockValidatorClassCreate(&hClass,
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync true, /*fAutodidact*/
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync RT_SRC_POS,
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync aClasses[i].pcszDescription);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync AssertRC(vrc);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync // teach the new class that the classes created previously can be held
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync // while the new class is being acquired
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync for (LockValidationClassesMap::iterator it = g_mapLockValidationClasses.begin();
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync it != g_mapLockValidationClasses.end();
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync ++it)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync {
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync RTLOCKVALCLASS &canBeHeld = it->second;
9ad5e3912962c3dbccc1afc4e7d62890fe906814vboxsync vrc = RTLockValidatorClassAddPriorClass(hClass,
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync canBeHeld);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync AssertRC(vrc);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync }
b1c3cdef473df2fbc621d5da81acc82dbfb8a11avboxsync
a9981806c72edadef6cccd253f4747c35677e9a1vboxsync // and store the new class
1d94b54d4bd24729c7383e89aa95518204192b22vboxsync g_mapLockValidationClasses[aClasses[i].cls] = hClass;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync }
289060a0c3cb1d509f2cb01fca060796212376f6vboxsync
289060a0c3cb1d509f2cb01fca060796212376f6vboxsync/* WriteLockHandle critsect1(LOCKCLASS_VIRTUALBOXOBJECT);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync WriteLockHandle critsect2(LOCKCLASS_VIRTUALBOXLIST);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync AutoWriteLock lock1(critsect1 COMMA_LOCKVAL_SRC_POS);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync AutoWriteLock lock2(critsect2 COMMA_LOCKVAL_SRC_POS);*/
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#endif
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync}
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync////////////////////////////////////////////////////////////////////////////////
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync//
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync// RWLockHandle
a9981806c72edadef6cccd253f4747c35677e9a1vboxsync//
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync////////////////////////////////////////////////////////////////////////////////
a9981806c72edadef6cccd253f4747c35677e9a1vboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsyncstruct RWLockHandle::Data
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync{
a9981806c72edadef6cccd253f4747c35677e9a1vboxsync Data()
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync { }
a9981806c72edadef6cccd253f4747c35677e9a1vboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync RTSEMRW sem;
a9981806c72edadef6cccd253f4747c35677e9a1vboxsync VBoxLockingClass lockClass;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
a9981806c72edadef6cccd253f4747c35677e9a1vboxsync#ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync com::Utf8Str strDescription;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#endif
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync};
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsyncRWLockHandle::RWLockHandle(VBoxLockingClass lockClass)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync{
a9981806c72edadef6cccd253f4747c35677e9a1vboxsync m = new Data();
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
a9981806c72edadef6cccd253f4747c35677e9a1vboxsync m->lockClass = lockClass;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
a9981806c72edadef6cccd253f4747c35677e9a1vboxsync m->strDescription = com::Utf8StrFmt("r/w %RCv", this);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync int vrc = RTSemRWCreateEx(&m->sem, 0 /*fFlags*/, g_mapLockValidationClasses[lockClass], RTLOCKVAL_SUB_CLASS_ANY, NULL);
a9981806c72edadef6cccd253f4747c35677e9a1vboxsync#else
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync int vrc = RTSemRWCreateEx(&m->sem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_ANY, NULL);
a9981806c72edadef6cccd253f4747c35677e9a1vboxsync#endif
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync AssertRC(vrc);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync}
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync/*virtual*/ RWLockHandle::~RWLockHandle()
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync{
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync RTSemRWDestroy(m->sem);
9ad5e3912962c3dbccc1afc4e7d62890fe906814vboxsync delete m;
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync}
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync/*virtual*/ bool RWLockHandle::isWriteLockOnCurrentThread() const
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync{
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync return RTSemRWIsWriteOwner(m->sem);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync}
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync/*virtual*/ void RWLockHandle::lockWrite(LOCKVAL_SRC_POS_DECL)
50fdc90dae026b2086f85b0f028aa63dd6bbe14evboxsync{
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync int vrc = RTSemRWRequestWriteDebug(m->sem, RT_INDEFINITE_WAIT, (uintptr_t)ASMReturnAddress(), RT_SRC_POS_ARGS);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#else
50fdc90dae026b2086f85b0f028aa63dd6bbe14evboxsync int vrc = RTSemRWRequestWrite(m->sem, RT_INDEFINITE_WAIT);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#endif
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync AssertRC(vrc);
289060a0c3cb1d509f2cb01fca060796212376f6vboxsync}
289060a0c3cb1d509f2cb01fca060796212376f6vboxsync
289060a0c3cb1d509f2cb01fca060796212376f6vboxsync/*virtual*/ void RWLockHandle::unlockWrite()
50fdc90dae026b2086f85b0f028aa63dd6bbe14evboxsync{
50fdc90dae026b2086f85b0f028aa63dd6bbe14evboxsync int vrc = RTSemRWReleaseWrite(m->sem);
e8ac7dce6d625856c57792a6af738e2fe2667264vboxsync AssertRC(vrc);
50fdc90dae026b2086f85b0f028aa63dd6bbe14evboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync}
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync/*virtual*/ void RWLockHandle::lockRead(LOCKVAL_SRC_POS_DECL)
1d94b54d4bd24729c7383e89aa95518204192b22vboxsync{
a9981806c72edadef6cccd253f4747c35677e9a1vboxsync#ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
a9981806c72edadef6cccd253f4747c35677e9a1vboxsync int vrc = RTSemRWRequestReadDebug(m->sem, RT_INDEFINITE_WAIT, (uintptr_t)ASMReturnAddress(), RT_SRC_POS_ARGS);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#else
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync int vrc = RTSemRWRequestRead(m->sem, RT_INDEFINITE_WAIT);
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync#endif
AssertRC(vrc);
}
/*virtual*/ void RWLockHandle::unlockRead()
{
int vrc = RTSemRWReleaseRead(m->sem);
AssertRC(vrc);
}
/*virtual*/ uint32_t RWLockHandle::writeLockLevel() const
{
/* Note! This does not include read recursions done by the writer! */
return RTSemRWGetWriteRecursion(m->sem);
}
#ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
/*virtual*/ const char* RWLockHandle::describe() const
{
return m->strDescription.c_str();
}
#endif
////////////////////////////////////////////////////////////////////////////////
//
// WriteLockHandle
//
////////////////////////////////////////////////////////////////////////////////
struct WriteLockHandle::Data
{
Data()
{ }
mutable RTCRITSECT sem;
VBoxLockingClass lockClass;
#ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
com::Utf8Str strDescription;
#endif
};
WriteLockHandle::WriteLockHandle(VBoxLockingClass lockClass)
{
m = new Data;
m->lockClass = lockClass;
#ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
m->strDescription = com::Utf8StrFmt("crit %RCv", this);
int vrc = RTCritSectInitEx(&m->sem, 0/*fFlags*/, g_mapLockValidationClasses[lockClass], RTLOCKVAL_SUB_CLASS_ANY, NULL);
#else
int vrc = RTCritSectInitEx(&m->sem, 0/*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_ANY, NULL);
#endif
AssertRC(vrc);
}
WriteLockHandle::~WriteLockHandle()
{
RTCritSectDelete(&m->sem);
delete m;
}
/*virtual*/ bool WriteLockHandle::isWriteLockOnCurrentThread() const
{
return RTCritSectIsOwner(&m->sem);
}
/*virtual*/ void WriteLockHandle::lockWrite(LOCKVAL_SRC_POS_DECL)
{
#ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
RTCritSectEnterDebug(&m->sem, (uintptr_t)ASMReturnAddress(), RT_SRC_POS_ARGS);
#else
RTCritSectEnter(&m->sem);
#endif
}
/*virtual*/ void WriteLockHandle::unlockWrite()
{
RTCritSectLeave(&m->sem);
}
/*virtual*/ void WriteLockHandle::lockRead(LOCKVAL_SRC_POS_DECL)
{
lockWrite(LOCKVAL_SRC_POS_ARGS);
}
/*virtual*/ void WriteLockHandle::unlockRead()
{
unlockWrite();
}
/*virtual*/ uint32_t WriteLockHandle::writeLockLevel() const
{
return RTCritSectGetRecursion(&m->sem);
}
#ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
/*virtual*/ const char* WriteLockHandle::describe() const
{
return m->strDescription.c_str();
}
#endif
////////////////////////////////////////////////////////////////////////////////
//
// AutoLockBase
//
////////////////////////////////////////////////////////////////////////////////
typedef std::vector<LockHandle*> HandlesVector;
typedef std::vector<uint32_t> CountsVector;
struct AutoLockBase::Data
{
Data(size_t cHandles
#ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
, const char *pcszFile_,
unsigned uLine_,
const char *pcszFunction_
#endif
)
: fIsLocked(false),
aHandles(cHandles), // size of array
acUnlockedInLeave(cHandles)
#ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
, pcszFile(pcszFile_),
uLine(uLine_),
pcszFunction(pcszFunction_)
#endif
{
for (uint32_t i = 0; i < cHandles; ++i)
{
acUnlockedInLeave[i] = 0;
aHandles[i] = NULL;
}
}
bool fIsLocked; // if true, then all items in aHandles are locked by this AutoLock and
// need to be unlocked in the destructor
HandlesVector aHandles; // array (vector) of LockHandle instances; in the case of AutoWriteLock
// and AutoReadLock, there will only be one item on the list; with the
// AutoMulti* derivatives, there will be multiple
CountsVector acUnlockedInLeave; // for each lock handle, how many times the handle was unlocked in leave(); otherwise 0
#ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
// information about where the lock occured (passed down from the AutoLock classes)
const char *pcszFile;
unsigned uLine;
const char *pcszFunction;
#endif
};
AutoLockBase::AutoLockBase(uint32_t cHandles
COMMA_LOCKVAL_SRC_POS_DECL)
{
m = new Data(cHandles
COMMA_LOCKVAL_SRC_POS_ARGS);
}
AutoLockBase::AutoLockBase(uint32_t cHandles,
LockHandle *pHandle
COMMA_LOCKVAL_SRC_POS_DECL)
{
Assert(cHandles == 1);
m = new Data(1
COMMA_LOCKVAL_SRC_POS_ARGS);
m->aHandles[0] = pHandle;
}
AutoLockBase::~AutoLockBase()
{
delete m;
}
/**
* Requests ownership of all contained lock handles by calling
* the pure virtual callLockImpl() function on each of them,
* which must be implemented by the descendant class; in the
* implementation, AutoWriteLock will request a write lock
* whereas AutoReadLock will request a read lock.
*
* Does *not* modify the lock counts in the member variables.
*/
void AutoLockBase::callLockOnAllHandles()
{
for (HandlesVector::iterator it = m->aHandles.begin();
it != m->aHandles.end();
++it)
{
LockHandle *pHandle = *it;
if (pHandle)
// call virtual function implemented in AutoWriteLock or AutoReadLock
this->callLockImpl(*pHandle);
}
}
/**
* Releases ownership of all contained lock handles by calling
* the pure virtual callUnlockImpl() function on each of them,
* which must be implemented by the descendant class; in the
* implementation, AutoWriteLock will release a write lock
* whereas AutoReadLock will release a read lock.
*
* Does *not* modify the lock counts in the member variables.
*/
void AutoLockBase::callUnlockOnAllHandles()
{
// unlock in reverse order!
for (HandlesVector::reverse_iterator it = m->aHandles.rbegin();
it != m->aHandles.rend();
++it)
{
LockHandle *pHandle = *it;
if (pHandle)
// call virtual function implemented in AutoWriteLock or AutoReadLock
this->callUnlockImpl(*pHandle);
}
}
/**
* Destructor implementation that can also be called explicitly, if required.
* Restores the exact state before the AutoLock was created; that is, unlocks
* all contained semaphores and might actually lock them again if leave()
* was called during the AutoLock's lifetime.
*/
void AutoLockBase::cleanup()
{
bool fAnyUnlockedInLeave = false;
uint32_t i = 0;
for (HandlesVector::iterator it = m->aHandles.begin();
it != m->aHandles.end();
++it)
{
LockHandle *pHandle = *it;
if (pHandle)
{
if (m->acUnlockedInLeave[i])
{
// there was a leave() before the destruction: then restore the
// lock level that might have been set by locks other than our own
if (m->fIsLocked)
{
--m->acUnlockedInLeave[i];
fAnyUnlockedInLeave = true;
}
for (; m->acUnlockedInLeave[i]; --m->acUnlockedInLeave[i])
callLockImpl(*pHandle);
}
}
++i;
}
if (m->fIsLocked && !fAnyUnlockedInLeave)
callUnlockOnAllHandles();
}
/**
* Requests ownership of all contained semaphores. Public method that can
* only be called once and that also gets called by the AutoLock constructors.
*/
void AutoLockBase::acquire()
{
AssertMsg(!m->fIsLocked, ("m->fIsLocked is true, attempting to lock twice!"));
callLockOnAllHandles();
m->fIsLocked = true;
}
/**
* Releases ownership of all contained semaphores. Public method.
*/
void AutoLockBase::release()
{
AssertMsg(m->fIsLocked, ("m->fIsLocked is false, cannot release!"));
callUnlockOnAllHandles();
m->fIsLocked = false;
}
////////////////////////////////////////////////////////////////////////////////
//
// AutoReadLock
//
////////////////////////////////////////////////////////////////////////////////
/**
* Release all read locks acquired by this instance through the #lock()
* call and destroys the instance.
*
* Note that if there there are nested #lock() calls without the
* corresponding number of #unlock() calls when the destructor is called, it
* will assert. This is because having an unbalanced number of nested locks
* is a program logic error which must be fixed.
*/
/*virtual*/ AutoReadLock::~AutoReadLock()
{
LockHandle *pHandle = m->aHandles[0];
if (pHandle)
{
if (m->fIsLocked)
callUnlockImpl(*pHandle);
}
}
/**
* Implementation of the pure virtual declared in AutoLockBase.
* This gets called by AutoLockBase.acquire() to actually request
* the semaphore; in the AutoReadLock implementation, we request
* the semaphore in read mode.
*/
/*virtual*/ void AutoReadLock::callLockImpl(LockHandle &l)
{
#ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
l.lockRead(m->pcszFile, m->uLine, m->pcszFunction);
#else
l.lockRead();
#endif
}
/**
* Implementation of the pure virtual declared in AutoLockBase.
* This gets called by AutoLockBase.release() to actually release
* the semaphore; in the AutoReadLock implementation, we release
* the semaphore in read mode.
*/
/*virtual*/ void AutoReadLock::callUnlockImpl(LockHandle &l)
{
l.unlockRead();
}
////////////////////////////////////////////////////////////////////////////////
//
// AutoWriteLockBase
//
////////////////////////////////////////////////////////////////////////////////
/**
* Implementation of the pure virtual declared in AutoLockBase.
* This gets called by AutoLockBase.acquire() to actually request
* the semaphore; in the AutoWriteLock implementation, we request
* the semaphore in write mode.
*/
/*virtual*/ void AutoWriteLockBase::callLockImpl(LockHandle &l)
{
#ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
l.lockWrite(m->pcszFile, m->uLine, m->pcszFunction);
#else
l.lockWrite();
#endif
}
/**
* Implementation of the pure virtual declared in AutoLockBase.
* This gets called by AutoLockBase.release() to actually release
* the semaphore; in the AutoWriteLock implementation, we release
* the semaphore in write mode.
*/
/*virtual*/ void AutoWriteLockBase::callUnlockImpl(LockHandle &l)
{
l.unlockWrite();
}
/**
* Causes the current thread to completely release the write lock to make
* the managed semaphore immediately available for locking by other threads.
*
* This implies that all nested write locks on the semaphore will be
* released, even those that were acquired through the calls to #lock()
* methods of all other AutoWriteLock/AutoReadLock instances managing the
* <b>same</b> read/write semaphore.
*
* After calling this method, the only method you are allowed to call is
* #enter(). It will acquire the write lock again and restore the same
* level of nesting as it had before calling #leave().
*
* If this instance is destroyed without calling #enter(), the destructor
* will try to restore the write lock level that existed when #leave() was
* called minus the number of nested #lock() calls made on this instance
* itself. This is done to preserve lock levels of other
* AutoWriteLock/AutoReadLock instances managing the same semaphore (if
* any). Tiis also means that the destructor may indefinitely block if a
* write or a read lock is owned by some other thread by that time.
*/
void AutoWriteLockBase::leave()
{
AssertMsg(m->fIsLocked, ("m->fIsLocked is false, cannot leave()!"));
// unlock in reverse order!
uint32_t i = m->aHandles.size();
for (HandlesVector::reverse_iterator it = m->aHandles.rbegin();
it != m->aHandles.rend();
++it)
{
--i; // array index is zero based, decrement with every loop since we iterate backwards
LockHandle *pHandle = *it;
if (pHandle)
{
AssertMsg(m->acUnlockedInLeave[i] == 0, ("m->cUnlockedInLeave[%d] is %d, must be 0! Called leave() twice?", i, m->acUnlockedInLeave[i]));
m->acUnlockedInLeave[i] = pHandle->writeLockLevel();
AssertMsg(m->acUnlockedInLeave[i] >= 1, ("m->cUnlockedInLeave[%d] is %d, must be >=1!", i, m->acUnlockedInLeave[i]));
for (uint32_t left = m->acUnlockedInLeave[i];
left;
--left)
callUnlockImpl(*pHandle);
}
}
}
/**
* Causes the current thread to restore the write lock level after the
* #leave() call. This call will indefinitely block if another thread has
* successfully acquired a write or a read lock on the same semaphore in
* between.
*/
void AutoWriteLockBase::enter()
{
AssertMsg(m->fIsLocked, ("m->fIsLocked is false, cannot enter()!"));
uint32_t i = 0;
for (HandlesVector::iterator it = m->aHandles.begin();
it != m->aHandles.end();
++it)
{
LockHandle *pHandle = *it;
if (pHandle)
{
AssertMsg(m->acUnlockedInLeave[i] != 0, ("m->cUnlockedInLeave[%d] is 0! enter() without leave()?", i));
for (; m->acUnlockedInLeave[i]; --m->acUnlockedInLeave[i])
callLockImpl(*pHandle);
}
++i;
}
}
/**
* Same as #enter() but checks if the current thread actally owns the lock
* and only proceeds if not. As a result, as opposed to #enter(), doesn't
* assert when called with the lock already being held.
*/
void AutoWriteLockBase::maybeEnter()
{
uint32_t i = 0;
for (HandlesVector::iterator it = m->aHandles.begin();
it != m->aHandles.end();
++it)
{
LockHandle *pHandle = *it;
if (pHandle)
{
if (!pHandle->isWriteLockOnCurrentThread())
{
for (; m->acUnlockedInLeave[i]; --m->acUnlockedInLeave[i])
callLockImpl(*pHandle);
}
}
++i;
}
}
////////////////////////////////////////////////////////////////////////////////
//
// AutoWriteLock
//
////////////////////////////////////////////////////////////////////////////////
/**
* Attaches another handle to this auto lock instance.
*
* The previous object's lock is completely released before the new one is
* acquired. The lock level of the new handle will be the same. This
* also means that if the lock was not acquired at all before #attach(), it
* will not be acquired on the new handle too.
*
* @param aHandle New handle to attach.
*/
void AutoWriteLock::attach(LockHandle *aHandle)
{
LockHandle *pHandle = m->aHandles[0];
/* detect simple self-reattachment */
if (pHandle != aHandle)
{
bool fWasLocked = m->fIsLocked;
cleanup();
m->aHandles[0] = aHandle;
m->fIsLocked = fWasLocked;
if (aHandle)
if (fWasLocked)
callLockImpl(*aHandle);
}
}
/**
* Returns @c true if the current thread holds a write lock on the managed
* read/write semaphore. Returns @c false if the managed semaphore is @c
* NULL.
*
* @note Intended for debugging only.
*/
bool AutoWriteLock::isWriteLockOnCurrentThread() const
{
return m->aHandles[0] ? m->aHandles[0]->isWriteLockOnCurrentThread() : false;
}
/**
* Returns the current write lock level of the managed smaphore. The lock
* level determines the number of nested #lock() calls on the given
* semaphore handle. Returns @c 0 if the managed semaphore is @c
* NULL.
*
* Note that this call is valid only when the current thread owns a write
* lock on the given semaphore handle and will assert otherwise.
*
* @note Intended for debugging only.
*/
uint32_t AutoWriteLock::writeLockLevel() const
{
return m->aHandles[0] ? m->aHandles[0]->writeLockLevel() : 0;
}
////////////////////////////////////////////////////////////////////////////////
//
// AutoMultiWriteLock*
//
////////////////////////////////////////////////////////////////////////////////
AutoMultiWriteLock2::AutoMultiWriteLock2(Lockable *pl1,
Lockable *pl2
COMMA_LOCKVAL_SRC_POS_DECL)
: AutoWriteLockBase(2
COMMA_LOCKVAL_SRC_POS_ARGS)
{
if (pl1)
m->aHandles[0] = pl1->lockHandle();
if (pl2)
m->aHandles[1] = pl2->lockHandle();
acquire();
}
AutoMultiWriteLock2::AutoMultiWriteLock2(LockHandle *pl1,
LockHandle *pl2
COMMA_LOCKVAL_SRC_POS_DECL)
: AutoWriteLockBase(2
COMMA_LOCKVAL_SRC_POS_ARGS)
{
m->aHandles[0] = pl1;
m->aHandles[1] = pl2;
acquire();
}
AutoMultiWriteLock3::AutoMultiWriteLock3(Lockable *pl1,
Lockable *pl2,
Lockable *pl3
COMMA_LOCKVAL_SRC_POS_DECL)
: AutoWriteLockBase(3
COMMA_LOCKVAL_SRC_POS_ARGS)
{
if (pl1)
m->aHandles[0] = pl1->lockHandle();
if (pl2)
m->aHandles[1] = pl2->lockHandle();
if (pl3)
m->aHandles[2] = pl3->lockHandle();
acquire();
}
AutoMultiWriteLock3::AutoMultiWriteLock3(LockHandle *pl1,
LockHandle *pl2,
LockHandle *pl3
COMMA_LOCKVAL_SRC_POS_DECL)
: AutoWriteLockBase(3
COMMA_LOCKVAL_SRC_POS_ARGS)
{
m->aHandles[0] = pl1;
m->aHandles[1] = pl2;
m->aHandles[2] = pl3;
acquire();
}
} /* namespace util */
/* vi: set tabstop=4 shiftwidth=4 expandtab: */