5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * IPRT - Single Release Event Semaphores, Ring-0 Driver, NT.
c58f1213e628a545081c70e26c6b67a841cff880vboxsync * Copyright (C) 2006-2011 Oracle Corporation
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * available from http://www.virtualbox.org. This file is free software;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * you can redistribute it and/or modify it under the terms of the GNU
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * General Public License (GPL) as published by the Free Software
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * The contents of this file may alternatively be used under the terms
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * of the Common Development and Distribution License Version 1.0
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution, in which case the provisions of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * CDDL are applicable instead of those of the GPL.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * You may elect to license modified versions of this file under the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * terms and conditions of either the GPL or the CDDL or both.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync/*******************************************************************************
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync* Header Files *
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync*******************************************************************************/
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync/*******************************************************************************
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync* Structures and Typedefs *
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync*******************************************************************************/
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * NT event semaphore.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /** Magic value (RTSEMEVENT_MAGIC). */
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsync /** Reference counter. */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /** The NT Event object. */
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsyncRTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsyncRTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
418b9db49fbc652ef9c3f030fdc0f1a322403d95vboxsync AssertReturn(!(fFlags & ~(RTSEMEVENT_FLAGS_NO_LOCK_VAL | RTSEMEVENT_FLAGS_BOOTSTRAP_HACK)), VERR_INVALID_PARAMETER);
418b9db49fbc652ef9c3f030fdc0f1a322403d95vboxsync Assert(!(fFlags & RTSEMEVENT_FLAGS_BOOTSTRAP_HACK) || (fFlags & RTSEMEVENT_FLAGS_NO_LOCK_VAL));
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
36c508c1569b4ff2c4392b32266d2bef62064242vboxsync PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
99aa2f6ae259b9b90562d09f3f663f07c2828eeevboxsync KeInitializeEvent(&pThis->Event, SynchronizationEvent, FALSE /* not signalled */);
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsync * Retains a reference to the semaphore.
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsync * @param pThis The semaphore to retain.
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsyncDECLINLINE(void) rtR0SemEventNtRetain(PRTSEMEVENTINTERNAL pThis)
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsync * Releases a reference to the semaphore.
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsync * @param pThis The semaphore to release
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsyncDECLINLINE(void) rtR0SemEventNtRelease(PRTSEMEVENTINTERNAL pThis)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Validate input.
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Invalidate it and signal the object just in case.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Validate input.
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Signal the event object.
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsync * Worker for RTSemEventWaitEx and RTSemEventWaitExDebug.
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsync * @returns VBox status code.
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsync * @param pThis The event semaphore.
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsync * @param fFlags See RTSemEventWaitEx.
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsync * @param uTimeout See RTSemEventWaitEx.
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsync * @param pSrcPos The source code position of the wait.
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsyncDECLINLINE(int) rtR0SemEventNtWait(PRTSEMEVENTINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Validate input.
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsync AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
81a2bceb78e8b2f05efef55789b1689b31b3acacvboxsync AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsync * Convert the timeout to a relative one because KeWaitForSingleObject
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsync * takes system time instead of interrupt time as input for absolute
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsync * timeout specifications. So, we're best of by giving it relative time.
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsync * Lazy bird converts uTimeout to relative nanoseconds and then to Nt time.
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsync uTimeout = uTimeout < UINT64_MAX / UINT32_C(1000000) * UINT32_C(1000000)
1198fb18b50cc20479302d19c0aa7ae93f69d359vboxsync * Wait for it.
1198fb18b50cc20479302d19c0aa7ae93f69d359vboxsync * We're assuming interruptible waits should happen at UserMode level.
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsync BOOLEAN fInterruptible = !!(fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE);
1198fb18b50cc20479302d19c0aa7ae93f69d359vboxsync KPROCESSOR_MODE WaitMode = fInterruptible ? UserMode : KernelMode;
36c508c1569b4ff2c4392b32266d2bef62064242vboxsync rcNt = KeWaitForSingleObject(&pThis->Event, Executive, WaitMode, fInterruptible, NULL);
36c508c1569b4ff2c4392b32266d2bef62064242vboxsync rcNt = KeWaitForSingleObject(&pThis->Event, Executive, WaitMode, fInterruptible, &Timeout);
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsync AssertMsgFailed(("pThis->u32Magic=%RX32 pThis=%p: wait returned %lx!\n",
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsyncRTDECL(int) RTSemEventWaitEx(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout)
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsync return rtR0SemEventNtWait(hEventSem, fFlags, uTimeout, NULL);
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsync RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsync return rtR0SemEventNtWait(hEventSem, fFlags, uTimeout, &SrcPos);
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsyncRTDECL(int) RTSemEventWaitExDebug(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout,
4c5ed14a824b5df6b1d5aee5f524eea7b0d9459fvboxsync RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();