AutoLock.cpp revision 00cbcf486e6ccfd206062b33d823949c169512d3
/** @file
*
* Automatic locks, implementation
*/
/*
* Copyright (C) 2006-2009 Sun Microsystems, Inc.
*
* 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.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
#include "AutoLock.h"
#include "Logging.h"
#include <iprt/critsect.h>
#include <iprt/semaphore.h>
#if defined(DEBUG)
#endif
#include <vector>
#include <list>
namespace util
{
////////////////////////////////////////////////////////////////////////////////
//
// RuntimeLockClass
//
////////////////////////////////////////////////////////////////////////////////
class RuntimeLockClass
{
};
/**
* Called from initterm.cpp on process initialization (on the main thread)
* to give us a chance to initialize lock validation runtime data.
*/
void InitAutoLockSystem()
{
RTPrintf("InitAutoLockSystem\n");
}
////////////////////////////////////////////////////////////////////////////////
//
// RWLockHandle
//
////////////////////////////////////////////////////////////////////////////////
struct RWLockHandle::Data
{
Data()
{ }
#ifdef RT_LOCK_STRICT
#endif
};
{
m = new Data();
int vrc = RTSemRWCreateEx(&m->sem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
#ifdef RT_LOCK_STRICT
#endif
}
{
RTSemRWDestroy(m->sem);
delete m;
}
{
return RTSemRWIsWriteOwner(m->sem);
}
{
#if defined(RT_LOCK_STRICT)
int vrc = RTSemRWRequestWriteDebug(m->sem, RT_INDEFINITE_WAIT, (uintptr_t)ASMReturnAddress(), RT_SRC_POS_ARGS);
#else
#endif
}
{
}
{
#if defined(RT_LOCK_STRICT)
int vrc = RTSemRWRequestReadDebug(m->sem, RT_INDEFINITE_WAIT, (uintptr_t)ASMReturnAddress(), RT_SRC_POS_ARGS);
#else
#endif
}
{
}
{
/* Note! This does not include read recursions done by the writer! */
return RTSemRWGetWriteRecursion(m->sem);
}
#ifdef RT_LOCK_STRICT
{
return m->strDescription.c_str();
}
#endif
////////////////////////////////////////////////////////////////////////////////
//
// WriteLockHandle
//
////////////////////////////////////////////////////////////////////////////////
struct WriteLockHandle::Data
{
Data()
{ }
mutable RTCRITSECT sem;
#ifdef RT_LOCK_STRICT
#endif
};
{
m = new Data;
int vrc = RTCritSectInitEx(&m->sem, 0/*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
#ifdef RT_LOCK_STRICT
#endif
}
{
RTCritSectDelete(&m->sem);
delete m;
}
{
return RTCritSectIsOwner(&m->sem);
}
{
#if defined(RT_LOCK_STRICT)
#else
RTCritSectEnter(&m->sem);
#endif
}
{
RTCritSectLeave(&m->sem);
}
{
}
{
unlockWrite();
}
{
return RTCritSectGetRecursion(&m->sem);
}
#ifdef RT_LOCK_STRICT
{
return m->strDescription.c_str();
}
#endif
////////////////////////////////////////////////////////////////////////////////
//
// AutoLockBase
//
////////////////////////////////////////////////////////////////////////////////
struct AutoLockBase::Data
{
#ifdef RT_LOCK_STRICT
, const char *pcszFile_,
unsigned uLine_,
const char *pcszFunction_
#endif
)
: fIsLocked(false),
#ifdef RT_LOCK_STRICT
#endif
{
{
acUnlockedInLeave[i] = 0;
}
}
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
CountsVector acUnlockedInLeave; // for each lock handle, how many times the handle was unlocked in leave(); otherwise 0
#ifdef RT_LOCK_STRICT
// information about where the lock occured (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 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;
++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])
}
}
++i;
}
if (m->fIsLocked && !fAnyUnlockedInLeave)
}
/**
* 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.
*/
{
#ifdef RT_LOCK_STRICT
#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.
*/
{
#ifdef RT_LOCK_STRICT
#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();
}
/**
* 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
*
* 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()
{
// unlock in reverse order!
++it)
{
--i; // array index is zero based, decrement with every loop since we iterate backwards
if (pHandle)
{
AssertMsg(m->acUnlockedInLeave[i] == 0, ("m->cUnlockedInLeave[%d] is %d, must be 0! Called leave() twice?", i, m->acUnlockedInLeave[i]));
AssertMsg(m->acUnlockedInLeave[i] >= 1, ("m->cUnlockedInLeave[%d] is %d, must be >=1!", i, m->acUnlockedInLeave[i]));
#ifdef RT_LOCK_STRICT
LogFlowFunc(("LOCKVAL: will unlock handle %d [%s] %d times\n", i, pHandle->describe(), m->acUnlockedInLeave[i]));
#endif
left;
--left)
}
}
}
/**
* 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()
{
uint32_t i = 0;
++it)
{
if (pHandle)
{
AssertMsg(m->acUnlockedInLeave[i] != 0, ("m->cUnlockedInLeave[%d] is 0! enter() without leave()?", i));
#ifdef RT_LOCK_STRICT
LogFlowFunc(("LOCKVAL: will lock handle %d [%s] %d times\n", i, pHandle->describe(), m->acUnlockedInLeave[i]));
#endif
for (; m->acUnlockedInLeave[i]; --m->acUnlockedInLeave[i])
}
++i;
}
}
/**
* Same as #leave() but checks if the current thread actally owns the lock
* and only proceeds in this case. As a result, as opposed to #leave(),
* doesn't assert when called with no lock being held.
*/
void AutoWriteLockBase::maybeLeave()
{
// unlock in reverse order!
++it)
{
--i; // array index is zero based, decrement with every loop since we iterate backwards
if (pHandle)
{
if (pHandle->isWriteLockOnCurrentThread())
{
AssertMsg(m->acUnlockedInLeave[i] >= 1, ("m->cUnlockedInLeave[%d] is %d, must be >=1!", i, m->acUnlockedInLeave[i]));
#ifdef RT_LOCK_STRICT
LogFlowFunc(("LOCKVAL: will unlock handle %d [%s] %d times\n", i, pHandle->describe(), m->acUnlockedInLeave[i]));
#endif
left;
--left)
}
}
++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;
++it)
{
if (pHandle)
{
if (!pHandle->isWriteLockOnCurrentThread())
{
#ifdef RT_LOCK_STRICT
LogFlowFunc(("LOCKVAL: will lock handle %d [%s] %d times\n", i, pHandle->describe(), m->acUnlockedInLeave[i]));
#endif
for (; m->acUnlockedInLeave[i]; --m->acUnlockedInLeave[i])
}
}
++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.
*/
{
/* 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 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.
*/
{
}
////////////////////////////////////////////////////////////////////////////////
//
// AutoMultiWriteLock*
//
////////////////////////////////////////////////////////////////////////////////
{
if (pl1)
if (pl2)
acquire();
}
{
acquire();
}
{
if (pl1)
if (pl2)
if (pl3)
acquire();
}
{
acquire();
}
} /* namespace util */
/* vi: set tabstop=4 shiftwidth=4 expandtab: */