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