semevent-linux.cpp revision 4dfd6183e24104912e45b4b7d052cee00993ff1a
05afe08870681beb0792f384475077c988916762vboxsync/* $Id$ */
05afe08870681beb0792f384475077c988916762vboxsync/** @file
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * IPRT - Event Semaphore, Linux (2.6.x+).
05afe08870681beb0792f384475077c988916762vboxsync */
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync/*
c58f1213e628a545081c70e26c6b67a841cff880vboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
05afe08870681beb0792f384475077c988916762vboxsync *
05afe08870681beb0792f384475077c988916762vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
05afe08870681beb0792f384475077c988916762vboxsync * available from http://www.virtualbox.org. This file is free software;
05afe08870681beb0792f384475077c988916762vboxsync * you can redistribute it and/or modify it under the terms of the GNU
05afe08870681beb0792f384475077c988916762vboxsync * General Public License (GPL) as published by the Free Software
05afe08870681beb0792f384475077c988916762vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
05afe08870681beb0792f384475077c988916762vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
05afe08870681beb0792f384475077c988916762vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
05afe08870681beb0792f384475077c988916762vboxsync *
05afe08870681beb0792f384475077c988916762vboxsync * The contents of this file may alternatively be used under the terms
05afe08870681beb0792f384475077c988916762vboxsync * of the Common Development and Distribution License Version 1.0
05afe08870681beb0792f384475077c988916762vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
05afe08870681beb0792f384475077c988916762vboxsync * VirtualBox OSE distribution, in which case the provisions of the
05afe08870681beb0792f384475077c988916762vboxsync * CDDL are applicable instead of those of the GPL.
05afe08870681beb0792f384475077c988916762vboxsync *
05afe08870681beb0792f384475077c988916762vboxsync * You may elect to license modified versions of this file under the
05afe08870681beb0792f384475077c988916762vboxsync * terms and conditions of either the GPL or the CDDL or both.
05afe08870681beb0792f384475077c988916762vboxsync *
05afe08870681beb0792f384475077c988916762vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
05afe08870681beb0792f384475077c988916762vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
05afe08870681beb0792f384475077c988916762vboxsync * additional information or have any questions.
05afe08870681beb0792f384475077c988916762vboxsync */
05afe08870681beb0792f384475077c988916762vboxsync
715e49c31b15c23c17a9ce3be42a75e7c48d4b78vboxsync#include <features.h>
05afe08870681beb0792f384475077c988916762vboxsync#if __GLIBC_PREREQ(2,6)
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync/*
05afe08870681beb0792f384475077c988916762vboxsync * glibc 2.6 fixed a serious bug in the mutex implementation. We wrote this
05afe08870681beb0792f384475077c988916762vboxsync * linux specific event semaphores code in order to work around the bug. We
05afe08870681beb0792f384475077c988916762vboxsync * will fall back on the pthread-based implementation if glibc is known to
a8f65e585466d1267633cea76b4f97a69b7f1cc0vboxsync * contain the bug fix.
a8f65e585466d1267633cea76b4f97a69b7f1cc0vboxsync *
a8f65e585466d1267633cea76b4f97a69b7f1cc0vboxsync * The external refernce to epoll_pwait is a hack which prevents that we link
05afe08870681beb0792f384475077c988916762vboxsync * against glibc < 2.6.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include "../posix/semevent-posix.cpp"
a8f65e585466d1267633cea76b4f97a69b7f1cc0vboxsyncasm volatile (".global epoll_pwait");
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync#else /* glibc < 2.6 */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
05afe08870681beb0792f384475077c988916762vboxsync/*******************************************************************************
05afe08870681beb0792f384475077c988916762vboxsync* Header Files *
05afe08870681beb0792f384475077c988916762vboxsync*******************************************************************************/
05afe08870681beb0792f384475077c988916762vboxsync#include <iprt/semaphore.h>
05afe08870681beb0792f384475077c988916762vboxsync#include <iprt/assert.h>
05afe08870681beb0792f384475077c988916762vboxsync#include <iprt/alloc.h>
05afe08870681beb0792f384475077c988916762vboxsync#include <iprt/asm.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <iprt/err.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include "internal/magics.h"
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <errno.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <limits.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <pthread.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <unistd.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <sys/time.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <sys/syscall.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#if 0 /* With 2.6.17 futex.h has become C++ unfriendly. */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync# include <linux/futex.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#else
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync# define FUTEX_WAIT 0
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync# define FUTEX_WAKE 1
05afe08870681beb0792f384475077c988916762vboxsync#endif
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync/*******************************************************************************
05afe08870681beb0792f384475077c988916762vboxsync* Structures and Typedefs *
05afe08870681beb0792f384475077c988916762vboxsync*******************************************************************************/
05afe08870681beb0792f384475077c988916762vboxsync/**
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * Linux (single wakup) event semaphore.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstruct RTSEMEVENTINTERNAL
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync{
05afe08870681beb0792f384475077c988916762vboxsync /** Magic value. */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync intptr_t volatile iMagic;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /** The futex state variable.
e961f5bfe1727c6816d3dad3805ebe21b6ba1c64vboxsync * 0 means not signalled.
05afe08870681beb0792f384475077c988916762vboxsync * 1 means signalled */
05afe08870681beb0792f384475077c988916762vboxsync uint32_t volatile fSignalled;
05afe08870681beb0792f384475077c988916762vboxsync /** The number of waiting threads */
05afe08870681beb0792f384475077c988916762vboxsync int32_t volatile cWaiters;
05afe08870681beb0792f384475077c988916762vboxsync};
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync/**
05afe08870681beb0792f384475077c988916762vboxsync * Wrapper for the futex syscall.
05afe08870681beb0792f384475077c988916762vboxsync */
05afe08870681beb0792f384475077c988916762vboxsyncstatic long sys_futex(uint32_t volatile *uaddr, int op, int val, struct timespec *utime, int32_t *uaddr2, int val3)
05afe08870681beb0792f384475077c988916762vboxsync{
05afe08870681beb0792f384475077c988916762vboxsync errno = 0;
05afe08870681beb0792f384475077c988916762vboxsync long rc = syscall(__NR_futex, uaddr, op, val, utime, uaddr2, val3);
05afe08870681beb0792f384475077c988916762vboxsync if (rc < 0)
418b9db49fbc652ef9c3f030fdc0f1a322403d95vboxsync {
418b9db49fbc652ef9c3f030fdc0f1a322403d95vboxsync Assert(rc == -1);
05afe08870681beb0792f384475077c988916762vboxsync rc = -errno;
05afe08870681beb0792f384475077c988916762vboxsync }
05afe08870681beb0792f384475077c988916762vboxsync return rc;
05afe08870681beb0792f384475077c988916762vboxsync}
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncRTDECL(int) RTSemEventCreate(PRTSEMEVENT pEventSem)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync{
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /*
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * Allocate semaphore handle.
05afe08870681beb0792f384475077c988916762vboxsync */
05afe08870681beb0792f384475077c988916762vboxsync struct RTSEMEVENTINTERNAL *pThis = (struct RTSEMEVENTINTERNAL *)RTMemAlloc(sizeof(struct RTSEMEVENTINTERNAL));
05afe08870681beb0792f384475077c988916762vboxsync if (pThis)
05afe08870681beb0792f384475077c988916762vboxsync {
05afe08870681beb0792f384475077c988916762vboxsync pThis->iMagic = RTSEMEVENT_MAGIC;
05afe08870681beb0792f384475077c988916762vboxsync pThis->cWaiters = 0;
05afe08870681beb0792f384475077c988916762vboxsync pThis->fSignalled = 0;
05afe08870681beb0792f384475077c988916762vboxsync *pEventSem = pThis;
05afe08870681beb0792f384475077c988916762vboxsync return VINF_SUCCESS;
05afe08870681beb0792f384475077c988916762vboxsync }
05afe08870681beb0792f384475077c988916762vboxsync return VERR_NO_MEMORY;
05afe08870681beb0792f384475077c988916762vboxsync}
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsyncRTDECL(int) RTSemEventDestroy(RTSEMEVENT EventSem)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync{
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /*
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * Validate input.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (EventSem == NIL_RTSEMEVENT) /* don't bitch */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync return VERR_INVALID_HANDLE;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync struct RTSEMEVENTINTERNAL *pThis = EventSem;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync AssertReturn(pThis->iMagic == RTSEMEVENT_MAGIC, VERR_INVALID_HANDLE);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /*
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * Invalidate the semaphore and wake up anyone waiting on it.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync ASMAtomicXchgSize(&pThis->iMagic, RTSEMEVENT_MAGIC | UINT32_C(0x80000000));
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (ASMAtomicXchgS32(&pThis->cWaiters, INT32_MIN / 2) > 0)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync sys_futex(&pThis->fSignalled, FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync usleep(1000);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync }
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /*
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * Free the semaphore memory and be gone.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync RTMemFree(pThis);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync return VINF_SUCCESS;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync}
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsyncRTDECL(int) RTSemEventSignal(RTSEMEVENT EventSem)
05afe08870681beb0792f384475077c988916762vboxsync{
05afe08870681beb0792f384475077c988916762vboxsync /*
05afe08870681beb0792f384475077c988916762vboxsync * Validate input.
05afe08870681beb0792f384475077c988916762vboxsync */
05afe08870681beb0792f384475077c988916762vboxsync struct RTSEMEVENTINTERNAL *pThis = EventSem;
05afe08870681beb0792f384475077c988916762vboxsync AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENT_MAGIC,
05afe08870681beb0792f384475077c988916762vboxsync VERR_INVALID_HANDLE);
05afe08870681beb0792f384475077c988916762vboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync ASMAtomicWriteU32(&pThis->fSignalled, 1);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (ASMAtomicReadS32(&pThis->cWaiters) < 1)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync return VINF_SUCCESS;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /* somebody is waiting, try wake up one of them. */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync long cWoken = sys_futex(&pThis->fSignalled, FUTEX_WAKE, 1, NULL, NULL, 0);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (RT_LIKELY(cWoken >= 0))
05afe08870681beb0792f384475077c988916762vboxsync return VINF_SUCCESS;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (RT_UNLIKELY(pThis->iMagic != RTSEMEVENT_MAGIC))
05afe08870681beb0792f384475077c988916762vboxsync return VERR_SEM_DESTROYED;
05afe08870681beb0792f384475077c988916762vboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync return VERR_INVALID_PARAMETER;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync}
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsyncstatic int rtSemEventWait(RTSEMEVENT EventSem, unsigned cMillies, bool fAutoResume)
05afe08870681beb0792f384475077c988916762vboxsync{
05afe08870681beb0792f384475077c988916762vboxsync /*
05afe08870681beb0792f384475077c988916762vboxsync * Validate input.
05afe08870681beb0792f384475077c988916762vboxsync */
05afe08870681beb0792f384475077c988916762vboxsync struct RTSEMEVENTINTERNAL *pThis = EventSem;
05afe08870681beb0792f384475077c988916762vboxsync AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENT_MAGIC,
05afe08870681beb0792f384475077c988916762vboxsync VERR_INVALID_HANDLE);
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync /*
05afe08870681beb0792f384475077c988916762vboxsync * Quickly check whether it's signaled.
05afe08870681beb0792f384475077c988916762vboxsync */
05afe08870681beb0792f384475077c988916762vboxsync if (ASMAtomicCmpXchgU32(&pThis->fSignalled, 0, 1))
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync return VINF_SUCCESS;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
05afe08870681beb0792f384475077c988916762vboxsync /*
05afe08870681beb0792f384475077c988916762vboxsync * Convert timeout value.
05afe08870681beb0792f384475077c988916762vboxsync */
05afe08870681beb0792f384475077c988916762vboxsync struct timespec ts;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync struct timespec tsEnd;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync struct timespec *pTimeout = NULL;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (cMillies != RT_INDEFINITE_WAIT)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync ts.tv_sec = cMillies / 1000;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync ts.tv_nsec = (cMillies % 1000) * 1000000;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync clock_gettime(CLOCK_REALTIME, &tsEnd);
05afe08870681beb0792f384475077c988916762vboxsync tsEnd.tv_sec += ts.tv_sec;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync tsEnd.tv_nsec += ts.tv_nsec;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (tsEnd.tv_nsec >= 1000000000)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync tsEnd.tv_nsec -= 1000000000;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync tsEnd.tv_sec++;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync }
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync pTimeout = &ts;
05afe08870681beb0792f384475077c988916762vboxsync }
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync ASMAtomicIncS32(&pThis->cWaiters);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
05afe08870681beb0792f384475077c988916762vboxsync /*
05afe08870681beb0792f384475077c988916762vboxsync * The wait loop.
05afe08870681beb0792f384475077c988916762vboxsync */
05afe08870681beb0792f384475077c988916762vboxsync int rc = VINF_SUCCESS;
05afe08870681beb0792f384475077c988916762vboxsync for (;;)
05afe08870681beb0792f384475077c988916762vboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync long lrc = sys_futex(&pThis->fSignalled, FUTEX_WAIT, 0, pTimeout, NULL, 0);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (RT_UNLIKELY(pThis->iMagic != RTSEMEVENT_MAGIC))
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync rc = VERR_SEM_DESTROYED;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync break;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync }
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (RT_LIKELY(lrc == 0 || lrc == -EWOULDBLOCK))
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /* successful wakeup or fSignalled > 0 in the meantime */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (ASMAtomicCmpXchgU32(&pThis->fSignalled, 0, 1))
05afe08870681beb0792f384475077c988916762vboxsync break;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync }
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync else if (lrc == -ETIMEDOUT)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync rc = VERR_TIMEOUT;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync break;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync }
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync else if (lrc == -EINTR)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync {
05afe08870681beb0792f384475077c988916762vboxsync if (!fAutoResume)
05afe08870681beb0792f384475077c988916762vboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync rc = VERR_INTERRUPTED;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync break;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync }
05afe08870681beb0792f384475077c988916762vboxsync }
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync else
05afe08870681beb0792f384475077c988916762vboxsync {
05afe08870681beb0792f384475077c988916762vboxsync /* this shouldn't happen! */
05afe08870681beb0792f384475077c988916762vboxsync AssertMsgFailed(("rc=%ld errno=%d\n", lrc, errno));
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync rc = RTErrConvertFromErrno(lrc);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync break;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync }
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /* adjust the relative timeout */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (pTimeout)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync {
05afe08870681beb0792f384475077c988916762vboxsync clock_gettime(CLOCK_REALTIME, &ts);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync ts.tv_nsec = tsEnd.tv_nsec - ts.tv_nsec;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync ts.tv_sec = tsEnd.tv_sec - ts.tv_sec;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (ts.tv_nsec < 0)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync ts.tv_nsec += 1000000000; /* not correct if ts.tv_sec is negative but we
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync leave on negative timeouts in any case */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync ts.tv_sec--;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync }
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /* don't wait for less than 1 microsecond */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if ( ts.tv_sec < 0
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync || (ts.tv_sec == 0 && ts.tv_nsec < 1000))
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync rc = VERR_TIMEOUT;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync break;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync }
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync }
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync }
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync ASMAtomicDecS32(&pThis->cWaiters);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync return rc;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync}
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncRTDECL(int) RTSemEventWait(RTSEMEVENT EventSem, unsigned cMillies)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync{
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync int rc = rtSemEventWait(EventSem, cMillies, true);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync Assert(rc != VERR_INTERRUPTED);
05afe08870681beb0792f384475077c988916762vboxsync Assert(rc != VERR_TIMEOUT || cMillies != RT_INDEFINITE_WAIT);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync return rc;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync}
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncRTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync{
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync return rtSemEventWait(EventSem, cMillies, false);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync}
05afe08870681beb0792f384475077c988916762vboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#endif /* glibc < 2.6 */
05afe08870681beb0792f384475077c988916762vboxsync