VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/nt/semevent-r0drv-nt.cpp@ 25724

Last change on this file since 25724 was 25724, checked in by vboxsync, 15 years ago

iprt: Use RTMSINTERVAL for timeouts. Fixed missing timeout underflow checks in two RTFileAioCtxWait implementations.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.7 KB
Line 
1/* $Id: semevent-r0drv-nt.cpp 25724 2010-01-11 14:45:34Z vboxsync $ */
2/** @file
3 * IPRT - Single Release Event Semaphores, Ring-0 Driver, NT.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32
33/*******************************************************************************
34* Header Files *
35*******************************************************************************/
36#include "the-nt-kernel.h"
37#include <iprt/semaphore.h>
38#include <iprt/alloc.h>
39#include <iprt/assert.h>
40#include <iprt/asm.h>
41#include <iprt/err.h>
42
43#include "internal/magics.h"
44
45
46/*******************************************************************************
47* Structures and Typedefs *
48*******************************************************************************/
49/**
50 * NT event semaphore.
51 */
52typedef struct RTSEMEVENTINTERNAL
53{
54 /** Magic value (RTSEMEVENT_MAGIC). */
55 uint32_t volatile u32Magic;
56 /** The NT Event object. */
57 KEVENT Event;
58} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
59
60
61RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
62{
63 return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
64}
65
66
67RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
68{
69 AssertReturn(!(fFlags & ~RTSEMEVENT_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
70 AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
71
72 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
73 if (!pThis)
74 return VERR_NO_MEMORY;
75
76 pThis->u32Magic = RTSEMEVENT_MAGIC;
77 KeInitializeEvent(&pThis->Event, SynchronizationEvent, FALSE);
78
79 *phEventSem = pThis;
80 return VINF_SUCCESS;
81}
82
83
84RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
85{
86 /*
87 * Validate input.
88 */
89 PRTSEMEVENTINTERNAL pThis = hEventSem;
90 if (pThis == NIL_RTSEMEVENT)
91 return VINF_SUCCESS;
92 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
93 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
94
95 /*
96 * Invalidate it and signal the object just in case.
97 */
98 ASMAtomicIncU32(&pThis->u32Magic);
99 KeSetEvent(&pThis->Event, 0xfff, FALSE);
100 RTMemFree(pThis);
101 return VINF_SUCCESS;
102}
103
104
105RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
106{
107 /*
108 * Validate input.
109 */
110 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
111 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
112 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
113
114 /*
115 * Signal the event object.
116 */
117 KeSetEvent(&pThis->Event, 1, FALSE);
118 return VINF_SUCCESS;
119}
120
121
122static int rtSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies, bool fInterruptible)
123{
124 /*
125 * Validate input.
126 */
127 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
128 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
129 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
130
131 /*
132 * Wait for it.
133 * We're assuming interruptible waits should happen at UserMode level.
134 */
135 NTSTATUS rcNt;
136 KPROCESSOR_MODE WaitMode = fInterruptible ? UserMode : KernelMode;
137 if (cMillies == RT_INDEFINITE_WAIT)
138 rcNt = KeWaitForSingleObject(&pThis->Event, Executive, WaitMode, fInterruptible, NULL);
139 else
140 {
141 LARGE_INTEGER Timeout;
142 Timeout.QuadPart = -(int64_t)cMillies * 10000;
143 rcNt = KeWaitForSingleObject(&pThis->Event, Executive, WaitMode, fInterruptible, &Timeout);
144 }
145 switch (rcNt)
146 {
147 case STATUS_SUCCESS:
148 if (pThis->u32Magic == RTSEMEVENT_MAGIC)
149 return VINF_SUCCESS;
150 return VERR_SEM_DESTROYED;
151 case STATUS_ALERTED:
152 return VERR_INTERRUPTED;
153 case STATUS_USER_APC:
154 return VERR_INTERRUPTED;
155 case STATUS_TIMEOUT:
156 return VERR_TIMEOUT;
157 default:
158 AssertMsgFailed(("pThis->u32Magic=%RX32 pThis=%p: wait returned %lx!\n",
159 pThis->u32Magic, pThis, (long)rcNt));
160 return VERR_INTERNAL_ERROR;
161 }
162}
163
164
165RTDECL(int) RTSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
166{
167 return rtSemEventWait(hEventSem, cMillies, false /* fInterruptible */);
168}
169
170
171RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
172{
173 return rtSemEventWait(hEventSem, cMillies, true /* fInterruptible */);
174}
175
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette