semeventmulti-r0drv-nt.cpp revision ab93606043a9881487aa83be04191d2f4ea24071
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync/* $Id$ */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync/** @file
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * IPRT - Multiple Release Event Semaphores, Ring-0 Driver, NT.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync/*
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * 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
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * General Public License (GPL) as published by the Free Software
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync *
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * The contents of this file may alternatively be used under the terms
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * of the Common Development and Distribution License Version 1.0
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * VirtualBox OSE distribution, in which case the provisions of the
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * CDDL are applicable instead of those of the GPL.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync *
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * You may elect to license modified versions of this file under the
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * terms and conditions of either the GPL or the CDDL or both.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync *
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * additional information or have any questions.
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 RTSEMEVENTMULTIINTERNAL
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync{
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /** Magic value (RTSEMEVENTMULTI_MAGIC). */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync uint32_t volatile u32Magic;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /** The NT Event object. */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync KEVENT Event;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync} RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsyncRTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI phEventMultiSem)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync{
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return RTSemEventMultiCreateEx(phEventMultiSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync}
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsyncRTDECL(int) RTSemEventMultiCreateEx(PRTSEMEVENTMULTI phEventMultiSem, uint32_t fFlags, RTLOCKVALCLASS hClass,
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync const char *pszNameFmt, ...)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync{
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync AssertReturn(!(fFlags & ~RTSEMEVENTMULTI_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync AssertCompile(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis));
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync if (pThis)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync {
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync pThis->u32Magic = RTSEMEVENTMULTI_MAGIC;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync KeInitializeEvent(&pThis->Event, NotificationEvent, FALSE /* not signalled */);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync *phEventSem = pThis;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VINF_SUCCESS;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync }
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VERR_NO_MEMORY;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync}
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsyncRTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI hEventMultiSem)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync{
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /*
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Validate input.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync if (pThis == NIL_RTSEMEVENTMULTI)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VINF_SUCCESS;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /*
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Invalidate it and signal the object just in case.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync ASMAtomicIncU32(&pThis->u32Magic);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync KeSetEvent(&pThis->Event, 0xfff, FALSE);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync RTMemFree(pThis);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VINF_SUCCESS;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync}
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsyncRTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI hEventMultiSem)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync{
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /*
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Validate input.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync if (!pThis)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VERR_INVALID_PARAMETER;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /*
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Signal the event object.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync KeSetEvent(&pThis->Event, 1, FALSE);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VINF_SUCCESS;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync}
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsyncRTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync{
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /*
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Validate input.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync if (!pThis)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VERR_INVALID_PARAMETER;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /*
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Reset the event object.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync KeResetEvent(&pThis->Event);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VINF_SUCCESS;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync}
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsyncstatic int rtSemEventMultiWait(RTSEMEVENTMULTI hEventMultiSem, unsigned cMillies, bool fInterruptible)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync{
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /*
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Validate input.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync if (!pThis)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VERR_INVALID_PARAMETER;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /*
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Wait for it.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * We're assuming interruptible waits should happen at UserMode level.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync NTSTATUS rcNt;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync KPROCESSOR_MODE WaitMode = fInterruptible ? UserMode : KernelMode;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync if (cMillies == RT_INDEFINITE_WAIT)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync rcNt = KeWaitForSingleObject(&pThis->Event, Executive, WaitMode, fInterruptible, NULL);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync else
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync {
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync LARGE_INTEGER Timeout;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync Timeout.QuadPart = -(int64_t)cMillies * 10000;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync rcNt = KeWaitForSingleObject(&pThis->Event, Executive, WaitMode, fInterruptible, &Timeout);
}
switch (rcNt)
{
case STATUS_SUCCESS:
if (pThis->u32Magic == RTSEMEVENTMULTI_MAGIC)
return VINF_SUCCESS;
return VERR_SEM_DESTROYED;
case STATUS_ALERTED:
return VERR_INTERRUPTED;
case STATUS_USER_APC:
return VERR_INTERRUPTED;
case STATUS_TIMEOUT:
return VERR_TIMEOUT;
default:
AssertMsgFailed(("pThis->u32Magic=%RX32 pThis=%p: wait returned %lx!\n",
pThis->u32Magic, pThis, (long)rcNt));
return VERR_INTERNAL_ERROR;
}
}
RTDECL(int) RTSemEventMultiWait(RTSEMEVENTMULTI hEventMultiSem, unsigned cMillies)
{
return rtSemEventMultiWait(hEventMultiSem, cMillies, false /* fInterruptible */);
}
RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI hEventMultiSem, unsigned cMillies)
{
return rtSemEventMultiWait(hEventMultiSem, cMillies, true /* fInterruptible */);
}