semevent-linux.cpp revision 4dfd6183e24104912e45b4b7d052cee00993ff1a
/* $Id$ */
/** @file
* IPRT - Event 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.
*/
#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 <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 */
};
/**
* 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;
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.
*/
return VINF_SUCCESS;
}
{
/*
* Validate input.
*/
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.
*/
return VINF_SUCCESS;
/*
* Convert timeout value.
*/
if (cMillies != RT_INDEFINITE_WAIT)
{
{
}
}
/*
* The wait loop.
*/
int rc = VINF_SUCCESS;
for (;;)
{
{
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)
{
{
leave on negative timeouts in any case */
}
/* don't wait for less than 1 microsecond */
{
rc = VERR_TIMEOUT;
break;
}
}
}
return rc;
}
{
return rc;
}
{
}
#endif /* glibc < 2.6 */