semspinmutex-r0drv-generic.cpp revision 0b4890533cbae4f30a0241d0780d4ff9daa304f4
/* $Id$ */
/** @file
* IPRT - Spinning Mutex Semaphores, Ring-0 Driver, Generic.
*/
/*
* Copyright (C) 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 *
*******************************************************************************/
#ifdef RT_OS_WINDOWS
# include "../nt/the-nt-kernel.h"
#endif
#include <iprt/semaphore.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
typedef struct RTSEMSPINMUTEXINTERNAL
{
/** Magic value (RTSEMSPINMUTEX_MAGIC)
* RTCRITSECT_MAGIC is the value of an initialized & operational section. */
/** Number of lockers.
* -1 if the section is free. */
/** The owner thread. */
RTNATIVETHREAD volatile hOwner;
/** Flags. This is a combination of RTSEMSPINMUTEX_FLAGS_XXX and
* RTSEMSPINMUTEX_INT_FLAGS_XXX. */
/** The semaphore to block on. */
/** Saved flags register of the owner. (AMD64 & x86) */
/** The preemption disable state of the owner. */
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
//#define RTSEMSPINMUTEX_INT_FLAGS_MUST
/** Validates the handle, returning if invalid. */
#define RTSEMSPINMUTEX_VALIDATE_RETURN(pThis) \
do \
{ \
if (u32Magic != RTSEMSPINMUTEX_MAGIC) \
{ \
} \
} while (0)
{
/*
* Allocate and initialize the structure.
*/
if (!pThis)
return VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
return rc;
}
{
RTCCUINTREG fSavedFlags = 0;
/*
* Check context, disable preemption and save flags if necessary.
*/
#ifdef RT_OS_WINDOWS
/* NT: IRQL <= DISPATCH_LEVEL for waking up threads. */
return VERR_SEM_BAD_CONTEXT; /** @todo Can be optimized by switching the thing into spinlock mode. But later. */
#else /* PORTME: Check for context where we cannot wake up threads. */
#endif
/*
* Try take the lock. (cLockers is -1 if it's free)
*/
{
/* busy, too bad. */
int rc = VERR_SEM_BUSY;
{
AssertMsgFailed(("%p attempt at nested access\n"));
}
/*
* Restore preemption and flags.
*/
#ifdef RT_OS_WINDOWS
#else
#endif
return rc;
}
/*
* We've got the lock.
*/
return VINF_SUCCESS;
}
{
/** If the critical section has already been destroyed, then inform the caller. */
return VERR_SEM_DESTROYED;
/*
* Increment the waiter counter.
* This becomes 0 when the section is free.
*/
{
/*
* Nested?
*/
{
{
return VINF_SUCCESS;
}
else
{
return VERR_SEM_NESTED;
}
}
for (;;)
{
return VERR_SEM_DESTROYED;
if (rc == VINF_SUCCESS)
break;
}
AssertMsg(pCritSect->NativeThreadOwner == NIL_RTNATIVETHREAD, ("pCritSect->NativeThreadOwner=%p\n", pCritSect->NativeThreadOwner));
}
/*
* First time
*/
return VINF_SUCCESS;
}
/**
* Leave a critical section.
*
* @returns VINF_SUCCESS.
* @param pCritSect The critical section.
*/
{
/*
* Assert ownership and so on.
*/
/*
* 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;
}