1 | /* $Id: semevent-r0drv-solaris.c 22770 2009-09-04 09:51: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 pEventSem)
|
---|
73 | {
|
---|
74 | Assert(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
|
---|
75 | AssertPtrReturn(pEventSem, VERR_INVALID_POINTER);
|
---|
76 | RT_ASSERT_PREEMPTIBLE();
|
---|
77 |
|
---|
78 | PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pEventInt));
|
---|
79 | if (pEventInt)
|
---|
80 | {
|
---|
81 | pEventInt->u32Magic = RTSEMEVENT_MAGIC;
|
---|
82 | pEventInt->cWaiters = 0;
|
---|
83 | pEventInt->cWaking = 0;
|
---|
84 | pEventInt->fSignaled = 0;
|
---|
85 | mutex_init(&pEventInt->Mtx, "IPRT Event Semaphore", MUTEX_DRIVER, (void *)ipltospl(DISP_LEVEL));
|
---|
86 | cv_init(&pEventInt->Cnd, "IPRT CV", CV_DRIVER, NULL);
|
---|
87 | *pEventSem = pEventInt;
|
---|
88 | return VINF_SUCCESS;
|
---|
89 | }
|
---|
90 | return VERR_NO_MEMORY;
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | RTDECL(int) RTSemEventDestroy(RTSEMEVENT EventSem)
|
---|
95 | {
|
---|
96 | if (EventSem == NIL_RTSEMEVENT)
|
---|
97 | return VERR_INVALID_HANDLE;
|
---|
98 | PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
|
---|
99 | AssertPtrReturn(pEventInt, VERR_INVALID_HANDLE);
|
---|
100 | AssertMsgReturn(pEventInt->u32Magic == RTSEMEVENT_MAGIC,
|
---|
101 | ("pEventInt=%p u32Magic=%#x\n", pEventInt, pEventInt->u32Magic),
|
---|
102 | VERR_INVALID_HANDLE);
|
---|
103 | RT_ASSERT_INTS_ON();
|
---|
104 |
|
---|
105 | mutex_enter(&pEventInt->Mtx);
|
---|
106 | ASMAtomicIncU32(&pEventInt->u32Magic); /* make the handle invalid */
|
---|
107 | if (pEventInt->cWaiters > 0)
|
---|
108 | {
|
---|
109 | /* abort waiting thread, last man cleans up. */
|
---|
110 | ASMAtomicXchgU32(&pEventInt->cWaking, pEventInt->cWaking + pEventInt->cWaiters);
|
---|
111 | cv_broadcast(&pEventInt->Cnd);
|
---|
112 | mutex_exit(&pEventInt->Mtx);
|
---|
113 | }
|
---|
114 | else if (pEventInt->cWaking)
|
---|
115 | {
|
---|
116 | /* the last waking thread is gonna do the cleanup */
|
---|
117 | mutex_exit(&pEventInt->Mtx);
|
---|
118 | }
|
---|
119 | else
|
---|
120 | {
|
---|
121 | mutex_exit(&pEventInt->Mtx);
|
---|
122 | cv_destroy(&pEventInt->Cnd);
|
---|
123 | mutex_destroy(&pEventInt->Mtx);
|
---|
124 | RTMemFree(pEventInt);
|
---|
125 | }
|
---|
126 |
|
---|
127 | return VINF_SUCCESS;
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | RTDECL(int) RTSemEventSignal(RTSEMEVENT EventSem)
|
---|
132 | {
|
---|
133 | PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
|
---|
134 | RT_ASSERT_PREEMPT_CPUID_VAR();
|
---|
135 | AssertPtrReturn(pEventInt, VERR_INVALID_HANDLE);
|
---|
136 | AssertMsgReturn(pEventInt->u32Magic == RTSEMEVENT_MAGIC,
|
---|
137 | ("pEventInt=%p u32Magic=%#x\n", pEventInt, pEventInt->u32Magic),
|
---|
138 | VERR_INVALID_HANDLE);
|
---|
139 | RT_ASSERT_INTS_ON();
|
---|
140 |
|
---|
141 | /*
|
---|
142 | * If we're in interrupt context we need to unpin the underlying current
|
---|
143 | * thread as this could lead to a deadlock (see #4259 for the full explanation)
|
---|
144 | */
|
---|
145 | int fAcquired = mutex_tryenter(&pEventInt->Mtx);
|
---|
146 | if (!fAcquired)
|
---|
147 | {
|
---|
148 | if (curthread->t_intr && getpil() < DISP_LEVEL)
|
---|
149 | swtch();
|
---|
150 |
|
---|
151 | mutex_enter(&pEventInt->Mtx);
|
---|
152 | }
|
---|
153 |
|
---|
154 | if (pEventInt->cWaiters > 0)
|
---|
155 | {
|
---|
156 | ASMAtomicDecU32(&pEventInt->cWaiters);
|
---|
157 | ASMAtomicIncU32(&pEventInt->cWaking);
|
---|
158 | cv_signal(&pEventInt->Cnd);
|
---|
159 | }
|
---|
160 | else
|
---|
161 | ASMAtomicXchgU8(&pEventInt->fSignaled, true);
|
---|
162 |
|
---|
163 | mutex_exit(&pEventInt->Mtx);
|
---|
164 |
|
---|
165 | RT_ASSERT_PREEMPT_CPUID();
|
---|
166 | return VINF_SUCCESS;
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | static int rtSemEventWait(RTSEMEVENT EventSem, unsigned cMillies, bool fInterruptible)
|
---|
171 | {
|
---|
172 | int rc;
|
---|
173 | PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
|
---|
174 | AssertPtrReturn(pEventInt, VERR_INVALID_HANDLE);
|
---|
175 | AssertMsgReturn(pEventInt->u32Magic == RTSEMEVENT_MAGIC,
|
---|
176 | ("pEventInt=%p u32Magic=%#x\n", pEventInt, pEventInt->u32Magic),
|
---|
177 | VERR_INVALID_HANDLE);
|
---|
178 | if (cMillies)
|
---|
179 | RT_ASSERT_PREEMPTIBLE();
|
---|
180 |
|
---|
181 | mutex_enter(&pEventInt->Mtx);
|
---|
182 |
|
---|
183 | if (pEventInt->fSignaled)
|
---|
184 | {
|
---|
185 | Assert(!pEventInt->cWaiters);
|
---|
186 | ASMAtomicXchgU8(&pEventInt->fSignaled, false);
|
---|
187 | rc = VINF_SUCCESS;
|
---|
188 | }
|
---|
189 | else if (!cMillies)
|
---|
190 | rc = VERR_TIMEOUT;
|
---|
191 | else
|
---|
192 | {
|
---|
193 | ASMAtomicIncU32(&pEventInt->cWaiters);
|
---|
194 |
|
---|
195 | /*
|
---|
196 | * Translate milliseconds into ticks and go to sleep.
|
---|
197 | */
|
---|
198 | if (cMillies != RT_INDEFINITE_WAIT)
|
---|
199 | {
|
---|
200 | clock_t cTicks = drv_usectohz((clock_t)(cMillies * 1000L));
|
---|
201 | clock_t timeout = ddi_get_lbolt();
|
---|
202 | timeout += cTicks;
|
---|
203 | if (fInterruptible)
|
---|
204 | rc = cv_timedwait_sig(&pEventInt->Cnd, &pEventInt->Mtx, timeout);
|
---|
205 | else
|
---|
206 | rc = cv_timedwait(&pEventInt->Cnd, &pEventInt->Mtx, timeout);
|
---|
207 | }
|
---|
208 | else
|
---|
209 | {
|
---|
210 | if (fInterruptible)
|
---|
211 | rc = cv_wait_sig(&pEventInt->Cnd, &pEventInt->Mtx);
|
---|
212 | else
|
---|
213 | {
|
---|
214 | cv_wait(&pEventInt->Cnd, &pEventInt->Mtx);
|
---|
215 | rc = 1;
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | if (rc > 0)
|
---|
220 | {
|
---|
221 | /* Retured due to call to cv_signal() or cv_broadcast() */
|
---|
222 | if (pEventInt->u32Magic != RTSEMEVENT_MAGIC)
|
---|
223 | {
|
---|
224 | rc = VERR_SEM_DESTROYED;
|
---|
225 | if (!ASMAtomicDecU32(&pEventInt->cWaking))
|
---|
226 | {
|
---|
227 | mutex_exit(&pEventInt->Mtx);
|
---|
228 | cv_destroy(&pEventInt->Cnd);
|
---|
229 | mutex_destroy(&pEventInt->Mtx);
|
---|
230 | RTMemFree(pEventInt);
|
---|
231 | return rc;
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | ASMAtomicDecU32(&pEventInt->cWaking);
|
---|
236 | rc = VINF_SUCCESS;
|
---|
237 | }
|
---|
238 | else if (rc == -1)
|
---|
239 | {
|
---|
240 | /* Returned due to timeout being reached */
|
---|
241 | if (pEventInt->cWaiters > 0)
|
---|
242 | ASMAtomicDecU32(&pEventInt->cWaiters);
|
---|
243 | rc = VERR_TIMEOUT;
|
---|
244 | }
|
---|
245 | else
|
---|
246 | {
|
---|
247 | /* Returned due to pending signal */
|
---|
248 | if (pEventInt->cWaiters > 0)
|
---|
249 | ASMAtomicDecU32(&pEventInt->cWaiters);
|
---|
250 | rc = VERR_INTERRUPTED;
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | mutex_exit(&pEventInt->Mtx);
|
---|
255 | return rc;
|
---|
256 | }
|
---|
257 |
|
---|
258 |
|
---|
259 | RTDECL(int) RTSemEventWait(RTSEMEVENT EventSem, unsigned cMillies)
|
---|
260 | {
|
---|
261 | return rtSemEventWait(EventSem, cMillies, false /* not interruptible */);
|
---|
262 | }
|
---|
263 |
|
---|
264 |
|
---|
265 | RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)
|
---|
266 | {
|
---|
267 | return rtSemEventWait(EventSem, cMillies, true /* interruptible */);
|
---|
268 | }
|
---|