VirtualBox

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

Last change on this file since 29698 was 29284, checked in by vboxsync, 15 years ago

r0drv/solaris: asm*.h fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.1 KB
Line 
1/* $Id: semevent-r0drv-solaris.c 29284 2010-05-10 00:22:16Z vboxsync $ */
2/** @file
3 * IPRT - Semaphores, Ring-0 Driver, Solaris.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Oracle Corporation
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
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 event semaphore.
52 */
53typedef struct RTSEMEVENTINTERNAL
54{
55 /** Magic value (RTSEMEVENT_MAGIC). */
56 uint32_t volatile u32Magic;
57 /** The number of waiting threads. */
58 uint32_t volatile cWaiters;
59 /** Set if the next waiter is to be signaled. */
60 uint8_t volatile fPendingSignal;
61 /** Set if the event object is signaled. */
62 uint8_t volatile fSignaled;
63 /** The number of threads referencing this object. */
64 uint32_t volatile cRefs;
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} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
70
71
72
73RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
74{
75 return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
76}
77
78
79RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
80{
81 AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
82 AssertReturn(!(fFlags & ~RTSEMEVENT_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
83 AssertPtrReturn(phEventSem, VERR_INVALID_POINTER);
84 RT_ASSERT_PREEMPTIBLE();
85
86 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
87 if (!pThis)
88 return VERR_NO_MEMORY;
89
90 pThis->u32Magic = RTSEMEVENT_MAGIC;
91 pThis->cWaiters = 0;
92 pThis->cRefs = 1;
93 pThis->fSignaled = 0;
94 pThis->fPendingSignal = 0;
95 mutex_init(&pThis->Mtx, "IPRT Event Semaphore", MUTEX_DRIVER, (void *)ipltospl(DISP_LEVEL));
96 cv_init(&pThis->Cnd, "IPRT CV", CV_DRIVER, NULL);
97
98 *phEventSem = pThis;
99 return VINF_SUCCESS;
100}
101
102
103RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
104{
105 PRTSEMEVENTINTERNAL pThis = hEventSem;
106 if (pThis == NIL_RTSEMEVENT)
107 return VINF_SUCCESS;
108 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
109 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
110 RT_ASSERT_INTS_ON();
111
112 mutex_enter(&pThis->Mtx);
113
114 ASMAtomicDecU32(&pThis->cRefs);
115
116 ASMAtomicIncU32(&pThis->u32Magic); /* make the handle invalid */
117 if (pThis->cWaiters > 0)
118 {
119 /*
120 * Signal all threads to destroy.
121 */
122 cv_broadcast(&pThis->Cnd);
123 mutex_exit(&pThis->Mtx);
124 }
125 else if (pThis->cRefs == 0)
126 {
127 /*
128 * We're the last thread referencing this object, destroy it.
129 */
130 mutex_exit(&pThis->Mtx);
131 cv_destroy(&pThis->Cnd);
132 mutex_destroy(&pThis->Mtx);
133 RTMemFree(pThis);
134 }
135 else
136 {
137 /*
138 * There are other threads still referencing this object, last one cleans up.
139 */
140 mutex_exit(&pThis->Mtx);
141 }
142
143 return VINF_SUCCESS;
144}
145
146
147RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
148{
149 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
150 RT_ASSERT_PREEMPT_CPUID_VAR();
151 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
152 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
153 RT_ASSERT_INTS_ON();
154
155 /*
156 * If we're in interrupt context we need to unpin the underlying current
157 * thread as this could lead to a deadlock (see #4259 for the full explanation)
158 *
159 * Note! This assumes nobody is using the RTThreadPreemptDisable in an
160 * interrupt context and expects it to work right. The swtch will
161 * result in a voluntary preemption. To fix this, we would have to
162 * do our own counting in RTThreadPreemptDisable/Restore like we do
163 * on systems which doesn't do preemption (OS/2, linux, ...) and
164 * check whether preemption was disabled via RTThreadPreemptDisable
165 * or not and only call swtch if RTThreadPreemptDisable wasn't called.
166 */
167 int fAcquired = mutex_tryenter(&pThis->Mtx);
168 if (!fAcquired)
169 {
170 if (curthread->t_intr && getpil() < DISP_LEVEL)
171 {
172 RTTHREADPREEMPTSTATE PreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
173 RTThreadPreemptDisable(&PreemptState);
174 preempt();
175 RTThreadPreemptRestore(&PreemptState);
176 }
177 mutex_enter(&pThis->Mtx);
178 }
179
180 if (pThis->cWaiters > 0)
181 {
182 /*
183 * We decrement waiters here so that we don't keep signalling threads that
184 * have already been signalled but not yet scheduled. So cWaiters might be
185 * 0 even when there are threads actually waiting.
186 */
187 ASMAtomicDecU32(&pThis->cWaiters);
188 ASMAtomicXchgU8(&pThis->fSignaled, true);
189 cv_signal(&pThis->Cnd);
190 }
191 else
192 ASMAtomicXchgU8(&pThis->fPendingSignal, true);
193
194 mutex_exit(&pThis->Mtx);
195
196 RT_ASSERT_PREEMPT_CPUID();
197 return VINF_SUCCESS;
198}
199
200
201static int rtSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies, bool fInterruptible)
202{
203 int rc;
204 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
205 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
206 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
207 if (cMillies)
208 RT_ASSERT_PREEMPTIBLE();
209
210 mutex_enter(&pThis->Mtx);
211
212 ASMAtomicIncU32(&pThis->cRefs);
213
214 if (pThis->fPendingSignal)
215 {
216 /*
217 * The last signal occurred without any waiters and now we're the first thread
218 * waiting for the event signal. So no real need to wait for one.
219 */
220 Assert(!pThis->cWaiters);
221 ASMAtomicXchgU8(&pThis->fPendingSignal, false);
222 rc = VINF_SUCCESS;
223 }
224 else if (!cMillies)
225 rc = VERR_TIMEOUT;
226 else
227 {
228 ASMAtomicIncU32(&pThis->cWaiters);
229
230 /*
231 * Translate milliseconds into ticks and go to sleep.
232 */
233 if (cMillies != RT_INDEFINITE_WAIT)
234 {
235 clock_t cTicks = drv_usectohz((clock_t)(cMillies * 1000L));
236 clock_t cTimeout = ddi_get_lbolt();
237 cTimeout += cTicks;
238 if (fInterruptible)
239 rc = cv_timedwait_sig(&pThis->Cnd, &pThis->Mtx, cTimeout);
240 else
241 rc = cv_timedwait(&pThis->Cnd, &pThis->Mtx, cTimeout);
242 }
243 else
244 {
245 if (fInterruptible)
246 rc = cv_wait_sig(&pThis->Cnd, &pThis->Mtx);
247 else
248 {
249 cv_wait(&pThis->Cnd, &pThis->Mtx);
250 rc = 1;
251 }
252 }
253
254 if (rc > 0)
255 {
256 if (pThis->u32Magic != RTSEMEVENT_MAGIC)
257 {
258 /*
259 * We're being destroyed.
260 */
261 rc = VERR_SEM_DESTROYED;
262 ASMAtomicDecU32(&pThis->cWaiters);
263 }
264 else
265 {
266 if (pThis->fSignaled)
267 {
268 /*
269 * We've been signaled by RTSemEventSignal().
270 */
271 ASMAtomicXchgU8(&pThis->fSignaled, false);
272 rc = VINF_SUCCESS;
273 }
274 else
275 {
276 /*
277 * Premature wakeup due to some signal.
278 */
279 rc = VERR_INTERRUPTED;
280 ASMAtomicDecU32(&pThis->cWaiters);
281 }
282 }
283 }
284 else if (rc == -1)
285 {
286 /*
287 * Timeout reached.
288 */
289 rc = VERR_TIMEOUT;
290 ASMAtomicDecU32(&pThis->cWaiters);
291 }
292 else
293 {
294 /* Returned due to pending signal */
295 rc = VERR_INTERRUPTED;
296 ASMAtomicDecU32(&pThis->cWaiters);
297 }
298 }
299
300 if (!ASMAtomicDecU32(&pThis->cRefs))
301 {
302 Assert(RT_FAILURE_NP(rc));
303 mutex_exit(&pThis->Mtx);
304 cv_destroy(&pThis->Cnd);
305 mutex_destroy(&pThis->Mtx);
306 RTMemFree(pThis);
307 return rc;
308 }
309
310 mutex_exit(&pThis->Mtx);
311 return rc;
312}
313
314
315RTDECL(int) RTSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
316{
317 return rtSemEventWait(hEventSem, cMillies, false /* not interruptible */);
318}
319
320
321RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
322{
323 return rtSemEventWait(hEventSem, cMillies, true /* interruptible */);
324}
325
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