1 | /* $Id: semeventmulti-r0drv-nt.cpp 20884 2009-06-24 09:09:46Z 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 pEventSem)
|
---|
61 | {
|
---|
62 | Assert(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
|
---|
63 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
64 | if (pThis)
|
---|
65 | {
|
---|
66 | pThis->u32Magic = RTSEMEVENTMULTI_MAGIC;
|
---|
67 | KeInitializeEvent(&pThis->Event, NotificationEvent, FALSE /* not signalled */);
|
---|
68 | *pEventSem = pThis;
|
---|
69 | return VINF_SUCCESS;
|
---|
70 | }
|
---|
71 | return VERR_NO_MEMORY;
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI EventMultiSem)
|
---|
76 | {
|
---|
77 | /*
|
---|
78 | * Validate input.
|
---|
79 | */
|
---|
80 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
|
---|
81 | if (!pThis)
|
---|
82 | return VERR_INVALID_PARAMETER;
|
---|
83 | AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
|
---|
84 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
|
---|
85 |
|
---|
86 | /*
|
---|
87 | * Invalidate it and signal the object just in case.
|
---|
88 | */
|
---|
89 | ASMAtomicIncU32(&pThis->u32Magic);
|
---|
90 | KeSetEvent(&pThis->Event, 0xfff, FALSE);
|
---|
91 | RTMemFree(pThis);
|
---|
92 | return VINF_SUCCESS;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI EventMultiSem)
|
---|
97 | {
|
---|
98 | /*
|
---|
99 | * Validate input.
|
---|
100 | */
|
---|
101 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
|
---|
102 | if (!pThis)
|
---|
103 | return VERR_INVALID_PARAMETER;
|
---|
104 | AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
|
---|
105 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
|
---|
106 |
|
---|
107 | /*
|
---|
108 | * Signal the event object.
|
---|
109 | */
|
---|
110 | KeSetEvent(&pThis->Event, 1, FALSE);
|
---|
111 | return VINF_SUCCESS;
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI EventMultiSem)
|
---|
116 | {
|
---|
117 | /*
|
---|
118 | * Validate input.
|
---|
119 | */
|
---|
120 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
|
---|
121 | if (!pThis)
|
---|
122 | return VERR_INVALID_PARAMETER;
|
---|
123 | AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
|
---|
124 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
|
---|
125 |
|
---|
126 | /*
|
---|
127 | * Reset the event object.
|
---|
128 | */
|
---|
129 | KeResetEvent(&pThis->Event);
|
---|
130 | return VINF_SUCCESS;
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | static int rtSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies, bool fInterruptible)
|
---|
135 | {
|
---|
136 | /*
|
---|
137 | * Validate input.
|
---|
138 | */
|
---|
139 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
|
---|
140 | if (!pThis)
|
---|
141 | return VERR_INVALID_PARAMETER;
|
---|
142 | AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
|
---|
143 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
|
---|
144 |
|
---|
145 | /*
|
---|
146 | * Wait for it.
|
---|
147 | *
|
---|
148 | * We default to UserMode here as the waits might be aborted due to process termination.
|
---|
149 | * @todo As far as I can tell this is currently safe as all calls are made on behalf of user threads.
|
---|
150 | */
|
---|
151 | NTSTATUS rcNt;
|
---|
152 | if (cMillies == RT_INDEFINITE_WAIT)
|
---|
153 | rcNt = KeWaitForSingleObject(&pThis->Event, Executive, UserMode, fInterruptible, NULL);
|
---|
154 | else
|
---|
155 | {
|
---|
156 | /* Can't use the stack here as the wait is UserMode. */
|
---|
157 | PLARGE_INTEGER pTimeout = (PLARGE_INTEGER)RTMemAlloc(sizeof(*pTimeout));
|
---|
158 | if (!pTimeout)
|
---|
159 | return VERR_NO_MEMORY;
|
---|
160 |
|
---|
161 | pTimeout->QuadPart = -(int64_t)cMillies * 10000;
|
---|
162 | rcNt = KeWaitForSingleObject(&pThis->Event, Executive, UserMode, fInterruptible, pTimeout);
|
---|
163 | RTMemFree(pTimeout);
|
---|
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 EventMultiSem, unsigned cMillies)
|
---|
186 | {
|
---|
187 | return rtSemEventMultiWait(EventMultiSem, cMillies, false /* fInterruptible */);
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
|
---|
192 | {
|
---|
193 | return rtSemEventMultiWait(EventMultiSem, cMillies, true /* fInterruptible */);
|
---|
194 | }
|
---|
195 |
|
---|