1 | /* $Id: semeventmulti-r0drv-nt.cpp 25724 2010-01-11 14:45:34Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Multiple 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 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include "the-nt-kernel.h"
|
---|
36 | #include <iprt/semaphore.h>
|
---|
37 | #include <iprt/alloc.h>
|
---|
38 | #include <iprt/assert.h>
|
---|
39 | #include <iprt/asm.h>
|
---|
40 | #include <iprt/err.h>
|
---|
41 |
|
---|
42 | #include "internal/magics.h"
|
---|
43 |
|
---|
44 |
|
---|
45 | /*******************************************************************************
|
---|
46 | * Structures and Typedefs *
|
---|
47 | *******************************************************************************/
|
---|
48 | /**
|
---|
49 | * NT event semaphore.
|
---|
50 | */
|
---|
51 | typedef struct RTSEMEVENTMULTIINTERNAL
|
---|
52 | {
|
---|
53 | /** Magic value (RTSEMEVENTMULTI_MAGIC). */
|
---|
54 | uint32_t volatile u32Magic;
|
---|
55 | /** The NT Event object. */
|
---|
56 | KEVENT Event;
|
---|
57 | } RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
|
---|
58 |
|
---|
59 |
|
---|
60 | RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI phEventMultiSem)
|
---|
61 | {
|
---|
62 | return RTSemEventMultiCreateEx(phEventMultiSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | RTDECL(int) RTSemEventMultiCreateEx(PRTSEMEVENTMULTI phEventMultiSem, uint32_t fFlags, RTLOCKVALCLASS hClass,
|
---|
67 | const char *pszNameFmt, ...)
|
---|
68 | {
|
---|
69 | AssertReturn(!(fFlags & ~RTSEMEVENTMULTI_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
|
---|
70 |
|
---|
71 | AssertCompile(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
|
---|
72 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
73 | if (pThis)
|
---|
74 | {
|
---|
75 | pThis->u32Magic = RTSEMEVENTMULTI_MAGIC;
|
---|
76 | KeInitializeEvent(&pThis->Event, NotificationEvent, FALSE /* not signalled */);
|
---|
77 |
|
---|
78 | *phEventMultiSem = pThis;
|
---|
79 | return VINF_SUCCESS;
|
---|
80 | }
|
---|
81 | return VERR_NO_MEMORY;
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI hEventMultiSem)
|
---|
86 | {
|
---|
87 | /*
|
---|
88 | * Validate input.
|
---|
89 | */
|
---|
90 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
91 | if (pThis == NIL_RTSEMEVENTMULTI)
|
---|
92 | return VINF_SUCCESS;
|
---|
93 | AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
|
---|
94 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
|
---|
95 |
|
---|
96 | /*
|
---|
97 | * Invalidate it and signal the object just in case.
|
---|
98 | */
|
---|
99 | ASMAtomicIncU32(&pThis->u32Magic);
|
---|
100 | KeSetEvent(&pThis->Event, 0xfff, FALSE);
|
---|
101 | RTMemFree(pThis);
|
---|
102 | return VINF_SUCCESS;
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI hEventMultiSem)
|
---|
107 | {
|
---|
108 | /*
|
---|
109 | * Validate input.
|
---|
110 | */
|
---|
111 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
112 | if (!pThis)
|
---|
113 | return VERR_INVALID_PARAMETER;
|
---|
114 | AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
|
---|
115 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
|
---|
116 |
|
---|
117 | /*
|
---|
118 | * Signal the event object.
|
---|
119 | */
|
---|
120 | KeSetEvent(&pThis->Event, 1, FALSE);
|
---|
121 | return VINF_SUCCESS;
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem)
|
---|
126 | {
|
---|
127 | /*
|
---|
128 | * Validate input.
|
---|
129 | */
|
---|
130 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
131 | if (!pThis)
|
---|
132 | return VERR_INVALID_PARAMETER;
|
---|
133 | AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
|
---|
134 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
|
---|
135 |
|
---|
136 | /*
|
---|
137 | * Reset the event object.
|
---|
138 | */
|
---|
139 | KeResetEvent(&pThis->Event);
|
---|
140 | return VINF_SUCCESS;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | static int rtSemEventMultiWait(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies, bool fInterruptible)
|
---|
145 | {
|
---|
146 | /*
|
---|
147 | * Validate input.
|
---|
148 | */
|
---|
149 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
150 | if (!pThis)
|
---|
151 | return VERR_INVALID_PARAMETER;
|
---|
152 | AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
|
---|
153 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
|
---|
154 |
|
---|
155 | /*
|
---|
156 | * Wait for it.
|
---|
157 | * We're assuming interruptible waits should happen at UserMode level.
|
---|
158 | */
|
---|
159 | NTSTATUS rcNt;
|
---|
160 | KPROCESSOR_MODE WaitMode = fInterruptible ? UserMode : KernelMode;
|
---|
161 | if (cMillies == RT_INDEFINITE_WAIT)
|
---|
162 | rcNt = KeWaitForSingleObject(&pThis->Event, Executive, WaitMode, fInterruptible, NULL);
|
---|
163 | else
|
---|
164 | {
|
---|
165 | LARGE_INTEGER Timeout;
|
---|
166 | Timeout.QuadPart = -(int64_t)cMillies * 10000;
|
---|
167 | rcNt = KeWaitForSingleObject(&pThis->Event, Executive, WaitMode, fInterruptible, &Timeout);
|
---|
168 | }
|
---|
169 | switch (rcNt)
|
---|
170 | {
|
---|
171 | case STATUS_SUCCESS:
|
---|
172 | if (pThis->u32Magic == RTSEMEVENTMULTI_MAGIC)
|
---|
173 | return VINF_SUCCESS;
|
---|
174 | return VERR_SEM_DESTROYED;
|
---|
175 | case STATUS_ALERTED:
|
---|
176 | return VERR_INTERRUPTED;
|
---|
177 | case STATUS_USER_APC:
|
---|
178 | return VERR_INTERRUPTED;
|
---|
179 | case STATUS_TIMEOUT:
|
---|
180 | return VERR_TIMEOUT;
|
---|
181 | default:
|
---|
182 | AssertMsgFailed(("pThis->u32Magic=%RX32 pThis=%p: wait returned %lx!\n",
|
---|
183 | pThis->u32Magic, pThis, (long)rcNt));
|
---|
184 | return VERR_INTERNAL_ERROR;
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 | RTDECL(int) RTSemEventMultiWait(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies)
|
---|
190 | {
|
---|
191 | return rtSemEventMultiWait(hEventMultiSem, cMillies, false /* fInterruptible */);
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies)
|
---|
196 | {
|
---|
197 | return rtSemEventMultiWait(hEventMultiSem, cMillies, true /* fInterruptible */);
|
---|
198 | }
|
---|
199 |
|
---|