VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/freebsd/semeventmulti-r0drv-freebsd.c@ 23454

Last change on this file since 23454 was 22819, checked in by vboxsync, 15 years ago

FreeBSD: Fix R0 semevent implementation for FreeBSD 7.x

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.8 KB
Line 
1/* $Id: semeventmulti-r0drv-freebsd.c 22819 2009-09-07 19:10:55Z vboxsync $ */
2/** @file
3 * IPRT - Multiple 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* Structures and Typedefs *
47*******************************************************************************/
48/**
49 * FreeBSD multiple release event semaphore.
50 */
51typedef struct RTSEMEVENTMULTIINTERNAL
52{
53 /** Magic value (RTSEMEVENTMULTI_MAGIC). */
54 uint32_t volatile u32Magic;
55 /** The number of waiting threads. */
56 uint32_t volatile cWaiters;
57 /** Set if the event object is signaled. */
58 uint8_t volatile fSignaled;
59 /** The number of threads in the process of waking up. */
60 uint32_t volatile cWaking;
61 /** Spinlock protecting this structure. */
62 RTSPINLOCK hSpinLock;
63} RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
64
65
66RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI pEventMultiSem)
67{
68 Assert(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
69 AssertPtrReturn(pEventMultiSem, VERR_INVALID_POINTER);
70
71 PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)RTMemAllocZ(sizeof(*pEventMultiInt));
72 if (pEventMultiInt)
73 {
74 pEventMultiInt->u32Magic = RTSEMEVENTMULTI_MAGIC;
75 pEventMultiInt->cWaiters = 0;
76 pEventMultiInt->cWaking = 0;
77 pEventMultiInt->fSignaled = 0;
78 int rc = RTSpinlockCreate(&pEventMultiInt->hSpinLock);
79 if (RT_SUCCESS(rc))
80 {
81 *pEventMultiSem = pEventMultiInt;
82 return VINF_SUCCESS;
83 }
84
85 RTMemFree(pEventMultiInt);
86 return rc;
87 }
88 return VERR_NO_MEMORY;
89}
90
91
92RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI EventMultiSem)
93{
94 if (EventMultiSem == NIL_RTSEMEVENTMULTI) /* don't bitch */
95 return VERR_INVALID_HANDLE;
96 PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
97 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
98
99 AssertPtrReturn(pEventMultiInt, VERR_INVALID_HANDLE);
100 AssertMsgReturn(pEventMultiInt->u32Magic == RTSEMEVENTMULTI_MAGIC,
101 ("pEventMultiInt=%p u32Magic=%#x\n", pEventMultiInt, pEventMultiInt->u32Magic),
102 VERR_INVALID_HANDLE);
103
104 RTSpinlockAcquire(pEventMultiInt->hSpinLock, &Tmp);
105 ASMAtomicIncU32(&pEventMultiInt->u32Magic); /* make the handle invalid */
106 if (pEventMultiInt->cWaiters > 0)
107 {
108 /* abort waiting thread, last man cleans up. */
109 ASMAtomicXchgU32(&pEventMultiInt->cWaking, pEventMultiInt->cWaking + pEventMultiInt->cWaiters);
110 sleepq_lock(pEventMultiInt);
111 sleepq_broadcast(pEventMultiInt, SLEEPQ_CONDVAR, 0, 0);
112 sleepq_release(pEventMultiInt);
113 RTSpinlockRelease(pEventMultiInt->hSpinLock, &Tmp);
114 }
115 else if (pEventMultiInt->cWaking)
116 /* the last waking thread is gonna do the cleanup */
117 RTSpinlockRelease(pEventMultiInt->hSpinLock, &Tmp);
118 else
119 {
120 RTSpinlockRelease(pEventMultiInt->hSpinLock, &Tmp);
121 RTSpinlockDestroy(pEventMultiInt->hSpinLock);
122 RTMemFree(pEventMultiInt);
123 }
124
125 return VINF_SUCCESS;
126}
127
128
129RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI EventMultiSem)
130{
131 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
132 PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
133 AssertPtrReturn(pEventMultiInt, VERR_INVALID_HANDLE);
134 AssertMsgReturn(pEventMultiInt->u32Magic == RTSEMEVENTMULTI_MAGIC,
135 ("pEventMultiInt=%p u32Magic=%#x\n", pEventMultiInt, pEventMultiInt->u32Magic),
136 VERR_INVALID_HANDLE);
137
138 RTSpinlockAcquire(pEventMultiInt->hSpinLock, &Tmp);
139
140 ASMAtomicXchgU8(&pEventMultiInt->fSignaled, true);
141 if (pEventMultiInt->cWaiters > 0)
142 {
143 ASMAtomicXchgU32(&pEventMultiInt->cWaking, pEventMultiInt->cWaking + pEventMultiInt->cWaiters);
144 ASMAtomicXchgU32(&pEventMultiInt->cWaiters, 0);
145 sleepq_lock(pEventMultiInt);
146 int fWakeupSwapProc = sleepq_signal(pEventMultiInt, SLEEPQ_CONDVAR, 0, 0);
147 sleepq_release(pEventMultiInt);
148 if (fWakeupSwapProc)
149 kick_proc0();
150 }
151
152 RTSpinlockRelease(pEventMultiInt->hSpinLock, &Tmp);
153 return VINF_SUCCESS;
154}
155
156
157RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI EventMultiSem)
158{
159 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
160 PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
161 AssertPtrReturn(pEventMultiInt, VERR_INVALID_HANDLE);
162 AssertMsgReturn(pEventMultiInt->u32Magic == RTSEMEVENTMULTI_MAGIC,
163 ("pEventMultiInt=%p u32Magic=%#x\n", pEventMultiInt, pEventMultiInt->u32Magic),
164 VERR_INVALID_HANDLE);
165
166 RTSpinlockAcquire(pEventMultiInt->hSpinLock, &Tmp);
167 ASMAtomicXchgU8(&pEventMultiInt->fSignaled, false);
168 RTSpinlockRelease(pEventMultiInt->hSpinLock, &Tmp);
169 return VINF_SUCCESS;
170}
171
172
173static int rtSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies, bool fInterruptible)
174{
175 int rc;
176 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
177 PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
178 AssertPtrReturn(pEventMultiInt, VERR_INVALID_HANDLE);
179 AssertMsgReturn(pEventMultiInt->u32Magic == RTSEMEVENTMULTI_MAGIC,
180 ("pEventMultiInt=%p u32Magic=%#x\n", pEventMultiInt, pEventMultiInt->u32Magic),
181 VERR_INVALID_HANDLE);
182
183 RTSpinlockAcquire(pEventMultiInt->hSpinLock, &Tmp);
184
185 if (pEventMultiInt->fSignaled)
186 rc = VINF_SUCCESS;
187 else
188 {
189 if (cMillies == 0)
190 rc = VERR_TIMEOUT;
191 else
192 {
193 ASMAtomicIncU32(&pEventMultiInt->cWaiters);
194
195 int fFlags = SLEEPQ_CONDVAR;
196
197 if (fInterruptible)
198 fFlags |= SLEEPQ_INTERRUPTIBLE;
199
200 sleepq_lock(pEventMultiInt);
201 sleepq_add(pEventMultiInt, NULL, "IPRT Event Semaphore", fFlags, 0);
202
203 if (cMillies != RT_INDEFINITE_WAIT)
204 {
205 /*
206 * Translate milliseconds into ticks and go to sleep.
207 */
208 struct timeval tv;
209
210 tv.tv_sec = cMillies / 1000;
211 tv.tv_usec = (cMillies % 1000) * 1000;
212
213 sleepq_set_timeout(pEventMultiInt, tvtohz(&tv));
214
215 RTSpinlockRelease(pEventMultiInt->hSpinLock, &Tmp);
216
217 if (fInterruptible)
218 rc = SLEEPQ_TIMEDWAIT_SIG(pEventMultiInt);
219 else
220 rc = SLEEPQ_TIMEDWAIT(pEventMultiInt);
221 }
222 else
223 {
224 RTSpinlockRelease(pEventMultiInt->hSpinLock, &Tmp);
225
226 if (fInterruptible)
227 rc = SLEEPQ_WAIT_SIG(pEventMultiInt);
228 else
229 {
230 rc = 0;
231 SLEEPQ_WAIT(pEventMultiInt);
232 }
233 }
234
235 RTSpinlockAcquire(pEventMultiInt->hSpinLock, &Tmp);
236
237 switch (rc)
238 {
239 case 0:
240 if (pEventMultiInt->u32Magic == RTSEMEVENTMULTI_MAGIC)
241 {
242 ASMAtomicDecU32(&pEventMultiInt->cWaking);
243 rc = VINF_SUCCESS;
244 }
245 else
246 {
247 rc = VERR_SEM_DESTROYED; /** @todo this isn't necessarily correct, we've
248 * could've woken up just before destruction... */
249 if (!ASMAtomicDecU32(&pEventMultiInt->cWaking))
250 {
251 /* The event was destroyed, as the last thread do the cleanup.
252 we don't actually know whether */
253 RTSpinlockRelease(pEventMultiInt->hSpinLock, &Tmp);
254 RTSpinlockDestroy(pEventMultiInt->hSpinLock);
255 RTMemFree(pEventMultiInt);
256 return rc;
257 }
258 }
259 break;
260
261 case EWOULDBLOCK:
262 Assert(cMillies != RT_INDEFINITE_WAIT);
263 if (pEventMultiInt->cWaiters > 0)
264 ASMAtomicDecU32(&pEventMultiInt->cWaiters);
265 rc = VERR_TIMEOUT;
266 break;
267
268 case EINTR:
269 case ERESTART:
270 Assert(fInterruptible);
271 if (pEventMultiInt->cWaiters > 0)
272 ASMAtomicDecU32(&pEventMultiInt->cWaiters);
273 rc = VERR_INTERRUPTED;
274 break;
275
276 default:
277 AssertMsgFailed(("sleepq_* -> %d\n", rc));
278 rc = VERR_GENERAL_FAILURE;
279 break;
280 }
281 }
282 }
283
284 RTSpinlockRelease(pEventMultiInt->hSpinLock, &Tmp);
285 return rc;
286}
287
288
289RTDECL(int) RTSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
290{
291 return rtSemEventMultiWait(EventMultiSem, cMillies, false /* not interruptible */);
292}
293
294
295RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
296{
297 return rtSemEventMultiWait(EventMultiSem, cMillies, true /* interruptible */);
298}
299
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