1 | /* $Id: semmutex-r0drv-nt.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Mutex Semaphores, Ring-0 Driver, NT.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 |
|
---|
39 | /*********************************************************************************************************************************
|
---|
40 | * Header Files *
|
---|
41 | *********************************************************************************************************************************/
|
---|
42 | #define RTSEMMUTEX_WITHOUT_REMAPPING
|
---|
43 | #include "the-nt-kernel.h"
|
---|
44 | #include <iprt/semaphore.h>
|
---|
45 | #include <iprt/alloc.h>
|
---|
46 | #include <iprt/assert.h>
|
---|
47 | #include <iprt/asm.h>
|
---|
48 | #include <iprt/err.h>
|
---|
49 |
|
---|
50 | #include "internal/magics.h"
|
---|
51 |
|
---|
52 |
|
---|
53 | /*********************************************************************************************************************************
|
---|
54 | * Structures and Typedefs *
|
---|
55 | *********************************************************************************************************************************/
|
---|
56 | /**
|
---|
57 | * NT mutex semaphore.
|
---|
58 | */
|
---|
59 | typedef struct RTSEMMUTEXINTERNAL
|
---|
60 | {
|
---|
61 | /** Magic value (RTSEMMUTEX_MAGIC). */
|
---|
62 | uint32_t volatile u32Magic;
|
---|
63 | #ifdef RT_USE_FAST_MUTEX
|
---|
64 | /** The fast mutex object. */
|
---|
65 | FAST_MUTEX Mutex;
|
---|
66 | #else
|
---|
67 | /** The NT Mutex object. */
|
---|
68 | KMUTEX Mutex;
|
---|
69 | #endif
|
---|
70 | } RTSEMMUTEXINTERNAL, *PRTSEMMUTEXINTERNAL;
|
---|
71 |
|
---|
72 |
|
---|
73 |
|
---|
74 | RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX phMutexSem)
|
---|
75 | {
|
---|
76 | return RTSemMutexCreateEx(phMutexSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | RTDECL(int) RTSemMutexCreateEx(PRTSEMMUTEX phMutexSem, uint32_t fFlags,
|
---|
81 | RTLOCKVALCLASS hClass, uint32_t uSubClass, const char *pszNameFmt, ...)
|
---|
82 | {
|
---|
83 | AssertReturn(!(fFlags & ~RTSEMMUTEX_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
|
---|
84 | RT_NOREF3(hClass, uSubClass, pszNameFmt);
|
---|
85 |
|
---|
86 | AssertCompile(sizeof(RTSEMMUTEXINTERNAL) > sizeof(void *));
|
---|
87 | PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
88 | if (!pThis)
|
---|
89 | return VERR_NO_MEMORY;
|
---|
90 |
|
---|
91 | pThis->u32Magic = RTSEMMUTEX_MAGIC;
|
---|
92 | #ifdef RT_USE_FAST_MUTEX
|
---|
93 | ExInitializeFastMutex(&pThis->Mutex);
|
---|
94 | #else
|
---|
95 | KeInitializeMutex(&pThis->Mutex, 0);
|
---|
96 | #endif
|
---|
97 |
|
---|
98 | *phMutexSem = pThis;
|
---|
99 | return VINF_SUCCESS;
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX hMutexSem)
|
---|
104 | {
|
---|
105 | /*
|
---|
106 | * Validate input.
|
---|
107 | */
|
---|
108 | PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)hMutexSem;
|
---|
109 | if (pThis == NIL_RTSEMMUTEX)
|
---|
110 | return VINF_SUCCESS;
|
---|
111 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
112 | AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
|
---|
113 |
|
---|
114 | /*
|
---|
115 | * Invalidate it and signal the object just in case.
|
---|
116 | */
|
---|
117 | AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, RTSEMMUTEX_MAGIC_DEAD, RTSEMMUTEX_MAGIC), VERR_INVALID_HANDLE);
|
---|
118 | RTMemFree(pThis);
|
---|
119 | return VINF_SUCCESS;
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Internal worker for RTSemMutexRequest and RTSemMutexRequestNoResume
|
---|
125 | *
|
---|
126 | * @returns IPRT status code.
|
---|
127 | * @param hMutexSem The mutex handle.
|
---|
128 | * @param cMillies The timeout.
|
---|
129 | * @param fInterruptible Whether it's interruptible
|
---|
130 | * (RTSemMutexRequestNoResume) or not
|
---|
131 | * (RTSemMutexRequest).
|
---|
132 | */
|
---|
133 | static int rtSemMutexRequest(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, BOOLEAN fInterruptible)
|
---|
134 | {
|
---|
135 | /*
|
---|
136 | * Validate input.
|
---|
137 | */
|
---|
138 | PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)hMutexSem;
|
---|
139 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
140 | AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
|
---|
141 |
|
---|
142 | /*
|
---|
143 | * Get the mutex.
|
---|
144 | */
|
---|
145 | #ifdef RT_USE_FAST_MUTEX
|
---|
146 | AssertMsg(cMillies == RT_INDEFINITE_WAIT, ("timeouts are not supported when using fast mutexes!\n"));
|
---|
147 | ExAcquireFastMutex(&pThis->Mutex);
|
---|
148 | return VINF_SUCCESS;
|
---|
149 |
|
---|
150 | #else /* !RT_USE_FAST_MUTEX */
|
---|
151 | NTSTATUS rcNt;
|
---|
152 | if (cMillies == RT_INDEFINITE_WAIT)
|
---|
153 | rcNt = KeWaitForSingleObject(&pThis->Mutex, Executive, KernelMode, fInterruptible, NULL);
|
---|
154 | else
|
---|
155 | {
|
---|
156 | LARGE_INTEGER Timeout;
|
---|
157 | Timeout.QuadPart = -(int64_t)cMillies * 10000;
|
---|
158 | rcNt = KeWaitForSingleObject(&pThis->Mutex, Executive, KernelMode, fInterruptible, &Timeout);
|
---|
159 | }
|
---|
160 | switch (rcNt)
|
---|
161 | {
|
---|
162 | case STATUS_SUCCESS:
|
---|
163 | if (pThis->u32Magic == RTSEMMUTEX_MAGIC)
|
---|
164 | return VINF_SUCCESS;
|
---|
165 | return VERR_SEM_DESTROYED;
|
---|
166 |
|
---|
167 | case STATUS_ALERTED:
|
---|
168 | case STATUS_USER_APC:
|
---|
169 | Assert(fInterruptible);
|
---|
170 | return VERR_INTERRUPTED;
|
---|
171 |
|
---|
172 | case STATUS_TIMEOUT:
|
---|
173 | return VERR_TIMEOUT;
|
---|
174 |
|
---|
175 | default:
|
---|
176 | AssertMsgFailed(("pThis->u32Magic=%RX32 pThis=%p: wait returned %lx!\n",
|
---|
177 | pThis->u32Magic, pThis, (long)rcNt));
|
---|
178 | return VERR_INTERNAL_ERROR;
|
---|
179 | }
|
---|
180 | #endif /* !RT_USE_FAST_MUTEX */
|
---|
181 | }
|
---|
182 |
|
---|
183 |
|
---|
184 | RTDECL(int) RTSemMutexRequest(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
|
---|
185 | {
|
---|
186 | return rtSemMutexRequest(hMutexSem, cMillies, FALSE /*fInterruptible*/);
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 | RTDECL(int) RTSemMutexRequestDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
191 | {
|
---|
192 | RT_NOREF1(uId); RT_SRC_POS_NOREF();
|
---|
193 | return RTSemMutexRequest(hMutexSem, cMillies);
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
|
---|
198 | {
|
---|
199 | return rtSemMutexRequest(hMutexSem, cMillies, TRUE /*fInterruptible*/);
|
---|
200 | }
|
---|
201 |
|
---|
202 |
|
---|
203 | RTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
204 | {
|
---|
205 | RT_NOREF1(uId); RT_SRC_POS_NOREF();
|
---|
206 | return RTSemMutexRequestNoResume(hMutexSem, cMillies);
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | RTDECL(int) RTSemMutexRelease(RTSEMMUTEX hMutexSem)
|
---|
211 | {
|
---|
212 | /*
|
---|
213 | * Validate input.
|
---|
214 | */
|
---|
215 | PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)hMutexSem;
|
---|
216 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
217 | AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
|
---|
218 |
|
---|
219 | /*
|
---|
220 | * Release the mutex.
|
---|
221 | */
|
---|
222 | #ifdef RT_USE_FAST_MUTEX
|
---|
223 | ExReleaseFastMutex(&pThis->Mutex);
|
---|
224 | #else
|
---|
225 | KeReleaseMutex(&pThis->Mutex, FALSE /*Wait*/);
|
---|
226 | #endif
|
---|
227 | return VINF_SUCCESS;
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | RTDECL(bool) RTSemMutexIsOwned(RTSEMMUTEX hMutexSem)
|
---|
232 | {
|
---|
233 | /*
|
---|
234 | * Validate.
|
---|
235 | */
|
---|
236 | RTSEMMUTEXINTERNAL *pThis = hMutexSem;
|
---|
237 | AssertPtrReturn(pThis, false);
|
---|
238 | AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, false);
|
---|
239 |
|
---|
240 | #ifdef RT_USE_FAST_MUTEX
|
---|
241 | return pThis->Mutex && pThis->Mutex->Owner != NULL;
|
---|
242 | #else
|
---|
243 | return KeReadStateMutex(&pThis->Mutex) == 0;
|
---|
244 | #endif
|
---|
245 | }
|
---|
246 |
|
---|