VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/SUPLibSem.cpp

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.8 KB
Line 
1/* $Id: SUPLibSem.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * VirtualBox Support Library - Semaphores, ring-3 implementation.
4 */
5
6/*
7 * Copyright (C) 2009-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* Header Files *
40*********************************************************************************************************************************/
41#define LOG_GROUP LOG_GROUP_SUP
42#include <VBox/sup.h>
43
44#include <iprt/errcore.h>
45#include <VBox/param.h>
46#include <iprt/assert.h>
47#include <iprt/semaphore.h>
48#include <iprt/time.h>
49
50#include "SUPLibInternal.h"
51#include "SUPDrvIOC.h"
52
53
54/**
55 * Worker that makes a SUP_IOCTL_SEM_OP2 request.
56 *
57 * @returns VBox status code.
58 * @param pSession The session handle.
59 * @param uType The semaphore type.
60 * @param hSem The semaphore handle.
61 * @param uOp The operation.
62 * @param u64Arg The argument if applicable, otherwise 0.
63 */
64DECLINLINE(int) supSemOp2(PSUPDRVSESSION pSession, uint32_t uType, uintptr_t hSem, uint32_t uOp, uint64_t u64Arg)
65{
66 NOREF(pSession);
67 SUPSEMOP2 Req;
68 Req.Hdr.u32Cookie = g_u32Cookie;
69 Req.Hdr.u32SessionCookie = g_u32SessionCookie;
70 Req.Hdr.cbIn = SUP_IOCTL_SEM_OP2_SIZE_IN;
71 Req.Hdr.cbOut = SUP_IOCTL_SEM_OP2_SIZE_OUT;
72 Req.Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
73 Req.Hdr.rc = VERR_INTERNAL_ERROR;
74 Req.u.In.uType = uType;
75 Req.u.In.hSem = (uint32_t)hSem;
76 AssertReturn(Req.u.In.hSem == hSem, VERR_INVALID_HANDLE);
77 Req.u.In.uOp = uOp;
78 Req.u.In.uReserved = 0;
79 Req.u.In.uArg.u64 = u64Arg;
80 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_SEM_OP2, &Req, sizeof(Req));
81 if (RT_SUCCESS(rc))
82 rc = Req.Hdr.rc;
83
84 return rc;
85}
86
87
88/**
89 * Worker that makes a SUP_IOCTL_SEM_OP3 request.
90 *
91 * @returns VBox status code.
92 * @param pSession The session handle.
93 * @param uType The semaphore type.
94 * @param hSem The semaphore handle.
95 * @param uOp The operation.
96 * @param pReq The request structure. The caller should pick
97 * the output data from it himself.
98 */
99DECLINLINE(int) supSemOp3(PSUPDRVSESSION pSession, uint32_t uType, uintptr_t hSem, uint32_t uOp, PSUPSEMOP3 pReq)
100{
101 NOREF(pSession);
102 pReq->Hdr.u32Cookie = g_u32Cookie;
103 pReq->Hdr.u32SessionCookie = g_u32SessionCookie;
104 pReq->Hdr.cbIn = SUP_IOCTL_SEM_OP3_SIZE_IN;
105 pReq->Hdr.cbOut = SUP_IOCTL_SEM_OP3_SIZE_OUT;
106 pReq->Hdr.fFlags = SUPREQHDR_FLAGS_DEFAULT;
107 pReq->Hdr.rc = VERR_INTERNAL_ERROR;
108 pReq->u.In.uType = uType;
109 pReq->u.In.hSem = (uint32_t)hSem;
110 AssertReturn(pReq->u.In.hSem == hSem, VERR_INVALID_HANDLE);
111 pReq->u.In.uOp = uOp;
112 pReq->u.In.u32Reserved = 0;
113 pReq->u.In.u64Reserved = 0;
114 int rc = suplibOsIOCtl(&g_supLibData, SUP_IOCTL_SEM_OP3, pReq, sizeof(*pReq));
115 if (RT_SUCCESS(rc))
116 rc = pReq->Hdr.rc;
117
118 return rc;
119}
120
121
122SUPDECL(int) SUPSemEventCreate(PSUPDRVSESSION pSession, PSUPSEMEVENT phEvent)
123{
124 AssertPtrReturn(phEvent, VERR_INVALID_POINTER);
125
126 int rc;
127 if (!g_supLibData.fDriverless)
128 {
129 SUPSEMOP3 Req;
130 rc = supSemOp3(pSession, SUP_SEM_TYPE_EVENT, (uintptr_t)NIL_SUPSEMEVENT, SUPSEMOP3_CREATE, &Req);
131 if (RT_SUCCESS(rc))
132 *phEvent = (SUPSEMEVENT)(uintptr_t)Req.u.Out.hSem;
133 }
134 else
135 {
136 RTSEMEVENT hEvent;
137 rc = RTSemEventCreate(&hEvent);
138 if (RT_SUCCESS(rc))
139 *phEvent = (SUPSEMEVENT)hEvent;
140 }
141 return rc;
142}
143
144
145SUPDECL(int) SUPSemEventClose(PSUPDRVSESSION pSession, SUPSEMEVENT hEvent)
146{
147 if (hEvent == NIL_SUPSEMEVENT)
148 return VINF_SUCCESS;
149 int rc;
150 if (!g_supLibData.fDriverless)
151 rc = supSemOp2(pSession, SUP_SEM_TYPE_EVENT, (uintptr_t)hEvent, SUPSEMOP2_CLOSE, 0);
152 else
153 rc = RTSemEventDestroy((RTSEMEVENT)hEvent);
154 return rc;
155}
156
157
158SUPDECL(int) SUPSemEventSignal(PSUPDRVSESSION pSession, SUPSEMEVENT hEvent)
159{
160 int rc;
161 if (!g_supLibData.fDriverless)
162 rc = supSemOp2(pSession, SUP_SEM_TYPE_EVENT, (uintptr_t)hEvent, SUPSEMOP2_SIGNAL, 0);
163 else
164 rc = RTSemEventSignal((RTSEMEVENT)hEvent);
165 return rc;
166}
167
168
169SUPDECL(int) SUPSemEventWaitNoResume(PSUPDRVSESSION pSession, SUPSEMEVENT hEvent, uint32_t cMillies)
170{
171 int rc;
172 if (!g_supLibData.fDriverless)
173 rc = supSemOp2(pSession, SUP_SEM_TYPE_EVENT, (uintptr_t)hEvent, SUPSEMOP2_WAIT_MS_REL, cMillies);
174 else
175 rc = RTSemEventWaitNoResume((RTSEMEVENT)hEvent, cMillies);
176 return rc;
177}
178
179
180SUPDECL(int) SUPSemEventWaitNsAbsIntr(PSUPDRVSESSION pSession, SUPSEMEVENT hEvent, uint64_t uNsTimeout)
181{
182 int rc;
183 if (!g_supLibData.fDriverless)
184 rc = supSemOp2(pSession, SUP_SEM_TYPE_EVENT, (uintptr_t)hEvent, SUPSEMOP2_WAIT_NS_ABS, uNsTimeout);
185 else
186 {
187#if 0
188 rc = RTSemEventWaitEx((RTSEMEVENT)hEvent,
189 RTSEMWAIT_FLAGS_ABSOLUTE | RTSEMWAIT_FLAGS_NANOSECS | RTSEMWAIT_FLAGS_NORESUME, uNsTimeout);
190#else
191 uint64_t nsNow = RTTimeNanoTS();
192 if (nsNow < uNsTimeout)
193 rc = RTSemEventWaitNoResume((RTSEMEVENT)hEvent, (uNsTimeout - nsNow + RT_NS_1MS - 1) / RT_NS_1MS);
194 else
195 rc = VERR_TIMEOUT;
196#endif
197 }
198 return rc;
199}
200
201
202SUPDECL(int) SUPSemEventWaitNsRelIntr(PSUPDRVSESSION pSession, SUPSEMEVENT hEvent, uint64_t cNsTimeout)
203{
204 int rc;
205 if (!g_supLibData.fDriverless)
206 rc = supSemOp2(pSession, SUP_SEM_TYPE_EVENT, (uintptr_t)hEvent, SUPSEMOP2_WAIT_NS_REL, cNsTimeout);
207 else
208 {
209#if 0
210 rc = RTSemEventWaitEx((RTSEMEVENT)hEvent,
211 RTSEMWAIT_FLAGS_RELATIVE | RTSEMWAIT_FLAGS_NANOSECS | RTSEMWAIT_FLAGS_NORESUME, cNsTimeout);
212#else
213 rc = RTSemEventWaitNoResume((RTSEMEVENT)hEvent, (cNsTimeout + RT_NS_1MS - 1) / RT_NS_1MS);
214#endif
215 }
216 return rc;
217}
218
219
220SUPDECL(uint32_t) SUPSemEventGetResolution(PSUPDRVSESSION pSession)
221{
222 if (!g_supLibData.fDriverless)
223 {
224 SUPSEMOP3 Req;
225 int rc = supSemOp3(pSession, SUP_SEM_TYPE_EVENT, (uintptr_t)NIL_SUPSEMEVENT, SUPSEMOP3_GET_RESOLUTION, &Req);
226 if (RT_SUCCESS(rc))
227 return Req.u.Out.cNsResolution;
228 return 1000 / 100;
229 }
230#if 0
231 return RTSemEventGetResolution();
232#else
233 return RT_NS_1MS;
234#endif
235}
236
237
238
239
240
241SUPDECL(int) SUPSemEventMultiCreate(PSUPDRVSESSION pSession, PSUPSEMEVENTMULTI phEventMulti)
242{
243 AssertPtrReturn(phEventMulti, VERR_INVALID_POINTER);
244
245 int rc;
246 if (!g_supLibData.fDriverless)
247 {
248 SUPSEMOP3 Req;
249 rc = supSemOp3(pSession, SUP_SEM_TYPE_EVENT_MULTI, (uintptr_t)NIL_SUPSEMEVENTMULTI, SUPSEMOP3_CREATE, &Req);
250 if (RT_SUCCESS(rc))
251 *phEventMulti = (SUPSEMEVENTMULTI)(uintptr_t)Req.u.Out.hSem;
252 }
253 else
254 {
255 RTSEMEVENTMULTI hEventMulti;
256 rc = RTSemEventMultiCreate(&hEventMulti);
257 if (RT_SUCCESS(rc))
258 *phEventMulti = (SUPSEMEVENTMULTI)hEventMulti;
259 }
260 return rc;
261}
262
263
264SUPDECL(int) SUPSemEventMultiClose(PSUPDRVSESSION pSession, SUPSEMEVENTMULTI hEventMulti)
265{
266 if (hEventMulti == NIL_SUPSEMEVENTMULTI)
267 return VINF_SUCCESS;
268 int rc;
269 if (!g_supLibData.fDriverless)
270 rc = supSemOp2(pSession, SUP_SEM_TYPE_EVENT_MULTI, (uintptr_t)hEventMulti, SUPSEMOP2_CLOSE, 0);
271 else
272 rc = RTSemEventMultiDestroy((RTSEMEVENTMULTI)hEventMulti);
273 return rc;
274}
275
276
277SUPDECL(int) SUPSemEventMultiSignal(PSUPDRVSESSION pSession, SUPSEMEVENTMULTI hEventMulti)
278{
279 int rc;
280 if (!g_supLibData.fDriverless)
281 rc = supSemOp2(pSession, SUP_SEM_TYPE_EVENT_MULTI, (uintptr_t)hEventMulti, SUPSEMOP2_SIGNAL, 0);
282 else
283 rc = RTSemEventMultiSignal((RTSEMEVENTMULTI)hEventMulti);
284 return rc;
285}
286
287
288SUPDECL(int) SUPSemEventMultiReset(PSUPDRVSESSION pSession, SUPSEMEVENTMULTI hEventMulti)
289{
290 int rc;
291 if (!g_supLibData.fDriverless)
292 rc = supSemOp2(pSession, SUP_SEM_TYPE_EVENT_MULTI, (uintptr_t)hEventMulti, SUPSEMOP2_RESET, 0);
293 else
294 rc = RTSemEventMultiReset((RTSEMEVENTMULTI)hEventMulti);
295 return rc;
296}
297
298
299SUPDECL(int) SUPSemEventMultiWaitNoResume(PSUPDRVSESSION pSession, SUPSEMEVENTMULTI hEventMulti, uint32_t cMillies)
300{
301 int rc;
302 if (!g_supLibData.fDriverless)
303 rc = supSemOp2(pSession, SUP_SEM_TYPE_EVENT_MULTI, (uintptr_t)hEventMulti, SUPSEMOP2_WAIT_MS_REL, cMillies);
304 else
305 rc = RTSemEventMultiWaitNoResume((RTSEMEVENTMULTI)hEventMulti, cMillies);
306 return rc;
307}
308
309
310SUPDECL(int) SUPSemEventMultiWaitNsAbsIntr(PSUPDRVSESSION pSession, SUPSEMEVENTMULTI hEventMulti, uint64_t uNsTimeout)
311{
312 int rc;
313 if (!g_supLibData.fDriverless)
314 rc = supSemOp2(pSession, SUP_SEM_TYPE_EVENT_MULTI, (uintptr_t)hEventMulti, SUPSEMOP2_WAIT_NS_ABS, uNsTimeout);
315 else
316 {
317#if 0
318 rc = RTSemEventMultiWaitEx((RTSEMEVENTMULTI)hEventMulti,
319 RTSEMWAIT_FLAGS_ABSOLUTE | RTSEMWAIT_FLAGS_NANOSECS | RTSEMWAIT_FLAGS_NORESUME, uNsTimeout);
320#else
321 uint64_t nsNow = RTTimeNanoTS();
322 if (nsNow < uNsTimeout)
323 rc = RTSemEventMultiWaitNoResume((RTSEMEVENTMULTI)hEventMulti, (uNsTimeout - nsNow + RT_NS_1MS - 1) / RT_NS_1MS);
324 else
325 rc = VERR_TIMEOUT;
326#endif
327 }
328 return rc;
329}
330
331
332SUPDECL(int) SUPSemEventMultiWaitNsRelIntr(PSUPDRVSESSION pSession, SUPSEMEVENTMULTI hEventMulti, uint64_t cNsTimeout)
333{
334 int rc;
335 if (!g_supLibData.fDriverless)
336 rc = supSemOp2(pSession, SUP_SEM_TYPE_EVENT_MULTI, (uintptr_t)hEventMulti, SUPSEMOP2_WAIT_NS_REL, cNsTimeout);
337 else
338 {
339#if 0
340 rc = RTSemEventMultiWaitEx((RTSEMEVENTMULTI)hEventMulti,
341 RTSEMWAIT_FLAGS_RELATIVE | RTSEMWAIT_FLAGS_NANOSECS | RTSEMWAIT_FLAGS_NORESUME, cNsTimeout);
342#else
343 rc = RTSemEventMultiWaitNoResume((RTSEMEVENTMULTI)hEventMulti, (cNsTimeout + RT_NS_1MS - 1) / RT_NS_1MS);
344#endif
345 }
346 return rc;
347}
348
349
350SUPDECL(uint32_t) SUPSemEventMultiGetResolution(PSUPDRVSESSION pSession)
351{
352 if (!g_supLibData.fDriverless)
353 {
354 SUPSEMOP3 Req;
355 int rc = supSemOp3(pSession, SUP_SEM_TYPE_EVENT_MULTI, (uintptr_t)NIL_SUPSEMEVENTMULTI, SUPSEMOP3_GET_RESOLUTION, &Req);
356 if (RT_SUCCESS(rc))
357 return Req.u.Out.cNsResolution;
358 return 1000 / 100;
359 }
360#if 0
361 return RTSemEventMultiGetResolution();
362#else
363 return RT_NS_1MS;
364#endif
365}
366
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use