VirtualBox

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

Last change on this file since 94293 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

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