VirtualBox

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

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

semevent-r0drv-solaris.c: review comments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.6 KB
Line 
1/* $Id: semevent-r0drv-solaris.c 30933 2010-07-20 16:16:06Z vboxsync $ */
2/** @file
3 * IPRT - Semaphores, Ring-0 Driver, Solaris.
4 */
5
6/*
7 * Copyright (C) 2006-2010 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 threads referencing this object. */
58 uint32_t volatile cRefs;
59 /** Set if the object is signalled when there are no waiters. */
60 bool fSignaled;
61 /** Object generation.
62 * This is incremented every time the object is signalled and used to
63 * check for spurious wake-ups. */
64 uint32_t uSignalGen;
65 /** The number of waiting threads. */
66 uint32_t cWaiters;
67 /** The number of signalled threads. */
68 uint32_t cWakeUp;
69 /** The Solaris mutex protecting this structure and pairing up the with the cv. */
70 kmutex_t Mtx;
71 /** The Solaris condition variable. */
72 kcondvar_t Cnd;
73} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
74
75
76
77RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
78{
79 return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
80}
81
82
83RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
84{
85 AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
86 AssertReturn(!(fFlags & ~RTSEMEVENT_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
87 AssertPtrReturn(phEventSem, VERR_INVALID_POINTER);
88 RT_ASSERT_PREEMPTIBLE();
89
90 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
91 if (!pThis)
92 return VERR_NO_MEMORY;
93
94 pThis->u32Magic = RTSEMEVENT_MAGIC;
95 pThis->cRefs = 1;
96 pThis->fSignaled = false;
97 pThis->uSignalGen = 0;
98 pThis->cWaiters = 0;
99 pThis->cWakeUp = 0;
100 mutex_init(&pThis->Mtx, "IPRT Event Semaphore", MUTEX_DRIVER, (void *)ipltospl(DISP_LEVEL));
101 cv_init(&pThis->Cnd, "IPRT CV", CV_DRIVER, NULL);
102
103 *phEventSem = pThis;
104 return VINF_SUCCESS;
105}
106
107
108RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
109{
110 PRTSEMEVENTINTERNAL pThis = hEventSem;
111 if (pThis == NIL_RTSEMEVENT)
112 return VINF_SUCCESS;
113 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
114 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
115 RT_ASSERT_INTS_ON();
116
117 mutex_enter(&pThis->Mtx);
118
119 ASMAtomicDecU32(&pThis->cRefs);
120
121 pThis->u32Magic = RTSEMEVENT_MAGIC_DEAD; /* make the handle invalid */
122 if (pThis->cWaiters > 0)
123 {
124 /*
125 * Signal all threads to destroy.
126 */
127 cv_broadcast(&pThis->Cnd);
128 mutex_exit(&pThis->Mtx);
129 }
130 else if (pThis->cRefs == 0)
131 {
132 /*
133 * We're the last thread referencing this object, destroy it.
134 */
135 mutex_exit(&pThis->Mtx);
136 cv_destroy(&pThis->Cnd);
137 mutex_destroy(&pThis->Mtx);
138 RTMemFree(pThis);
139 }
140 else
141 {
142 /*
143 * There are other threads still referencing this object, last one cleans up.
144 */
145 mutex_exit(&pThis->Mtx);
146 }
147
148 return VINF_SUCCESS;
149}
150
151
152RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
153{
154 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
155 RT_ASSERT_PREEMPT_CPUID_VAR();
156 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
157 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
158 RT_ASSERT_INTS_ON();
159
160 /*
161 * If we're in interrupt context we need to unpin the underlying current
162 * thread as this could lead to a deadlock (see #4259 for the full explanation)
163 *
164 * Note! This assumes nobody is using the RTThreadPreemptDisable in an
165 * interrupt context and expects it to work right. The swtch will
166 * result in a voluntary preemption. To fix this, we would have to
167 * do our own counting in RTThreadPreemptDisable/Restore like we do
168 * on systems which doesn't do preemption (OS/2, linux, ...) and
169 * check whether preemption was disabled via RTThreadPreemptDisable
170 * or not and only call swtch if RTThreadPreemptDisable wasn't called.
171 */
172 int fAcquired = mutex_tryenter(&pThis->Mtx);
173 if (!fAcquired)
174 {
175 if (curthread->t_intr && getpil() < DISP_LEVEL)
176 {
177 RTTHREADPREEMPTSTATE PreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
178 RTThreadPreemptDisable(&PreemptState);
179 preempt();
180 RTThreadPreemptRestore(&PreemptState);
181 }
182 mutex_enter(&pThis->Mtx);
183 }
184
185 /*
186 * If there are more waiting threads, wake them up. Otherwise leave the
187 * semaphore in the signalled state.
188 */
189 pThis->cWakeUp++;
190 if (pThis->cWakeUp <= pThis->cWaiters) /** @todo r=bird: see cWakeup = 0 below. */
191 {
192 cv_signal(&pThis->Cnd);
193 pThis->uSignalGen++;
194 }
195 else
196 pThis->fSignaled = true;
197
198 mutex_exit(&pThis->Mtx);
199
200 RT_ASSERT_PREEMPT_CPUID();
201 return VINF_SUCCESS;
202}
203
204
205static int rtSemEventWaitWorker(PRTSEMEVENTINTERNAL pThis, RTMSINTERVAL cMillies, bool fInterruptible)
206{
207 /*
208 * Translate milliseconds into ticks and go to sleep.
209 */
210 int rc = 0;
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 return rc;
233}
234
235
236static int rtSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies, bool fInterruptible)
237{
238 int rc;
239 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
240 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
241 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
242 if (cMillies)
243 RT_ASSERT_PREEMPTIBLE();
244
245 mutex_enter(&pThis->Mtx);
246
247 ASMAtomicIncU32(&pThis->cRefs);
248
249 if (pThis->fSignaled)
250 {
251 /*
252 * The last signal occurred without any waiters and now we're the first thread
253 * waiting for the event signal. So no real need to wait for one.
254 */
255 Assert(!pThis->cWaiters);
256 pThis->fSignaled = false;
257 /** @todo r=bird: This will get out of whack if someone is in the
258 * process of waking up (waiting to be scheduled). Further
259 * more, a race between a cv_signal and a
260 * timeout/interruption may cause wakeups to go unconsumed.
261 * Not sure how we could easily deal with this rigth now... */
262 pThis->cWakeUp = 0;
263 rc = VINF_SUCCESS;
264 }
265 else if (!cMillies)
266 rc = VERR_TIMEOUT;
267 else
268 {
269 pThis->cWaiters++;
270 /* This loop is only for continuing after a spurious wake-up. */
271 for (;;)
272 {
273 uint32_t const uSignalGenBeforeWait = pThis->uSignalGen;
274 rc = rtSemEventWaitWorker(pThis, cMillies, fInterruptible);
275 if (rc > 0)
276 {
277 if (pThis->u32Magic == RTSEMEVENT_MAGIC)
278 {
279 if (pThis->uSignalGen != uSignalGenBeforeWait)
280 {
281 /* We've been signaled by cv_signal(), consume the wake up. */
282 --pThis->cWakeUp; /** @todo r=bird: May cause underflow, see above. */
283 rc = VINF_SUCCESS;
284 }
285 else
286 /* Spurious wakeup due to some signal, go back to waiting. */
287 continue;
288 }
289 else
290 /* We're being destroyed. */
291 rc = VERR_SEM_DESTROYED;
292 }
293 else if (rc == -1)
294 /* Timeout reached. */
295 rc = VERR_TIMEOUT;
296 else
297 /* Returned due to pending signal */
298 rc = VERR_INTERRUPTED;
299
300 break;
301 }
302 --pThis->cWaiters;
303 }
304
305 if (!ASMAtomicDecU32(&pThis->cRefs))
306 {
307 Assert(RT_FAILURE_NP(rc));
308 mutex_exit(&pThis->Mtx);
309 cv_destroy(&pThis->Cnd);
310 mutex_destroy(&pThis->Mtx);
311 RTMemFree(pThis);
312 return rc;
313 }
314
315 mutex_exit(&pThis->Mtx);
316 return rc;
317}
318
319
320RTDECL(int) RTSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
321{
322 return rtSemEventWait(hEventSem, cMillies, false /* not interruptible */);
323}
324
325
326RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
327{
328 return rtSemEventWait(hEventSem, cMillies, true /* interruptible */);
329}
330
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