VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/nt/semaphore-r0drv-nt.cpp@ 5165

Last change on this file since 5165 was 5165, checked in by vboxsync, 17 years ago

RTSemEventWaitNoResume

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.0 KB
Line 
1/* $Id: semaphore-r0drv-nt.cpp 5165 2007-10-05 13:32:50Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - Semaphores, Ring-0 Driver, NT.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19
20/*******************************************************************************
21* Header Files *
22*******************************************************************************/
23#include "the-nt-kernel.h"
24#include <iprt/semaphore.h>
25#include <iprt/alloc.h>
26#include <iprt/assert.h>
27#include <iprt/asm.h>
28#include <iprt/err.h>
29
30#include "internal/magics.h"
31
32
33/*******************************************************************************
34* Structures and Typedefs *
35*******************************************************************************/
36/**
37 * NT event semaphore.
38 */
39typedef struct RTSEMEVENTINTERNAL
40{
41 /** Magic value (RTSEMEVENT_MAGIC). */
42 uint32_t volatile u32Magic;
43 /** The NT Event object. */
44 KEVENT Event;
45} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
46
47
48
49/**
50 * NT mutex semaphore.
51 */
52typedef struct RTSEMMUTEXINTERNAL
53{
54 /** Magic value (RTSEMMUTEX_MAGIC). */
55 uint32_t volatile u32Magic;
56#ifdef RT_USE_FAST_MUTEX
57 /** The fast mutex object. */
58 FAST_MUTEX Mutex;
59#else
60 /** The NT Mutex object. */
61 KMUTEX Mutex;
62#endif
63} RTSEMMUTEXINTERNAL, *PRTSEMMUTEXINTERNAL;
64
65
66
67/**
68 * Wrapper for the linux semaphore structure.
69 */
70typedef struct RTSEMFASTMUTEXINTERNAL
71{
72 /** Magic value (RTSEMFASTMUTEX_MAGIC). */
73 uint32_t u32Magic;
74 /** the NT fast mutex. */
75 FAST_MUTEX Mutex;
76} RTSEMFASTMUTEXINTERNAL, *PRTSEMFASTMUTEXINTERNAL;
77
78
79
80
81RTDECL(int) RTSemEventCreate(PRTSEMEVENT pEventSem)
82{
83 Assert(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
84 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pEventInt));
85 if (pEventInt)
86 {
87 pEventInt->u32Magic = RTSEMEVENT_MAGIC;
88 KeInitializeEvent(&pEventInt->Event, SynchronizationEvent, FALSE);
89 *pEventSem = pEventInt;
90 return VINF_SUCCESS;
91 }
92 return VERR_NO_MEMORY;
93}
94
95
96RTDECL(int) RTSemEventDestroy(RTSEMEVENT EventSem)
97{
98 /*
99 * Validate input.
100 */
101 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
102 if (!pEventInt)
103 return VERR_INVALID_PARAMETER;
104 if (pEventInt->u32Magic != RTSEMEVENT_MAGIC)
105 {
106 AssertMsgFailed(("pEventInt->u32Magic=%RX32 pEventInt=%p\n", pEventInt->u32Magic, pEventInt));
107 return VERR_INVALID_PARAMETER;
108 }
109
110 /*
111 * Invalidate it and signal the object just in case.
112 */
113 ASMAtomicIncU32(&pEventInt->u32Magic);
114 KeSetEvent(&pEventInt->Event, 0xfff, FALSE);
115 RTMemFree(pEventInt);
116 return VINF_SUCCESS;
117}
118
119
120RTDECL(int) RTSemEventSignal(RTSEMEVENT EventSem)
121{
122 /*
123 * Validate input.
124 */
125 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
126 if (!pEventInt)
127 return VERR_INVALID_PARAMETER;
128 if ( !pEventInt
129 || pEventInt->u32Magic != RTSEMEVENT_MAGIC)
130 {
131 AssertMsgFailed(("pEventInt->u32Magic=%RX32 pEventInt=%p\n", pEventInt ? pEventInt->u32Magic : 0, pEventInt));
132 return VERR_INVALID_PARAMETER;
133 }
134
135 /*
136 * Signal the event object.
137 */
138 KeSetEvent(&pEventInt->Event, 1, FALSE);
139 return VINF_SUCCESS;
140}
141
142
143static int rtSemEventWait(RTSEMEVENT EventSem, unsigned cMillies, bool fInterruptible)
144{
145 /*
146 * Validate input.
147 */
148 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
149 if (!pEventInt)
150 return VERR_INVALID_PARAMETER;
151 if ( !pEventInt
152 || pEventInt->u32Magic != RTSEMEVENT_MAGIC)
153 {
154 AssertMsgFailed(("pEventInt->u32Magic=%RX32 pEventInt=%p\n", pEventInt ? pEventInt->u32Magic : 0, pEventInt));
155 return VERR_INVALID_PARAMETER;
156 }
157
158 /*
159 * Wait for it.
160 */
161 NTSTATUS rcNt;
162 if (cMillies == RT_INDEFINITE_WAIT)
163 rcNt = KeWaitForSingleObject(&pEventInt->Event, Executive, KernelMode, fInterruptible, NULL);
164 else
165 {
166 LARGE_INTEGER Timeout;
167 Timeout.QuadPart = -(int64_t)cMillies * 10000;
168 rcNt = KeWaitForSingleObject(&pEventInt->Event, Executive, KernelMode, fInterruptible, &Timeout);
169 }
170 switch (rcNt)
171 {
172 case STATUS_SUCCESS:
173 if (pEventInt->u32Magic == RTSEMEVENT_MAGIC)
174 return VINF_SUCCESS;
175 return VERR_SEM_DESTROYED;
176 case STATUS_ALERTED:
177 return VERR_INTERRUPTED;
178 case STATUS_USER_APC:
179 return VERR_INTERRUPTED;
180 case STATUS_TIMEOUT:
181 return VERR_TIMEOUT;
182 default:
183 AssertMsgFailed(("pEventInt->u32Magic=%RX32 pEventInt=%p: wait returned %lx!\n",
184 pEventInt->u32Magic, pEventInt, (long)rcNt));
185 return VERR_INTERNAL_ERROR;
186 }
187}
188
189
190RTDECL(int) RTSemEventWait(RTSEMEVENT EventSem, unsigned cMillies)
191{
192 return rtSemEventWait(EventSem, cMillies, false /* fInterruptible */);
193}
194
195
196RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)
197{
198 return rtSemEventWait(EventSem, cMillies, true /* fInterruptible */);
199}
200
201
202RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX pMutexSem)
203{
204 Assert(sizeof(RTSEMMUTEXINTERNAL) > sizeof(void *));
205 PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)RTMemAlloc(sizeof(*pMutexInt));
206 if (pMutexInt)
207 {
208 pMutexInt->u32Magic = RTSEMMUTEX_MAGIC;
209#ifdef RT_USE_FAST_MUTEX
210 ExInitializeFastMutex(&pMutexInt->Mutex);
211#else
212 KeInitializeMutex(&pMutexInt->Mutex, 0);
213#endif
214 *pMutexSem = pMutexInt;
215 return VINF_SUCCESS;
216 }
217 return VERR_NO_MEMORY;
218}
219
220
221RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX MutexSem)
222{
223 /*
224 * Validate input.
225 */
226 PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
227 if (!pMutexInt)
228 return VERR_INVALID_PARAMETER;
229 if (pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
230 {
231 AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt->u32Magic, pMutexInt));
232 return VERR_INVALID_PARAMETER;
233 }
234
235 /*
236 * Invalidate it and signal the object just in case.
237 */
238 ASMAtomicIncU32(&pMutexInt->u32Magic);
239 RTMemFree(pMutexInt);
240 return VINF_SUCCESS;
241}
242
243
244RTDECL(int) RTSemMutexRequest(RTSEMMUTEX MutexSem, unsigned cMillies)
245{
246 /*
247 * Validate input.
248 */
249 PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
250 if (!pMutexInt)
251 return VERR_INVALID_PARAMETER;
252 if ( !pMutexInt
253 || pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
254 {
255 AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt ? pMutexInt->u32Magic : 0, pMutexInt));
256 return VERR_INVALID_PARAMETER;
257 }
258
259 /*
260 * Get the mutex.
261 */
262#ifdef RT_USE_FAST_MUTEX
263 AssertMsg(cMillies == RT_INDEFINITE_WAIT, ("timeouts are not supported when using fast mutexes!\n"));
264 ExAcquireFastMutex(&pMutexInt->Mutex);
265#else
266 NTSTATUS rcNt;
267 if (cMillies == RT_INDEFINITE_WAIT)
268 rcNt = KeWaitForSingleObject(&pMutexInt->Mutex, Executive, KernelMode, TRUE, NULL);
269 else
270 {
271 LARGE_INTEGER Timeout;
272 Timeout.QuadPart = -(int64_t)cMillies * 10000;
273 rcNt = KeWaitForSingleObject(&pMutexInt->Mutex, Executive, KernelMode, TRUE, &Timeout);
274 }
275 switch (rcNt)
276 {
277 case STATUS_SUCCESS:
278 if (pMutexInt->u32Magic == RTSEMMUTEX_MAGIC)
279 return VINF_SUCCESS;
280 return VERR_SEM_DESTROYED;
281 case STATUS_ALERTED:
282 return VERR_INTERRUPTED; /** @todo VERR_INTERRUPTED isn't correct anylonger. please fix r0drv stuff! */
283 case STATUS_USER_APC:
284 return VERR_INTERRUPTED; /** @todo VERR_INTERRUPTED isn't correct anylonger. please fix r0drv stuff! */
285 case STATUS_TIMEOUT:
286 return VERR_TIMEOUT;
287 default:
288 AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p: wait returned %lx!\n",
289 pMutexInt->u32Magic, pMutexInt, (long)rcNt));
290 return VERR_INTERNAL_ERROR;
291 }
292#endif
293 return VINF_SUCCESS;
294}
295
296
297RTDECL(int) RTSemMutexRelease(RTSEMMUTEX MutexSem)
298{
299 /*
300 * Validate input.
301 */
302 PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
303 if (!pMutexInt)
304 return VERR_INVALID_PARAMETER;
305 if ( !pMutexInt
306 || pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
307 {
308 AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt ? pMutexInt->u32Magic : 0, pMutexInt));
309 return VERR_INVALID_PARAMETER;
310 }
311
312 /*
313 * Release the mutex.
314 */
315#ifdef RT_USE_FAST_MUTEX
316 ExReleaseFastMutex(&pMutexInt->Mutex);
317#else
318 KeReleaseMutex(&pMutexInt->Mutex, FALSE);
319#endif
320 return VINF_SUCCESS;
321}
322
323
324
325
326RTDECL(int) RTSemFastMutexCreate(PRTSEMFASTMUTEX pMutexSem)
327{
328 /*
329 * Allocate.
330 */
331 PRTSEMFASTMUTEXINTERNAL pFastInt;
332 Assert(sizeof(*pFastInt) > sizeof(void *));
333 pFastInt = (PRTSEMFASTMUTEXINTERNAL)RTMemAlloc(sizeof(*pFastInt));
334 if (!pFastInt)
335 return VERR_NO_MEMORY;
336
337 /*
338 * Initialize.
339 */
340 pFastInt->u32Magic = RTSEMFASTMUTEX_MAGIC;
341 ExInitializeFastMutex(&pFastInt->Mutex);
342 *pMutexSem = pFastInt;
343 return VINF_SUCCESS;
344}
345
346
347RTDECL(int) RTSemFastMutexDestroy(RTSEMFASTMUTEX MutexSem)
348{
349 /*
350 * Validate.
351 */
352 PRTSEMFASTMUTEXINTERNAL pFastInt = (PRTSEMFASTMUTEXINTERNAL)MutexSem;
353 if (!pFastInt)
354 return VERR_INVALID_PARAMETER;
355 if (pFastInt->u32Magic != RTSEMFASTMUTEX_MAGIC)
356 {
357 AssertMsgFailed(("pFastInt->u32Magic=%RX32 pMutexInt=%p\n", pFastInt->u32Magic, pFastInt));
358 return VERR_INVALID_PARAMETER;
359 }
360
361 ASMAtomicIncU32(&pFastInt->u32Magic);
362 RTMemFree(pFastInt);
363 return VINF_SUCCESS;
364}
365
366
367RTDECL(int) RTSemFastMutexRequest(RTSEMFASTMUTEX MutexSem)
368{
369 /*
370 * Validate.
371 */
372 PRTSEMFASTMUTEXINTERNAL pFastInt = (PRTSEMFASTMUTEXINTERNAL)MutexSem;
373 if ( !pFastInt
374 || pFastInt->u32Magic != RTSEMFASTMUTEX_MAGIC)
375 {
376 AssertMsgFailed(("pFastInt->u32Magic=%RX32 pMutexInt=%p\n", pFastInt ? pFastInt->u32Magic : 0, pFastInt));
377 return VERR_INVALID_PARAMETER;
378 }
379
380 ExAcquireFastMutex(&pFastInt->Mutex);
381 return VINF_SUCCESS;
382}
383
384
385RTDECL(int) RTSemFastMutexRelease(RTSEMFASTMUTEX MutexSem)
386{
387 /*
388 * Validate.
389 */
390 PRTSEMFASTMUTEXINTERNAL pFastInt = (PRTSEMFASTMUTEXINTERNAL)MutexSem;
391 if ( !pFastInt
392 || pFastInt->u32Magic != RTSEMFASTMUTEX_MAGIC)
393 {
394 AssertMsgFailed(("pFastInt->u32Magic=%RX32 pMutexInt=%p\n", pFastInt ? pFastInt->u32Magic : 0, pFastInt));
395 return VERR_INVALID_PARAMETER;
396 }
397
398 ExReleaseFastMutex(&pFastInt->Mutex);
399 return VINF_SUCCESS;
400}
401
402
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