semmutex-r0drv-linux.c revision 01f0d2858d4377ecf19f5aac2531efe99fef9ffc
/* $Id$ */
/** @file
* IPRT - Mutex Semaphores, Ring-0 Driver, Linux.
*/
/*
* Copyright (C) 2006-2010 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 "the-linux-kernel.h"
#include <iprt/semaphore.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
typedef struct RTSEMMUTEXLNXWAITER
{
/** The list entry. */
/** The waiting task. */
struct task_struct *pTask;
/** Why did we wake up? */
enum
{
/** Wakeup to take the semaphore. */
/** Mutex is being destroyed. */
/** Some other reason. */
} volatile enmReason;
/**
* Wrapper for the linux semaphore structure.
*/
typedef struct RTSEMMUTEXINTERNAL
{
/** Magic value (RTSEMMUTEX_MAGIC). */
/** The number of recursions. */
/** The list of waiting threads. */
/** The current owner, NULL if none. */
struct task_struct *pOwnerTask;
/** The number of references to this piece of memory. This is used to
* prevent it from being kicked from underneath us while waiting. */
/** The spinlock protecting the members and falling asleep. */
{
/*
* Allocate.
*/
if (!pThis)
return VERR_NO_MEMORY;
/*
* Initialize.
*/
pThis->cRecursions = 0;
return VINF_SUCCESS;
}
{
unsigned long fSavedIrq;
/*
* Validate.
*/
if (pThis == NIL_RTSEMMUTEX)
return VINF_SUCCESS;
AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
/*
* Kill it, kick waiters and release it.
*/
AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, RTSEMMUTEX_MAGIC_DEAD, RTSEMMUTEX_MAGIC), VERR_INVALID_HANDLE);
{
}
else
{
}
return VINF_SUCCESS;
}
/**
* Worker for rtSemMutexLinuxRequest that handles the case where we go to sleep.
*
* @returns VINF_SUCCESS, VERR_INTERRUPTED, VERR_TIMEOUT or VERR_SEM_DESTROYED.
* Returns without owning the spinlock.
* @param pThis The mutex instance.
* @param cMillies The timeout.
* @param fInterruptible The wait type.
* @param fSavedIrq The saved IRQ flags.
*/
bool fInterruptible, unsigned long fSavedIrq)
{
int rc = VERR_TIMEOUT;
/*
* Grab a reference to the mutex and add ourselves to the waiter list.
*/
/*
* Do the waiting.
*/
for (;;)
{
/* Check signal and timeout conditions. */
if ( fInterruptible
&& signal_pending(pSelf))
{
break;
}
if (!lTimeout)
break;
/* Go to sleep. */
/* Did someone wake us up? */
{
rc = VINF_SUCCESS;
break;
}
/* Is the mutex being destroyed? */
{
break;
}
}
/*
* Unlink ourself from the waiter list, dereference the mutex and exit the
* lock. We might have to free the mutex if it was the destroyed.
*/
else
{
}
return rc;
}
/**
* Internal worker.
*/
DECLINLINE(int) rtSemMutexLinuxRequest(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, bool fInterruptible)
{
unsigned long fSavedIrq;
int rc;
/*
* Validate.
*/
AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
/*
* Lock it and check if it's a recursion.
*/
{
pThis->cRecursions++;
rc = VINF_SUCCESS;
}
/*
* Not a recursion, maybe it's not owned by anyone then?
*/
{
rc = VINF_SUCCESS;
}
/*
* Was it a polling call?
*/
else if (cMillies == 0)
rc = VERR_TIMEOUT;
/*
* No, so go to sleep.
*/
else
return rc;
}
{
}
RTDECL(int) RTSemMutexRequestDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
{
}
{
}
RTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
{
}
{
unsigned long fSavedIrq;
int rc;
/*
* Validate.
*/
AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
/*
* Take the lock and release one recursion.
*/
{
if (--pThis->cRecursions == 0)
{
/* anyone to wake up? */
{
PRTSEMMUTEXLNXWAITER pWaiter = RTListNodeGetFirst(&pThis->WaiterList, RTSEMMUTEXLNXWAITER, ListEntry);
}
}
rc = VINF_SUCCESS;
}
else
rc = VERR_NOT_OWNER;
return rc;
}
{
unsigned long fSavedIrq;
bool fOwned;
/*
* Validate.
*/
AssertPtrReturn(pThis, false);
AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), false);
/*
* Take the lock and release one recursion.
*/
return fOwned;
}