semevent-r0drv-nt.cpp revision a734bb626f889ee3334bfc381be194b4ec56aae6
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync/* $Id$ */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync/** @file
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * IPRT - Single Release Event Semaphores, Ring-0 Driver, NT.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync/*
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync *
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 *
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 *
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.
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync *
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * additional information or have any questions.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync/*******************************************************************************
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync* Header Files *
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync*******************************************************************************/
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync#include "the-nt-kernel.h"
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync#include <iprt/semaphore.h>
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync#include <iprt/alloc.h>
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync#include <iprt/assert.h>
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync#include <iprt/asm.h>
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync#include <iprt/err.h>
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync#include "internal/magics.h"
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync/*******************************************************************************
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync* Structures and Typedefs *
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync*******************************************************************************/
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync/**
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * NT event semaphore.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsynctypedef struct RTSEMEVENTINTERNAL
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync{
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /** Magic value (RTSEMEVENT_MAGIC). */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync uint32_t volatile u32Magic;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /** The NT Event object. */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync KEVENT Event;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsyncRTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync{
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync}
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsyncRTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync{
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync AssertReturn(!(fFlags & ~RTSEMEVENT_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync
36c508c1569b4ff2c4392b32266d2bef62064242vboxsync PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync if (!pThis)
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync return VERR_NO_MEMORY;
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync pThis->u32Magic = RTSEMEVENT_MAGIC;
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync KeInitializeEvent(&pThis->Event, SynchronizationEvent, FALSE);
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync *phEventSem = pThis;
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync return VINF_SUCCESS;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync}
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsyncRTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync{
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /*
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Validate input.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync PRTSEMEVENTINTERNAL pThis = hEventSem;
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync if (pThis == NIL_RTSEMEVENT)
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync return VINF_SUCCESS;
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /*
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Invalidate it and signal the object just in case.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
36c508c1569b4ff2c4392b32266d2bef62064242vboxsync ASMAtomicIncU32(&pThis->u32Magic);
36c508c1569b4ff2c4392b32266d2bef62064242vboxsync KeSetEvent(&pThis->Event, 0xfff, FALSE);
36c508c1569b4ff2c4392b32266d2bef62064242vboxsync RTMemFree(pThis);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VINF_SUCCESS;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync}
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsyncRTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync{
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /*
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Validate input.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /*
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Signal the event object.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
36c508c1569b4ff2c4392b32266d2bef62064242vboxsync KeSetEvent(&pThis->Event, 1, FALSE);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VINF_SUCCESS;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync}
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsyncstatic int rtSemEventWait(RTSEMEVENT hEventSem, unsigned cMillies, bool fInterruptible)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync{
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /*
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Validate input.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
1198fb18b50cc20479302d19c0aa7ae93f69d359vboxsync /*
1198fb18b50cc20479302d19c0aa7ae93f69d359vboxsync * Wait for it.
1198fb18b50cc20479302d19c0aa7ae93f69d359vboxsync * We're assuming interruptible waits should happen at UserMode level.
1198fb18b50cc20479302d19c0aa7ae93f69d359vboxsync */
1198fb18b50cc20479302d19c0aa7ae93f69d359vboxsync NTSTATUS rcNt;
1198fb18b50cc20479302d19c0aa7ae93f69d359vboxsync KPROCESSOR_MODE WaitMode = fInterruptible ? UserMode : KernelMode;
1198fb18b50cc20479302d19c0aa7ae93f69d359vboxsync if (cMillies == RT_INDEFINITE_WAIT)
36c508c1569b4ff2c4392b32266d2bef62064242vboxsync rcNt = KeWaitForSingleObject(&pThis->Event, Executive, WaitMode, fInterruptible, NULL);
1198fb18b50cc20479302d19c0aa7ae93f69d359vboxsync else
1198fb18b50cc20479302d19c0aa7ae93f69d359vboxsync {
1198fb18b50cc20479302d19c0aa7ae93f69d359vboxsync LARGE_INTEGER Timeout;
1198fb18b50cc20479302d19c0aa7ae93f69d359vboxsync Timeout.QuadPart = -(int64_t)cMillies * 10000;
36c508c1569b4ff2c4392b32266d2bef62064242vboxsync rcNt = KeWaitForSingleObject(&pThis->Event, Executive, WaitMode, fInterruptible, &Timeout);
1198fb18b50cc20479302d19c0aa7ae93f69d359vboxsync }
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync switch (rcNt)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync {
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync case STATUS_SUCCESS:
36c508c1569b4ff2c4392b32266d2bef62064242vboxsync if (pThis->u32Magic == RTSEMEVENT_MAGIC)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VINF_SUCCESS;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VERR_SEM_DESTROYED;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync case STATUS_ALERTED:
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VERR_INTERRUPTED;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync case STATUS_USER_APC:
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VERR_INTERRUPTED;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync case STATUS_TIMEOUT:
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VERR_TIMEOUT;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync default:
36c508c1569b4ff2c4392b32266d2bef62064242vboxsync AssertMsgFailed(("pThis->u32Magic=%RX32 pThis=%p: wait returned %lx!\n",
36c508c1569b4ff2c4392b32266d2bef62064242vboxsync pThis->u32Magic, pThis, (long)rcNt));
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VERR_INTERNAL_ERROR;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync }
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync}
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsyncRTDECL(int) RTSemEventWait(RTSEMEVENT hEventSem, unsigned cMillies)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync{
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync return rtSemEventWait(hEventSem, cMillies, false /* fInterruptible */);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync}
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsyncRTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT hEventSem, unsigned cMillies)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync{
a734bb626f889ee3334bfc381be194b4ec56aae6vboxsync return rtSemEventWait(hEventSem, cMillies, true /* fInterruptible */);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync}
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync