1 | /* $Id: semeventmulti-r0drv-solaris.c 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Multiple Release Event Semaphores, Ring-0 Driver, Solaris.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2020 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 | */
|
---|
75 | typedef 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 |
|
---|
93 | RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI phEventMultiSem)
|
---|
94 | {
|
---|
95 | return RTSemEventMultiCreateEx(phEventMultiSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | RTDECL(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 | */
|
---|
128 | DECLINLINE(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 | */
|
---|
141 | static 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 | */
|
---|
155 | DECLINLINE(void) rtR0SemEventMultiSolRelease(PRTSEMEVENTMULTIINTERNAL pThis)
|
---|
156 | {
|
---|
157 | if (RT_UNLIKELY(ASMAtomicDecU32(&pThis->cRefs) == 0))
|
---|
158 | rtSemEventMultiDtor(pThis);
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 |
|
---|
163 | RTDECL(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 |
|
---|
189 | RTDECL(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 | #ifdef DEBUG_ramshankar
|
---|
217 | /** See @bugref{6318#c11}. */
|
---|
218 | return VINF_SUCCESS;
|
---|
219 | #endif
|
---|
220 | RT_ASSERT_PREEMPT_CPUID();
|
---|
221 | return VINF_SUCCESS;
|
---|
222 | }
|
---|
223 |
|
---|
224 |
|
---|
225 | RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem)
|
---|
226 | {
|
---|
227 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
228 | RT_ASSERT_PREEMPT_CPUID_VAR();
|
---|
229 |
|
---|
230 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
231 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
232 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
233 | VERR_INVALID_HANDLE);
|
---|
234 | RT_ASSERT_INTS_ON();
|
---|
235 |
|
---|
236 | rtR0SemEventMultiSolRetain(pThis);
|
---|
237 | rtR0SemSolWaitEnterMutexWithUnpinningHack(&pThis->Mtx);
|
---|
238 | Assert(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC);
|
---|
239 |
|
---|
240 | /*
|
---|
241 | * Do the job (could be done without the lock, but play safe).
|
---|
242 | */
|
---|
243 | ASMAtomicAndU32(&pThis->fStateAndGen, ~RTSEMEVENTMULTISOL_STATE_MASK);
|
---|
244 |
|
---|
245 | mutex_exit(&pThis->Mtx);
|
---|
246 | rtR0SemEventMultiSolRelease(pThis);
|
---|
247 |
|
---|
248 | #ifdef DEBUG_ramshankar
|
---|
249 | /** See @bugref{6318#c11}. */
|
---|
250 | return VINF_SUCCESS;
|
---|
251 | #endif
|
---|
252 | RT_ASSERT_PREEMPT_CPUID();
|
---|
253 | return VINF_SUCCESS;
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * Worker for RTSemEventMultiWaitEx and RTSemEventMultiWaitExDebug.
|
---|
259 | *
|
---|
260 | * @returns VBox status code.
|
---|
261 | * @param pThis The event semaphore.
|
---|
262 | * @param fFlags See RTSemEventMultiWaitEx.
|
---|
263 | * @param uTimeout See RTSemEventMultiWaitEx.
|
---|
264 | * @param pSrcPos The source code position of the wait.
|
---|
265 | */
|
---|
266 | static int rtR0SemEventMultiSolWait(PRTSEMEVENTMULTIINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
|
---|
267 | PCRTLOCKVALSRCPOS pSrcPos)
|
---|
268 | {
|
---|
269 | uint32_t fOrgStateAndGen;
|
---|
270 | int rc;
|
---|
271 |
|
---|
272 | /*
|
---|
273 | * Validate the input.
|
---|
274 | */
|
---|
275 | AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
|
---|
276 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
|
---|
277 | AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
|
---|
278 | rtR0SemEventMultiSolRetain(pThis);
|
---|
279 | mutex_enter(&pThis->Mtx); /* this could be moved down to the else, but play safe for now. */
|
---|
280 |
|
---|
281 | /*
|
---|
282 | * Is the event already signalled or do we have to wait?
|
---|
283 | */
|
---|
284 | fOrgStateAndGen = ASMAtomicUoReadU32(&pThis->fStateAndGen);
|
---|
285 | if (fOrgStateAndGen & RTSEMEVENTMULTISOL_STATE_MASK)
|
---|
286 | rc = VINF_SUCCESS;
|
---|
287 | else
|
---|
288 | {
|
---|
289 | /*
|
---|
290 | * We have to wait.
|
---|
291 | */
|
---|
292 | RTR0SEMSOLWAIT Wait;
|
---|
293 | rc = rtR0SemSolWaitInit(&Wait, fFlags, uTimeout);
|
---|
294 | if (RT_SUCCESS(rc))
|
---|
295 | {
|
---|
296 | for (;;)
|
---|
297 | {
|
---|
298 | /* The destruction test. */
|
---|
299 | if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENTMULTI_MAGIC))
|
---|
300 | rc = VERR_SEM_DESTROYED;
|
---|
301 | else
|
---|
302 | {
|
---|
303 | /* Check the exit conditions. */
|
---|
304 | if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENTMULTI_MAGIC))
|
---|
305 | rc = VERR_SEM_DESTROYED;
|
---|
306 | else if (ASMAtomicUoReadU32(&pThis->fStateAndGen) != fOrgStateAndGen)
|
---|
307 | rc = VINF_SUCCESS;
|
---|
308 | else if (rtR0SemSolWaitHasTimedOut(&Wait))
|
---|
309 | rc = VERR_TIMEOUT;
|
---|
310 | else if (rtR0SemSolWaitWasInterrupted(&Wait))
|
---|
311 | rc = VERR_INTERRUPTED;
|
---|
312 | else
|
---|
313 | {
|
---|
314 | /* Do the wait and then recheck the conditions. */
|
---|
315 | rtR0SemSolWaitDoIt(&Wait, &pThis->Cnd, &pThis->Mtx, &pThis->fStateAndGen, fOrgStateAndGen);
|
---|
316 | continue;
|
---|
317 | }
|
---|
318 | }
|
---|
319 | break;
|
---|
320 | }
|
---|
321 | rtR0SemSolWaitDelete(&Wait);
|
---|
322 | }
|
---|
323 | }
|
---|
324 |
|
---|
325 | mutex_exit(&pThis->Mtx);
|
---|
326 | rtR0SemEventMultiSolRelease(pThis);
|
---|
327 | return rc;
|
---|
328 | }
|
---|
329 |
|
---|
330 |
|
---|
331 |
|
---|
332 | RTDECL(int) RTSemEventMultiWaitEx(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout)
|
---|
333 | {
|
---|
334 | #ifndef RTSEMEVENT_STRICT
|
---|
335 | return rtR0SemEventMultiSolWait(hEventMultiSem, fFlags, uTimeout, NULL);
|
---|
336 | #else
|
---|
337 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
|
---|
338 | return rtR0SemEventMultiSolWait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
|
---|
339 | #endif
|
---|
340 | }
|
---|
341 |
|
---|
342 |
|
---|
343 | RTDECL(int) RTSemEventMultiWaitExDebug(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout,
|
---|
344 | RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
345 | {
|
---|
346 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
|
---|
347 | return rtR0SemEventMultiSolWait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
|
---|
348 | }
|
---|
349 |
|
---|
350 |
|
---|
351 | RTDECL(uint32_t) RTSemEventMultiGetResolution(void)
|
---|
352 | {
|
---|
353 | return rtR0SemSolWaitGetResolution();
|
---|
354 | }
|
---|
355 |
|
---|