semeventmulti-linux.cpp revision 8fdb63a0d23d1618724f651b8c3d11be48b44d35
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * innotek Portable Runtime - Multiple Release Event Semaphore, Linux (2.6.x+).
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * Copyright (C) 2006-2007 innotek GmbH
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * available from http://www.virtualbox.org. This file is free software;
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * you can redistribute it and/or modify it under the terms of the GNU
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * General Public License (GPL) as published by the Free Software
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * The contents of this file may alternatively be used under the terms
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * of the Common Development and Distribution License Version 1.0
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * VirtualBox OSE distribution, in which case the provisions of the
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * CDDL are applicable instead of those of the GPL.
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * You may elect to license modified versions of this file under the
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * terms and conditions of either the GPL or the CDDL or both.
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync/*******************************************************************************
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync* Header Files *
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync*******************************************************************************/
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync#if 0 /* With 2.6.17 futex.h has become C++ unfriendly. */
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync/*******************************************************************************
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync* Structures and Typedefs *
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync*******************************************************************************/
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * Linux multiple wakup event semaphore.
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync /** Magic value. */
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync /** The futex state variable.
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * -1 means signaled.
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * 0 means not signaled, no waiters.
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * >0 means not signaled, and the value gives the number of waiters.
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * Wrapper for the futex syscall.
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsyncstatic long sys_futex(int32_t volatile *uaddr, int op, int val, struct timespec *utime, int32_t *uaddr2, int val3)
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync long rc = syscall(__NR_futex, uaddr, op, val, utime, uaddr2, val3);
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsyncRTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI pEventMultiSem)
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * Allocate semaphore handle.
89dfdbb56cf9dddad3c7685b41bda1e4e4c1d6f9vboxsync struct RTSEMEVENTMULTIINTERNAL *pThis = (struct RTSEMEVENTMULTIINTERNAL *)RTMemAlloc(sizeof(struct RTSEMEVENTMULTIINTERNAL));
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsyncRTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI EventMultiSem)
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * Validate input.
89dfdbb56cf9dddad3c7685b41bda1e4e4c1d6f9vboxsync struct RTSEMEVENTMULTIINTERNAL *pThis = EventMultiSem;
89dfdbb56cf9dddad3c7685b41bda1e4e4c1d6f9vboxsync AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENTMULTI_MAGIC,
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * Invalidate the semaphore and wake up anyone waiting on it.
8fdb63a0d23d1618724f651b8c3d11be48b44d35vboxsync ASMAtomicWriteSize(&pThis->iMagic, RTSEMEVENTMULTI_MAGIC + 1);
89dfdbb56cf9dddad3c7685b41bda1e4e4c1d6f9vboxsync sys_futex(&pThis->iState, FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * Free the semaphore memory and be gone.
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsyncRTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI EventMultiSem)
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * Validate input.
89dfdbb56cf9dddad3c7685b41bda1e4e4c1d6f9vboxsync struct RTSEMEVENTMULTIINTERNAL *pThis = EventMultiSem;
89dfdbb56cf9dddad3c7685b41bda1e4e4c1d6f9vboxsync AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENTMULTI_MAGIC,
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * Signal it.
89dfdbb56cf9dddad3c7685b41bda1e4e4c1d6f9vboxsync int32_t iOld = ASMAtomicXchgS32(&pThis->iState, -1);
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync /* wake up sleeping threads. */
89dfdbb56cf9dddad3c7685b41bda1e4e4c1d6f9vboxsync long cWoken = sys_futex(&pThis->iState, FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync AssertMsg(cWoken >= 0, ("%ld\n", cWoken)); NOREF(cWoken);
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsyncRTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI EventMultiSem)
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * Validate input.
89dfdbb56cf9dddad3c7685b41bda1e4e4c1d6f9vboxsync struct RTSEMEVENTMULTIINTERNAL *pThis = EventMultiSem;
89dfdbb56cf9dddad3c7685b41bda1e4e4c1d6f9vboxsync AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENTMULTI_MAGIC,
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * Reset it.
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsyncstatic int rtSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies, bool fAutoResume)
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * Validate input.
89dfdbb56cf9dddad3c7685b41bda1e4e4c1d6f9vboxsync struct RTSEMEVENTMULTIINTERNAL *pThis = EventMultiSem;
89dfdbb56cf9dddad3c7685b41bda1e4e4c1d6f9vboxsync AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENTMULTI_MAGIC,
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * Quickly check whether it's signaled.
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * Convert timeout value.
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * The wait loop.
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync for (unsigned i = 0;; i++)
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * Start waiting. We only account for there being or having been
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * threads waiting on the semaphore to keep things simple.
89dfdbb56cf9dddad3c7685b41bda1e4e4c1d6f9vboxsync long rc = sys_futex(&pThis->iState, FUTEX_WAIT, 1, pTimeout, NULL, 0);
89dfdbb56cf9dddad3c7685b41bda1e4e4c1d6f9vboxsync if (RT_UNLIKELY(pThis->iMagic != RTSEMEVENTMULTI_MAGIC))
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync * Act on the wakup code.
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync /* retry, the value changed. */;
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync /* this shouldn't happen! */
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsyncRTDECL(int) RTSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsync int rc = rtSemEventMultiWait(EventMultiSem, cMillies, true);
9e5c26690d45216629b5f588aced8fcfb68c23b6vboxsyncRTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)