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