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