semevent-linux.cpp revision 4dfd6183e24104912e45b4b7d052cee00993ff1a
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * IPRT - Event Semaphore, Linux (2.6.x+).
c58f1213e628a545081c70e26c6b67a841cff880vboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * 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 * 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 * 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 * 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 * The external refernce to epoll_pwait is a hack which prevents that we link
05afe08870681beb0792f384475077c988916762vboxsync * against glibc < 2.6.
a8f65e585466d1267633cea76b4f97a69b7f1cc0vboxsyncasm volatile (".global epoll_pwait");
05afe08870681beb0792f384475077c988916762vboxsync#else /* glibc < 2.6 */
05afe08870681beb0792f384475077c988916762vboxsync/*******************************************************************************
05afe08870681beb0792f384475077c988916762vboxsync* Header Files *
05afe08870681beb0792f384475077c988916762vboxsync*******************************************************************************/
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#if 0 /* With 2.6.17 futex.h has become C++ unfriendly. */
05afe08870681beb0792f384475077c988916762vboxsync/*******************************************************************************
05afe08870681beb0792f384475077c988916762vboxsync* Structures and Typedefs *
05afe08870681beb0792f384475077c988916762vboxsync*******************************************************************************/
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * Linux (single wakup) event semaphore.
05afe08870681beb0792f384475077c988916762vboxsync /** Magic value. */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /** The futex state variable.
e961f5bfe1727c6816d3dad3805ebe21b6ba1c64vboxsync * 0 means not signalled.
05afe08870681beb0792f384475077c988916762vboxsync * 1 means signalled */
05afe08870681beb0792f384475077c988916762vboxsync /** The number of waiting threads */
05afe08870681beb0792f384475077c988916762vboxsync * Wrapper for the futex syscall.
05afe08870681beb0792f384475077c988916762vboxsyncstatic long sys_futex(uint32_t volatile *uaddr, int op, int val, struct timespec *utime, int32_t *uaddr2, int val3)
05afe08870681beb0792f384475077c988916762vboxsync long rc = syscall(__NR_futex, uaddr, op, val, utime, uaddr2, val3);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * Allocate semaphore handle.
05afe08870681beb0792f384475077c988916762vboxsync struct RTSEMEVENTINTERNAL *pThis = (struct RTSEMEVENTINTERNAL *)RTMemAlloc(sizeof(struct RTSEMEVENTINTERNAL));
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * Validate input.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync AssertReturn(pThis->iMagic == RTSEMEVENT_MAGIC, VERR_INVALID_HANDLE);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * Invalidate the semaphore and wake up anyone waiting on it.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync ASMAtomicXchgSize(&pThis->iMagic, RTSEMEVENT_MAGIC | UINT32_C(0x80000000));
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (ASMAtomicXchgS32(&pThis->cWaiters, INT32_MIN / 2) > 0)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync sys_futex(&pThis->fSignalled, FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * Free the semaphore memory and be gone.
05afe08870681beb0792f384475077c988916762vboxsync * Validate input.
05afe08870681beb0792f384475077c988916762vboxsync AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENT_MAGIC,
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_UNLIKELY(pThis->iMagic != RTSEMEVENT_MAGIC))
05afe08870681beb0792f384475077c988916762vboxsyncstatic int rtSemEventWait(RTSEMEVENT EventSem, unsigned cMillies, bool fAutoResume)
05afe08870681beb0792f384475077c988916762vboxsync * Validate input.
05afe08870681beb0792f384475077c988916762vboxsync AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENT_MAGIC,
05afe08870681beb0792f384475077c988916762vboxsync * Quickly check whether it's signaled.
05afe08870681beb0792f384475077c988916762vboxsync * Convert timeout value.
05afe08870681beb0792f384475077c988916762vboxsync * The wait loop.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync long lrc = sys_futex(&pThis->fSignalled, FUTEX_WAIT, 0, pTimeout, NULL, 0);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (RT_UNLIKELY(pThis->iMagic != RTSEMEVENT_MAGIC))
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /* successful wakeup or fSignalled > 0 in the meantime */
05afe08870681beb0792f384475077c988916762vboxsync /* this shouldn't happen! */
05afe08870681beb0792f384475077c988916762vboxsync AssertMsgFailed(("rc=%ld errno=%d\n", lrc, errno));
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /* adjust the relative timeout */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync ts.tv_nsec += 1000000000; /* not correct if ts.tv_sec is negative but we
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync leave on negative timeouts in any case */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /* don't wait for less than 1 microsecond */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncRTDECL(int) RTSemEventWait(RTSEMEVENT EventSem, unsigned cMillies)
05afe08870681beb0792f384475077c988916762vboxsync Assert(rc != VERR_TIMEOUT || cMillies != RT_INDEFINITE_WAIT);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncRTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#endif /* glibc < 2.6 */