semevent-r0drv-nt.cpp revision 5b281ba489ca18f0380d7efc7a5108b606cce449
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
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsyncRTDECL(int) RTSemEventCreate(PRTSEMEVENT pEventSem)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync{
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync Assert(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pEventInt));
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync if (pEventInt)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync {
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync pEventInt->u32Magic = RTSEMEVENT_MAGIC;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync KeInitializeEvent(&pEventInt->Event, SynchronizationEvent, FALSE);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync *pEventSem = pEventInt;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VINF_SUCCESS;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync }
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VERR_NO_MEMORY;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync}
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsyncRTDECL(int) RTSemEventDestroy(RTSEMEVENT EventSem)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync{
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /*
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Validate input.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync if (!pEventInt)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VERR_INVALID_PARAMETER;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync if (pEventInt->u32Magic != RTSEMEVENT_MAGIC)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync {
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync AssertMsgFailed(("pEventInt->u32Magic=%RX32 pEventInt=%p\n", pEventInt->u32Magic, pEventInt));
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VERR_INVALID_PARAMETER;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync }
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /*
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Invalidate it and signal the object just in case.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync ASMAtomicIncU32(&pEventInt->u32Magic);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync KeSetEvent(&pEventInt->Event, 0xfff, FALSE);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync RTMemFree(pEventInt);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VINF_SUCCESS;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync}
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsyncRTDECL(int) RTSemEventSignal(RTSEMEVENT EventSem)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync{
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /*
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Validate input.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync if (!pEventInt)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VERR_INVALID_PARAMETER;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync if ( !pEventInt
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync || pEventInt->u32Magic != RTSEMEVENT_MAGIC)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync {
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync AssertMsgFailed(("pEventInt->u32Magic=%RX32 pEventInt=%p\n", pEventInt ? pEventInt->u32Magic : 0, pEventInt));
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VERR_INVALID_PARAMETER;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync }
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /*
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Signal the event object.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync KeSetEvent(&pEventInt->Event, 1, FALSE);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VINF_SUCCESS;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync}
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsyncstatic int rtSemEventWait(RTSEMEVENT EventSem, unsigned cMillies, bool fInterruptible)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync{
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /*
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Validate input.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync if (!pEventInt)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VERR_INVALID_PARAMETER;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync if ( !pEventInt
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync || pEventInt->u32Magic != RTSEMEVENT_MAGIC)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync {
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync AssertMsgFailed(("pEventInt->u32Magic=%RX32 pEventInt=%p\n", pEventInt ? pEventInt->u32Magic : 0, pEventInt));
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VERR_INVALID_PARAMETER;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync }
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /*
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Wait for it.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync NTSTATUS rcNt;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync if (cMillies == RT_INDEFINITE_WAIT)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync rcNt = KeWaitForSingleObject(&pEventInt->Event, Executive, KernelMode, fInterruptible, NULL);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync else
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync {
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync LARGE_INTEGER Timeout;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync Timeout.QuadPart = -(int64_t)cMillies * 10000;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync rcNt = KeWaitForSingleObject(&pEventInt->Event, Executive, KernelMode, fInterruptible, &Timeout);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync }
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync switch (rcNt)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync {
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync case STATUS_SUCCESS:
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync if (pEventInt->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:
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync AssertMsgFailed(("pEventInt->u32Magic=%RX32 pEventInt=%p: wait returned %lx!\n",
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync pEventInt->u32Magic, pEventInt, (long)rcNt));
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VERR_INTERNAL_ERROR;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync }
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync}
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsyncRTDECL(int) RTSemEventWait(RTSEMEVENT EventSem, unsigned cMillies)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync{
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return rtSemEventWait(EventSem, cMillies, false /* fInterruptible */);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync}
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsyncRTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync{
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return rtSemEventWait(EventSem, cMillies, true /* fInterruptible */);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync}
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync