semmutex-linux.cpp revision 9e5c26690d45216629b5f588aced8fcfb68c23b6
/* $Id$ */
/** @file
* innotek Portable Runtime - Mutex Semaphore, Linux (2.6.x+).
*/
/*
* Copyright (C) 2006-2007 innotek GmbH
*
* 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/semaphore.h>
#include <errno.h>
#include <limits.h>
#include <pthread.h>
#include <unistd.h>
#if 0 /* With 2.6.17 futex.h has become C++ unfriendly. */
#else
# define FUTEX_WAIT 0
# define FUTEX_WAKE 1
#endif
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Linux internal representation of a Mutex semaphore.
*/
struct RTSEMMUTEXINTERNAL
{
/** Magic value. */
/** The futex state variable.
* 0 means unlocked.
* 1 means locked, no waiters.
* 2 means locked, one or more waiters.
*/
/** The owner of the mutex. */
/** Nesting count. */
};
/**
* Wrapper for the futex syscall.
*/
static long sys_futex(int32_t volatile *uaddr, int op, int val, struct timespec *utime, int32_t *uaddr2, int val3)
{
errno = 0;
if (rc < 0)
{
}
return rc;
}
/**
* Validate a Mutex semaphore handle passed to one of the interface.
*
* @returns true if valid.
* @returns false if invalid.
* @param pIntMutexSem Pointer to the mutex semaphore to validate.
*/
{
return false;
return false;
return false;
return true;
}
{
/*
* Allocate semaphore handle.
*/
struct RTSEMMUTEXINTERNAL *pIntMutexSem = (struct RTSEMMUTEXINTERNAL *)RTMemAlloc(sizeof(struct RTSEMMUTEXINTERNAL));
if (pIntMutexSem)
{
pIntMutexSem->iState = 0;
pIntMutexSem->cNesting = 0;
return VINF_SUCCESS;
}
return VERR_NO_MEMORY;
}
{
/*
* Validate input.
*/
if (!rtsemMutexValid(pIntMutexSem))
{
return VERR_INVALID_HANDLE;
}
/*
* Invalidate the semaphore and wake up anyone waiting on it.
*/
{
usleep(1000);
}
pIntMutexSem->cNesting = ~0;
/*
* Free the semaphore memory and be gone.
*/
return VINF_SUCCESS;
}
{
/*
* Validate input.
*/
if (!rtsemMutexValid(pIntMutexSem))
{
return VERR_INVALID_HANDLE;
}
/*
* Check if nested request.
*/
&& pIntMutexSem->cNesting > 0)
{
pIntMutexSem->cNesting++;
return VINF_SUCCESS;
}
/*
* Convert timeout value.
*/
if (cMillies != RT_INDEFINITE_WAIT)
{
}
/*
* Lock the mutex.
*/
if (RT_UNLIKELY(iOld != 0))
{
for (;;)
{
/*
* Was the lock released in the meantime? This is unlikely (but possible)
*/
if (RT_UNLIKELY(iOld == 0))
break;
/*
* Go to sleep.
*/
return VERR_SEM_DESTROYED;
/*
* Act on the wakup code.
*/
{
return VERR_TIMEOUT;
}
if (rc == 0)
/* we'll leave the loop now unless another thread is faster */;
else if (rc == -EWOULDBLOCK)
/* retry with new value. */;
{
if (!fAutoResume)
return VERR_INTERRUPTED;
}
else
{
/* this shouldn't happen! */
return RTErrConvertFromErrno(rc);
}
}
/*
* When leaving this loop, iState is set to 2. This means that we gained the
* Lock and there are _possibly_ some waiters. We don't know exactly as another
* thread might entered this loop at nearly the same time. Therefore we will
* call futex_wakeup once too often (if _no_ other thread entered this loop).
* The key problem is the simple futex_wait test for x != y (iState != 2) in
* our case).
*/
}
/*
* Set the owner and nesting.
*/
return VINF_SUCCESS;
}
{
return rc;
}
{
}
{
/*
* Validate input.
*/
if (!rtsemMutexValid(pIntMutexSem))
{
return VERR_INVALID_HANDLE;
}
/*
* Check if nested.
*/
{
AssertMsgFailed(("Not owner of mutex %p!! Self=%08x Owner=%08x cNesting=%d\n",
return VERR_NOT_OWNER;
}
/*
* If nested we'll just pop a nesting.
*/
{
pIntMutexSem->cNesting--;
return VINF_SUCCESS;
}
/*
* Clear the state. (cNesting == 1)
*/
/*
* Release the mutex.
*/
if (iNew != 0)
{
/* somebody is waiting, try wake up one of them. */
}
return VINF_SUCCESS;
}