1 /* $Id: semeventmulti-r0drv-linux.c $ */
3 * IPRT - Multiple Release Event Semaphores, Ring-0 Driver, Linux.
7 * Copyright (C) 2006-2016 Oracle Corporation
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.
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.
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.
28 /*********************************************************************************************************************************
30 *********************************************************************************************************************************/
31 #define RTSEMEVENTMULTI_WITHOUT_REMAPPING
32 #include "the-linux-kernel.h"
33 #include "internal/iprt.h"
34 #include <iprt/semaphore.h>
36 #include <iprt/assert.h>
40 #include <iprt/lockvalidator.h>
42 #include "waitqueue-r0drv-linux.h"
43 #include "internal/magics.h"
46 /*********************************************************************************************************************************
47 * Defined Constants And Macros *
48 *********************************************************************************************************************************/
49 /** @name fStateAndGen values
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)
64 /*********************************************************************************************************************************
65 * Structures and Typedefs *
66 *********************************************************************************************************************************/
68 * Linux event semaphore.
70 typedef struct RTSEMEVENTMULTIINTERNAL
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
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
;
88 RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI phEventMultiSem
)
90 return RTSemEventMultiCreateEx(phEventMultiSem
, 0 /*fFlags*/, NIL_RTLOCKVALCLASS
, NULL
);
94 RTDECL(int) RTSemEventMultiCreateEx(PRTSEMEVENTMULTI phEventMultiSem
, uint32_t fFlags
, RTLOCKVALCLASS hClass
,
95 const char *pszNameFmt
, ...)
97 PRTSEMEVENTMULTIINTERNAL pThis
;
98 IPRT_LINUX_SAVE_EFL_AC();
99 RT_NOREF_PV(hClass
); RT_NOREF_PV(pszNameFmt
);
101 AssertReturn(!(fFlags
& ~RTSEMEVENTMULTI_FLAGS_NO_LOCK_VAL
), VERR_INVALID_PARAMETER
);
102 pThis
= (PRTSEMEVENTMULTIINTERNAL
)RTMemAlloc(sizeof(*pThis
));
105 pThis
->u32Magic
= RTSEMEVENTMULTI_MAGIC
;
106 pThis
->fStateAndGen
= RTSEMEVENTMULTILNX_STATE_GEN_INIT
;
108 init_waitqueue_head(&pThis
->Head
);
110 *phEventMultiSem
= pThis
;
111 IPRT_LINUX_RESTORE_EFL_AC();
114 IPRT_LINUX_RESTORE_EFL_AC();
115 return VERR_NO_MEMORY
;
117 RT_EXPORT_SYMBOL(RTSemEventMultiCreate
);
121 * Retain a reference to the semaphore.
123 * @param pThis The semaphore.
125 DECLINLINE(void) rtR0SemEventMultiLnxRetain(PRTSEMEVENTMULTIINTERNAL pThis
)
127 uint32_t cRefs
= ASMAtomicIncU32(&pThis
->cRefs
);
129 Assert(cRefs
&& cRefs
< 100000);
134 * Release a reference, destroy the thing if necessary.
136 * @param pThis The semaphore.
138 DECLINLINE(void) rtR0SemEventMultiLnxRelease(PRTSEMEVENTMULTIINTERNAL pThis
)
140 if (RT_UNLIKELY(ASMAtomicDecU32(&pThis
->cRefs
) == 0))
142 Assert(pThis
->u32Magic
!= RTSEMEVENTMULTI_MAGIC
);
148 RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI hEventMultiSem
)
150 IPRT_LINUX_SAVE_EFL_AC();
155 PRTSEMEVENTMULTIINTERNAL pThis
= (PRTSEMEVENTMULTIINTERNAL
)hEventMultiSem
;
156 if (pThis
== NIL_RTSEMEVENTMULTI
)
158 AssertPtrReturn(pThis
, VERR_INVALID_PARAMETER
);
159 AssertMsgReturn(pThis
->u32Magic
== RTSEMEVENTMULTI_MAGIC
, ("%p u32Magic=%RX32\n", pThis
, pThis
->u32Magic
), VERR_INVALID_PARAMETER
);
160 Assert(pThis
->cRefs
> 0);
163 * Invalidate it and signal the object just in case.
165 ASMAtomicWriteU32(&pThis
->u32Magic
, ~RTSEMEVENTMULTI_MAGIC
);
166 ASMAtomicAndU32(&pThis
->fStateAndGen
, RTSEMEVENTMULTILNX_GEN_MASK
);
167 Assert(!waitqueue_active(&pThis
->Head
));
168 wake_up_all(&pThis
->Head
);
169 rtR0SemEventMultiLnxRelease(pThis
);
171 IPRT_LINUX_RESTORE_EFL_AC();
174 RT_EXPORT_SYMBOL(RTSemEventMultiDestroy
);
177 RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI hEventMultiSem
)
179 IPRT_LINUX_SAVE_EFL_AC();
186 PRTSEMEVENTMULTIINTERNAL pThis
= (PRTSEMEVENTMULTIINTERNAL
)hEventMultiSem
;
188 return VERR_INVALID_PARAMETER
;
189 AssertPtrReturn(pThis
, VERR_INVALID_PARAMETER
);
190 AssertMsgReturn(pThis
->u32Magic
== RTSEMEVENTMULTI_MAGIC
, ("%p u32Magic=%RX32\n", pThis
, pThis
->u32Magic
), VERR_INVALID_PARAMETER
);
191 rtR0SemEventMultiLnxRetain(pThis
);
194 * Signal the event object. The cause of the paranoia here is racing to try
195 * deal with racing RTSemEventMultiSignal calls (should probably be
196 * forbidden, but it's relatively easy to handle).
200 fNew
= fOld
= ASMAtomicUoReadU32(&pThis
->fStateAndGen
);
201 fNew
+= 1 << RTSEMEVENTMULTILNX_GEN_SHIFT
;
202 fNew
|= RTSEMEVENTMULTILNX_STATE_MASK
;
204 while (!ASMAtomicCmpXchgU32(&pThis
->fStateAndGen
, fNew
, fOld
));
206 wake_up_all(&pThis
->Head
);
208 rtR0SemEventMultiLnxRelease(pThis
);
209 IPRT_LINUX_RESTORE_EFL_AC();
212 RT_EXPORT_SYMBOL(RTSemEventMultiSignal
);
215 RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem
)
220 PRTSEMEVENTMULTIINTERNAL pThis
= (PRTSEMEVENTMULTIINTERNAL
)hEventMultiSem
;
222 return VERR_INVALID_PARAMETER
;
223 AssertPtrReturn(pThis
, VERR_INVALID_PARAMETER
);
224 AssertMsgReturn(pThis
->u32Magic
== RTSEMEVENTMULTI_MAGIC
, ("%p u32Magic=%RX32\n", pThis
, pThis
->u32Magic
), VERR_INVALID_PARAMETER
);
225 rtR0SemEventMultiLnxRetain(pThis
);
230 ASMAtomicAndU32(&pThis
->fStateAndGen
, ~RTSEMEVENTMULTILNX_STATE_MASK
);
232 rtR0SemEventMultiLnxRelease(pThis
);
235 RT_EXPORT_SYMBOL(RTSemEventMultiReset
);
239 * Worker for RTSemEventMultiWaitEx and RTSemEventMultiWaitExDebug.
241 * @returns VBox status code.
242 * @param pThis The event semaphore.
243 * @param fFlags See RTSemEventMultiWaitEx.
244 * @param uTimeout See RTSemEventMultiWaitEx.
245 * @param pSrcPos The source code position of the wait.
247 static int rtR0SemEventMultiLnxWait(PRTSEMEVENTMULTIINTERNAL pThis
, uint32_t fFlags
, uint64_t uTimeout
,
248 PCRTLOCKVALSRCPOS pSrcPos
)
250 uint32_t fOrgStateAndGen
;
252 RT_NOREF_PV(pSrcPos
);
255 * Validate the input.
257 AssertPtrReturn(pThis
, VERR_INVALID_PARAMETER
);
258 AssertMsgReturn(pThis
->u32Magic
== RTSEMEVENTMULTI_MAGIC
, ("%p u32Magic=%RX32\n", pThis
, pThis
->u32Magic
), VERR_INVALID_PARAMETER
);
259 AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags
), VERR_INVALID_PARAMETER
);
260 rtR0SemEventMultiLnxRetain(pThis
);
263 * Is the event already signalled or do we have to wait?
265 fOrgStateAndGen
= ASMAtomicUoReadU32(&pThis
->fStateAndGen
);
266 if (fOrgStateAndGen
& RTSEMEVENTMULTILNX_STATE_MASK
)
274 IPRT_LINUX_SAVE_EFL_AC();
275 rc
= rtR0SemLnxWaitInit(&Wait
, fFlags
, uTimeout
, &pThis
->Head
);
278 IPRT_DEBUG_SEMS_STATE(pThis
, 'E');
281 /* The destruction test. */
282 if (RT_UNLIKELY(pThis
->u32Magic
!= RTSEMEVENTMULTI_MAGIC
))
283 rc
= VERR_SEM_DESTROYED
;
286 rtR0SemLnxWaitPrepare(&Wait
);
288 /* Check the exit conditions. */
289 if (RT_UNLIKELY(pThis
->u32Magic
!= RTSEMEVENTMULTI_MAGIC
))
290 rc
= VERR_SEM_DESTROYED
;
291 else if (ASMAtomicUoReadU32(&pThis
->fStateAndGen
) != fOrgStateAndGen
)
293 else if (rtR0SemLnxWaitHasTimedOut(&Wait
))
295 else if (rtR0SemLnxWaitWasInterrupted(&Wait
))
296 rc
= VERR_INTERRUPTED
;
299 /* Do the wait and then recheck the conditions. */
300 rtR0SemLnxWaitDoIt(&Wait
);
307 rtR0SemLnxWaitDelete(&Wait
);
308 IPRT_DEBUG_SEMS_STATE_RC(pThis
, 'E', rc
);
310 IPRT_LINUX_RESTORE_EFL_AC();
313 rtR0SemEventMultiLnxRelease(pThis
);
318 RTDECL(int) RTSemEventMultiWaitEx(RTSEMEVENTMULTI hEventMultiSem
, uint32_t fFlags
, uint64_t uTimeout
)
320 #ifndef RTSEMEVENT_STRICT
321 return rtR0SemEventMultiLnxWait(hEventMultiSem
, fFlags
, uTimeout
, NULL
);
323 RTLOCKVALSRCPOS SrcPos
= RTLOCKVALSRCPOS_INIT_NORMAL_API();
324 return rtR0SemEventMultiLnxWait(hEventMultiSem
, fFlags
, uTimeout
, &SrcPos
);
327 RT_EXPORT_SYMBOL(RTSemEventMultiWaitEx
);
330 RTDECL(int) RTSemEventMultiWaitExDebug(RTSEMEVENTMULTI hEventMultiSem
, uint32_t fFlags
, uint64_t uTimeout
,
331 RTHCUINTPTR uId
, RT_SRC_POS_DECL
)
333 RTLOCKVALSRCPOS SrcPos
= RTLOCKVALSRCPOS_INIT_DEBUG_API();
334 return rtR0SemEventMultiLnxWait(hEventMultiSem
, fFlags
, uTimeout
, &SrcPos
);
336 RT_EXPORT_SYMBOL(RTSemEventMultiWaitExDebug
);
339 RTDECL(uint32_t) RTSemEventMultiGetResolution(void)
341 return rtR0SemLnxWaitGetResolution();
343 RT_EXPORT_SYMBOL(RTSemEventMultiGetResolution
);