1 | /* $Id: semevent-r0drv-freebsd.c 77120 2019-02-01 15:08:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Single 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 RTSEMEVENT_WITHOUT_REMAPPING
|
---|
61 | #include "the-freebsd-kernel.h"
|
---|
62 | #include "internal/iprt.h"
|
---|
63 | #include <iprt/semaphore.h>
|
---|
64 |
|
---|
65 | #include <iprt/asm.h>
|
---|
66 | #include <iprt/assert.h>
|
---|
67 | #include <iprt/err.h>
|
---|
68 | #include <iprt/lockvalidator.h>
|
---|
69 | #include <iprt/mem.h>
|
---|
70 |
|
---|
71 | #include "sleepqueue-r0drv-freebsd.h"
|
---|
72 | #include "internal/magics.h"
|
---|
73 |
|
---|
74 |
|
---|
75 | /*********************************************************************************************************************************
|
---|
76 | * Structures and Typedefs *
|
---|
77 | *********************************************************************************************************************************/
|
---|
78 | /**
|
---|
79 | * FreeBSD event semaphore.
|
---|
80 | */
|
---|
81 | typedef struct RTSEMEVENTINTERNAL
|
---|
82 | {
|
---|
83 | /** Magic value (RTSEMEVENT_MAGIC). */
|
---|
84 | uint32_t volatile u32Magic;
|
---|
85 | /** The object status - !0 when signaled and 0 when reset. */
|
---|
86 | uint32_t volatile fState;
|
---|
87 | /** Reference counter. */
|
---|
88 | uint32_t volatile cRefs;
|
---|
89 | } RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
|
---|
90 |
|
---|
91 |
|
---|
92 | RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
|
---|
93 | {
|
---|
94 | return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
|
---|
99 | {
|
---|
100 | AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
|
---|
101 | AssertReturn(!(fFlags & ~(RTSEMEVENT_FLAGS_NO_LOCK_VAL | RTSEMEVENT_FLAGS_BOOTSTRAP_HACK)), VERR_INVALID_PARAMETER);
|
---|
102 | Assert(!(fFlags & RTSEMEVENT_FLAGS_BOOTSTRAP_HACK) || (fFlags & RTSEMEVENT_FLAGS_NO_LOCK_VAL));
|
---|
103 | AssertPtrReturn(phEventSem, VERR_INVALID_POINTER);
|
---|
104 |
|
---|
105 | PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAllocZ(sizeof(*pThis));
|
---|
106 | if (!pThis)
|
---|
107 | return VERR_NO_MEMORY;
|
---|
108 |
|
---|
109 | pThis->u32Magic = RTSEMEVENT_MAGIC;
|
---|
110 | pThis->cRefs = 1;
|
---|
111 | pThis->fState = 0;
|
---|
112 |
|
---|
113 | *phEventSem = pThis;
|
---|
114 | return VINF_SUCCESS;
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Retains a reference to the event semaphore.
|
---|
120 | *
|
---|
121 | * @param pThis The event semaphore.
|
---|
122 | */
|
---|
123 | DECLINLINE(void) rtR0SemEventBsdRetain(PRTSEMEVENTINTERNAL pThis)
|
---|
124 | {
|
---|
125 | uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
|
---|
126 | Assert(cRefs < 100000); NOREF(cRefs);
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Releases a reference to the event semaphore.
|
---|
132 | *
|
---|
133 | * @param pThis The event semaphore.
|
---|
134 | */
|
---|
135 | DECLINLINE(void) rtR0SemEventBsdRelease(PRTSEMEVENTINTERNAL pThis)
|
---|
136 | {
|
---|
137 | if (RT_UNLIKELY(ASMAtomicDecU32(&pThis->cRefs) == 0))
|
---|
138 | RTMemFree(pThis);
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 | RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
|
---|
143 | {
|
---|
144 | /*
|
---|
145 | * Validate input.
|
---|
146 | */
|
---|
147 | PRTSEMEVENTINTERNAL pThis = hEventSem;
|
---|
148 | if (pThis == NIL_RTSEMEVENT)
|
---|
149 | return VINF_SUCCESS;
|
---|
150 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
|
---|
151 | Assert(pThis->cRefs > 0);
|
---|
152 |
|
---|
153 | /*
|
---|
154 | * Invalidate it and signal the object just in case.
|
---|
155 | */
|
---|
156 | ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMEVENT_MAGIC);
|
---|
157 | ASMAtomicWriteU32(&pThis->fState, 0);
|
---|
158 | rtR0SemBsdBroadcast(pThis);
|
---|
159 | rtR0SemEventBsdRelease(pThis);
|
---|
160 | return VINF_SUCCESS;
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 | RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
|
---|
165 | {
|
---|
166 | /*
|
---|
167 | * Validate input.
|
---|
168 | */
|
---|
169 | PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
|
---|
170 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
171 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
|
---|
172 | rtR0SemEventBsdRetain(pThis);
|
---|
173 |
|
---|
174 | /*
|
---|
175 | * Signal the event object.
|
---|
176 | */
|
---|
177 | ASMAtomicWriteU32(&pThis->fState, 1);
|
---|
178 | rtR0SemBsdSignal(pThis);
|
---|
179 | rtR0SemEventBsdRelease(pThis);
|
---|
180 | return VINF_SUCCESS;
|
---|
181 | }
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Worker for RTSemEventWaitEx and RTSemEventWaitExDebug.
|
---|
185 | *
|
---|
186 | * @returns VBox status code.
|
---|
187 | * @param pThis The event semaphore.
|
---|
188 | * @param fFlags See RTSemEventWaitEx.
|
---|
189 | * @param uTimeout See RTSemEventWaitEx.
|
---|
190 | * @param pSrcPos The source code position of the wait.
|
---|
191 | */
|
---|
192 | static int rtR0SemEventWait(PRTSEMEVENTINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
|
---|
193 | PCRTLOCKVALSRCPOS pSrcPos)
|
---|
194 | {
|
---|
195 | int rc;
|
---|
196 |
|
---|
197 | /*
|
---|
198 | * Validate the input.
|
---|
199 | */
|
---|
200 | AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
|
---|
201 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
|
---|
202 | AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
|
---|
203 | rtR0SemEventBsdRetain(pThis);
|
---|
204 |
|
---|
205 | /*
|
---|
206 | * Try grab the event without setting up the wait.
|
---|
207 | */
|
---|
208 | if (ASMAtomicCmpXchgU32(&pThis->fState, 0, 1))
|
---|
209 | rc = VINF_SUCCESS;
|
---|
210 | else
|
---|
211 | {
|
---|
212 | /*
|
---|
213 | * We have to wait.
|
---|
214 | */
|
---|
215 | RTR0SEMBSDSLEEP Wait;
|
---|
216 | rc = rtR0SemBsdWaitInit(&Wait, fFlags, uTimeout, pThis);
|
---|
217 | if (RT_SUCCESS(rc))
|
---|
218 | {
|
---|
219 | for (;;)
|
---|
220 | {
|
---|
221 | /* The destruction test. */
|
---|
222 | if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENT_MAGIC))
|
---|
223 | rc = VERR_SEM_DESTROYED;
|
---|
224 | else
|
---|
225 | {
|
---|
226 | rtR0SemBsdWaitPrepare(&Wait);
|
---|
227 |
|
---|
228 | /* Check the exit conditions. */
|
---|
229 | if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENT_MAGIC))
|
---|
230 | rc = VERR_SEM_DESTROYED;
|
---|
231 | else if (ASMAtomicCmpXchgU32(&pThis->fState, 0, 1))
|
---|
232 | rc = VINF_SUCCESS;
|
---|
233 | else if (rtR0SemBsdWaitHasTimedOut(&Wait))
|
---|
234 | rc = VERR_TIMEOUT;
|
---|
235 | else if (rtR0SemBsdWaitWasInterrupted(&Wait))
|
---|
236 | rc = VERR_INTERRUPTED;
|
---|
237 | else
|
---|
238 | {
|
---|
239 | /* Do the wait and then recheck the conditions. */
|
---|
240 | rtR0SemBsdWaitDoIt(&Wait);
|
---|
241 | continue;
|
---|
242 | }
|
---|
243 | }
|
---|
244 | break;
|
---|
245 | }
|
---|
246 |
|
---|
247 | rtR0SemBsdWaitDelete(&Wait);
|
---|
248 | }
|
---|
249 | }
|
---|
250 |
|
---|
251 | rtR0SemEventBsdRelease(pThis);
|
---|
252 | return rc;
|
---|
253 | }
|
---|
254 |
|
---|
255 |
|
---|
256 | RTDECL(int) RTSemEventWaitEx(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout)
|
---|
257 | {
|
---|
258 | #ifndef RTSEMEVENT_STRICT
|
---|
259 | return rtR0SemEventWait(hEventSem, fFlags, uTimeout, NULL);
|
---|
260 | #else
|
---|
261 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
|
---|
262 | return rtR0SemEventWait(hEventSem, fFlags, uTimeout, &SrcPos);
|
---|
263 | #endif
|
---|
264 | }
|
---|
265 | RT_EXPORT_SYMBOL(RTSemEventWaitEx);
|
---|
266 |
|
---|
267 |
|
---|
268 | RTDECL(int) RTSemEventWaitExDebug(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout,
|
---|
269 | RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
270 | {
|
---|
271 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
|
---|
272 | return rtR0SemEventWait(hEventSem, fFlags, uTimeout, &SrcPos);
|
---|
273 | }
|
---|
274 | RT_EXPORT_SYMBOL(RTSemEventWaitExDebug);
|
---|
275 |
|
---|
276 |
|
---|
277 | RTDECL(uint32_t) RTSemEventGetResolution(void)
|
---|
278 | {
|
---|
279 | return 1000000000 / hz;
|
---|
280 | }
|
---|
281 |
|
---|