VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/netbsd/semevent-r0drv-netbsd.c@ 69111

Last change on this file since 69111 was 63346, checked in by vboxsync, 8 years ago

r0drv/netbsd: add/remove blank lines to reduce diff to FreeBSD.

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