VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/freebsd/semevent-r0drv-freebsd.c@ 33269

最後變更 在這個檔案從33269是 33269,由 vboxsync 提交於 14 年 前

IPRT: A quick replacement of the RTMemPage* and RTMemExec* APIs on posix. (Turned out to be a bit more work than expected because of the electric fence heap and init dependencies.)

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 9.6 KB
 
1/* $Id: semevent-r0drv-freebsd.c 33269 2010-10-20 15:42:28Z 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
36#include <iprt/semaphore.h>
37#include <iprt/alloc.h>
38#include <iprt/asm.h>
39#include <iprt/assert.h>
40#include <iprt/err.h>
41#include <iprt/spinlock.h>
42
43#include "internal/magics.h"
44
45
46/*******************************************************************************
47* Structures and Typedefs *
48*******************************************************************************/
49/**
50 * FreeBSD event semaphore.
51 */
52typedef struct RTSEMEVENTINTERNAL
53{
54 /** Magic value (RTSEMEVENT_MAGIC). */
55 uint32_t volatile u32Magic;
56 /** The number of waiting threads. */
57 uint32_t volatile cWaiters;
58 /** Set if the event object is signaled. */
59 uint8_t volatile fSignaled;
60 /** The number of threads in the process of waking up. */
61 uint32_t volatile cWaking;
62 /** Spinlock protecting this structure. */
63 RTSPINLOCK hSpinLock;
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->cWaiters = 0;
86 pThis->cWaking = 0;
87 pThis->fSignaled = 0;
88 int rc = RTSpinlockCreate(&pThis->hSpinLock);
89 if (RT_SUCCESS(rc))
90 {
91 *phEventSem = pThis;
92 return VINF_SUCCESS;
93 }
94
95 RTMemFree(pThis);
96 return rc;
97}
98
99
100RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
101{
102 PRTSEMEVENTINTERNAL pThis = hEventSem;
103 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
104
105 if (pThis == NIL_RTSEMEVENT)
106 return VINF_SUCCESS;
107 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
108 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
109
110 RTSpinlockAcquire(pThis->hSpinLock, &Tmp);
111
112 ASMAtomicIncU32(&pThis->u32Magic); /* make the handle invalid */
113 if (pThis->cWaiters > 0)
114 {
115 /* abort waiting thread, last man cleans up. */
116 ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
117 sleepq_lock(pThis);
118 sleepq_broadcast(pThis, SLEEPQ_CONDVAR, 0, 0);
119 sleepq_release(pThis);
120 RTSpinlockRelease(pThis->hSpinLock, &Tmp);
121 }
122 else if (pThis->cWaking)
123 /* the last waking thread is gonna do the cleanup */
124 RTSpinlockRelease(pThis->hSpinLock, &Tmp);
125 else
126 {
127 RTSpinlockRelease(pThis->hSpinLock, &Tmp);
128 RTSpinlockDestroy(pThis->hSpinLock);
129 RTMemFree(pThis);
130 }
131
132 return VINF_SUCCESS;
133}
134
135
136RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
137{
138 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
139 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
140 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
141 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC,
142 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
143 VERR_INVALID_HANDLE);
144
145 RTSpinlockAcquire(pThis->hSpinLock, &Tmp);
146
147 if (pThis->cWaiters > 0)
148 {
149 ASMAtomicDecU32(&pThis->cWaiters);
150 ASMAtomicIncU32(&pThis->cWaking);
151 sleepq_lock(pThis);
152 int fWakeupSwapProc = sleepq_signal(pThis, SLEEPQ_CONDVAR, 0, 0);
153 sleepq_release(pThis);
154 if (fWakeupSwapProc)
155 kick_proc0();
156 }
157 else
158 ASMAtomicXchgU8(&pThis->fSignaled, true);
159
160 RTSpinlockRelease(pThis->hSpinLock, &Tmp);
161 return VINF_SUCCESS;
162}
163
164
165static int rtSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies, bool fInterruptible)
166{
167 int rc;
168 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
169 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
170 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
171 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC,
172 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
173 VERR_INVALID_HANDLE);
174
175 RTSpinlockAcquire(pThis->hSpinLock, &Tmp);
176
177 if (pThis->fSignaled)
178 {
179 Assert(!pThis->cWaiters);
180 ASMAtomicXchgU8(&pThis->fSignaled, false);
181 rc = VINF_SUCCESS;
182 }
183 else
184 {
185 if (cMillies == 0)
186 rc = VERR_TIMEOUT;
187 else
188 {
189 ASMAtomicIncU32(&pThis->cWaiters);
190
191 int fFlags = SLEEPQ_CONDVAR;
192
193 if (fInterruptible)
194 fFlags |= SLEEPQ_INTERRUPTIBLE;
195
196 sleepq_lock(pThis);
197 sleepq_add(pThis, NULL, "IPRT Event Semaphore", fFlags, 0);
198
199 if (cMillies != RT_INDEFINITE_WAIT)
200 {
201 /*
202 * Translate milliseconds into ticks and go to sleep.
203 */
204 struct timeval tv;
205
206 tv.tv_sec = cMillies / 1000;
207 tv.tv_usec = (cMillies % 1000) * 1000;
208
209 sleepq_set_timeout(pThis, tvtohz(&tv));
210
211 RTSpinlockRelease(pThis->hSpinLock, &Tmp);
212
213 if (fInterruptible)
214 rc = SLEEPQ_TIMEDWAIT_SIG(pThis);
215 else
216 rc = SLEEPQ_TIMEDWAIT(pThis);
217 }
218 else
219 {
220 RTSpinlockRelease(pThis->hSpinLock, &Tmp);
221
222 if (fInterruptible)
223 rc = SLEEPQ_WAIT_SIG(pThis);
224 else
225 {
226 rc = 0;
227 SLEEPQ_WAIT(pThis);
228 }
229 }
230
231 RTSpinlockAcquire(pThis->hSpinLock, &Tmp);
232
233 switch (rc)
234 {
235 case 0:
236 if (pThis->u32Magic == RTSEMEVENT_MAGIC)
237 {
238 ASMAtomicDecU32(&pThis->cWaking);
239 rc = VINF_SUCCESS;
240 }
241 else
242 {
243 rc = VERR_SEM_DESTROYED; /** @todo this isn't necessarily correct, we've
244 * could've woken up just before destruction... */
245 if (!ASMAtomicDecU32(&pThis->cWaking))
246 {
247 /* The event was destroyed, as the last thread do the cleanup.
248 we don't actually know whether */
249 RTSpinlockRelease(pThis->hSpinLock, &Tmp);
250 RTSpinlockDestroy(pThis->hSpinLock);
251 RTMemFree(pThis);
252 return rc;
253 }
254 }
255 break;
256
257 case EWOULDBLOCK:
258 Assert(cMillies != RT_INDEFINITE_WAIT);
259 if (pThis->cWaiters > 0)
260 ASMAtomicDecU32(&pThis->cWaiters);
261 rc = VERR_TIMEOUT;
262 break;
263
264 case EINTR:
265 case ERESTART:
266 Assert(fInterruptible);
267 if (pThis->cWaiters > 0)
268 ASMAtomicDecU32(&pThis->cWaiters);
269 rc = VERR_INTERRUPTED;
270 break;
271
272 default:
273 AssertMsgFailed(("sleepq_* -> %d\n", rc));
274 rc = VERR_GENERAL_FAILURE;
275 break;
276 }
277 }
278 }
279
280 RTSpinlockRelease(pThis->hSpinLock, &Tmp);
281
282 return rc;
283}
284
285
286RTDECL(int) RTSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
287{
288 return rtSemEventWait(hEventSem, cMillies, false /* not interruptible */);
289}
290
291
292RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
293{
294 return rtSemEventWait(hEventSem, cMillies, true /* interruptible */);
295}
296
297
298RTDECL(uint32_t) RTSemEventGetResolution(void)
299{
300 return 1000000000 / hz;
301}
302
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette