VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/solaris/semevent-r0drv-solaris.c@ 24330

Last change on this file since 24330 was 22991, checked in by vboxsync, 15 years ago

Solaris/sem-r0drv: fix for #4259 host deadlock.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 KB
Line 
1/* $Id: semevent-r0drv-solaris.c 22991 2009-09-14 10:16:08Z 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 */
54typedef 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
72RTDECL(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
94RTDECL(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
131RTDECL(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 * Note! This assumes nobody is using the RTThreadPreemptDisable in an
146 * interrupt context and expects it to work right. The swtch will
147 * result in a voluntary preemption. To fix this, we would have to
148 * do our own counting in RTThreadPreemptDisable/Restore like we do
149 * on systems which doesn't do preemption (OS/2, linux, ...) and
150 * check whether preemption was disabled via RTThreadPreemptDisable
151 * or not and only call swtch if RTThreadPreemptDisable wasn't called.
152 */
153 int fAcquired = mutex_tryenter(&pEventInt->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(&pEventInt->Mtx);
164 }
165
166 if (pEventInt->cWaiters > 0)
167 {
168 ASMAtomicDecU32(&pEventInt->cWaiters);
169 ASMAtomicIncU32(&pEventInt->cWaking);
170 cv_signal(&pEventInt->Cnd);
171 }
172 else
173 ASMAtomicXchgU8(&pEventInt->fSignaled, true);
174
175 mutex_exit(&pEventInt->Mtx);
176
177 RT_ASSERT_PREEMPT_CPUID();
178 return VINF_SUCCESS;
179}
180
181
182static int rtSemEventWait(RTSEMEVENT EventSem, unsigned cMillies, bool fInterruptible)
183{
184 int rc;
185 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
186 AssertPtrReturn(pEventInt, VERR_INVALID_HANDLE);
187 AssertMsgReturn(pEventInt->u32Magic == RTSEMEVENT_MAGIC,
188 ("pEventInt=%p u32Magic=%#x\n", pEventInt, pEventInt->u32Magic),
189 VERR_INVALID_HANDLE);
190 if (cMillies)
191 RT_ASSERT_PREEMPTIBLE();
192
193 mutex_enter(&pEventInt->Mtx);
194
195 if (pEventInt->fSignaled)
196 {
197 Assert(!pEventInt->cWaiters);
198 ASMAtomicXchgU8(&pEventInt->fSignaled, false);
199 rc = VINF_SUCCESS;
200 }
201 else if (!cMillies)
202 rc = VERR_TIMEOUT;
203 else
204 {
205 ASMAtomicIncU32(&pEventInt->cWaiters);
206
207 /*
208 * Translate milliseconds into ticks and go to sleep.
209 */
210 if (cMillies != RT_INDEFINITE_WAIT)
211 {
212 clock_t cTicks = drv_usectohz((clock_t)(cMillies * 1000L));
213 clock_t timeout = ddi_get_lbolt();
214 timeout += cTicks;
215 if (fInterruptible)
216 rc = cv_timedwait_sig(&pEventInt->Cnd, &pEventInt->Mtx, timeout);
217 else
218 rc = cv_timedwait(&pEventInt->Cnd, &pEventInt->Mtx, timeout);
219 }
220 else
221 {
222 if (fInterruptible)
223 rc = cv_wait_sig(&pEventInt->Cnd, &pEventInt->Mtx);
224 else
225 {
226 cv_wait(&pEventInt->Cnd, &pEventInt->Mtx);
227 rc = 1;
228 }
229 }
230
231 if (rc > 0)
232 {
233 /* Retured due to call to cv_signal() or cv_broadcast() */
234 if (pEventInt->u32Magic != RTSEMEVENT_MAGIC)
235 {
236 rc = VERR_SEM_DESTROYED;
237 if (!ASMAtomicDecU32(&pEventInt->cWaking))
238 {
239 mutex_exit(&pEventInt->Mtx);
240 cv_destroy(&pEventInt->Cnd);
241 mutex_destroy(&pEventInt->Mtx);
242 RTMemFree(pEventInt);
243 return rc;
244 }
245 }
246
247 ASMAtomicDecU32(&pEventInt->cWaking);
248 rc = VINF_SUCCESS;
249 }
250 else if (rc == -1)
251 {
252 /* Returned due to timeout being reached */
253 if (pEventInt->cWaiters > 0)
254 ASMAtomicDecU32(&pEventInt->cWaiters);
255 rc = VERR_TIMEOUT;
256 }
257 else
258 {
259 /* Returned due to pending signal */
260 if (pEventInt->cWaiters > 0)
261 ASMAtomicDecU32(&pEventInt->cWaiters);
262 rc = VERR_INTERRUPTED;
263 }
264 }
265
266 mutex_exit(&pEventInt->Mtx);
267 return rc;
268}
269
270
271RTDECL(int) RTSemEventWait(RTSEMEVENT EventSem, unsigned cMillies)
272{
273 return rtSemEventWait(EventSem, cMillies, false /* not interruptible */);
274}
275
276
277RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)
278{
279 return rtSemEventWait(EventSem, cMillies, true /* interruptible */);
280}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette