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