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