VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/netbsd/semeventmulti-r0drv-netbsd.c

Last change on this file was 98103, checked in by vboxsync, 17 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: 12.4 KB
Line 
1/* $Id: semeventmulti-r0drv-netbsd.c 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Multiple Release Event Semaphores, Ring-0 Driver, NetBSD.
4 */
5
6/*
7 * Contributed by knut st. osmundsen.
8 *
9 * Copyright (C) 2007-2023 Oracle and/or its affiliates.
10 *
11 * This file is part of VirtualBox base platform packages, as
12 * available from https://www.virtualbox.org.
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation, in version 3 of the
17 * License.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see <https://www.gnu.org/licenses>.
26 *
27 * The contents of this file may alternatively be used under the terms
28 * of the Common Development and Distribution License Version 1.0
29 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
30 * in the VirtualBox distribution, in which case the provisions of the
31 * CDDL are applicable instead of those of the GPL.
32 *
33 * You may elect to license modified versions of this file under the
34 * terms and conditions of either the GPL or the CDDL or both.
35 *
36 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
37 * --------------------------------------------------------------------
38 *
39 * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
40 *
41 * Permission is hereby granted, free of charge, to any person
42 * obtaining a copy of this software and associated documentation
43 * files (the "Software"), to deal in the Software without
44 * restriction, including without limitation the rights to use,
45 * copy, modify, merge, publish, distribute, sublicense, and/or sell
46 * copies of the Software, and to permit persons to whom the
47 * Software is furnished to do so, subject to the following
48 * conditions:
49 *
50 * The above copyright notice and this permission notice shall be
51 * included in all copies or substantial portions of the Software.
52 *
53 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
54 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
55 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
56 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
57 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
58 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
59 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
60 * OTHER DEALINGS IN THE SOFTWARE.
61 */
62
63
64/*********************************************************************************************************************************
65* Header Files *
66*********************************************************************************************************************************/
67#define RTSEMEVENTMULTI_WITHOUT_REMAPPING
68#include "the-netbsd-kernel.h"
69#include "internal/iprt.h"
70#include <iprt/semaphore.h>
71
72#include <iprt/assert.h>
73#include <iprt/asm.h>
74#include <iprt/err.h>
75#include <iprt/mem.h>
76#include <iprt/lockvalidator.h>
77
78#include "sleepqueue-r0drv-netbsd.h"
79#include "internal/magics.h"
80
81
82/*********************************************************************************************************************************
83* Defined Constants And Macros *
84*********************************************************************************************************************************/
85/** @name fStateAndGen values
86 * @{ */
87/** The state bit number. */
88#define RTSEMEVENTMULTIBSD_STATE_BIT 0
89/** The state mask. */
90#define RTSEMEVENTMULTIBSD_STATE_MASK RT_BIT_32(RTSEMEVENTMULTIBSD_STATE_BIT)
91/** The generation mask. */
92#define RTSEMEVENTMULTIBSD_GEN_MASK ~RTSEMEVENTMULTIBSD_STATE_MASK
93/** The generation shift. */
94#define RTSEMEVENTMULTIBSD_GEN_SHIFT 1
95/** The initial variable value. */
96#define RTSEMEVENTMULTIBSD_STATE_GEN_INIT UINT32_C(0xfffffffc)
97/** @} */
98
99
100/*********************************************************************************************************************************
101* Structures and Typedefs *
102*********************************************************************************************************************************/
103/**
104 * NetBSD multiple release event semaphore.
105 */
106typedef struct RTSEMEVENTMULTIINTERNAL
107{
108 /** Magic value (RTSEMEVENTMULTI_MAGIC). */
109 uint32_t volatile u32Magic;
110 /** The object state bit and generation counter.
111 * The generation counter is incremented every time the object is
112 * signalled. */
113 uint32_t volatile fStateAndGen;
114 /** Reference counter. */
115 uint32_t volatile cRefs;
116} RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
117
118
119RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI phEventMultiSem)
120{
121 return RTSemEventMultiCreateEx(phEventMultiSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
122}
123
124
125RTDECL(int) RTSemEventMultiCreateEx(PRTSEMEVENTMULTI phEventMultiSem, uint32_t fFlags, RTLOCKVALCLASS hClass,
126 const char *pszNameFmt, ...)
127{
128 PRTSEMEVENTMULTIINTERNAL pThis;
129
130 AssertReturn(!(fFlags & ~RTSEMEVENTMULTI_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
131 pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis));
132 if (pThis)
133 {
134 pThis->u32Magic = RTSEMEVENTMULTI_MAGIC;
135 pThis->fStateAndGen = RTSEMEVENTMULTIBSD_STATE_GEN_INIT;
136 pThis->cRefs = 1;
137
138 *phEventMultiSem = pThis;
139 return VINF_SUCCESS;
140 }
141 return VERR_NO_MEMORY;
142}
143
144
145/**
146 * Retain a reference to the semaphore.
147 *
148 * @param pThis The semaphore.
149 */
150DECLINLINE(void) rtR0SemEventMultiBsdRetain(PRTSEMEVENTMULTIINTERNAL pThis)
151{
152 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
153 Assert(cRefs && cRefs < 100000);
154}
155
156
157/**
158 * Release a reference, destroy the thing if necessary.
159 *
160 * @param pThis The semaphore.
161 */
162DECLINLINE(void) rtR0SemEventMultiBsdRelease(PRTSEMEVENTMULTIINTERNAL pThis)
163{
164 if (RT_UNLIKELY(ASMAtomicDecU32(&pThis->cRefs) == 0))
165 {
166 Assert(pThis->u32Magic != RTSEMEVENTMULTI_MAGIC);
167 RTMemFree(pThis);
168 }
169}
170
171
172RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI hEventMultiSem)
173{
174 /*
175 * Validate input.
176 */
177 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
178 if (pThis == NIL_RTSEMEVENTMULTI)
179 return VINF_SUCCESS;
180 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
181 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
182 Assert(pThis->cRefs > 0);
183
184 /*
185 * Invalidate it and signal the object just in case.
186 */
187 ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMEVENTMULTI_MAGIC);
188 ASMAtomicAndU32(&pThis->fStateAndGen, RTSEMEVENTMULTIBSD_GEN_MASK);
189 rtR0SemBsdBroadcast(pThis);
190 rtR0SemEventMultiBsdRelease(pThis);
191 return VINF_SUCCESS;
192}
193
194
195RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI hEventMultiSem)
196{
197 uint32_t fNew;
198 uint32_t fOld;
199
200 /*
201 * Validate input.
202 */
203 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
204 if (!pThis)
205 return VERR_INVALID_PARAMETER;
206 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
207 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
208 rtR0SemEventMultiBsdRetain(pThis);
209
210 /*
211 * Signal the event object. The cause of the parnoia here is racing to try
212 * deal with racing RTSemEventMultiSignal calls (should probably be
213 * forbidden, but it's relatively easy to handle).
214 */
215 do
216 {
217 fNew = fOld = ASMAtomicUoReadU32(&pThis->fStateAndGen);
218 fNew += 1 << RTSEMEVENTMULTIBSD_GEN_SHIFT;
219 fNew |= RTSEMEVENTMULTIBSD_STATE_MASK;
220 }
221 while (!ASMAtomicCmpXchgU32(&pThis->fStateAndGen, fNew, fOld));
222
223 rtR0SemBsdBroadcast(pThis);
224 rtR0SemEventMultiBsdRelease(pThis);
225 return VINF_SUCCESS;
226}
227
228
229RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem)
230{
231 /*
232 * Validate input.
233 */
234 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
235 if (!pThis)
236 return VERR_INVALID_PARAMETER;
237 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
238 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
239 rtR0SemEventMultiBsdRetain(pThis);
240
241 /*
242 * Reset it.
243 */
244 ASMAtomicAndU32(&pThis->fStateAndGen, ~RTSEMEVENTMULTIBSD_STATE_MASK);
245
246 rtR0SemEventMultiBsdRelease(pThis);
247 return VINF_SUCCESS;
248}
249
250
251/**
252 * Worker for RTSemEventMultiWaitEx and RTSemEventMultiWaitExDebug.
253 *
254 * @returns VBox status code.
255 * @param pThis The event semaphore.
256 * @param fFlags See RTSemEventMultiWaitEx.
257 * @param uTimeout See RTSemEventMultiWaitEx.
258 * @param pSrcPos The source code position of the wait.
259 */
260static int rtR0SemEventMultiBsdWait(PRTSEMEVENTMULTIINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
261 PCRTLOCKVALSRCPOS pSrcPos)
262{
263 uint32_t fOrgStateAndGen;
264 int rc;
265
266 /*
267 * Validate the input.
268 */
269 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
270 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
271 AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
272 rtR0SemEventMultiBsdRetain(pThis);
273
274 /*
275 * Is the event already signalled or do we have to wait?
276 */
277 fOrgStateAndGen = ASMAtomicUoReadU32(&pThis->fStateAndGen);
278 if (fOrgStateAndGen & RTSEMEVENTMULTIBSD_STATE_MASK)
279 rc = VINF_SUCCESS;
280 else
281 {
282 /*
283 * We have to wait.
284 */
285 RTR0SEMBSDSLEEP Wait;
286 rc = rtR0SemBsdWaitInit(&Wait, fFlags, uTimeout, pThis);
287 if (RT_SUCCESS(rc))
288 {
289 for (;;)
290 {
291 /* The destruction test. */
292 if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENTMULTI_MAGIC))
293 rc = VERR_SEM_DESTROYED;
294 else
295 {
296 rtR0SemBsdWaitPrepare(&Wait);
297
298 /* Check the exit conditions. */
299 if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENTMULTI_MAGIC))
300 rc = VERR_SEM_DESTROYED;
301 else if (ASMAtomicUoReadU32(&pThis->fStateAndGen) != fOrgStateAndGen)
302 rc = VINF_SUCCESS;
303 else if (rtR0SemBsdWaitHasTimedOut(&Wait))
304 rc = VERR_TIMEOUT;
305 else if (rtR0SemBsdWaitWasInterrupted(&Wait))
306 rc = VERR_INTERRUPTED;
307 else
308 {
309 /* Do the wait and then recheck the conditions. */
310 rtR0SemBsdWaitDoIt(&Wait);
311 continue;
312 }
313 }
314 break;
315 }
316
317 rtR0SemBsdWaitDelete(&Wait);
318 }
319 }
320
321 rtR0SemEventMultiBsdRelease(pThis);
322 return rc;
323}
324
325
326RTDECL(int) RTSemEventMultiWaitEx(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout)
327{
328#ifndef RTSEMEVENT_STRICT
329 return rtR0SemEventMultiBsdWait(hEventMultiSem, fFlags, uTimeout, NULL);
330#else
331 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
332 return rtR0SemEventMultiBsdWait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
333#endif
334}
335RT_EXPORT_SYMBOL(RTSemEventMultiWaitEx);
336
337
338RTDECL(int) RTSemEventMultiWaitExDebug(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout,
339 RTHCUINTPTR uId, RT_SRC_POS_DECL)
340{
341 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
342 return rtR0SemEventMultiBsdWait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
343}
344RT_EXPORT_SYMBOL(RTSemEventMultiWaitExDebug);
345
346
347RTDECL(uint32_t) RTSemEventMultiGetResolution(void)
348{
349 return rtR0SemBsdWaitGetResolution();
350}
351RT_EXPORT_SYMBOL(RTSemEventMultiGetResolution);
352
353
354RTR0DECL(bool) RTSemEventMultiIsSignalSafe(void)
355{
356 /** @todo check the code... */
357 return false;
358}
359RT_EXPORT_SYMBOL(RTSemEventMultiIsSignalSafe);
360
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use