1 | /* $Id: semeventmulti-r0drv-solaris.c 29284 2010-05-10 00:22:16Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Multiple Release Event Semaphores, Ring-0 Driver, Solaris.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
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 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #include "the-solaris-kernel.h"
|
---|
32 | #include "internal/iprt.h"
|
---|
33 | #include <iprt/semaphore.h>
|
---|
34 |
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/asm.h>
|
---|
37 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
38 | # include <iprt/asm-amd64-x86.h>
|
---|
39 | #endif
|
---|
40 | #include <iprt/err.h>
|
---|
41 | #include <iprt/mem.h>
|
---|
42 | #include <iprt/mp.h>
|
---|
43 | #include <iprt/thread.h>
|
---|
44 | #include "internal/magics.h"
|
---|
45 |
|
---|
46 |
|
---|
47 | /*******************************************************************************
|
---|
48 | * Structures and Typedefs *
|
---|
49 | *******************************************************************************/
|
---|
50 | /**
|
---|
51 | * FreeBSD multiple release event semaphore.
|
---|
52 | */
|
---|
53 | typedef struct RTSEMEVENTMULTIINTERNAL
|
---|
54 | {
|
---|
55 | /** Magic value (RTSEMEVENTMULTI_MAGIC). */
|
---|
56 | uint32_t volatile u32Magic;
|
---|
57 | /** The number of waiting threads. */
|
---|
58 | uint32_t volatile cWaiters;
|
---|
59 | /** Set if the event object is signaled. */
|
---|
60 | uint8_t volatile fSignaled;
|
---|
61 | /** The number of threads in the process of waking up. */
|
---|
62 | uint32_t volatile cWaking;
|
---|
63 | /** The Solaris mutex protecting this structure and pairing up the with the cv. */
|
---|
64 | kmutex_t Mtx;
|
---|
65 | /** The Solaris condition variable. */
|
---|
66 | kcondvar_t Cnd;
|
---|
67 | } RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
|
---|
68 |
|
---|
69 |
|
---|
70 |
|
---|
71 | RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI phEventMultiSem)
|
---|
72 | {
|
---|
73 | return RTSemEventMultiCreateEx(phEventMultiSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | RTDECL(int) RTSemEventMultiCreateEx(PRTSEMEVENTMULTI phEventMultiSem, uint32_t fFlags, RTLOCKVALCLASS hClass,
|
---|
78 | const char *pszNameFmt, ...)
|
---|
79 | {
|
---|
80 | AssertReturn(!(fFlags & ~RTSEMEVENTMULTI_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
|
---|
81 | AssertPtrReturn(phEventMultiSem, VERR_INVALID_POINTER);
|
---|
82 | RT_ASSERT_PREEMPTIBLE();
|
---|
83 |
|
---|
84 | AssertCompile(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
|
---|
85 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
86 | if (pThis)
|
---|
87 | {
|
---|
88 | pThis->u32Magic = RTSEMEVENTMULTI_MAGIC;
|
---|
89 | pThis->cWaiters = 0;
|
---|
90 | pThis->cWaking = 0;
|
---|
91 | pThis->fSignaled = 0;
|
---|
92 | mutex_init(&pThis->Mtx, "IPRT Multiple Release Event Semaphore", MUTEX_DRIVER, (void *)ipltospl(DISP_LEVEL));
|
---|
93 | cv_init(&pThis->Cnd, "IPRT CV", CV_DRIVER, NULL);
|
---|
94 |
|
---|
95 | *phEventMultiSem = pThis;
|
---|
96 | return VINF_SUCCESS;
|
---|
97 | }
|
---|
98 | return VERR_NO_MEMORY;
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI hEventMultiSem)
|
---|
103 | {
|
---|
104 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
105 | if (pThis == NIL_RTSEMEVENTMULTI)
|
---|
106 | return VINF_SUCCESS;
|
---|
107 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
108 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
109 | RT_ASSERT_INTS_ON();
|
---|
110 |
|
---|
111 | mutex_enter(&pThis->Mtx);
|
---|
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 | cv_broadcast(&pThis->Cnd);
|
---|
118 | mutex_exit(&pThis->Mtx);
|
---|
119 | }
|
---|
120 | else if (pThis->cWaking)
|
---|
121 | /* the last waking thread is gonna do the cleanup */
|
---|
122 | mutex_exit(&pThis->Mtx);
|
---|
123 | else
|
---|
124 | {
|
---|
125 | mutex_exit(&pThis->Mtx);
|
---|
126 | cv_destroy(&pThis->Cnd);
|
---|
127 | mutex_destroy(&pThis->Mtx);
|
---|
128 | RTMemFree(pThis);
|
---|
129 | }
|
---|
130 |
|
---|
131 | return VINF_SUCCESS;
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI hEventMultiSem)
|
---|
136 | {
|
---|
137 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
138 | RT_ASSERT_PREEMPT_CPUID_VAR();
|
---|
139 |
|
---|
140 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
141 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
142 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
143 | VERR_INVALID_HANDLE);
|
---|
144 | RT_ASSERT_INTS_ON();
|
---|
145 |
|
---|
146 | /*
|
---|
147 | * If we're in interrupt context we need to unpin the underlying current
|
---|
148 | * thread as this could lead to a deadlock (see #4259 for the full explanation)
|
---|
149 | *
|
---|
150 | * Note! See remarks about preemption in RTSemEventSignal.
|
---|
151 | */
|
---|
152 | int fAcquired = mutex_tryenter(&pThis->Mtx);
|
---|
153 | if (!fAcquired)
|
---|
154 | {
|
---|
155 | if (curthread->t_intr && getpil() < DISP_LEVEL)
|
---|
156 | {
|
---|
157 | RTTHREADPREEMPTSTATE PreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
|
---|
158 | RTThreadPreemptDisable(&PreemptState);
|
---|
159 | preempt();
|
---|
160 | RTThreadPreemptRestore(&PreemptState);
|
---|
161 | }
|
---|
162 | mutex_enter(&pThis->Mtx);
|
---|
163 | }
|
---|
164 |
|
---|
165 | ASMAtomicXchgU8(&pThis->fSignaled, true);
|
---|
166 | if (pThis->cWaiters > 0)
|
---|
167 | {
|
---|
168 | ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
|
---|
169 | ASMAtomicXchgU32(&pThis->cWaiters, 0);
|
---|
170 | cv_broadcast(&pThis->Cnd);
|
---|
171 | }
|
---|
172 |
|
---|
173 | mutex_exit(&pThis->Mtx);
|
---|
174 |
|
---|
175 | RT_ASSERT_PREEMPT_CPUID();
|
---|
176 | return VINF_SUCCESS;
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem)
|
---|
181 | {
|
---|
182 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
183 | RT_ASSERT_PREEMPT_CPUID_VAR();
|
---|
184 |
|
---|
185 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
186 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
187 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
188 | VERR_INVALID_HANDLE);
|
---|
189 | RT_ASSERT_INTS_ON();
|
---|
190 |
|
---|
191 | /*
|
---|
192 | * If we're in interrupt context we need to unpin the underlying current
|
---|
193 | * thread as this could lead to a deadlock (see #4259 for the full explanation)
|
---|
194 | *
|
---|
195 | * Note! See remarks about preemption in RTSemEventSignal.
|
---|
196 | */
|
---|
197 | int fAcquired = mutex_tryenter(&pThis->Mtx);
|
---|
198 | if (!fAcquired)
|
---|
199 | {
|
---|
200 | if (curthread->t_intr && getpil() < DISP_LEVEL)
|
---|
201 | {
|
---|
202 | RTTHREADPREEMPTSTATE PreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
|
---|
203 | RTThreadPreemptDisable(&PreemptState);
|
---|
204 | preempt();
|
---|
205 | RTThreadPreemptRestore(&PreemptState);
|
---|
206 | }
|
---|
207 | mutex_enter(&pThis->Mtx);
|
---|
208 | }
|
---|
209 |
|
---|
210 | ASMAtomicXchgU8(&pThis->fSignaled, false);
|
---|
211 | mutex_exit(&pThis->Mtx);
|
---|
212 |
|
---|
213 | RT_ASSERT_PREEMPT_CPUID();
|
---|
214 | return VINF_SUCCESS;
|
---|
215 | }
|
---|
216 |
|
---|
217 |
|
---|
218 | static int rtSemEventMultiWait(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies, bool fInterruptible)
|
---|
219 | {
|
---|
220 | int rc;
|
---|
221 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
222 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
223 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
224 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
225 | VERR_INVALID_HANDLE);
|
---|
226 | if (cMillies)
|
---|
227 | RT_ASSERT_PREEMPTIBLE();
|
---|
228 |
|
---|
229 | mutex_enter(&pThis->Mtx);
|
---|
230 |
|
---|
231 | if (pThis->fSignaled)
|
---|
232 | rc = VINF_SUCCESS;
|
---|
233 | else if (!cMillies)
|
---|
234 | rc = VERR_TIMEOUT;
|
---|
235 | else
|
---|
236 | {
|
---|
237 | ASMAtomicIncU32(&pThis->cWaiters);
|
---|
238 |
|
---|
239 | /*
|
---|
240 | * Translate milliseconds into ticks and go to sleep.
|
---|
241 | */
|
---|
242 | if (cMillies != RT_INDEFINITE_WAIT)
|
---|
243 | {
|
---|
244 | clock_t cTicks = drv_usectohz((clock_t)(cMillies * 1000L));
|
---|
245 | clock_t cTimeout = ddi_get_lbolt();
|
---|
246 | cTimeout += cTicks;
|
---|
247 | if (fInterruptible)
|
---|
248 | rc = cv_timedwait_sig(&pThis->Cnd, &pThis->Mtx, cTimeout);
|
---|
249 | else
|
---|
250 | rc = cv_timedwait(&pThis->Cnd, &pThis->Mtx, cTimeout);
|
---|
251 | }
|
---|
252 | else
|
---|
253 | {
|
---|
254 | if (fInterruptible)
|
---|
255 | rc = cv_wait_sig(&pThis->Cnd, &pThis->Mtx);
|
---|
256 | else
|
---|
257 | {
|
---|
258 | cv_wait(&pThis->Cnd, &pThis->Mtx);
|
---|
259 | rc = 1;
|
---|
260 | }
|
---|
261 | }
|
---|
262 | if (rc > 0)
|
---|
263 | {
|
---|
264 | /* Retured due to call to cv_signal() or cv_broadcast() */
|
---|
265 | if (RT_LIKELY(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC))
|
---|
266 | {
|
---|
267 | rc = VINF_SUCCESS;
|
---|
268 | ASMAtomicDecU32(&pThis->cWaking);
|
---|
269 | }
|
---|
270 | else
|
---|
271 | {
|
---|
272 | rc = VERR_SEM_DESTROYED;
|
---|
273 | if (!ASMAtomicDecU32(&pThis->cWaking))
|
---|
274 | {
|
---|
275 | mutex_exit(&pThis->Mtx);
|
---|
276 | cv_destroy(&pThis->Cnd);
|
---|
277 | mutex_destroy(&pThis->Mtx);
|
---|
278 | RTMemFree(pThis);
|
---|
279 | return rc;
|
---|
280 | }
|
---|
281 | }
|
---|
282 | }
|
---|
283 | else if (rc == -1)
|
---|
284 | {
|
---|
285 | /* Returned due to timeout being reached */
|
---|
286 | if (pThis->cWaiters > 0)
|
---|
287 | ASMAtomicDecU32(&pThis->cWaiters);
|
---|
288 | rc = VERR_TIMEOUT;
|
---|
289 | }
|
---|
290 | else
|
---|
291 | {
|
---|
292 | /* Returned due to pending signal */
|
---|
293 | if (pThis->cWaiters > 0)
|
---|
294 | ASMAtomicDecU32(&pThis->cWaiters);
|
---|
295 | rc = VERR_INTERRUPTED;
|
---|
296 | }
|
---|
297 | }
|
---|
298 |
|
---|
299 | mutex_exit(&pThis->Mtx);
|
---|
300 | return rc;
|
---|
301 | }
|
---|
302 |
|
---|
303 |
|
---|
304 | RTDECL(int) RTSemEventMultiWait(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies)
|
---|
305 | {
|
---|
306 | return rtSemEventMultiWait(hEventMultiSem, cMillies, false /* not interruptible */);
|
---|
307 | }
|
---|
308 |
|
---|
309 |
|
---|
310 | RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies)
|
---|
311 | {
|
---|
312 | return rtSemEventMultiWait(hEventMultiSem, cMillies, true /* interruptible */);
|
---|
313 | }
|
---|
314 |
|
---|