VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/solaris/semeventmulti-r0drv-solaris.c@ 37275

Last change on this file since 37275 was 36392, checked in by vboxsync, 14 years ago

r0drv/solaris/solevent*: Must re-check the wait condition before going to sleep.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.6 KB
Line 
1/* $Id: semeventmulti-r0drv-solaris.c 36392 2011-03-24 11:20:37Z vboxsync $ */
2/** @file
3 * IPRT - Multiple Release Event 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#define RTSEMEVENTMULTI_WITHOUT_REMAPPING
32#include "the-solaris-kernel.h"
33#include "internal/iprt.h"
34#include <iprt/semaphore.h>
35
36#include <iprt/assert.h>
37#include <iprt/asm.h>
38#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
39# include <iprt/asm-amd64-x86.h>
40#endif
41#include <iprt/err.h>
42#include <iprt/lockvalidator.h>
43#include <iprt/mem.h>
44#include <iprt/mp.h>
45#include <iprt/thread.h>
46#include <iprt/time.h>
47#include "internal/magics.h"
48#include "semeventwait-r0drv-solaris.h"
49
50
51/*******************************************************************************
52* Defined Constants And Macros *
53*******************************************************************************/
54/** @name fStateAndGen values
55 * @{ */
56/** The state bit number. */
57#define RTSEMEVENTMULTISOL_STATE_BIT 0
58/** The state mask. */
59#define RTSEMEVENTMULTISOL_STATE_MASK RT_BIT_32(RTSEMEVENTMULTISOL_STATE_BIT)
60/** The generation mask. */
61#define RTSEMEVENTMULTISOL_GEN_MASK ~RTSEMEVENTMULTISOL_STATE_MASK
62/** The generation shift. */
63#define RTSEMEVENTMULTISOL_GEN_SHIFT 1
64/** The initial variable value. */
65#define RTSEMEVENTMULTISOL_STATE_GEN_INIT UINT32_C(0xfffffffc)
66/** @} */
67
68
69/*******************************************************************************
70* Structures and Typedefs *
71*******************************************************************************/
72/**
73 * Solaris multiple release event semaphore.
74 */
75typedef struct RTSEMEVENTMULTIINTERNAL
76{
77 /** Magic value (RTSEMEVENTMULTI_MAGIC). */
78 uint32_t volatile u32Magic;
79 /** The number of references. */
80 uint32_t volatile cRefs;
81 /** The object state bit and generation counter.
82 * The generation counter is incremented every time the object is
83 * signalled. */
84 uint32_t volatile fStateAndGen;
85 /** The Solaris mutex protecting this structure and pairing up the with the cv. */
86 kmutex_t Mtx;
87 /** The Solaris condition variable. */
88 kcondvar_t Cnd;
89} RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
90
91
92
93RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI phEventMultiSem)
94{
95 return RTSemEventMultiCreateEx(phEventMultiSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
96}
97
98
99RTDECL(int) RTSemEventMultiCreateEx(PRTSEMEVENTMULTI phEventMultiSem, uint32_t fFlags, RTLOCKVALCLASS hClass,
100 const char *pszNameFmt, ...)
101{
102 AssertReturn(!(fFlags & ~RTSEMEVENTMULTI_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
103 AssertPtrReturn(phEventMultiSem, VERR_INVALID_POINTER);
104 RT_ASSERT_PREEMPTIBLE();
105
106 AssertCompile(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
107 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis));
108 if (pThis)
109 {
110 pThis->u32Magic = RTSEMEVENTMULTI_MAGIC;
111 pThis->cRefs = 1;
112 pThis->fStateAndGen = RTSEMEVENTMULTISOL_STATE_GEN_INIT;
113 mutex_init(&pThis->Mtx, "IPRT Multiple Release Event Semaphore", MUTEX_DRIVER, (void *)ipltospl(DISP_LEVEL));
114 cv_init(&pThis->Cnd, "IPRT CV", CV_DRIVER, NULL);
115
116 *phEventMultiSem = pThis;
117 return VINF_SUCCESS;
118 }
119 return VERR_NO_MEMORY;
120}
121
122
123/**
124 * Retain a reference to the semaphore.
125 *
126 * @param pThis The semaphore.
127 */
128DECLINLINE(void) rtR0SemEventMultiSolRetain(PRTSEMEVENTMULTIINTERNAL pThis)
129{
130 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
131 Assert(cRefs && cRefs < 100000);
132 NOREF(cRefs);
133}
134
135
136/**
137 * Destructor that is called when cRefs == 0.
138 *
139 * @param pThis The instance to destroy.
140 */
141static void rtSemEventMultiDtor(PRTSEMEVENTMULTIINTERNAL pThis)
142{
143 Assert(pThis->u32Magic != RTSEMEVENTMULTI_MAGIC);
144 cv_destroy(&pThis->Cnd);
145 mutex_destroy(&pThis->Mtx);
146 RTMemFree(pThis);
147}
148
149
150/**
151 * Release a reference, destroy the thing if necessary.
152 *
153 * @param pThis The semaphore.
154 */
155DECLINLINE(void) rtR0SemEventMultiSolRelease(PRTSEMEVENTMULTIINTERNAL pThis)
156{
157 if (RT_UNLIKELY(ASMAtomicDecU32(&pThis->cRefs) == 0))
158 rtSemEventMultiDtor(pThis);
159}
160
161
162
163RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI hEventMultiSem)
164{
165 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
166 if (pThis == NIL_RTSEMEVENTMULTI)
167 return VINF_SUCCESS;
168 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
169 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
170 AssertMsgReturn(pThis->cRefs > 0, ("pThis=%p cRefs=%d\n", pThis, pThis->cRefs), VERR_INVALID_HANDLE);
171 RT_ASSERT_INTS_ON();
172
173 mutex_enter(&pThis->Mtx);
174
175 /* Invalidate the handle and wake up all threads that might be waiting on the semaphore. */
176 Assert(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC);
177 ASMAtomicWriteU32(&pThis->u32Magic, RTSEMEVENTMULTI_MAGIC_DEAD);
178 ASMAtomicAndU32(&pThis->fStateAndGen, RTSEMEVENTMULTISOL_GEN_MASK);
179 cv_broadcast(&pThis->Cnd);
180
181 /* Drop the reference from RTSemEventMultiCreateEx. */
182 mutex_exit(&pThis->Mtx);
183 rtR0SemEventMultiSolRelease(pThis);
184
185 return VINF_SUCCESS;
186}
187
188
189RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI hEventMultiSem)
190{
191 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
192 RT_ASSERT_PREEMPT_CPUID_VAR();
193
194 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
195 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
196 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
197 VERR_INVALID_HANDLE);
198 RT_ASSERT_INTS_ON();
199 rtR0SemEventMultiSolRetain(pThis);
200 rtR0SemSolWaitEnterMutexWithUnpinningHack(&pThis->Mtx);
201 Assert(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC);
202
203 /*
204 * Do the job.
205 */
206 uint32_t fNew = ASMAtomicUoReadU32(&pThis->fStateAndGen);
207 fNew += 1 << RTSEMEVENTMULTISOL_GEN_SHIFT;
208 fNew |= RTSEMEVENTMULTISOL_STATE_MASK;
209 ASMAtomicWriteU32(&pThis->fStateAndGen, fNew);
210
211 cv_broadcast(&pThis->Cnd);
212
213 mutex_exit(&pThis->Mtx);
214
215 rtR0SemEventMultiSolRelease(pThis);
216 RT_ASSERT_PREEMPT_CPUID();
217 return VINF_SUCCESS;
218}
219
220
221RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem)
222{
223 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
224 RT_ASSERT_PREEMPT_CPUID_VAR();
225
226 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
227 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
228 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
229 VERR_INVALID_HANDLE);
230 RT_ASSERT_INTS_ON();
231
232 rtR0SemEventMultiSolRetain(pThis);
233 rtR0SemSolWaitEnterMutexWithUnpinningHack(&pThis->Mtx);
234 Assert(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC);
235
236 /*
237 * Do the job (could be done without the lock, but play safe).
238 */
239 ASMAtomicAndU32(&pThis->fStateAndGen, ~RTSEMEVENTMULTISOL_STATE_MASK);
240
241 mutex_exit(&pThis->Mtx);
242 rtR0SemEventMultiSolRelease(pThis);
243
244 RT_ASSERT_PREEMPT_CPUID();
245 return VINF_SUCCESS;
246}
247
248
249/**
250 * Worker for RTSemEventMultiWaitEx and RTSemEventMultiWaitExDebug.
251 *
252 * @returns VBox status code.
253 * @param pThis The event semaphore.
254 * @param fFlags See RTSemEventMultiWaitEx.
255 * @param uTimeout See RTSemEventMultiWaitEx.
256 * @param pSrcPos The source code position of the wait.
257 */
258static int rtR0SemEventMultiSolWait(PRTSEMEVENTMULTIINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
259 PCRTLOCKVALSRCPOS pSrcPos)
260{
261 uint32_t fOrgStateAndGen;
262 int rc;
263
264 /*
265 * Validate the input.
266 */
267 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
268 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
269 AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
270 rtR0SemEventMultiSolRetain(pThis);
271 mutex_enter(&pThis->Mtx); /* this could be moved down to the else, but play safe for now. */
272
273 /*
274 * Is the event already signalled or do we have to wait?
275 */
276 fOrgStateAndGen = ASMAtomicUoReadU32(&pThis->fStateAndGen);
277 if (fOrgStateAndGen & RTSEMEVENTMULTISOL_STATE_MASK)
278 rc = VINF_SUCCESS;
279 else
280 {
281 /*
282 * We have to wait.
283 */
284 RTR0SEMSOLWAIT Wait;
285 rc = rtR0SemSolWaitInit(&Wait, fFlags, uTimeout);
286 if (RT_SUCCESS(rc))
287 {
288 for (;;)
289 {
290 /* The destruction test. */
291 if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENTMULTI_MAGIC))
292 rc = VERR_SEM_DESTROYED;
293 else
294 {
295 /* Check the exit conditions. */
296 if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENTMULTI_MAGIC))
297 rc = VERR_SEM_DESTROYED;
298 else if (ASMAtomicUoReadU32(&pThis->fStateAndGen) != fOrgStateAndGen)
299 rc = VINF_SUCCESS;
300 else if (rtR0SemSolWaitHasTimedOut(&Wait))
301 rc = VERR_TIMEOUT;
302 else if (rtR0SemSolWaitWasInterrupted(&Wait))
303 rc = VERR_INTERRUPTED;
304 else
305 {
306 /* Do the wait and then recheck the conditions. */
307 rtR0SemSolWaitDoIt(&Wait, &pThis->Cnd, &pThis->Mtx, &pThis->fStateAndGen, fOrgStateAndGen);
308 continue;
309 }
310 }
311 break;
312 }
313 rtR0SemSolWaitDelete(&Wait);
314 }
315 }
316
317 mutex_exit(&pThis->Mtx);
318 rtR0SemEventMultiSolRelease(pThis);
319 return rc;
320}
321
322
323
324RTDECL(int) RTSemEventMultiWaitEx(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout)
325{
326#ifndef RTSEMEVENT_STRICT
327 return rtR0SemEventMultiSolWait(hEventMultiSem, fFlags, uTimeout, NULL);
328#else
329 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
330 return rtR0SemEventMultiSolWait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
331#endif
332}
333
334
335RTDECL(int) RTSemEventMultiWaitExDebug(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout,
336 RTHCUINTPTR uId, RT_SRC_POS_DECL)
337{
338 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
339 return rtR0SemEventMultiSolWait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
340}
341
342
343RTDECL(uint32_t) RTSemEventMultiGetResolution(void)
344{
345 return rtR0SemSolWaitGetResolution();
346}
347
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