1 | /* $Id: semevent-r0drv-solaris.c 25724 2010-01-11 14:45:34Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - 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 | * Solaris event semaphore.
|
---|
53 | */
|
---|
54 | typedef struct RTSEMEVENTINTERNAL
|
---|
55 | {
|
---|
56 | /** Magic value (RTSEMEVENT_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 | } RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
|
---|
69 |
|
---|
70 |
|
---|
71 |
|
---|
72 | RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
|
---|
73 | {
|
---|
74 | return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
|
---|
79 | {
|
---|
80 | AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
|
---|
81 | AssertReturn(!(fFlags & ~RTSEMEVENT_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
|
---|
82 | AssertPtrReturn(phEventSem, VERR_INVALID_POINTER);
|
---|
83 | RT_ASSERT_PREEMPTIBLE();
|
---|
84 |
|
---|
85 | PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
86 | if (!pThis)
|
---|
87 | return VERR_NO_MEMORY;
|
---|
88 |
|
---|
89 | pThis->u32Magic = RTSEMEVENT_MAGIC;
|
---|
90 | pThis->cWaiters = 0;
|
---|
91 | pThis->cWaking = 0;
|
---|
92 | pThis->fSignaled = 0;
|
---|
93 | mutex_init(&pThis->Mtx, "IPRT Event Semaphore", MUTEX_DRIVER, (void *)ipltospl(DISP_LEVEL));
|
---|
94 | cv_init(&pThis->Cnd, "IPRT CV", CV_DRIVER, NULL);
|
---|
95 |
|
---|
96 | *phEventSem = pThis;
|
---|
97 | return VINF_SUCCESS;
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
|
---|
102 | {
|
---|
103 | PRTSEMEVENTINTERNAL pThis = hEventSem;
|
---|
104 | if (pThis == NIL_RTSEMEVENT)
|
---|
105 | return VINF_SUCCESS;
|
---|
106 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
107 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
|
---|
108 | RT_ASSERT_INTS_ON();
|
---|
109 |
|
---|
110 | mutex_enter(&pThis->Mtx);
|
---|
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 | cv_broadcast(&pThis->Cnd);
|
---|
117 | mutex_exit(&pThis->Mtx);
|
---|
118 | }
|
---|
119 | else if (pThis->cWaking)
|
---|
120 | {
|
---|
121 | /* the last waking thread is gonna do the cleanup */
|
---|
122 | mutex_exit(&pThis->Mtx);
|
---|
123 | }
|
---|
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) RTSemEventSignal(RTSEMEVENT hEventSem)
|
---|
137 | {
|
---|
138 | PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
|
---|
139 | RT_ASSERT_PREEMPT_CPUID_VAR();
|
---|
140 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
141 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
|
---|
142 | RT_ASSERT_INTS_ON();
|
---|
143 |
|
---|
144 | /*
|
---|
145 | * If we're in interrupt context we need to unpin the underlying current
|
---|
146 | * thread as this could lead to a deadlock (see #4259 for the full explanation)
|
---|
147 | *
|
---|
148 | * Note! This assumes nobody is using the RTThreadPreemptDisable in an
|
---|
149 | * interrupt context and expects it to work right. The swtch will
|
---|
150 | * result in a voluntary preemption. To fix this, we would have to
|
---|
151 | * do our own counting in RTThreadPreemptDisable/Restore like we do
|
---|
152 | * on systems which doesn't do preemption (OS/2, linux, ...) and
|
---|
153 | * check whether preemption was disabled via RTThreadPreemptDisable
|
---|
154 | * or not and only call swtch if RTThreadPreemptDisable wasn't called.
|
---|
155 | */
|
---|
156 | int fAcquired = mutex_tryenter(&pThis->Mtx);
|
---|
157 | if (!fAcquired)
|
---|
158 | {
|
---|
159 | if (curthread->t_intr && getpil() < DISP_LEVEL)
|
---|
160 | {
|
---|
161 | RTTHREADPREEMPTSTATE PreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
|
---|
162 | RTThreadPreemptDisable(&PreemptState);
|
---|
163 | preempt();
|
---|
164 | RTThreadPreemptRestore(&PreemptState);
|
---|
165 | }
|
---|
166 | mutex_enter(&pThis->Mtx);
|
---|
167 | }
|
---|
168 |
|
---|
169 | if (pThis->cWaiters > 0)
|
---|
170 | {
|
---|
171 | ASMAtomicDecU32(&pThis->cWaiters);
|
---|
172 | ASMAtomicIncU32(&pThis->cWaking);
|
---|
173 | cv_signal(&pThis->Cnd);
|
---|
174 | }
|
---|
175 | else
|
---|
176 | ASMAtomicXchgU8(&pThis->fSignaled, true);
|
---|
177 |
|
---|
178 | mutex_exit(&pThis->Mtx);
|
---|
179 |
|
---|
180 | RT_ASSERT_PREEMPT_CPUID();
|
---|
181 | return VINF_SUCCESS;
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 | static int rtSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies, bool fInterruptible)
|
---|
186 | {
|
---|
187 | int rc;
|
---|
188 | PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
|
---|
189 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
190 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
|
---|
191 | if (cMillies)
|
---|
192 | RT_ASSERT_PREEMPTIBLE();
|
---|
193 |
|
---|
194 | mutex_enter(&pThis->Mtx);
|
---|
195 |
|
---|
196 | if (pThis->fSignaled)
|
---|
197 | {
|
---|
198 | Assert(!pThis->cWaiters);
|
---|
199 | ASMAtomicXchgU8(&pThis->fSignaled, false);
|
---|
200 | rc = VINF_SUCCESS;
|
---|
201 | }
|
---|
202 | else if (!cMillies)
|
---|
203 | rc = VERR_TIMEOUT;
|
---|
204 | else
|
---|
205 | {
|
---|
206 | ASMAtomicIncU32(&pThis->cWaiters);
|
---|
207 |
|
---|
208 | /*
|
---|
209 | * Translate milliseconds into ticks and go to sleep.
|
---|
210 | */
|
---|
211 | if (cMillies != RT_INDEFINITE_WAIT)
|
---|
212 | {
|
---|
213 | clock_t cTicks = drv_usectohz((clock_t)(cMillies * 1000L));
|
---|
214 | clock_t cTimeout = ddi_get_lbolt();
|
---|
215 | cTimeout += cTicks;
|
---|
216 | if (fInterruptible)
|
---|
217 | rc = cv_timedwait_sig(&pThis->Cnd, &pThis->Mtx, cTimeout);
|
---|
218 | else
|
---|
219 | rc = cv_timedwait(&pThis->Cnd, &pThis->Mtx, cTimeout);
|
---|
220 | }
|
---|
221 | else
|
---|
222 | {
|
---|
223 | if (fInterruptible)
|
---|
224 | rc = cv_wait_sig(&pThis->Cnd, &pThis->Mtx);
|
---|
225 | else
|
---|
226 | {
|
---|
227 | cv_wait(&pThis->Cnd, &pThis->Mtx);
|
---|
228 | rc = 1;
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | if (rc > 0)
|
---|
233 | {
|
---|
234 | /* Retured due to call to cv_signal() or cv_broadcast() */
|
---|
235 | if (pThis->u32Magic != RTSEMEVENT_MAGIC)
|
---|
236 | {
|
---|
237 | rc = VERR_SEM_DESTROYED;
|
---|
238 | if (!ASMAtomicDecU32(&pThis->cWaking))
|
---|
239 | {
|
---|
240 | mutex_exit(&pThis->Mtx);
|
---|
241 | cv_destroy(&pThis->Cnd);
|
---|
242 | mutex_destroy(&pThis->Mtx);
|
---|
243 | RTMemFree(pThis);
|
---|
244 | return rc;
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | ASMAtomicDecU32(&pThis->cWaking);
|
---|
249 | rc = VINF_SUCCESS;
|
---|
250 | }
|
---|
251 | else if (rc == -1)
|
---|
252 | {
|
---|
253 | /* Returned due to timeout being reached */
|
---|
254 | if (pThis->cWaiters > 0)
|
---|
255 | ASMAtomicDecU32(&pThis->cWaiters);
|
---|
256 | rc = VERR_TIMEOUT;
|
---|
257 | }
|
---|
258 | else
|
---|
259 | {
|
---|
260 | /* Returned due to pending signal */
|
---|
261 | if (pThis->cWaiters > 0)
|
---|
262 | ASMAtomicDecU32(&pThis->cWaiters);
|
---|
263 | rc = VERR_INTERRUPTED;
|
---|
264 | }
|
---|
265 | }
|
---|
266 |
|
---|
267 | mutex_exit(&pThis->Mtx);
|
---|
268 | return rc;
|
---|
269 | }
|
---|
270 |
|
---|
271 |
|
---|
272 | RTDECL(int) RTSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
|
---|
273 | {
|
---|
274 | return rtSemEventWait(hEventSem, cMillies, false /* not interruptible */);
|
---|
275 | }
|
---|
276 |
|
---|
277 |
|
---|
278 | RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
|
---|
279 | {
|
---|
280 | return rtSemEventWait(hEventSem, cMillies, true /* interruptible */);
|
---|
281 | }
|
---|