VirtualBox

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

Last change on this file since 77923 was 77120, checked in by vboxsync, 6 years ago

IPRT: Some license header cleanups.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette