1 | /* $Id: semeventmulti-r0drv-nt.cpp 5999 2007-12-07 15:05:06Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - Multiple Release Event 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 (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 pEventSem)
|
---|
57 | {
|
---|
58 | Assert(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
|
---|
59 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
60 | if (pThis)
|
---|
61 | {
|
---|
62 | pThis->u32Magic = RTSEMEVENTMULTI_MAGIC;
|
---|
63 | KeInitializeEvent(&pThis->Event, NotificationEvent, FALSE /* not signalled */);
|
---|
64 | *pEventSem = pThis;
|
---|
65 | return VINF_SUCCESS;
|
---|
66 | }
|
---|
67 | return VERR_NO_MEMORY;
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI EventMultiSem)
|
---|
72 | {
|
---|
73 | /*
|
---|
74 | * Validate input.
|
---|
75 | */
|
---|
76 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
|
---|
77 | if (!pThis)
|
---|
78 | return VERR_INVALID_PARAMETER;
|
---|
79 | AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
|
---|
80 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * Invalidate it and signal the object just in case.
|
---|
84 | */
|
---|
85 | ASMAtomicIncU32(&pThis->u32Magic);
|
---|
86 | KeSetEvent(&pThis->Event, 0xfff, FALSE);
|
---|
87 | RTMemFree(pThis);
|
---|
88 | return VINF_SUCCESS;
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI EventMultiSem)
|
---|
93 | {
|
---|
94 | /*
|
---|
95 | * Validate input.
|
---|
96 | */
|
---|
97 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
|
---|
98 | if (!pThis)
|
---|
99 | return VERR_INVALID_PARAMETER;
|
---|
100 | AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
|
---|
101 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
|
---|
102 |
|
---|
103 | /*
|
---|
104 | * Signal the event object.
|
---|
105 | */
|
---|
106 | KeSetEvent(&pThis->Event, 1, FALSE);
|
---|
107 | return VINF_SUCCESS;
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI EventMultiSem)
|
---|
112 | {
|
---|
113 | /*
|
---|
114 | * Validate input.
|
---|
115 | */
|
---|
116 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
|
---|
117 | if (!pThis)
|
---|
118 | return VERR_INVALID_PARAMETER;
|
---|
119 | AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
|
---|
120 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
|
---|
121 |
|
---|
122 | /*
|
---|
123 | * Reset the event object.
|
---|
124 | */
|
---|
125 | KeResetEvent(&pThis->Event);
|
---|
126 | return VINF_SUCCESS;
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | static int rtSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies, bool fInterruptible)
|
---|
131 | {
|
---|
132 | /*
|
---|
133 | * Validate input.
|
---|
134 | */
|
---|
135 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
|
---|
136 | if (!pThis)
|
---|
137 | return VERR_INVALID_PARAMETER;
|
---|
138 | AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
|
---|
139 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
|
---|
140 |
|
---|
141 | /*
|
---|
142 | * Wait for it.
|
---|
143 | */
|
---|
144 | NTSTATUS rcNt;
|
---|
145 | if (cMillies == RT_INDEFINITE_WAIT)
|
---|
146 | rcNt = KeWaitForSingleObject(&pThis->Event, Executive, KernelMode, fInterruptible, NULL);
|
---|
147 | else
|
---|
148 | {
|
---|
149 | LARGE_INTEGER Timeout;
|
---|
150 | Timeout.QuadPart = -(int64_t)cMillies * 10000;
|
---|
151 | rcNt = KeWaitForSingleObject(&pThis->Event, Executive, KernelMode, fInterruptible, &Timeout);
|
---|
152 | }
|
---|
153 | switch (rcNt)
|
---|
154 | {
|
---|
155 | case STATUS_SUCCESS:
|
---|
156 | if (pThis->u32Magic == RTSEMEVENTMULTI_MAGIC)
|
---|
157 | return VINF_SUCCESS;
|
---|
158 | return VERR_SEM_DESTROYED;
|
---|
159 | case STATUS_ALERTED:
|
---|
160 | return VERR_INTERRUPTED;
|
---|
161 | case STATUS_USER_APC:
|
---|
162 | return VERR_INTERRUPTED;
|
---|
163 | case STATUS_TIMEOUT:
|
---|
164 | return VERR_TIMEOUT;
|
---|
165 | default:
|
---|
166 | AssertMsgFailed(("pThis->u32Magic=%RX32 pThis=%p: wait returned %lx!\n",
|
---|
167 | pThis->u32Magic, pThis, (long)rcNt));
|
---|
168 | return VERR_INTERNAL_ERROR;
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | RTDECL(int) RTSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
|
---|
174 | {
|
---|
175 | return rtSemEventMultiWait(EventMultiSem, cMillies, false /* fInterruptible */);
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
|
---|
180 | {
|
---|
181 | return rtSemEventMultiWait(EventMultiSem, cMillies, true /* fInterruptible */);
|
---|
182 | }
|
---|
183 |
|
---|