VirtualBox

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

最後變更 在這個檔案從22773是 22773,由 vboxsync 提交於 16 年 前

semevent-r0drv-solaris.c, semeventmulti-r0drv-solaris.c: Added preemption note and applied the workaround to RTSemEventMultiReset as well just to be on the safe side.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 9.0 KB
 
1/* $Id: semevent-r0drv-solaris.c 22773 2009-09-04 10:14:17Z 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.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 * 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 swtch();
158
159 mutex_enter(&pEventInt->Mtx);
160 }
161
162 if (pEventInt->cWaiters > 0)
163 {
164 ASMAtomicDecU32(&pEventInt->cWaiters);
165 ASMAtomicIncU32(&pEventInt->cWaking);
166 cv_signal(&pEventInt->Cnd);
167 }
168 else
169 ASMAtomicXchgU8(&pEventInt->fSignaled, true);
170
171 mutex_exit(&pEventInt->Mtx);
172
173 RT_ASSERT_PREEMPT_CPUID();
174 return VINF_SUCCESS;
175}
176
177
178static int rtSemEventWait(RTSEMEVENT EventSem, unsigned cMillies, bool fInterruptible)
179{
180 int rc;
181 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
182 AssertPtrReturn(pEventInt, VERR_INVALID_HANDLE);
183 AssertMsgReturn(pEventInt->u32Magic == RTSEMEVENT_MAGIC,
184 ("pEventInt=%p u32Magic=%#x\n", pEventInt, pEventInt->u32Magic),
185 VERR_INVALID_HANDLE);
186 if (cMillies)
187 RT_ASSERT_PREEMPTIBLE();
188
189 mutex_enter(&pEventInt->Mtx);
190
191 if (pEventInt->fSignaled)
192 {
193 Assert(!pEventInt->cWaiters);
194 ASMAtomicXchgU8(&pEventInt->fSignaled, false);
195 rc = VINF_SUCCESS;
196 }
197 else if (!cMillies)
198 rc = VERR_TIMEOUT;
199 else
200 {
201 ASMAtomicIncU32(&pEventInt->cWaiters);
202
203 /*
204 * Translate milliseconds into ticks and go to sleep.
205 */
206 if (cMillies != RT_INDEFINITE_WAIT)
207 {
208 clock_t cTicks = drv_usectohz((clock_t)(cMillies * 1000L));
209 clock_t timeout = ddi_get_lbolt();
210 timeout += cTicks;
211 if (fInterruptible)
212 rc = cv_timedwait_sig(&pEventInt->Cnd, &pEventInt->Mtx, timeout);
213 else
214 rc = cv_timedwait(&pEventInt->Cnd, &pEventInt->Mtx, timeout);
215 }
216 else
217 {
218 if (fInterruptible)
219 rc = cv_wait_sig(&pEventInt->Cnd, &pEventInt->Mtx);
220 else
221 {
222 cv_wait(&pEventInt->Cnd, &pEventInt->Mtx);
223 rc = 1;
224 }
225 }
226
227 if (rc > 0)
228 {
229 /* Retured due to call to cv_signal() or cv_broadcast() */
230 if (pEventInt->u32Magic != RTSEMEVENT_MAGIC)
231 {
232 rc = VERR_SEM_DESTROYED;
233 if (!ASMAtomicDecU32(&pEventInt->cWaking))
234 {
235 mutex_exit(&pEventInt->Mtx);
236 cv_destroy(&pEventInt->Cnd);
237 mutex_destroy(&pEventInt->Mtx);
238 RTMemFree(pEventInt);
239 return rc;
240 }
241 }
242
243 ASMAtomicDecU32(&pEventInt->cWaking);
244 rc = VINF_SUCCESS;
245 }
246 else if (rc == -1)
247 {
248 /* Returned due to timeout being reached */
249 if (pEventInt->cWaiters > 0)
250 ASMAtomicDecU32(&pEventInt->cWaiters);
251 rc = VERR_TIMEOUT;
252 }
253 else
254 {
255 /* Returned due to pending signal */
256 if (pEventInt->cWaiters > 0)
257 ASMAtomicDecU32(&pEventInt->cWaiters);
258 rc = VERR_INTERRUPTED;
259 }
260 }
261
262 mutex_exit(&pEventInt->Mtx);
263 return rc;
264}
265
266
267RTDECL(int) RTSemEventWait(RTSEMEVENT EventSem, unsigned cMillies)
268{
269 return rtSemEventWait(EventSem, cMillies, false /* not interruptible */);
270}
271
272
273RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)
274{
275 return rtSemEventWait(EventSem, cMillies, true /* interruptible */);
276}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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