1 | /* $Id: semeventmulti-r0drv-solaris.c 25724 2010-01-11 14:45:34Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Multiple Release Event Semaphores, Ring-0 Driver, Solaris.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | *
|
---|
26 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include "the-solaris-kernel.h"
|
---|
36 | #include "internal/iprt.h"
|
---|
37 | #include <iprt/semaphore.h>
|
---|
38 |
|
---|
39 | #include <iprt/assert.h>
|
---|
40 | #include <iprt/asm.h>
|
---|
41 | #include <iprt/err.h>
|
---|
42 | #include <iprt/mem.h>
|
---|
43 | #include <iprt/mp.h>
|
---|
44 | #include <iprt/thread.h>
|
---|
45 | #include "internal/magics.h"
|
---|
46 |
|
---|
47 |
|
---|
48 | /*******************************************************************************
|
---|
49 | * Structures and Typedefs *
|
---|
50 | *******************************************************************************/
|
---|
51 | /**
|
---|
52 | * FreeBSD multiple release event semaphore.
|
---|
53 | */
|
---|
54 | typedef struct RTSEMEVENTMULTIINTERNAL
|
---|
55 | {
|
---|
56 | /** Magic value (RTSEMEVENTMULTI_MAGIC). */
|
---|
57 | uint32_t volatile u32Magic;
|
---|
58 | /** The number of waiting threads. */
|
---|
59 | uint32_t volatile cWaiters;
|
---|
60 | /** Set if the event object is signaled. */
|
---|
61 | uint8_t volatile fSignaled;
|
---|
62 | /** The number of threads in the process of waking up. */
|
---|
63 | uint32_t volatile cWaking;
|
---|
64 | /** The Solaris mutex protecting this structure and pairing up the with the cv. */
|
---|
65 | kmutex_t Mtx;
|
---|
66 | /** The Solaris condition variable. */
|
---|
67 | kcondvar_t Cnd;
|
---|
68 | } RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
|
---|
69 |
|
---|
70 |
|
---|
71 |
|
---|
72 | RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI phEventMultiSem)
|
---|
73 | {
|
---|
74 | return RTSemEventMultiCreateEx(phEventMultiSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | RTDECL(int) RTSemEventMultiCreateEx(PRTSEMEVENTMULTI phEventMultiSem, uint32_t fFlags, RTLOCKVALCLASS hClass,
|
---|
79 | const char *pszNameFmt, ...)
|
---|
80 | {
|
---|
81 | AssertReturn(!(fFlags & ~RTSEMEVENTMULTI_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
|
---|
82 | AssertPtrReturn(phEventMultiSem, VERR_INVALID_POINTER);
|
---|
83 | RT_ASSERT_PREEMPTIBLE();
|
---|
84 |
|
---|
85 | AssertCompile(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
|
---|
86 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
87 | if (pThis)
|
---|
88 | {
|
---|
89 | pThis->u32Magic = RTSEMEVENTMULTI_MAGIC;
|
---|
90 | pThis->cWaiters = 0;
|
---|
91 | pThis->cWaking = 0;
|
---|
92 | pThis->fSignaled = 0;
|
---|
93 | mutex_init(&pThis->Mtx, "IPRT Multiple Release Event Semaphore", MUTEX_DRIVER, (void *)ipltospl(DISP_LEVEL));
|
---|
94 | cv_init(&pThis->Cnd, "IPRT CV", CV_DRIVER, NULL);
|
---|
95 |
|
---|
96 | *phEventMultiSem = pThis;
|
---|
97 | return VINF_SUCCESS;
|
---|
98 | }
|
---|
99 | return VERR_NO_MEMORY;
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI hEventMultiSem)
|
---|
104 | {
|
---|
105 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
106 | if (pThis == NIL_RTSEMEVENTMULTI)
|
---|
107 | return VINF_SUCCESS;
|
---|
108 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
109 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
110 | RT_ASSERT_INTS_ON();
|
---|
111 |
|
---|
112 | mutex_enter(&pThis->Mtx);
|
---|
113 | ASMAtomicIncU32(&pThis->u32Magic); /* make the handle invalid */
|
---|
114 | if (pThis->cWaiters > 0)
|
---|
115 | {
|
---|
116 | /* abort waiting thread, last man cleans up. */
|
---|
117 | ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
|
---|
118 | cv_broadcast(&pThis->Cnd);
|
---|
119 | mutex_exit(&pThis->Mtx);
|
---|
120 | }
|
---|
121 | else if (pThis->cWaking)
|
---|
122 | /* the last waking thread is gonna do the cleanup */
|
---|
123 | mutex_exit(&pThis->Mtx);
|
---|
124 | else
|
---|
125 | {
|
---|
126 | mutex_exit(&pThis->Mtx);
|
---|
127 | cv_destroy(&pThis->Cnd);
|
---|
128 | mutex_destroy(&pThis->Mtx);
|
---|
129 | RTMemFree(pThis);
|
---|
130 | }
|
---|
131 |
|
---|
132 | return VINF_SUCCESS;
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI hEventMultiSem)
|
---|
137 | {
|
---|
138 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
139 | RT_ASSERT_PREEMPT_CPUID_VAR();
|
---|
140 |
|
---|
141 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
142 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
143 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
144 | VERR_INVALID_HANDLE);
|
---|
145 | RT_ASSERT_INTS_ON();
|
---|
146 |
|
---|
147 | /*
|
---|
148 | * If we're in interrupt context we need to unpin the underlying current
|
---|
149 | * thread as this could lead to a deadlock (see #4259 for the full explanation)
|
---|
150 | *
|
---|
151 | * Note! See remarks about preemption in RTSemEventSignal.
|
---|
152 | */
|
---|
153 | int fAcquired = mutex_tryenter(&pThis->Mtx);
|
---|
154 | if (!fAcquired)
|
---|
155 | {
|
---|
156 | if (curthread->t_intr && getpil() < DISP_LEVEL)
|
---|
157 | {
|
---|
158 | RTTHREADPREEMPTSTATE PreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
|
---|
159 | RTThreadPreemptDisable(&PreemptState);
|
---|
160 | preempt();
|
---|
161 | RTThreadPreemptRestore(&PreemptState);
|
---|
162 | }
|
---|
163 | mutex_enter(&pThis->Mtx);
|
---|
164 | }
|
---|
165 |
|
---|
166 | ASMAtomicXchgU8(&pThis->fSignaled, true);
|
---|
167 | if (pThis->cWaiters > 0)
|
---|
168 | {
|
---|
169 | ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
|
---|
170 | ASMAtomicXchgU32(&pThis->cWaiters, 0);
|
---|
171 | cv_broadcast(&pThis->Cnd);
|
---|
172 | }
|
---|
173 |
|
---|
174 | mutex_exit(&pThis->Mtx);
|
---|
175 |
|
---|
176 | RT_ASSERT_PREEMPT_CPUID();
|
---|
177 | return VINF_SUCCESS;
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem)
|
---|
182 | {
|
---|
183 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
184 | RT_ASSERT_PREEMPT_CPUID_VAR();
|
---|
185 |
|
---|
186 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
187 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
188 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
189 | VERR_INVALID_HANDLE);
|
---|
190 | RT_ASSERT_INTS_ON();
|
---|
191 |
|
---|
192 | /*
|
---|
193 | * If we're in interrupt context we need to unpin the underlying current
|
---|
194 | * thread as this could lead to a deadlock (see #4259 for the full explanation)
|
---|
195 | *
|
---|
196 | * Note! See remarks about preemption in RTSemEventSignal.
|
---|
197 | */
|
---|
198 | int fAcquired = mutex_tryenter(&pThis->Mtx);
|
---|
199 | if (!fAcquired)
|
---|
200 | {
|
---|
201 | if (curthread->t_intr && getpil() < DISP_LEVEL)
|
---|
202 | {
|
---|
203 | RTTHREADPREEMPTSTATE PreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
|
---|
204 | RTThreadPreemptDisable(&PreemptState);
|
---|
205 | preempt();
|
---|
206 | RTThreadPreemptRestore(&PreemptState);
|
---|
207 | }
|
---|
208 | mutex_enter(&pThis->Mtx);
|
---|
209 | }
|
---|
210 |
|
---|
211 | ASMAtomicXchgU8(&pThis->fSignaled, false);
|
---|
212 | mutex_exit(&pThis->Mtx);
|
---|
213 |
|
---|
214 | RT_ASSERT_PREEMPT_CPUID();
|
---|
215 | return VINF_SUCCESS;
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | static int rtSemEventMultiWait(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies, bool fInterruptible)
|
---|
220 | {
|
---|
221 | int rc;
|
---|
222 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
223 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
224 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
225 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
226 | VERR_INVALID_HANDLE);
|
---|
227 | if (cMillies)
|
---|
228 | RT_ASSERT_PREEMPTIBLE();
|
---|
229 |
|
---|
230 | mutex_enter(&pThis->Mtx);
|
---|
231 |
|
---|
232 | if (pThis->fSignaled)
|
---|
233 | rc = VINF_SUCCESS;
|
---|
234 | else if (!cMillies)
|
---|
235 | rc = VERR_TIMEOUT;
|
---|
236 | else
|
---|
237 | {
|
---|
238 | ASMAtomicIncU32(&pThis->cWaiters);
|
---|
239 |
|
---|
240 | /*
|
---|
241 | * Translate milliseconds into ticks and go to sleep.
|
---|
242 | */
|
---|
243 | if (cMillies != RT_INDEFINITE_WAIT)
|
---|
244 | {
|
---|
245 | clock_t cTicks = drv_usectohz((clock_t)(cMillies * 1000L));
|
---|
246 | clock_t cTimeout = ddi_get_lbolt();
|
---|
247 | cTimeout += cTicks;
|
---|
248 | if (fInterruptible)
|
---|
249 | rc = cv_timedwait_sig(&pThis->Cnd, &pThis->Mtx, cTimeout);
|
---|
250 | else
|
---|
251 | rc = cv_timedwait(&pThis->Cnd, &pThis->Mtx, cTimeout);
|
---|
252 | }
|
---|
253 | else
|
---|
254 | {
|
---|
255 | if (fInterruptible)
|
---|
256 | rc = cv_wait_sig(&pThis->Cnd, &pThis->Mtx);
|
---|
257 | else
|
---|
258 | {
|
---|
259 | cv_wait(&pThis->Cnd, &pThis->Mtx);
|
---|
260 | rc = 1;
|
---|
261 | }
|
---|
262 | }
|
---|
263 | if (rc > 0)
|
---|
264 | {
|
---|
265 | /* Retured due to call to cv_signal() or cv_broadcast() */
|
---|
266 | if (RT_LIKELY(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC))
|
---|
267 | rc = VINF_SUCCESS;
|
---|
268 | else
|
---|
269 | {
|
---|
270 | rc = VERR_SEM_DESTROYED;
|
---|
271 | if (!ASMAtomicDecU32(&pThis->cWaking))
|
---|
272 | {
|
---|
273 | mutex_exit(&pThis->Mtx);
|
---|
274 | cv_destroy(&pThis->Cnd);
|
---|
275 | mutex_destroy(&pThis->Mtx);
|
---|
276 | RTMemFree(pThis);
|
---|
277 | return rc;
|
---|
278 | }
|
---|
279 | }
|
---|
280 | ASMAtomicDecU32(&pThis->cWaking);
|
---|
281 | }
|
---|
282 | else if (rc == -1)
|
---|
283 | {
|
---|
284 | /* Returned due to timeout being reached */
|
---|
285 | if (pThis->cWaiters > 0)
|
---|
286 | ASMAtomicDecU32(&pThis->cWaiters);
|
---|
287 | rc = VERR_TIMEOUT;
|
---|
288 | }
|
---|
289 | else
|
---|
290 | {
|
---|
291 | /* Returned due to pending signal */
|
---|
292 | if (pThis->cWaiters > 0)
|
---|
293 | ASMAtomicDecU32(&pThis->cWaiters);
|
---|
294 | rc = VERR_INTERRUPTED;
|
---|
295 | }
|
---|
296 | }
|
---|
297 |
|
---|
298 | mutex_exit(&pThis->Mtx);
|
---|
299 | return rc;
|
---|
300 | }
|
---|
301 |
|
---|
302 |
|
---|
303 | RTDECL(int) RTSemEventMultiWait(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies)
|
---|
304 | {
|
---|
305 | return rtSemEventMultiWait(hEventMultiSem, cMillies, false /* not interruptible */);
|
---|
306 | }
|
---|
307 |
|
---|
308 |
|
---|
309 | RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies)
|
---|
310 | {
|
---|
311 | return rtSemEventMultiWait(hEventMultiSem, cMillies, true /* interruptible */);
|
---|
312 | }
|
---|
313 |
|
---|