AutoLock.cpp revision e76cf654e18a6b3e9d40b8f643401289471546b7
/* $Id$ */
/** @file
* Automatic locks, implementation.
*/
/*
* Copyright (C) 2006-2013 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
#define GLUE_USE_CRITSECTRW
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/critsect.h>
#include <iprt/semaphore.h>
#if defined(RT_LOCK_STRICT)
#endif
#include "VBox/com/AutoLock.h"
#include <vector>
#include <list>
#include <map>
namespace util
{
////////////////////////////////////////////////////////////////////////////////
//
// RuntimeLockClass
//
////////////////////////////////////////////////////////////////////////////////
#endif
/**
* Called from initterm.cpp on process initialization (on the main thread)
* to give us a chance to initialize lock validation runtime data.
*/
void InitAutoLockSystem()
{
struct
{
const char *pcszDescription;
} aClasses[] =
{
{ LOCKCLASS_VIRTUALBOXOBJECT, "2-VIRTUALBOXOBJECT" },
{ LOCKCLASS_HOSTOBJECT, "3-HOSTOBJECT" },
{ LOCKCLASS_LISTOFMACHINES, "4-LISTOFMACHINES" },
{ LOCKCLASS_MACHINEOBJECT, "5-MACHINEOBJECT" },
{ LOCKCLASS_SNAPSHOTOBJECT, "6-SNAPSHOTOBJECT" },
{ LOCKCLASS_MEDIUMQUERY, "7-MEDIUMQUERY" },
{ LOCKCLASS_LISTOFMEDIA, "8-LISTOFMEDIA" },
{ LOCKCLASS_LISTOFOTHEROBJECTS, "9-LISTOFOTHEROBJECTS" },
{ LOCKCLASS_OTHEROBJECT, "10-OTHEROBJECT" },
{ LOCKCLASS_PROGRESSLIST, "11-PROGRESSLIST" },
{ LOCKCLASS_OBJECTSTATE, "12-OBJECTSTATE" }
};
int vrc;
for (unsigned i = 0; i < RT_ELEMENTS(aClasses); ++i)
{
true, /*fAutodidact*/
aClasses[i].pcszDescription);
// teach the new class that the classes created previously can be held
// while the new class is being acquired
++it)
{
}
// and store the new class
}
/* WriteLockHandle critsect1(LOCKCLASS_VIRTUALBOXOBJECT);
WriteLockHandle critsect2(LOCKCLASS_VIRTUALBOXLIST);
AutoWriteLock lock1(critsect1 COMMA_LOCKVAL_SRC_POS);
AutoWriteLock lock2(critsect2 COMMA_LOCKVAL_SRC_POS);*/
#endif
}
{
#else /* !VBOX_WITH_MAIN_LOCK_VALIDATION */
return false;
#endif /* !VBOX_WITH_MAIN_LOCK_VALIDATION */
}
////////////////////////////////////////////////////////////////////////////////
//
// RWLockHandle
//
////////////////////////////////////////////////////////////////////////////////
struct RWLockHandle::Data
{
Data()
{ }
#ifdef GLUE_USE_CRITSECTRW
mutable RTCRITSECTRW CritSect;
#else
#endif
#endif
};
{
m = new Data();
#endif
#ifdef GLUE_USE_CRITSECTRW
# ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
int vrc = RTCritSectRwInitEx(&m->CritSect, 0 /*fFlags*/, g_mapLockValidationClasses[lockClass], RTLOCKVAL_SUB_CLASS_ANY, NULL);
# else
int vrc = RTCritSectRwInitEx(&m->CritSect, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_ANY, NULL);
# endif
#else
# ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
int vrc = RTSemRWCreateEx(&m->sem, 0 /*fFlags*/, g_mapLockValidationClasses[lockClass], RTLOCKVAL_SUB_CLASS_ANY, NULL);
# else
int vrc = RTSemRWCreateEx(&m->sem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_ANY, NULL);
# endif
#endif
}
{
#ifdef GLUE_USE_CRITSECTRW
RTCritSectRwDelete(&m->CritSect);
#else
RTSemRWDestroy(m->sem);
#endif
delete m;
}
{
#ifdef GLUE_USE_CRITSECTRW
return RTCritSectRwIsWriteOwner(&m->CritSect);
#else
return RTSemRWIsWriteOwner(m->sem);
#endif
}
{
#ifdef GLUE_USE_CRITSECTRW
# ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
# else
# endif
#else
# ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
int vrc = RTSemRWRequestWriteDebug(m->sem, RT_INDEFINITE_WAIT, (uintptr_t)ASMReturnAddress(), RT_SRC_POS_ARGS);
# else
# endif
#endif
}
{
#ifdef GLUE_USE_CRITSECTRW
#else
#endif
}
{
#ifdef GLUE_USE_CRITSECTRW
# ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
int vrc = RTCritSectRwEnterSharedDebug(&m->CritSect, (uintptr_t)ASMReturnAddress(), RT_SRC_POS_ARGS);
# else
# endif
#else
# ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
int vrc = RTSemRWRequestReadDebug(m->sem, RT_INDEFINITE_WAIT, (uintptr_t)ASMReturnAddress(), RT_SRC_POS_ARGS);
# else
# endif
#endif
}
{
#ifdef GLUE_USE_CRITSECTRW
#else
#endif
}
{
/* Note! This does not include read recursions done by the writer! */
#ifdef GLUE_USE_CRITSECTRW
return RTCritSectRwGetWriteRecursion(&m->CritSect);
#else
return RTSemRWGetWriteRecursion(m->sem);
#endif
}
{
return m->strDescription.c_str();
}
#endif
////////////////////////////////////////////////////////////////////////////////
//
// WriteLockHandle
//
////////////////////////////////////////////////////////////////////////////////
struct WriteLockHandle::Data
{
Data()
{ }
mutable RTCRITSECT sem;
#endif
};
{
m = new Data;
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
}
{
RTCritSectDelete(&m->sem);
delete m;
}
{
return RTCritSectIsOwner(&m->sem);
}
{
#else
RTCritSectEnter(&m->sem);
#endif
}
{
RTCritSectLeave(&m->sem);
}
{
}
{
unlockWrite();
}
{
return RTCritSectGetRecursion(&m->sem);
}
{
return m->strDescription.c_str();
}
#endif
////////////////////////////////////////////////////////////////////////////////
//
// AutoLockBase
//
////////////////////////////////////////////////////////////////////////////////
struct AutoLockBase::Data
{
, const char *pcszFile_,
unsigned uLine_,
const char *pcszFunction_
#endif
)
: fIsLocked(false),
#endif
{
}
bool fIsLocked; // if true, then all items in aHandles are locked by this AutoLock and
// need to be unlocked in the destructor
// and AutoReadLock, there will only be one item on the list; with the
// AutoMulti* derivatives, there will be multiple
// information about where the lock occurred (passed down from the AutoLock classes)
const char *pcszFile;
unsigned uLine;
const char *pcszFunction;
#endif
};
{
}
{
m = new Data(1
}
{
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()
{
++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!
++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.
*/
void AutoLockBase::cleanup()
{
if (m->fIsLocked)
}
/**
* 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()
{
m->fIsLocked = true;
}
/**
* Releases ownership of all contained semaphores. Public method.
*/
void AutoLockBase::release()
{
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.
*/
{
if (pHandle)
{
if (m->fIsLocked)
}
}
/**
* 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.
*/
{
#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.
*/
{
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.
*/
{
#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.
*/
{
l.unlockWrite();
}
////////////////////////////////////////////////////////////////////////////////
//
// AutoWriteLock
//
////////////////////////////////////////////////////////////////////////////////
{
acquire();
}
/**
* 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.
*/
{
/* detect simple self-reattachment */
{
bool fWasLocked = m->fIsLocked;
cleanup();
m->fIsLocked = fWasLocked;
if (aHandle)
if (fWasLocked)
}
}
/**
* Returns @c true if the current thread holds a write lock on the managed
* NULL.
*
* @note Intended for debugging only.
*/
bool AutoWriteLock::isWriteLockOnCurrentThread() const
{
}
/**
* Returns the current write lock level of the managed semaphore. 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.
*/
{
}
////////////////////////////////////////////////////////////////////////////////
//
// AutoMultiWriteLock*
//
////////////////////////////////////////////////////////////////////////////////
{
if (pl1)
if (pl2)
acquire();
}
{
acquire();
}
{
if (pl1)
if (pl2)
if (pl3)
acquire();
}
{
acquire();
}
{
if (pl1)
if (pl2)
if (pl3)
if (pl4)
acquire();
}
{
acquire();
}
} /* namespace util */
/* vi: set tabstop=4 shiftwidth=4 expandtab: */