VirtualBox

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

Last change on this file since 4219 was 4071, checked in by vboxsync, 18 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.8 KB
Line 
1/* $Id: semaphore-r0drv-nt.cpp 4071 2007-08-07 17:07:59Z 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
143RTDECL(int) RTSemEventWait(RTSEMEVENT EventSem, unsigned cMillies)
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, TRUE, NULL);
164 else
165 {
166 LARGE_INTEGER Timeout;
167 Timeout.QuadPart = -(int64_t)cMillies * 10000;
168 rcNt = KeWaitForSingleObject(&pEventInt->Event, Executive, KernelMode, TRUE, &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; /** @todo VERR_INTERRUPTED isn't correct anylonger. please fix r0drv stuff! */
178 case STATUS_USER_APC:
179 return VERR_INTERRUPTED; /** @todo VERR_INTERRUPTED isn't correct anylonger. please fix r0drv stuff! */
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
190
191RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX pMutexSem)
192{
193 Assert(sizeof(RTSEMMUTEXINTERNAL) > sizeof(void *));
194 PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)RTMemAlloc(sizeof(*pMutexInt));
195 if (pMutexInt)
196 {
197 pMutexInt->u32Magic = RTSEMMUTEX_MAGIC;
198#ifdef RT_USE_FAST_MUTEX
199 ExInitializeFastMutex(&pMutexInt->Mutex);
200#else
201 KeInitializeMutex(&pMutexInt->Mutex, 0);
202#endif
203 *pMutexSem = pMutexInt;
204 return VINF_SUCCESS;
205 }
206 return VERR_NO_MEMORY;
207}
208
209
210RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX MutexSem)
211{
212 /*
213 * Validate input.
214 */
215 PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
216 if (!pMutexInt)
217 return VERR_INVALID_PARAMETER;
218 if (pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
219 {
220 AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt->u32Magic, pMutexInt));
221 return VERR_INVALID_PARAMETER;
222 }
223
224 /*
225 * Invalidate it and signal the object just in case.
226 */
227 ASMAtomicIncU32(&pMutexInt->u32Magic);
228 RTMemFree(pMutexInt);
229 return VINF_SUCCESS;
230}
231
232
233RTDECL(int) RTSemMutexRequest(RTSEMMUTEX MutexSem, unsigned cMillies)
234{
235 /*
236 * Validate input.
237 */
238 PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
239 if (!pMutexInt)
240 return VERR_INVALID_PARAMETER;
241 if ( !pMutexInt
242 || pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
243 {
244 AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt ? pMutexInt->u32Magic : 0, pMutexInt));
245 return VERR_INVALID_PARAMETER;
246 }
247
248 /*
249 * Get the mutex.
250 */
251#ifdef RT_USE_FAST_MUTEX
252 AssertMsg(cMillies == RT_INDEFINITE_WAIT, ("timeouts are not supported when using fast mutexes!\n"));
253 ExAcquireFastMutex(&pMutexInt->Mutex);
254#else
255 NTSTATUS rcNt;
256 if (cMillies == RT_INDEFINITE_WAIT)
257 rcNt = KeWaitForSingleObject(&pMutexInt->Mutex, Executive, KernelMode, TRUE, NULL);
258 else
259 {
260 LARGE_INTEGER Timeout;
261 Timeout.QuadPart = -(int64_t)cMillies * 10000;
262 rcNt = KeWaitForSingleObject(&pMutexInt->Mutex, Executive, KernelMode, TRUE, &Timeout);
263 }
264 switch (rcNt)
265 {
266 case STATUS_SUCCESS:
267 if (pMutexInt->u32Magic == RTSEMMUTEX_MAGIC)
268 return VINF_SUCCESS;
269 return VERR_SEM_DESTROYED;
270 case STATUS_ALERTED:
271 return VERR_INTERRUPTED; /** @todo VERR_INTERRUPTED isn't correct anylonger. please fix r0drv stuff! */
272 case STATUS_USER_APC:
273 return VERR_INTERRUPTED; /** @todo VERR_INTERRUPTED isn't correct anylonger. please fix r0drv stuff! */
274 case STATUS_TIMEOUT:
275 return VERR_TIMEOUT;
276 default:
277 AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p: wait returned %lx!\n",
278 pMutexInt->u32Magic, pMutexInt, (long)rcNt));
279 return VERR_INTERNAL_ERROR;
280 }
281#endif
282 return VINF_SUCCESS;
283}
284
285
286RTDECL(int) RTSemMutexRelease(RTSEMMUTEX MutexSem)
287{
288 /*
289 * Validate input.
290 */
291 PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
292 if (!pMutexInt)
293 return VERR_INVALID_PARAMETER;
294 if ( !pMutexInt
295 || pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
296 {
297 AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt ? pMutexInt->u32Magic : 0, pMutexInt));
298 return VERR_INVALID_PARAMETER;
299 }
300
301 /*
302 * Release the mutex.
303 */
304#ifdef RT_USE_FAST_MUTEX
305 ExReleaseFastMutex(&pMutexInt->Mutex);
306#else
307 KeReleaseMutex(&pMutexInt->Mutex, FALSE);
308#endif
309 return VINF_SUCCESS;
310}
311
312
313
314
315RTDECL(int) RTSemFastMutexCreate(PRTSEMFASTMUTEX pMutexSem)
316{
317 /*
318 * Allocate.
319 */
320 PRTSEMFASTMUTEXINTERNAL pFastInt;
321 Assert(sizeof(*pFastInt) > sizeof(void *));
322 pFastInt = (PRTSEMFASTMUTEXINTERNAL)RTMemAlloc(sizeof(*pFastInt));
323 if (!pFastInt)
324 return VERR_NO_MEMORY;
325
326 /*
327 * Initialize.
328 */
329 pFastInt->u32Magic = RTSEMFASTMUTEX_MAGIC;
330 ExInitializeFastMutex(&pFastInt->Mutex);
331 *pMutexSem = pFastInt;
332 return VINF_SUCCESS;
333}
334
335
336RTDECL(int) RTSemFastMutexDestroy(RTSEMFASTMUTEX MutexSem)
337{
338 /*
339 * Validate.
340 */
341 PRTSEMFASTMUTEXINTERNAL pFastInt = (PRTSEMFASTMUTEXINTERNAL)MutexSem;
342 if (!pFastInt)
343 return VERR_INVALID_PARAMETER;
344 if (pFastInt->u32Magic != RTSEMFASTMUTEX_MAGIC)
345 {
346 AssertMsgFailed(("pFastInt->u32Magic=%RX32 pMutexInt=%p\n", pFastInt->u32Magic, pFastInt));
347 return VERR_INVALID_PARAMETER;
348 }
349
350 ASMAtomicIncU32(&pFastInt->u32Magic);
351 RTMemFree(pFastInt);
352 return VINF_SUCCESS;
353}
354
355
356RTDECL(int) RTSemFastMutexRequest(RTSEMFASTMUTEX MutexSem)
357{
358 /*
359 * Validate.
360 */
361 PRTSEMFASTMUTEXINTERNAL pFastInt = (PRTSEMFASTMUTEXINTERNAL)MutexSem;
362 if ( !pFastInt
363 || pFastInt->u32Magic != RTSEMFASTMUTEX_MAGIC)
364 {
365 AssertMsgFailed(("pFastInt->u32Magic=%RX32 pMutexInt=%p\n", pFastInt ? pFastInt->u32Magic : 0, pFastInt));
366 return VERR_INVALID_PARAMETER;
367 }
368
369 ExAcquireFastMutex(&pFastInt->Mutex);
370 return VINF_SUCCESS;
371}
372
373
374RTDECL(int) RTSemFastMutexRelease(RTSEMFASTMUTEX MutexSem)
375{
376 /*
377 * Validate.
378 */
379 PRTSEMFASTMUTEXINTERNAL pFastInt = (PRTSEMFASTMUTEXINTERNAL)MutexSem;
380 if ( !pFastInt
381 || pFastInt->u32Magic != RTSEMFASTMUTEX_MAGIC)
382 {
383 AssertMsgFailed(("pFastInt->u32Magic=%RX32 pMutexInt=%p\n", pFastInt ? pFastInt->u32Magic : 0, pFastInt));
384 return VERR_INVALID_PARAMETER;
385 }
386
387 ExReleaseFastMutex(&pFastInt->Mutex);
388 return VINF_SUCCESS;
389}
390
391
Note: See TracBrowser for help on using the repository browser.

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