PDMCritSect.cpp revision 3e9c5c3e44de15c28695c7b570bc2551639187e3
/* $Id$ */
/** @file
* PDM - Critical Sections, Ring-3.
*/
/*
* 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include "PDMInternal.h"
#include <VBox/pdmcritsect.h>
#include <iprt/lockvalidator.h>
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static int pdmR3CritSectDeleteOne(PVM pVM, PPDMCRITSECTINT pCritSect, PPDMCRITSECTINT pPrev, bool fFinal);
/**
* Initializes the critical section subcomponent.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @remark Not to be confused with PDMR3CritSectInit and pdmR3CritSectInitDevice which are
* for initializing a critical section.
*/
{
STAM_REG(pVM, &pVM->pdm.s.StatQueuedCritSectLeaves, STAMTYPE_COUNTER, "/PDM/QueuedCritSectLeaves", STAMUNIT_OCCURENCES,
"Number of times a critical section leave requesed needed to be queued for ring-3 execution.");
return VINF_SUCCESS;
}
/**
* Relocates all the critical sections.
*
* @param pVM The VM handle.
*/
{
pCur;
}
/**
* Deletes all remaining critical sections.
*
* This is called at the end of the termination process.
*
* @returns VBox status.
* First error code, rest is lost.
* @param pVM The VM handle.
* @remark Don't confuse this with PDMR3CritSectDelete.
*/
{
int rc = VINF_SUCCESS;
{
}
return rc;
}
/**
* Initalizes a critical section and inserts it into the list.
*
* @returns VBox status code.
* @param pVM The Vm handle.
* @param pCritSect The critical section.
* @param pvKey The owner key.
* @param RT_SRC_POS_DECL The source position.
* @param pszName The name of the critical section (for statistics).
* @param pszNameFmt Format string for namging the critical section. For
* statistics and lock validation.
* @param va Arguments for the format string.
*/
static int pdmR3CritSectInitOne(PVM pVM, PPDMCRITSECTINT pCritSect, void *pvKey, RT_SRC_POS_DECL, const char *pszNameFmt, va_list va)
{
/*
* Allocate the semaphore.
*/
if (RT_SUCCESS(rc))
{
/* Only format the name once. */
if (pszName)
{
#ifndef PDMCRITSECT_STRICT
#else
#endif
if (RT_SUCCESS(rc))
{
/*
* Initialize the structure (first bit is c&p from RTCritSectInitEx).
*/
STAMR3RegisterF(pVM, &pCritSect->StatContentionRZLock, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSects/%s/ContentionRZLock", pCritSect->pszName);
STAMR3RegisterF(pVM, &pCritSect->StatContentionRZUnlock,STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSects/%s/ContentionRZUnlock", pCritSect->pszName);
STAMR3RegisterF(pVM, &pCritSect->StatContentionR3, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSects/%s/ContentionR3", pCritSect->pszName);
#ifdef VBOX_WITH_STATISTICS
STAMR3RegisterF(pVM, &pCritSect->StatLocked, STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_OCCURENCE, NULL, "/PDM/CritSects/%s/Locked", pCritSect->pszName);
#endif
return VINF_SUCCESS;
}
}
else
}
return rc;
}
/**
* Initializes a PDM critical section for internal use.
*
* The PDM critical sections are derived from the IPRT critical sections, but
* works in GC as well.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pDevIns Device instance.
* @param pCritSect Pointer to the critical section.
* @param RT_SRC_POS_DECL Use RT_SRC_POS.
* @param pszNameFmt Format string for namging the critical section. For
* statistics and lock validation.
* @param ... Arguments for the format string.
* @thread EMT(0)
*/
VMMR3DECL(int) PDMR3CritSectInit(PVM pVM, PPDMCRITSECT pCritSect, RT_SRC_POS_DECL, const char *pszNameFmt, ...)
{
#endif
return rc;
}
/**
* Initializes a PDM critical section.
*
* The PDM critical sections are derived from the IPRT critical sections, but
* works in GC as well.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pDevIns Device instance.
* @param pCritSect Pointer to the critical section.
* @param pszNameFmt Format string for namging the critical section. For
* statistics and lock validation.
* @param va Arguments for the format string.
*/
{
}
/**
* Deletes one critical section.
*
* @returns Return code from RTCritSectDelete.
*
* @param pVM The VM handle.
* @param pCritSect The critical section.
* @param pPrev The previous critical section in the list.
* @param fFinal Set if this is the final call and statistics shouldn't be deregistered.
*
* @remarks Caller must've entered the MiscCritSect.
*/
static int pdmR3CritSectDeleteOne(PVM pVM, PPDMCRITSECTINT pCritSect, PPDMCRITSECTINT pPrev, bool fFinal)
{
/*
* Assert free waiters and so on (c&p from RTCritSectDelete).
*/
/*
* Unlink it.
*/
if (pPrev)
else
/*
* Delete it (parts taken from RTCritSectDelete).
* In case someone is waiting we'll signal the semaphore cLockers + 1 times.
*/
if (!fFinal)
{
#ifdef VBOX_WITH_STATISTICS
#endif
}
return rc;
}
/**
* Deletes all critical sections with a give initializer key.
*
* @returns VBox status code.
* The entire list is processed on failure, so we'll only
* return the first error code. This shouldn't be a problem
* since errors really shouldn't happen here.
* @param pVM The VM handle.
* @param pvKey The initializer key.
*/
{
/*
* Iterate the list and match key.
*/
int rc = VINF_SUCCESS;
while (pCur)
{
{
}
/* next */
}
return rc;
}
/**
* Deletes all undeleted critical sections initalized by a given device.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pDevIns The device handle.
*/
{
}
/**
* Deletes the critical section.
*
* @returns VBox status code.
* @param pCritSect The PDM critical section to destroy.
*/
{
return VINF_SUCCESS;
/*
* Find and unlink it.
*/
while (pCur)
{
{
return rc;
}
/* next */
}
return VERR_INTERNAL_ERROR;
}
/**
* Gets the name of the critical section.
*
*
* @returns Pointer to the critical section name (read only) on success,
* NULL on failure (invalid critical section).
* @param pCritSect The critical section.
*/
{
}
/**
* Yield the critical section if someone is waiting on it.
*
* When yielding, we'll leave the critical section and try to make sure the
* other waiting threads get a chance of entering before we reclaim it.
*
* @retval true if yielded.
* @retval false if not yielded.
* @param pCritSect The critical section.
*/
{
AssertPtrReturn(pCritSect, false);
/* No recursion allowed here. */
return false;
#ifdef PDMCRITSECT_STRICT
#endif
/*
* If we're lucky, then one of the waiters has entered the lock already.
* We spin a little bit in hope for this to happen so we can avoid the
* yield deatour.
*/
{
int cLoops = 20;
while ( cLoops > 0
{
ASMNopPause();
cLoops--;
}
if (cLoops == 0)
}
#ifdef PDMCRITSECT_STRICT
#else
#endif
return true;
}
/**
* Schedule a event semaphore for signalling upon critsect exit.
*
* @returns VINF_SUCCESS on success.
* @returns VERR_TOO_MANY_SEMAPHORES if an event was already scheduled.
* @returns VERR_NOT_OWNER if we're not the critsect owner.
* @returns VERR_SEM_DESTROYED if RTCritSectDelete was called while waiting.
*
* @param pCritSect The critical section.
* @param EventToSignal The semapore that should be signalled.
*/
{
return VERR_NOT_OWNER;
{
return VINF_SUCCESS;
}
return VERR_TOO_MANY_SEMAPHORES;
}
/**
* Counts the critical sections owned by the calling thread, optionally
* returning a comma separated list naming them.
*
* This is for diagnostic purposes only.
*
* @returns Lock count.
*
* @param pVM The VM handle.
* @param pszNames Where to return the critical section names.
* @param cbNames The size of the buffer.
*/
{
/*
* Init the name buffer.
*/
if (cchLeft)
{
cchLeft--;
}
/*
* Iterate the critical sections.
*/
/* This is unsafe, but wtf. */
uint32_t cCritSects = 0;
pCur;
{
/* Same as RTCritSectIsOwner(). */
{
cCritSects++;
/*
* Copy the name if there is space. Fun stuff.
*/
if (cchLeft)
{
/* try add comma. */
if (cCritSects != 1)
{
*pszNames++ = ',';
if (--cchLeft)
{
*pszNames++ = ' ';
cchLeft--;
}
}
/* try copy the name. */
if (cchLeft)
{
{
}
else
{
if (cchLeft > 2)
{
cchLeft = 2;
}
while (cchLeft-- > 0)
*pszNames++ = '+';
}
}
*pszNames = '\0';
}
}
}
return cCritSects;
}
/**
* Leave all critical sections the calling thread owns.
*
* @param pVM The VM handle.
*/
{
pCur;
{
}
}