critsect-generic.cpp revision f2af1be3e27a2fefe95332260d360f4b26cffbcf
/* $Id$ */
/** @file
* IPRT - Critical Section, Generic.
*/
/*
* 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.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* 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 <iprt/critsect.h>
#include <iprt/semaphore.h>
/* In strict mode we're redefining these, so undefine them now for the implementation. */
{
return RTCritSectInitEx(pCritSect, 0);
}
{
/*
* Initialize the structure and
*/
int rc = RTLockValidatorRecExclCreate(&pCritSect->pValidatorRec, NIL_RTLOCKVALCLASS, 0, "RTCritSect", pCritSect);
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
return VINF_SUCCESS;
}
return rc;
}
{
/*
* Try take the lock. (cLockers is -1 if it's free)
*/
{
/*
* Somebody is owning it (or will be soon). Perhaps it's us?
*/
{
{
#ifdef RTCRITSECT_STRICT
if (RT_FAILURE(rc9))
return rc9;
#endif
return VINF_SUCCESS;
}
return VERR_SEM_NESTED;
}
return VERR_SEM_BUSY;
}
/*
* First time
*/
#ifdef RTCRITSECT_STRICT
#endif
return VINF_SUCCESS;
}
{
#ifndef RTCRTISECT_STRICT
#else
#endif
}
{
}
{
/* If the critical section has already been destroyed, then inform the caller. */
return VERR_SEM_DESTROYED;
#ifdef RTCRITSECT_STRICT
if (RT_FAILURE(rc9))
return rc9;
#endif
/*
* Increment the waiter counter.
* This becomes 0 when the section is free.
*/
{
/*
* Nested?
*/
{
{
#ifdef RTCRITSECT_STRICT
if (RT_FAILURE(rc9))
{
return rc9;
}
#endif
return VINF_SUCCESS;
}
AssertBreakpoint(); /* don't do normal assertion here, the logger uses this code too. */
return VERR_SEM_NESTED;
}
/*
* Wait for the current owner to release it.
*/
#ifndef RTCRITSECT_STRICT
#endif
for (;;)
{
#ifdef RTCRITSECT_STRICT
RTTHREADSTATE_CRITSECT, false);
if (RT_FAILURE(rc9))
{
return rc9;
}
#else
#endif
return VERR_SEM_DESTROYED;
if (rc == VINF_SUCCESS)
break;
}
AssertMsg(pCritSect->NativeThreadOwner == NIL_RTNATIVETHREAD, ("pCritSect->NativeThreadOwner=%p\n", pCritSect->NativeThreadOwner));
}
/*
* First time
*/
#ifdef RTCRITSECT_STRICT
#endif
return VINF_SUCCESS;
}
{
#ifndef RTCRITSECT_STRICT
#else
#endif
}
{
}
{
/*
* Assert ownership and so on.
*/
#ifdef RTCRITSECT_STRICT
if (RT_FAILURE(rc9))
return rc9;
#endif
/*
* Decrement nestings, if <= 0 when we'll release the critsec.
*/
else
{
/*
* Set owner to zero.
* Decrement waiters, if >= 0 then we have to wake one of them up.
*/
{
}
}
return VINF_SUCCESS;
}
static int rtCritSectEnterMultiple(size_t cCritSects, PRTCRITSECT *papCritSects, PCRTLOCKVALSRCPOS pSrcPos)
{
Assert(cCritSects > 0);
/*
* Try get them all.
*/
int rc = VERR_INVALID_PARAMETER;
size_t i;
for (i = 0; i < cCritSects; i++)
{
if (RT_FAILURE(rc))
break;
}
if (RT_SUCCESS(rc))
return rc;
/*
* The retry loop.
*/
{
/*
* We've failed, release any locks we might have gotten. ('i' is the lock that failed btw.)
*/
size_t j = i;
while (j-- > 0)
{
}
if (rc != VERR_SEM_BUSY)
return rc;
/*
* Try prevent any theoretical synchronous races with other threads.
*/
if (cTries > 10000)
/*
* Wait on the one we failed to get.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Try take the others.
*/
for (j = 0; j < cCritSects; j++)
{
if (j != i)
{
if (RT_FAILURE(rc))
break;
}
}
if (RT_SUCCESS(rc))
return rc;
/*
* We failed.
*/
if (i > j)
{
}
i = j;
}
}
{
#ifndef RTCRITSECT_STRICT
#else
#endif
}
RTDECL(int) RTCritSectEnterMultipleDebug(size_t cCritSects, PRTCRITSECT *papCritSects, RTUINTPTR uId, RT_SRC_POS_DECL)
{
}
{
int rc = VINF_SUCCESS;
for (size_t i = 0; i < cCritSects; i++)
{
}
return rc;
}
{
/*
* Assert free waiters and so on.
*/
/*
* Invalidate the structure and free the mutex.
* In case someone is waiting we'll signal the semaphore cLockers + 1 times.
*/
return rc;
}