1 | /* $Id: semevent-r0drv-freebsd.c 36190 2011-03-07 16:28:50Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Single Release Event Semaphores, Ring-0 Driver, FreeBSD.
|
---|
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 | * Header Files *
|
---|
33 | *******************************************************************************/
|
---|
34 | #define RTSEMEVENT_WITHOUT_REMAPPING
|
---|
35 | #include "the-freebsd-kernel.h"
|
---|
36 | #include "internal/iprt.h"
|
---|
37 | #include <iprt/semaphore.h>
|
---|
38 |
|
---|
39 | #include <iprt/asm.h>
|
---|
40 | #include <iprt/assert.h>
|
---|
41 | #include <iprt/err.h>
|
---|
42 | #include <iprt/lockvalidator.h>
|
---|
43 | #include <iprt/mem.h>
|
---|
44 |
|
---|
45 | #include "sleepqueue-r0drv-freebsd.h"
|
---|
46 | #include "internal/magics.h"
|
---|
47 |
|
---|
48 |
|
---|
49 | /*******************************************************************************
|
---|
50 | * Structures and Typedefs *
|
---|
51 | *******************************************************************************/
|
---|
52 | /**
|
---|
53 | * FreeBSD event semaphore.
|
---|
54 | */
|
---|
55 | typedef struct RTSEMEVENTINTERNAL
|
---|
56 | {
|
---|
57 | /** Magic value (RTSEMEVENT_MAGIC). */
|
---|
58 | uint32_t volatile u32Magic;
|
---|
59 | /** The object status - !0 when signaled and 0 when reset. */
|
---|
60 | uint32_t volatile fState;
|
---|
61 | /** Reference counter. */
|
---|
62 | uint32_t volatile cRefs;
|
---|
63 | } RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
|
---|
64 |
|
---|
65 |
|
---|
66 | RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
|
---|
67 | {
|
---|
68 | return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
|
---|
73 | {
|
---|
74 | AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
|
---|
75 | AssertReturn(!(fFlags & ~(RTSEMEVENT_FLAGS_NO_LOCK_VAL | RTSEMEVENT_FLAGS_BOOTSTRAP_HACK)), VERR_INVALID_PARAMETER);
|
---|
76 | Assert(!(fFlags & RTSEMEVENT_FLAGS_BOOTSTRAP_HACK) || (fFlags & RTSEMEVENT_FLAGS_NO_LOCK_VAL));
|
---|
77 | AssertPtrReturn(phEventSem, VERR_INVALID_POINTER);
|
---|
78 |
|
---|
79 | PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAllocZ(sizeof(*pThis));
|
---|
80 | if (!pThis)
|
---|
81 | return VERR_NO_MEMORY;
|
---|
82 |
|
---|
83 | pThis->u32Magic = RTSEMEVENT_MAGIC;
|
---|
84 | pThis->cRefs = 1;
|
---|
85 | pThis->fState = 0;
|
---|
86 |
|
---|
87 | *phEventSem = pThis;
|
---|
88 | return VINF_SUCCESS;
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Retains a reference to the event semaphore.
|
---|
94 | *
|
---|
95 | * @param pThis The event semaphore.
|
---|
96 | */
|
---|
97 | DECLINLINE(void) rtR0SemEventBsdRetain(PRTSEMEVENTINTERNAL pThis)
|
---|
98 | {
|
---|
99 | uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
|
---|
100 | Assert(cRefs < 100000); NOREF(cRefs);
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Releases a reference to the event semaphore.
|
---|
106 | *
|
---|
107 | * @param pThis The event semaphore.
|
---|
108 | */
|
---|
109 | DECLINLINE(void) rtR0SemEventBsdRelease(PRTSEMEVENTINTERNAL pThis)
|
---|
110 | {
|
---|
111 | if (RT_UNLIKELY(ASMAtomicDecU32(&pThis->cRefs) == 0))
|
---|
112 | RTMemFree(pThis);
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
|
---|
117 | {
|
---|
118 | /*
|
---|
119 | * Validate input.
|
---|
120 | */
|
---|
121 | PRTSEMEVENTINTERNAL pThis = hEventSem;
|
---|
122 | if (pThis == NIL_RTSEMEVENT)
|
---|
123 | return VINF_SUCCESS;
|
---|
124 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
|
---|
125 | Assert(pThis->cRefs > 0);
|
---|
126 |
|
---|
127 | /*
|
---|
128 | * Invalidate it and signal the object just in case.
|
---|
129 | */
|
---|
130 | ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMEVENT_MAGIC);
|
---|
131 | ASMAtomicWriteU32(&pThis->fState, 0);
|
---|
132 | rtR0SemBsdBroadcast(pThis);
|
---|
133 | rtR0SemEventBsdRelease(pThis);
|
---|
134 | return VINF_SUCCESS;
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
|
---|
139 | {
|
---|
140 | /*
|
---|
141 | * Validate input.
|
---|
142 | */
|
---|
143 | PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
|
---|
144 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
145 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
|
---|
146 | rtR0SemEventBsdRetain(pThis);
|
---|
147 |
|
---|
148 | /*
|
---|
149 | * Signal the event object.
|
---|
150 | */
|
---|
151 | ASMAtomicWriteU32(&pThis->fState, 1);
|
---|
152 | rtR0SemBsdSignal(pThis);
|
---|
153 | rtR0SemEventBsdRelease(pThis);
|
---|
154 | return VINF_SUCCESS;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Worker for RTSemEventWaitEx and RTSemEventWaitExDebug.
|
---|
159 | *
|
---|
160 | * @returns VBox status code.
|
---|
161 | * @param pThis The event semaphore.
|
---|
162 | * @param fFlags See RTSemEventWaitEx.
|
---|
163 | * @param uTimeout See RTSemEventWaitEx.
|
---|
164 | * @param pSrcPos The source code position of the wait.
|
---|
165 | */
|
---|
166 | static int rtR0SemEventWait(PRTSEMEVENTINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
|
---|
167 | PCRTLOCKVALSRCPOS pSrcPos)
|
---|
168 | {
|
---|
169 | int rc;
|
---|
170 |
|
---|
171 | /*
|
---|
172 | * Validate the input.
|
---|
173 | */
|
---|
174 | AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
|
---|
175 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
|
---|
176 | AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
|
---|
177 | rtR0SemEventBsdRetain(pThis);
|
---|
178 |
|
---|
179 | /*
|
---|
180 | * Try grab the event without setting up the wait.
|
---|
181 | */
|
---|
182 | if (ASMAtomicCmpXchgU32(&pThis->fState, 0, 1))
|
---|
183 | rc = VINF_SUCCESS;
|
---|
184 | else
|
---|
185 | {
|
---|
186 | /*
|
---|
187 | * We have to wait.
|
---|
188 | */
|
---|
189 | RTR0SEMBSDSLEEP Wait;
|
---|
190 | rc = rtR0SemBsdWaitInit(&Wait, fFlags, uTimeout, pThis);
|
---|
191 | if (RT_SUCCESS(rc))
|
---|
192 | {
|
---|
193 | for (;;)
|
---|
194 | {
|
---|
195 | /* The destruction test. */
|
---|
196 | if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENT_MAGIC))
|
---|
197 | rc = VERR_SEM_DESTROYED;
|
---|
198 | else
|
---|
199 | {
|
---|
200 | rtR0SemBsdWaitPrepare(&Wait);
|
---|
201 |
|
---|
202 | /* Check the exit conditions. */
|
---|
203 | if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENT_MAGIC))
|
---|
204 | rc = VERR_SEM_DESTROYED;
|
---|
205 | else if (ASMAtomicCmpXchgU32(&pThis->fState, 0, 1))
|
---|
206 | rc = VINF_SUCCESS;
|
---|
207 | else if (rtR0SemBsdWaitHasTimedOut(&Wait))
|
---|
208 | rc = VERR_TIMEOUT;
|
---|
209 | else if (rtR0SemBsdWaitWasInterrupted(&Wait))
|
---|
210 | rc = VERR_INTERRUPTED;
|
---|
211 | else
|
---|
212 | {
|
---|
213 | /* Do the wait and then recheck the conditions. */
|
---|
214 | rtR0SemBsdWaitDoIt(&Wait);
|
---|
215 | continue;
|
---|
216 | }
|
---|
217 | }
|
---|
218 | break;
|
---|
219 | }
|
---|
220 |
|
---|
221 | rtR0SemBsdWaitDelete(&Wait);
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | rtR0SemEventBsdRelease(pThis);
|
---|
226 | return rc;
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | RTDECL(int) RTSemEventWaitEx(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout)
|
---|
231 | {
|
---|
232 | #ifndef RTSEMEVENT_STRICT
|
---|
233 | return rtR0SemEventWait(hEventSem, fFlags, uTimeout, NULL);
|
---|
234 | #else
|
---|
235 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
|
---|
236 | return rtR0SemEventWait(hEventSem, fFlags, uTimeout, &SrcPos);
|
---|
237 | #endif
|
---|
238 | }
|
---|
239 | RT_EXPORT_SYMBOL(RTSemEventWaitEx);
|
---|
240 |
|
---|
241 |
|
---|
242 | RTDECL(int) RTSemEventWaitExDebug(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout,
|
---|
243 | RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
244 | {
|
---|
245 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
|
---|
246 | return rtR0SemEventWait(hEventSem, fFlags, uTimeout, &SrcPos);
|
---|
247 | }
|
---|
248 | RT_EXPORT_SYMBOL(RTSemEventWaitExDebug);
|
---|
249 |
|
---|
250 |
|
---|
251 | RTDECL(uint32_t) RTSemEventGetResolution(void)
|
---|
252 | {
|
---|
253 | return 1000000000 / hz;
|
---|
254 | }
|
---|
255 |
|
---|