VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/freebsd/semevent-r0drv-freebsd.c@ 99743

Last change on this file since 99743 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.8 KB
Line 
1/* $Id: semevent-r0drv-freebsd.c 98103 2023-01-17 14:15: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-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 * This code is based on:
40 *
41 * Copyright (c) 2007 knut st. osmundsen <[email protected]>
42 *
43 * Permission is hereby granted, free of charge, to any person
44 * obtaining a copy of this software and associated documentation
45 * files (the "Software"), to deal in the Software without
46 * restriction, including without limitation the rights to use,
47 * copy, modify, merge, publish, distribute, sublicense, and/or sell
48 * copies of the Software, and to permit persons to whom the
49 * Software is furnished to do so, subject to the following
50 * conditions:
51 *
52 * The above copyright notice and this permission notice shall be
53 * included in all copies or substantial portions of the Software.
54 *
55 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
56 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
57 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
58 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
59 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
60 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
61 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
62 * OTHER DEALINGS IN THE SOFTWARE.
63 */
64
65
66/*********************************************************************************************************************************
67* Header Files *
68*********************************************************************************************************************************/
69#define RTSEMEVENT_WITHOUT_REMAPPING
70#include "the-freebsd-kernel.h"
71#include "internal/iprt.h"
72#include <iprt/semaphore.h>
73
74#include <iprt/asm.h>
75#include <iprt/assert.h>
76#include <iprt/err.h>
77#include <iprt/lockvalidator.h>
78#include <iprt/mem.h>
79
80#include "sleepqueue-r0drv-freebsd.h"
81#include "internal/magics.h"
82
83
84/*********************************************************************************************************************************
85* Structures and Typedefs *
86*********************************************************************************************************************************/
87/**
88 * FreeBSD event semaphore.
89 */
90typedef struct RTSEMEVENTINTERNAL
91{
92 /** Magic value (RTSEMEVENT_MAGIC). */
93 uint32_t volatile u32Magic;
94 /** The object status - !0 when signaled and 0 when reset. */
95 uint32_t volatile fState;
96 /** Reference counter. */
97 uint32_t volatile cRefs;
98} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
99
100
101RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
102{
103 return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
104}
105
106
107RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
108{
109 AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
110 AssertReturn(!(fFlags & ~(RTSEMEVENT_FLAGS_NO_LOCK_VAL | RTSEMEVENT_FLAGS_BOOTSTRAP_HACK)), VERR_INVALID_PARAMETER);
111 Assert(!(fFlags & RTSEMEVENT_FLAGS_BOOTSTRAP_HACK) || (fFlags & RTSEMEVENT_FLAGS_NO_LOCK_VAL));
112 AssertPtrReturn(phEventSem, VERR_INVALID_POINTER);
113
114 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAllocZ(sizeof(*pThis));
115 if (!pThis)
116 return VERR_NO_MEMORY;
117
118 pThis->u32Magic = RTSEMEVENT_MAGIC;
119 pThis->cRefs = 1;
120 pThis->fState = 0;
121
122 *phEventSem = pThis;
123 return VINF_SUCCESS;
124}
125
126
127/**
128 * Retains a reference to the event semaphore.
129 *
130 * @param pThis The event semaphore.
131 */
132DECLINLINE(void) rtR0SemEventBsdRetain(PRTSEMEVENTINTERNAL pThis)
133{
134 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
135 Assert(cRefs < 100000); NOREF(cRefs);
136}
137
138
139/**
140 * Releases a reference to the event semaphore.
141 *
142 * @param pThis The event semaphore.
143 */
144DECLINLINE(void) rtR0SemEventBsdRelease(PRTSEMEVENTINTERNAL pThis)
145{
146 if (RT_UNLIKELY(ASMAtomicDecU32(&pThis->cRefs) == 0))
147 RTMemFree(pThis);
148}
149
150
151RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
152{
153 /*
154 * Validate input.
155 */
156 PRTSEMEVENTINTERNAL pThis = hEventSem;
157 if (pThis == NIL_RTSEMEVENT)
158 return VINF_SUCCESS;
159 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
160 Assert(pThis->cRefs > 0);
161
162 /*
163 * Invalidate it and signal the object just in case.
164 */
165 ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMEVENT_MAGIC);
166 ASMAtomicWriteU32(&pThis->fState, 0);
167 rtR0SemBsdBroadcast(pThis);
168 rtR0SemEventBsdRelease(pThis);
169 return VINF_SUCCESS;
170}
171
172
173RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
174{
175 /*
176 * Validate input.
177 */
178 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
179 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
180 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
181 rtR0SemEventBsdRetain(pThis);
182
183 /*
184 * Signal the event object.
185 */
186 ASMAtomicWriteU32(&pThis->fState, 1);
187 rtR0SemBsdSignal(pThis);
188 rtR0SemEventBsdRelease(pThis);
189 return VINF_SUCCESS;
190}
191
192/**
193 * Worker for RTSemEventWaitEx and RTSemEventWaitExDebug.
194 *
195 * @returns VBox status code.
196 * @param pThis The event semaphore.
197 * @param fFlags See RTSemEventWaitEx.
198 * @param uTimeout See RTSemEventWaitEx.
199 * @param pSrcPos The source code position of the wait.
200 */
201static int rtR0SemEventWait(PRTSEMEVENTINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
202 PCRTLOCKVALSRCPOS pSrcPos)
203{
204 int rc;
205
206 /*
207 * Validate the input.
208 */
209 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
210 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
211 AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
212 rtR0SemEventBsdRetain(pThis);
213
214 /*
215 * Try grab the event without setting up the wait.
216 */
217 if (ASMAtomicCmpXchgU32(&pThis->fState, 0, 1))
218 rc = VINF_SUCCESS;
219 else
220 {
221 /*
222 * We have to wait.
223 */
224 RTR0SEMBSDSLEEP Wait;
225 rc = rtR0SemBsdWaitInit(&Wait, fFlags, uTimeout, pThis);
226 if (RT_SUCCESS(rc))
227 {
228 for (;;)
229 {
230 /* The destruction test. */
231 if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENT_MAGIC))
232 rc = VERR_SEM_DESTROYED;
233 else
234 {
235 rtR0SemBsdWaitPrepare(&Wait);
236
237 /* Check the exit conditions. */
238 if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENT_MAGIC))
239 rc = VERR_SEM_DESTROYED;
240 else if (ASMAtomicCmpXchgU32(&pThis->fState, 0, 1))
241 rc = VINF_SUCCESS;
242 else if (rtR0SemBsdWaitHasTimedOut(&Wait))
243 rc = VERR_TIMEOUT;
244 else if (rtR0SemBsdWaitWasInterrupted(&Wait))
245 rc = VERR_INTERRUPTED;
246 else
247 {
248 /* Do the wait and then recheck the conditions. */
249 rtR0SemBsdWaitDoIt(&Wait);
250 continue;
251 }
252 }
253 break;
254 }
255
256 rtR0SemBsdWaitDelete(&Wait);
257 }
258 }
259
260 rtR0SemEventBsdRelease(pThis);
261 return rc;
262}
263
264
265RTDECL(int) RTSemEventWaitEx(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout)
266{
267#ifndef RTSEMEVENT_STRICT
268 return rtR0SemEventWait(hEventSem, fFlags, uTimeout, NULL);
269#else
270 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
271 return rtR0SemEventWait(hEventSem, fFlags, uTimeout, &SrcPos);
272#endif
273}
274RT_EXPORT_SYMBOL(RTSemEventWaitEx);
275
276
277RTDECL(int) RTSemEventWaitExDebug(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout,
278 RTHCUINTPTR uId, RT_SRC_POS_DECL)
279{
280 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
281 return rtR0SemEventWait(hEventSem, fFlags, uTimeout, &SrcPos);
282}
283RT_EXPORT_SYMBOL(RTSemEventWaitExDebug);
284
285
286RTDECL(uint32_t) RTSemEventGetResolution(void)
287{
288 return 1000000000 / hz;
289}
290
291
292RTR0DECL(bool) RTSemEventIsSignalSafe(void)
293{
294 /** @todo check the code... */
295 return false;
296}
297
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