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