semevent-linux.cpp revision 47d9037afecc80d7a107338f0adfda45c1224fed
/* $Id$ */
/** @file
* IPRT - Event Semaphore, Linux (2.6.x+).
*/
/*
* 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.
*/
#include <features.h>
/*
* glibc 2.6 fixed a serious bug in the mutex implementation. We wrote this
* linux specific event semaphores code in order to work around the bug. We
* will fall back on the pthread-based implementation if glibc is known to
* contain the bug fix.
*
* The external refernce to epoll_pwait is a hack which prevents that we link
* against glibc < 2.6.
*/
#include "../posix/semevent-posix.cpp"
asm volatile (".global epoll_pwait");
#else /* glibc < 2.6 */
/*******************************************************************************
* 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 (single wakup) event semaphore.
*/
struct RTSEMEVENTINTERNAL
{
/** Magic value. */
/** The futex state variable.
* 0 means not signalled.
1 means signalled. */
uint32_t volatile fSignalled;
/** The number of waiting threads */
#ifdef RTSEMEVENT_STRICT
/** Signallers. */
/** Indicates that lock validation should be performed. */
bool volatile fEverHadSignallers;
#endif
};
/**
* Wrapper for the futex syscall.
*/
static long sys_futex(uint32_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 RTSEMEVENTINTERNAL *pThis = (struct RTSEMEVENTINTERNAL *)RTMemAlloc(sizeof(struct RTSEMEVENTINTERNAL));
if (pThis)
{
pThis->fSignalled = 0;
#ifdef RTSEMEVENT_STRICT
pThis->fEverHadSignallers = false;
#endif
return VINF_SUCCESS;
}
return VERR_NO_MEMORY;
}
{
/*
* Validate input.
*/
return VERR_INVALID_HANDLE;
/*
* Invalidate the semaphore and wake up anyone waiting on it.
*/
{
usleep(1000);
}
/*
* Free the semaphore memory and be gone.
*/
#ifdef RTSEMEVENT_STRICT
#endif
return VINF_SUCCESS;
}
{
/*
* Validate input.
*/
#ifdef RTSEMEVENT_STRICT
if (pThis->fEverHadSignallers)
{
if (RT_FAILURE(rc9))
return rc9;
}
#endif
return VINF_SUCCESS;
/* somebody is waiting, try wake up one of them. */
return VINF_SUCCESS;
return VERR_SEM_DESTROYED;
return VERR_INVALID_PARAMETER;
}
{
/*
* Validate input.
*/
/*
* Quickly check whether it's signaled.
*/
/** @todo this isn't fair if someone is already waiting on it. They should
* have the first go at it!
* (ASMAtomicReadS32(&pThis->cWaiters) == 0 || !cMillies) && ... */
return VINF_SUCCESS;
/*
* Convert the timeout value.
*/
if (cMillies != RT_INDEFINITE_WAIT)
{
if (!cMillies)
return VERR_TIMEOUT;
}
/*
* The wait loop.
*/
#ifdef RTSEMEVENT_STRICT
#else
#endif
int rc = VINF_SUCCESS;
for (;;)
{
#ifdef RTSEMEVENT_STRICT
if (pThis->fEverHadSignallers)
{
cMillies, RTTHREADSTATE_EVENT, true);
if (RT_FAILURE(rc))
break;
}
#endif
{
break;
}
{
/* successful wakeup or fSignalled > 0 in the meantime */
break;
}
{
rc = VERR_TIMEOUT;
break;
}
{
if (!fAutoResume)
{
break;
}
}
else
{
/* this shouldn't happen! */
break;
}
/* adjust the relative timeout */
if (pTimeout)
{
if (i64Diff < 1000)
{
rc = VERR_TIMEOUT;
break;
}
}
}
return rc;
}
{
return rc;
}
{
}
{
#ifdef RTSEMEVENT_STRICT
#endif
}
{
#ifdef RTSEMEVENT_STRICT
#endif
}
{
#ifdef RTSEMEVENT_STRICT
#endif
}
#endif /* glibc < 2.6 || IPRT_WITH_FUTEX_BASED_SEMS */