VirtualBox

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

Last change on this file since 25776 was 25724, checked in by vboxsync, 15 years ago

iprt: Use RTMSINTERVAL for timeouts. Fixed missing timeout underflow checks in two RTFileAioCtxWait implementations.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.4 KB
Line 
1/* $Id: semevent-r0drv-freebsd.c 25724 2010-01-11 14:45:34Z 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#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), VERR_INVALID_PARAMETER);
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->cWaiters = 0;
85 pThis->cWaking = 0;
86 pThis->fSignaled = 0;
87 int rc = RTSpinlockCreate(&pThis->hSpinLock);
88 if (RT_SUCCESS(rc))
89 {
90 *phEventSem = pThis;
91 return VINF_SUCCESS;
92 }
93
94 RTMemFree(pThis);
95 return rc;
96}
97
98
99RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
100{
101 PRTSEMEVENTINTERNAL pThis = hEventSem;
102 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
103
104 if (pThis == NIL_RTSEMEVENT)
105 return VINF_SUCCESS;
106 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
107 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
108
109 RTSpinlockAcquire(pThis->hSpinLock, &Tmp);
110
111 ASMAtomicIncU32(&pThis->u32Magic); /* make the handle invalid */
112 if (pThis->cWaiters > 0)
113 {
114 /* abort waiting thread, last man cleans up. */
115 ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
116 sleepq_lock(pThis);
117 sleepq_broadcast(pThis, SLEEPQ_CONDVAR, 0, 0);
118 sleepq_release(pThis);
119 RTSpinlockRelease(pThis->hSpinLock, &Tmp);
120 }
121 else if (pThis->cWaking)
122 /* the last waking thread is gonna do the cleanup */
123 RTSpinlockRelease(pThis->hSpinLock, &Tmp);
124 else
125 {
126 RTSpinlockRelease(pThis->hSpinLock, &Tmp);
127 RTSpinlockDestroy(pThis->hSpinLock);
128 RTMemFree(pThis);
129 }
130
131 return VINF_SUCCESS;
132}
133
134
135RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
136{
137 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
138 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
139 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
140 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC,
141 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
142 VERR_INVALID_HANDLE);
143
144 RTSpinlockAcquire(pThis->hSpinLock, &Tmp);
145
146 if (pThis->cWaiters > 0)
147 {
148 ASMAtomicDecU32(&pThis->cWaiters);
149 ASMAtomicIncU32(&pThis->cWaking);
150 sleepq_lock(pThis);
151 int fWakeupSwapProc = sleepq_signal(pThis, SLEEPQ_CONDVAR, 0, 0);
152 sleepq_release(pThis);
153 if (fWakeupSwapProc)
154 kick_proc0();
155 }
156 else
157 ASMAtomicXchgU8(&pThis->fSignaled, true);
158
159 RTSpinlockRelease(pThis->hSpinLock, &Tmp);
160 return VINF_SUCCESS;
161}
162
163
164static int rtSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies, bool fInterruptible)
165{
166 int rc;
167 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
168 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
169 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
170 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC,
171 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
172 VERR_INVALID_HANDLE);
173
174 RTSpinlockAcquire(pThis->hSpinLock, &Tmp);
175
176 if (pThis->fSignaled)
177 {
178 Assert(!pThis->cWaiters);
179 ASMAtomicXchgU8(&pThis->fSignaled, false);
180 rc = VINF_SUCCESS;
181 }
182 else
183 {
184 if (cMillies == 0)
185 rc = VERR_TIMEOUT;
186 else
187 {
188 ASMAtomicIncU32(&pThis->cWaiters);
189
190 int fFlags = SLEEPQ_CONDVAR;
191
192 if (fInterruptible)
193 fFlags |= SLEEPQ_INTERRUPTIBLE;
194
195 sleepq_lock(pThis);
196 sleepq_add(pThis, NULL, "IPRT Event Semaphore", fFlags, 0);
197
198 if (cMillies != RT_INDEFINITE_WAIT)
199 {
200 /*
201 * Translate milliseconds into ticks and go to sleep.
202 */
203 struct timeval tv;
204
205 tv.tv_sec = cMillies / 1000;
206 tv.tv_usec = (cMillies % 1000) * 1000;
207
208 sleepq_set_timeout(pThis, tvtohz(&tv));
209
210 RTSpinlockRelease(pThis->hSpinLock, &Tmp);
211
212 if (fInterruptible)
213 rc = SLEEPQ_TIMEDWAIT_SIG(pThis);
214 else
215 rc = SLEEPQ_TIMEDWAIT(pThis);
216 }
217 else
218 {
219 RTSpinlockRelease(pThis->hSpinLock, &Tmp);
220
221 if (fInterruptible)
222 rc = SLEEPQ_WAIT_SIG(pThis);
223 else
224 {
225 rc = 0;
226 SLEEPQ_WAIT(pThis);
227 }
228 }
229
230 RTSpinlockAcquire(pThis->hSpinLock, &Tmp);
231
232 switch (rc)
233 {
234 case 0:
235 if (pThis->u32Magic == RTSEMEVENT_MAGIC)
236 {
237 ASMAtomicDecU32(&pThis->cWaking);
238 rc = VINF_SUCCESS;
239 }
240 else
241 {
242 rc = VERR_SEM_DESTROYED; /** @todo this isn't necessarily correct, we've
243 * could've woken up just before destruction... */
244 if (!ASMAtomicDecU32(&pThis->cWaking))
245 {
246 /* The event was destroyed, as the last thread do the cleanup.
247 we don't actually know whether */
248 RTSpinlockRelease(pThis->hSpinLock, &Tmp);
249 RTSpinlockDestroy(pThis->hSpinLock);
250 RTMemFree(pThis);
251 return rc;
252 }
253 }
254 break;
255
256 case EWOULDBLOCK:
257 Assert(cMillies != RT_INDEFINITE_WAIT);
258 if (pThis->cWaiters > 0)
259 ASMAtomicDecU32(&pThis->cWaiters);
260 rc = VERR_TIMEOUT;
261 break;
262
263 case EINTR:
264 case ERESTART:
265 Assert(fInterruptible);
266 if (pThis->cWaiters > 0)
267 ASMAtomicDecU32(&pThis->cWaiters);
268 rc = VERR_INTERRUPTED;
269 break;
270
271 default:
272 AssertMsgFailed(("sleepq_* -> %d\n", rc));
273 rc = VERR_GENERAL_FAILURE;
274 break;
275 }
276 }
277 }
278
279 RTSpinlockRelease(pThis->hSpinLock, &Tmp);
280
281 return rc;
282}
283
284
285RTDECL(int) RTSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
286{
287 return rtSemEventWait(hEventSem, cMillies, false /* not interruptible */);
288}
289
290
291RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
292{
293 return rtSemEventWait(hEventSem, cMillies, true /* interruptible */);
294}
295
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