1 | /* $Id: semeventmulti-r0drv-solaris.c 30711 2010-07-07 15:54:20Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Multiple Release Event Semaphores, Ring-0 Driver, Solaris.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.alldomusa.eu.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 | * Solaris multiple release event semaphore.
|
---|
52 | */
|
---|
53 | typedef struct RTSEMEVENTMULTIINTERNAL
|
---|
54 | {
|
---|
55 | /** Magic value (RTSEMEVENTMULTI_MAGIC). */
|
---|
56 | uint32_t volatile u32Magic;
|
---|
57 | /** The number of references. */
|
---|
58 | int32_t volatile cRefs;
|
---|
59 | /** Set if the event object is signaled. */
|
---|
60 | bool fSignaled;
|
---|
61 | /** Object generation.
|
---|
62 | * This is incremented every time the object is signalled and used to
|
---|
63 | * check for spurious wake-ups. */
|
---|
64 | uint32_t uSignalGen;
|
---|
65 | /** The Solaris mutex protecting this structure and pairing up the with the cv. */
|
---|
66 | kmutex_t Mtx;
|
---|
67 | /** The Solaris condition variable. */
|
---|
68 | kcondvar_t Cnd;
|
---|
69 | } RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
|
---|
70 |
|
---|
71 |
|
---|
72 |
|
---|
73 | RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI phEventMultiSem)
|
---|
74 | {
|
---|
75 | return RTSemEventMultiCreateEx(phEventMultiSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | RTDECL(int) RTSemEventMultiCreateEx(PRTSEMEVENTMULTI phEventMultiSem, uint32_t fFlags, RTLOCKVALCLASS hClass,
|
---|
80 | const char *pszNameFmt, ...)
|
---|
81 | {
|
---|
82 | AssertReturn(!(fFlags & ~RTSEMEVENTMULTI_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
|
---|
83 | AssertPtrReturn(phEventMultiSem, VERR_INVALID_POINTER);
|
---|
84 | RT_ASSERT_PREEMPTIBLE();
|
---|
85 |
|
---|
86 | AssertCompile(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
|
---|
87 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
88 | if (pThis)
|
---|
89 | {
|
---|
90 | pThis->u32Magic = RTSEMEVENTMULTI_MAGIC;
|
---|
91 | pThis->cRefs = 1;
|
---|
92 | pThis->fSignaled = false;
|
---|
93 | pThis->uSignalGen = 0;
|
---|
94 | mutex_init(&pThis->Mtx, "IPRT Multiple Release Event Semaphore", MUTEX_DRIVER, (void *)ipltospl(DISP_LEVEL));
|
---|
95 | cv_init(&pThis->Cnd, "IPRT CV", CV_DRIVER, NULL);
|
---|
96 |
|
---|
97 | *phEventMultiSem = pThis;
|
---|
98 | return VINF_SUCCESS;
|
---|
99 | }
|
---|
100 | return VERR_NO_MEMORY;
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Destructor that is called when cRefs == 0.
|
---|
106 | * @param pThis The instance to destroy.
|
---|
107 | */
|
---|
108 | static void rtSemEventMultiDtor(PRTSEMEVENTMULTIINTERNAL pThis)
|
---|
109 | {
|
---|
110 | cv_destroy(&pThis->Cnd);
|
---|
111 | mutex_destroy(&pThis->Mtx);
|
---|
112 | RTMemFree(pThis);
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI hEventMultiSem)
|
---|
117 | {
|
---|
118 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
119 | if (pThis == NIL_RTSEMEVENTMULTI)
|
---|
120 | return VINF_SUCCESS;
|
---|
121 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
122 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
123 | AssertMsgReturn(pThis->cRefs > 0, ("pThis=%p cRefs=%d\n", pThis, pThis->cRefs), VERR_INVALID_HANDLE);
|
---|
124 | RT_ASSERT_INTS_ON();
|
---|
125 |
|
---|
126 | mutex_enter(&pThis->Mtx);
|
---|
127 |
|
---|
128 | /* Invalidate the handle and wake up all threads that might be waiting on the semaphore. */
|
---|
129 | Assert(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC);
|
---|
130 | pThis->u32Magic = RTSEMEVENTMULTI_MAGIC_DEAD;
|
---|
131 | cv_broadcast(&pThis->Cnd);
|
---|
132 |
|
---|
133 | /* Drop the reference from RTSemEventMultiCreateEx. */
|
---|
134 | if (ASMAtomicDecS32(&pThis->cRefs))
|
---|
135 | mutex_exit(&pThis->Mtx);
|
---|
136 | else
|
---|
137 | rtSemEventMultiDtor(pThis);
|
---|
138 | return VINF_SUCCESS;
|
---|
139 |
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI hEventMultiSem)
|
---|
144 | {
|
---|
145 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
146 | RT_ASSERT_PREEMPT_CPUID_VAR();
|
---|
147 |
|
---|
148 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
149 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
150 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
151 | VERR_INVALID_HANDLE);
|
---|
152 | RT_ASSERT_INTS_ON();
|
---|
153 |
|
---|
154 | /*
|
---|
155 | * If we're in interrupt context we need to unpin the underlying current
|
---|
156 | * thread as this could lead to a deadlock (see #4259 for the full explanation)
|
---|
157 | *
|
---|
158 | * Note! See remarks about preemption in RTSemEventSignal.
|
---|
159 | */
|
---|
160 | int fAcquired = mutex_tryenter(&pThis->Mtx);
|
---|
161 | if (!fAcquired)
|
---|
162 | {
|
---|
163 | if (curthread->t_intr && getpil() < DISP_LEVEL)
|
---|
164 | {
|
---|
165 | RTTHREADPREEMPTSTATE PreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
|
---|
166 | RTThreadPreemptDisable(&PreemptState);
|
---|
167 | preempt();
|
---|
168 | RTThreadPreemptRestore(&PreemptState);
|
---|
169 | }
|
---|
170 | mutex_enter(&pThis->Mtx);
|
---|
171 | }
|
---|
172 | Assert(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC);
|
---|
173 |
|
---|
174 | /*
|
---|
175 | * Do the job.
|
---|
176 | */
|
---|
177 | pThis->fSignaled = true;
|
---|
178 | pThis->uSignalGen++;
|
---|
179 | cv_broadcast(&pThis->Cnd);
|
---|
180 |
|
---|
181 | mutex_exit(&pThis->Mtx);
|
---|
182 |
|
---|
183 | RT_ASSERT_PREEMPT_CPUID();
|
---|
184 | return VINF_SUCCESS;
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 | RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem)
|
---|
189 | {
|
---|
190 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
191 | RT_ASSERT_PREEMPT_CPUID_VAR();
|
---|
192 |
|
---|
193 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
194 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
195 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
196 | VERR_INVALID_HANDLE);
|
---|
197 | RT_ASSERT_INTS_ON();
|
---|
198 |
|
---|
199 | /*
|
---|
200 | * If we're in interrupt context we need to unpin the underlying current
|
---|
201 | * thread as this could lead to a deadlock (see #4259 for the full explanation)
|
---|
202 | *
|
---|
203 | * Note! See remarks about preemption in RTSemEventSignal.
|
---|
204 | */
|
---|
205 | int fAcquired = mutex_tryenter(&pThis->Mtx);
|
---|
206 | if (!fAcquired)
|
---|
207 | {
|
---|
208 | if (curthread->t_intr && getpil() < DISP_LEVEL)
|
---|
209 | {
|
---|
210 | RTTHREADPREEMPTSTATE PreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
|
---|
211 | RTThreadPreemptDisable(&PreemptState);
|
---|
212 | preempt();
|
---|
213 | RTThreadPreemptRestore(&PreemptState);
|
---|
214 | }
|
---|
215 | mutex_enter(&pThis->Mtx);
|
---|
216 | }
|
---|
217 | Assert(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC);
|
---|
218 |
|
---|
219 | /*
|
---|
220 | * Do the job.
|
---|
221 | */
|
---|
222 | pThis->fSignaled = false;
|
---|
223 |
|
---|
224 | mutex_exit(&pThis->Mtx);
|
---|
225 |
|
---|
226 | RT_ASSERT_PREEMPT_CPUID();
|
---|
227 | return VINF_SUCCESS;
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * Translate milliseconds into ticks and go to sleep using the right method.
|
---|
233 | *
|
---|
234 | * @retval >0 on normal or spurious wake-up.
|
---|
235 | * @retval -1 on timeout.
|
---|
236 | * @retval 0 on signal.
|
---|
237 | */
|
---|
238 | static int rtSemEventMultiWaitWorker(PRTSEMEVENTMULTIINTERNAL pThis, RTMSINTERVAL cMillies, bool fInterruptible)
|
---|
239 | {
|
---|
240 | int rc;
|
---|
241 | if (cMillies != RT_INDEFINITE_WAIT)
|
---|
242 | {
|
---|
243 | clock_t cTicks = drv_usectohz((clock_t)(cMillies * 1000L));
|
---|
244 | clock_t cTimeout = ddi_get_lbolt();
|
---|
245 | cTimeout += cTicks;
|
---|
246 | if (fInterruptible)
|
---|
247 | rc = cv_timedwait_sig(&pThis->Cnd, &pThis->Mtx, cTimeout);
|
---|
248 | else
|
---|
249 | rc = cv_timedwait(&pThis->Cnd, &pThis->Mtx, cTimeout);
|
---|
250 | }
|
---|
251 | else
|
---|
252 | {
|
---|
253 | if (fInterruptible)
|
---|
254 | rc = cv_wait_sig(&pThis->Cnd, &pThis->Mtx);
|
---|
255 | else
|
---|
256 | {
|
---|
257 | cv_wait(&pThis->Cnd, &pThis->Mtx);
|
---|
258 | rc = 1;
|
---|
259 | }
|
---|
260 | }
|
---|
261 | return rc;
|
---|
262 | }
|
---|
263 |
|
---|
264 |
|
---|
265 | static int rtSemEventMultiWait(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies, bool fInterruptible)
|
---|
266 | {
|
---|
267 | int rc;
|
---|
268 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
269 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
270 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
271 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
272 | VERR_INVALID_HANDLE);
|
---|
273 | if (cMillies)
|
---|
274 | RT_ASSERT_PREEMPTIBLE();
|
---|
275 |
|
---|
276 | mutex_enter(&pThis->Mtx);
|
---|
277 | Assert(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC);
|
---|
278 | ASMAtomicIncS32(&pThis->cRefs);
|
---|
279 |
|
---|
280 | if (pThis->fSignaled)
|
---|
281 | rc = VINF_SUCCESS;
|
---|
282 | else if (!cMillies)
|
---|
283 | rc = VERR_TIMEOUT;
|
---|
284 | else
|
---|
285 | {
|
---|
286 | /* This loop is only for continuing after a spurious wake-up. */
|
---|
287 | for (;;)
|
---|
288 | {
|
---|
289 | uint32_t const uSignalGenBeforeWait = pThis->uSignalGen;
|
---|
290 | rc = rtSemEventMultiWaitWorker(pThis, cMillies, fInterruptible);
|
---|
291 | if (rc > 0)
|
---|
292 | {
|
---|
293 | if (RT_LIKELY(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC))
|
---|
294 | {
|
---|
295 | if (pThis->uSignalGen == uSignalGenBeforeWait)
|
---|
296 | continue; /* Spurious wake-up, go back to waiting. */
|
---|
297 |
|
---|
298 | /* Retured due to call to cv_signal() or cv_broadcast(). */
|
---|
299 | rc = VINF_SUCCESS;
|
---|
300 | }
|
---|
301 | else
|
---|
302 | /* We're being destroyed. */
|
---|
303 | rc = VERR_SEM_DESTROYED;
|
---|
304 | }
|
---|
305 | else if (rc == -1)
|
---|
306 | /* Returned due to timeout being reached. */
|
---|
307 | rc = VERR_TIMEOUT;
|
---|
308 | else
|
---|
309 | rc = VERR_INTERRUPTED;
|
---|
310 | /* Returned due to pending signal. */
|
---|
311 | break;
|
---|
312 | }
|
---|
313 | }
|
---|
314 |
|
---|
315 | if (RT_LIKELY(ASMAtomicDecS32(&pThis->cRefs)))
|
---|
316 | mutex_exit(&pThis->Mtx);
|
---|
317 | else
|
---|
318 | rtSemEventMultiDtor(pThis);
|
---|
319 | return rc;
|
---|
320 | }
|
---|
321 |
|
---|
322 |
|
---|
323 | RTDECL(int) RTSemEventMultiWait(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies)
|
---|
324 | {
|
---|
325 | return rtSemEventMultiWait(hEventMultiSem, cMillies, false /* not interruptible */);
|
---|
326 | }
|
---|
327 |
|
---|
328 |
|
---|
329 | RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies)
|
---|
330 | {
|
---|
331 | return rtSemEventMultiWait(hEventMultiSem, cMillies, true /* interruptible */);
|
---|
332 | }
|
---|
333 |
|
---|