1 | /* $Id: semeventmulti-r0drv-linux.c 35051 2010-12-14 08:14:36Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Multiple Release Event Semaphores, Ring-0 Driver, Linux.
|
---|
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-linux-kernel.h"
|
---|
32 | #include "internal/iprt.h"
|
---|
33 | #include <iprt/semaphore.h>
|
---|
34 |
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/asm.h>
|
---|
37 | #include <iprt/err.h>
|
---|
38 | #include <iprt/mem.h>
|
---|
39 | #include <iprt/lockvalidator.h>
|
---|
40 |
|
---|
41 | #include "waitqueue-r0drv-linux.h"
|
---|
42 | #include "internal/magics.h"
|
---|
43 |
|
---|
44 |
|
---|
45 | /*******************************************************************************
|
---|
46 | * Defined Constants And Macros *
|
---|
47 | *******************************************************************************/
|
---|
48 | /** @name fStateAndGen values
|
---|
49 | * @{ */
|
---|
50 | /** The state bit number. */
|
---|
51 | #define RTSEMEVENTMULTILNX_STATE_BIT 0
|
---|
52 | /** The state mask. */
|
---|
53 | #define RTSEMEVENTMULTILNX_STATE_MASK RT_BIT_32(RTSEMEVENTMULTILNX_STATE_BIT)
|
---|
54 | /** The generation mask. */
|
---|
55 | #define RTSEMEVENTMULTILNX_GEN_MASK ~RTSEMEVENTMULTILNX_STATE_MASK
|
---|
56 | /** The generation shift. */
|
---|
57 | #define RTSEMEVENTMULTILNX_GEN_SHIFT 1
|
---|
58 | /** The initial variable value. */
|
---|
59 | #define RTSEMEVENTMULTILNX_STATE_GEN_INIT UINT32_C(0xfffffffc)
|
---|
60 | /** @} */
|
---|
61 |
|
---|
62 |
|
---|
63 | /*******************************************************************************
|
---|
64 | * Structures and Typedefs *
|
---|
65 | *******************************************************************************/
|
---|
66 | /**
|
---|
67 | * Linux event semaphore.
|
---|
68 | */
|
---|
69 | typedef struct RTSEMEVENTMULTIINTERNAL
|
---|
70 | {
|
---|
71 | /** Magic value (RTSEMEVENTMULTI_MAGIC). */
|
---|
72 | uint32_t volatile u32Magic;
|
---|
73 | /** The object state bit and generation counter.
|
---|
74 | * The generation counter is incremented every time the object is
|
---|
75 | * signalled. */
|
---|
76 | uint32_t volatile fStateAndGen;
|
---|
77 | /** Reference counter. */
|
---|
78 | uint32_t volatile cRefs;
|
---|
79 | /** The wait queue. */
|
---|
80 | wait_queue_head_t Head;
|
---|
81 | } RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
|
---|
82 |
|
---|
83 |
|
---|
84 |
|
---|
85 |
|
---|
86 |
|
---|
87 | RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI phEventMultiSem)
|
---|
88 | {
|
---|
89 | return RTSemEventMultiCreateEx(phEventMultiSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | RTDECL(int) RTSemEventMultiCreateEx(PRTSEMEVENTMULTI phEventMultiSem, uint32_t fFlags, RTLOCKVALCLASS hClass,
|
---|
94 | const char *pszNameFmt, ...)
|
---|
95 | {
|
---|
96 | PRTSEMEVENTMULTIINTERNAL pThis;
|
---|
97 |
|
---|
98 | AssertReturn(!(fFlags & ~RTSEMEVENTMULTI_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
|
---|
99 | pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
100 | if (pThis)
|
---|
101 | {
|
---|
102 | pThis->u32Magic = RTSEMEVENTMULTI_MAGIC;
|
---|
103 | pThis->fStateAndGen = RTSEMEVENTMULTILNX_STATE_GEN_INIT;
|
---|
104 | pThis->cRefs = 1;
|
---|
105 | init_waitqueue_head(&pThis->Head);
|
---|
106 |
|
---|
107 | *phEventMultiSem = pThis;
|
---|
108 | return VINF_SUCCESS;
|
---|
109 | }
|
---|
110 | return VERR_NO_MEMORY;
|
---|
111 | }
|
---|
112 | RT_EXPORT_SYMBOL(RTSemEventMultiCreate);
|
---|
113 |
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Retain a reference to the semaphore.
|
---|
117 | *
|
---|
118 | * @param pThis The semaphore.
|
---|
119 | */
|
---|
120 | DECLINLINE(void) rtR0SemEventMultiLnxRetain(PRTSEMEVENTMULTIINTERNAL pThis)
|
---|
121 | {
|
---|
122 | uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
|
---|
123 | NOREF(cRefs);
|
---|
124 | Assert(cRefs && cRefs < 100000);
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Release a reference, destroy the thing if necessary.
|
---|
130 | *
|
---|
131 | * @param pThis The semaphore.
|
---|
132 | */
|
---|
133 | DECLINLINE(void) rtR0SemEventMultiLnxRelease(PRTSEMEVENTMULTIINTERNAL pThis)
|
---|
134 | {
|
---|
135 | if (RT_UNLIKELY(ASMAtomicDecU32(&pThis->cRefs) == 0))
|
---|
136 | {
|
---|
137 | Assert(pThis->u32Magic != RTSEMEVENTMULTI_MAGIC);
|
---|
138 | RTMemFree(pThis);
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI hEventMultiSem)
|
---|
144 | {
|
---|
145 | /*
|
---|
146 | * Validate input.
|
---|
147 | */
|
---|
148 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
149 | if (pThis == NIL_RTSEMEVENTMULTI)
|
---|
150 | return VINF_SUCCESS;
|
---|
151 | AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
|
---|
152 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
|
---|
153 | Assert(pThis->cRefs > 0);
|
---|
154 |
|
---|
155 | /*
|
---|
156 | * Invalidate it and signal the object just in case.
|
---|
157 | */
|
---|
158 | ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMEVENTMULTI_MAGIC);
|
---|
159 | ASMAtomicAndU32(&pThis->fStateAndGen, RTSEMEVENTMULTILNX_GEN_MASK);
|
---|
160 | Assert(!waitqueue_active(&pThis->Head));
|
---|
161 | wake_up_all(&pThis->Head);
|
---|
162 | rtR0SemEventMultiLnxRelease(pThis);
|
---|
163 | return VINF_SUCCESS;
|
---|
164 | }
|
---|
165 | RT_EXPORT_SYMBOL(RTSemEventMultiDestroy);
|
---|
166 |
|
---|
167 |
|
---|
168 | RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI hEventMultiSem)
|
---|
169 | {
|
---|
170 | uint32_t fNew;
|
---|
171 | uint32_t fOld;
|
---|
172 |
|
---|
173 | /*
|
---|
174 | * Validate input.
|
---|
175 | */
|
---|
176 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
177 | if (!pThis)
|
---|
178 | return VERR_INVALID_PARAMETER;
|
---|
179 | AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
|
---|
180 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
|
---|
181 | rtR0SemEventMultiLnxRetain(pThis);
|
---|
182 |
|
---|
183 | /*
|
---|
184 | * Signal the event object. The cause of the paranoia here is racing to try
|
---|
185 | * deal with racing RTSemEventMultiSignal calls (should probably be
|
---|
186 | * forbidden, but it's relatively easy to handle).
|
---|
187 | */
|
---|
188 | do
|
---|
189 | {
|
---|
190 | fNew = fOld = ASMAtomicUoReadU32(&pThis->fStateAndGen);
|
---|
191 | fNew += 1 << RTSEMEVENTMULTILNX_GEN_SHIFT;
|
---|
192 | fNew |= RTSEMEVENTMULTILNX_STATE_MASK;
|
---|
193 | }
|
---|
194 | while (!ASMAtomicCmpXchgU32(&pThis->fStateAndGen, fNew, fOld));
|
---|
195 |
|
---|
196 | wake_up_all(&pThis->Head);
|
---|
197 |
|
---|
198 | rtR0SemEventMultiLnxRelease(pThis);
|
---|
199 | return VINF_SUCCESS;
|
---|
200 | }
|
---|
201 | RT_EXPORT_SYMBOL(RTSemEventMultiSignal);
|
---|
202 |
|
---|
203 |
|
---|
204 | RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem)
|
---|
205 | {
|
---|
206 | /*
|
---|
207 | * Validate input.
|
---|
208 | */
|
---|
209 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
210 | if (!pThis)
|
---|
211 | return VERR_INVALID_PARAMETER;
|
---|
212 | AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
|
---|
213 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
|
---|
214 | rtR0SemEventMultiLnxRetain(pThis);
|
---|
215 |
|
---|
216 | /*
|
---|
217 | * Reset it.
|
---|
218 | */
|
---|
219 | ASMAtomicAndU32(&pThis->fStateAndGen, ~RTSEMEVENTMULTILNX_STATE_MASK);
|
---|
220 |
|
---|
221 | rtR0SemEventMultiLnxRelease(pThis);
|
---|
222 | return VINF_SUCCESS;
|
---|
223 | }
|
---|
224 | RT_EXPORT_SYMBOL(RTSemEventMultiReset);
|
---|
225 |
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Worker for RTSemEventMultiWaitEx and RTSemEventMultiWaitExDebug.
|
---|
229 | *
|
---|
230 | * @returns VBox status code.
|
---|
231 | * @param pThis The event semaphore.
|
---|
232 | * @param fFlags See RTSemEventMultiWaitEx.
|
---|
233 | * @param uTimeout See RTSemEventMultiWaitEx.
|
---|
234 | * @param pSrcPos The source code position of the wait.
|
---|
235 | */
|
---|
236 | static int rtR0SemEventMultiLnxWait(PRTSEMEVENTMULTIINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
|
---|
237 | PCRTLOCKVALSRCPOS pSrcPos)
|
---|
238 | {
|
---|
239 | uint32_t fOrgStateAndGen;
|
---|
240 | int rc;
|
---|
241 |
|
---|
242 | /*
|
---|
243 | * Validate the input.
|
---|
244 | */
|
---|
245 | AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
|
---|
246 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
|
---|
247 | AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
|
---|
248 | rtR0SemEventMultiLnxRetain(pThis);
|
---|
249 |
|
---|
250 | /*
|
---|
251 | * Is the event already signalled or do we have to wait?
|
---|
252 | */
|
---|
253 | fOrgStateAndGen = ASMAtomicUoReadU32(&pThis->fStateAndGen);
|
---|
254 | if (fOrgStateAndGen & RTSEMEVENTMULTILNX_STATE_MASK)
|
---|
255 | rc = VINF_SUCCESS;
|
---|
256 | else
|
---|
257 | {
|
---|
258 | /*
|
---|
259 | * We have to wait.
|
---|
260 | */
|
---|
261 | RTR0SEMLNXWAIT Wait;
|
---|
262 | rc = rtR0SemLnxWaitInit(&Wait, fFlags, uTimeout, &pThis->Head);
|
---|
263 | if (RT_SUCCESS(rc))
|
---|
264 | {
|
---|
265 | IPRT_DEBUG_SEMS_STATE(pThis, 'E');
|
---|
266 | for (;;)
|
---|
267 | {
|
---|
268 | /* The destruction test. */
|
---|
269 | if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENTMULTI_MAGIC))
|
---|
270 | rc = VERR_SEM_DESTROYED;
|
---|
271 | else
|
---|
272 | {
|
---|
273 | rtR0SemLnxWaitPrepare(&Wait);
|
---|
274 |
|
---|
275 | /* Check the exit conditions. */
|
---|
276 | if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENTMULTI_MAGIC))
|
---|
277 | rc = VERR_SEM_DESTROYED;
|
---|
278 | else if (ASMAtomicUoReadU32(&pThis->fStateAndGen) != fOrgStateAndGen)
|
---|
279 | rc = VINF_SUCCESS;
|
---|
280 | else if (rtR0SemLnxWaitHasTimedOut(&Wait))
|
---|
281 | rc = VERR_TIMEOUT;
|
---|
282 | else if (rtR0SemLnxWaitWasInterrupted(&Wait))
|
---|
283 | rc = VERR_INTERRUPTED;
|
---|
284 | else
|
---|
285 | {
|
---|
286 | /* Do the wait and then recheck the conditions. */
|
---|
287 | rtR0SemLnxWaitDoIt(&Wait);
|
---|
288 | continue;
|
---|
289 | }
|
---|
290 | }
|
---|
291 | break;
|
---|
292 | }
|
---|
293 |
|
---|
294 | rtR0SemLnxWaitDelete(&Wait);
|
---|
295 | IPRT_DEBUG_SEMS_STATE_RC(pThis, 'E', rc);
|
---|
296 | }
|
---|
297 | }
|
---|
298 |
|
---|
299 | rtR0SemEventMultiLnxRelease(pThis);
|
---|
300 | return rc;
|
---|
301 | }
|
---|
302 |
|
---|
303 |
|
---|
304 | #undef RTSemEventMultiWaitEx
|
---|
305 | RTDECL(int) RTSemEventMultiWaitEx(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout)
|
---|
306 | {
|
---|
307 | #ifndef RTSEMEVENT_STRICT
|
---|
308 | return rtR0SemEventMultiLnxWait(hEventMultiSem, fFlags, uTimeout, NULL);
|
---|
309 | #else
|
---|
310 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
|
---|
311 | return rtR0SemEventMultiLnxWait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
|
---|
312 | #endif
|
---|
313 | }
|
---|
314 | RT_EXPORT_SYMBOL(RTSemEventMultiWaitEx);
|
---|
315 |
|
---|
316 |
|
---|
317 | RTDECL(int) RTSemEventMultiWaitExDebug(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout,
|
---|
318 | RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
319 | {
|
---|
320 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
|
---|
321 | return rtR0SemEventMultiLnxWait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
|
---|
322 | }
|
---|
323 | RT_EXPORT_SYMBOL(RTSemEventMultiWaitExDebug);
|
---|
324 |
|
---|
325 |
|
---|
326 | RTDECL(uint32_t) RTSemEventMultiGetResolution(void)
|
---|
327 | {
|
---|
328 | return rtR0SemLnxWaitGetResolution();
|
---|
329 | }
|
---|
330 | RT_EXPORT_SYMBOL(RTSemEventMultiGetResolution);
|
---|
331 |
|
---|