semmutex-linux.cpp revision f2af1be3e27a2fefe95332260d360f4b26cffbcf
/* $Id$ */
/** @file
* IPRT - Mutex Semaphore, Linux (2.6.x+).
*/
/*
* Copyright (C) 2006-2007 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/semaphore.h>
#include <iprt/lockvalidator.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
{
/** The futex state variable.
* 0 means unlocked.
* 1 means locked, no waiters.
* 2 means locked, one or more waiters.
*/
/** Nesting count. */
/** The owner of the mutex. */
/** Magic value (RTSEMMUTEX_MAGIC). */
#ifdef RTSEMMUTEX_STRICT
/** Lock validator record associated with this mutex. */
#endif
};
/* Undefine debug mappings. */
/**
* 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;
}
{
/*
* Allocate semaphore handle.
*/
struct RTSEMMUTEXINTERNAL *pThis = (struct RTSEMMUTEXINTERNAL *)RTMemAlloc(sizeof(struct RTSEMMUTEXINTERNAL));
if (pThis)
{
#ifdef RTSEMMUTEX_STRICT
RTLockValidatorRecExclInit(&pThis->ValidatorRec, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, "RTSemMutex", pThis);
#endif
return VINF_SUCCESS;
}
return VERR_NO_MEMORY;
}
{
/*
* Validate input.
*/
if (MutexSem == NIL_RTSEMMUTEX)
return VINF_SUCCESS;
/*
* Invalidate the semaphore and wake up anyone waiting on it.
*/
{
usleep(1000);
}
#ifdef RTSEMMUTEX_STRICT
#endif
/*
* Free the semaphore memory and be gone.
*/
return VINF_SUCCESS;
}
DECL_FORCE_INLINE(int) rtSemMutexRequest(RTSEMMUTEX MutexSem, unsigned cMillies, bool fAutoResume, PCRTLOCKVALSRCPOS pSrcPos)
{
/*
* Validate input.
*/
/*
* Check if nested request.
*/
{
#ifdef RTSEMMUTEX_STRICT
if (RT_FAILURE(rc9))
return rc9;
#endif
return VINF_SUCCESS;
}
#ifdef RTSEMMUTEX_STRICT
if (cMillies)
{
if (RT_FAILURE(rc9))
return rc9;
}
#else
#endif
/*
* Convert timeout value.
*/
if (cMillies != RT_INDEFINITE_WAIT)
{
}
/*
* Lock the mutex.
* Optimize for the uncontended case (makes 1-2 ns difference).
*/
{
for (;;)
{
/*
* Was the lock released in the meantime? This is unlikely (but possible)
*/
if (RT_UNLIKELY(iOld == 0))
break;
/*
* Go to sleep.
*/
{
#ifdef RTSEMMUTEX_STRICT
RTTHREADSTATE_MUTEX, true);
if (RT_FAILURE(rc9))
return rc9;
#else
#endif
}
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);
}
/* adjust the relative timeout */
if (pTimeout)
{
if (i64Diff < 1000)
{
rc = VERR_TIMEOUT;
break;
}
}
}
/*
* 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.
*/
#ifdef RTSEMMUTEX_STRICT
#endif
return VINF_SUCCESS;
}
{
#ifndef RTSEMMUTEX_STRICT
#else
#endif
return rc;
}
RTDECL(int) RTSemMutexRequestDebug(RTSEMMUTEX MutexSem, unsigned cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
{
return rc;
}
{
#ifndef RTSEMMUTEX_STRICT
#else
#endif
}
RTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX MutexSem, unsigned cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
{
}
{
/*
* Validate input.
*/
#ifdef RTSEMMUTEX_STRICT
if (RT_FAILURE(rc9))
return rc9;
#endif
/*
* 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.
*/
{
return VINF_SUCCESS;
}
/*
* Clear the state. (cNesting == 1)
*/
/*
* Release the mutex.
*/
if (RT_UNLIKELY(iNew != 0))
{
/* somebody is waiting, try wake up one of them. */
}
return VINF_SUCCESS;
}
{
/*
* Validate.
*/
AssertPtrReturn(pThis, false);
}