VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/nt/semeventmulti-r0drv-nt.cpp@ 34015

最後變更 在這個檔案從34015是 33158,由 vboxsync 提交於 14 年 前

build fixes

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 9.5 KB
 
1/* $Id: semeventmulti-r0drv-nt.cpp 33158 2010-10-15 12:15:13Z vboxsync $ */
2/** @file
3 * IPRT - Multiple Release Event Semaphores, Ring-0 Driver, NT.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include "the-nt-kernel.h"
32#include <iprt/semaphore.h>
33
34#include <iprt/asm.h>
35#include <iprt/assert.h>
36#include <iprt/err.h>
37#include <iprt/lockvalidator.h>
38#include <iprt/mem.h>
39#include <iprt/time.h>
40#include <iprt/timer.h>
41
42#include "internal/magics.h"
43
44
45/*******************************************************************************
46* Structures and Typedefs *
47*******************************************************************************/
48/**
49 * NT event semaphore.
50 */
51typedef struct RTSEMEVENTMULTIINTERNAL
52{
53 /** Magic value (RTSEMEVENTMULTI_MAGIC). */
54 uint32_t volatile u32Magic;
55 /** Reference counter. */
56 uint32_t volatile cRefs;
57 /** The NT Event object. */
58 KEVENT Event;
59} RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
60
61
62RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI phEventMultiSem)
63{
64 return RTSemEventMultiCreateEx(phEventMultiSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
65}
66
67
68RTDECL(int) RTSemEventMultiCreateEx(PRTSEMEVENTMULTI phEventMultiSem, uint32_t fFlags, RTLOCKVALCLASS hClass,
69 const char *pszNameFmt, ...)
70{
71 AssertReturn(!(fFlags & ~RTSEMEVENTMULTI_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
72
73 AssertCompile(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
74 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis));
75 if (pThis)
76 {
77 pThis->u32Magic = RTSEMEVENTMULTI_MAGIC;
78 pThis->cRefs = 1;
79 KeInitializeEvent(&pThis->Event, NotificationEvent, FALSE /* not signalled */);
80
81 *phEventMultiSem = pThis;
82 return VINF_SUCCESS;
83 }
84 return VERR_NO_MEMORY;
85}
86
87
88/**
89 * Retains a reference to the semaphore.
90 *
91 * @param pThis The semaphore to retain.
92 */
93DECLINLINE(void) rtR0SemEventMultiNtRetain(PRTSEMEVENTMULTIINTERNAL pThis)
94{
95 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
96 Assert(cRefs < 100000); NOREF(cRefs);
97}
98
99
100/**
101 * Releases a reference to the semaphore.
102 *
103 * @param pThis The semaphore to release
104 */
105DECLINLINE(void) rtR0SemEventMultiNtRelease(PRTSEMEVENTMULTIINTERNAL pThis)
106{
107 if (ASMAtomicDecU32(&pThis->cRefs) == 0)
108 RTMemFree(pThis);
109}
110
111
112RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI hEventMultiSem)
113{
114 /*
115 * Validate input.
116 */
117 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
118 if (pThis == NIL_RTSEMEVENTMULTI)
119 return VINF_SUCCESS;
120 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
121 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
122
123 /*
124 * Invalidate it and signal the object just in case.
125 */
126 ASMAtomicIncU32(&pThis->u32Magic);
127 KeSetEvent(&pThis->Event, 0xfff, FALSE);
128 rtR0SemEventMultiNtRelease(pThis);
129 return VINF_SUCCESS;
130}
131
132
133RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI hEventMultiSem)
134{
135 /*
136 * Validate input.
137 */
138 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
139 if (!pThis)
140 return VERR_INVALID_PARAMETER;
141 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
142 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
143 rtR0SemEventMultiNtRetain(pThis);
144
145 /*
146 * Signal the event object.
147 */
148 KeSetEvent(&pThis->Event, 1, FALSE);
149
150 rtR0SemEventMultiNtRelease(pThis);
151 return VINF_SUCCESS;
152}
153
154
155RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem)
156{
157 /*
158 * Validate input.
159 */
160 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
161 if (!pThis)
162 return VERR_INVALID_PARAMETER;
163 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
164 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
165 rtR0SemEventMultiNtRetain(pThis);
166
167 /*
168 * Reset the event object.
169 */
170 KeResetEvent(&pThis->Event);
171
172 rtR0SemEventMultiNtRelease(pThis);
173 return VINF_SUCCESS;
174}
175
176
177/**
178 * Worker for RTSemEventMultiWaitEx and RTSemEventMultiWaitExDebug.
179 *
180 * @returns VBox status code.
181 * @param pThis The event semaphore.
182 * @param fFlags See RTSemEventMultiWaitEx.
183 * @param uTimeout See RTSemEventMultiWaitEx.
184 * @param pSrcPos The source code position of the wait.
185 */
186DECLINLINE(int) rtR0SemEventMultiNtWait(PRTSEMEVENTMULTIINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
187 PCRTLOCKVALSRCPOS pSrcPos)
188{
189 /*
190 * Validate input.
191 */
192 if (!pThis)
193 return VERR_INVALID_PARAMETER;
194 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
195 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
196 AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
197
198 rtR0SemEventMultiNtRetain(pThis);
199
200 /*
201 * Convert the timeout to a relative one because KeWaitForSingleObject
202 * takes system time instead of interrupt time as input for absolute
203 * timeout specifications. So, we're best of by giving it relative time.
204 *
205 * Lazy bird converts uTimeout to relative nanoseconds and then to Nt time.
206 */
207 if (!(fFlags & RTSEMWAIT_FLAGS_INDEFINITE))
208 {
209 if (fFlags & RTSEMWAIT_FLAGS_MILLISECS)
210 uTimeout = uTimeout < UINT64_MAX / UINT32_C(1000000) * UINT32_C(1000000)
211 ? uTimeout * UINT32_C(1000000)
212 : UINT64_MAX;
213 if (uTimeout == UINT64_MAX)
214 fFlags |= RTSEMWAIT_FLAGS_INDEFINITE;
215 else
216 {
217 if (fFlags & RTSEMWAIT_FLAGS_ABSOLUTE)
218 {
219 uint64_t u64Now = RTTimeSystemNanoTS();
220 uTimeout = u64Now < uTimeout
221 ? uTimeout - u64Now
222 : 0;
223 }
224 }
225 }
226
227 /*
228 * Wait for it.
229 * We're assuming interruptible waits should happen at UserMode level.
230 */
231 NTSTATUS rcNt;
232 BOOLEAN fInterruptible = !!(fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE);
233 KPROCESSOR_MODE WaitMode = fInterruptible ? UserMode : KernelMode;
234 if (fFlags & RTSEMWAIT_FLAGS_INDEFINITE)
235 rcNt = KeWaitForSingleObject(&pThis->Event, Executive, WaitMode, fInterruptible, NULL);
236 else
237 {
238 LARGE_INTEGER Timeout;
239 Timeout.QuadPart = -(int64_t)(uTimeout / 100);
240 rcNt = KeWaitForSingleObject(&pThis->Event, Executive, WaitMode, fInterruptible, &Timeout);
241 }
242 int rc;
243 if (pThis->u32Magic == RTSEMEVENTMULTI_MAGIC)
244 {
245 switch (rcNt)
246 {
247 case STATUS_SUCCESS:
248 rc = VINF_SUCCESS;
249 break;
250 case STATUS_ALERTED:
251 rc = VERR_INTERRUPTED;
252 break;
253 case STATUS_USER_APC:
254 rc = VERR_INTERRUPTED;
255 break;
256 case STATUS_TIMEOUT:
257 rc = VERR_TIMEOUT;
258 break;
259 default:
260 AssertMsgFailed(("pThis->u32Magic=%RX32 pThis=%p: wait returned %lx!\n",
261 pThis->u32Magic, pThis, (long)rcNt));
262 rc = VERR_INTERNAL_ERROR_4;
263 break;
264 }
265 }
266 else
267 rc = VERR_SEM_DESTROYED;
268
269 rtR0SemEventMultiNtRelease(pThis);
270 return rc;
271}
272
273
274#undef RTSemEventMultiWaitEx
275RTDECL(int) RTSemEventMultiWaitEx(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout)
276{
277#ifndef RTSEMEVENT_STRICT
278 return rtR0SemEventMultiNtWait(hEventMultiSem, fFlags, uTimeout, NULL);
279#else
280 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
281 return rtR0SemEventMultiNtWait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
282#endif
283}
284
285
286RTDECL(int) RTSemEventMultiWaitExDebug(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout,
287 RTHCUINTPTR uId, RT_SRC_POS_DECL)
288{
289 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
290 return rtR0SemEventMultiNtWait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
291}
292
293
294RTDECL(uint32_t) RTSemEventMultiGetResolution(void)
295{
296 return RTTimerGetSystemGranularity();
297}
298
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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