VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/linux/semevent-r0drv-linux.c@ 48935

Last change on this file since 48935 was 48935, checked in by vboxsync, 11 years ago

Runtime: Whitespace and svn:keyword cleanups by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 8.2 KB
Line 
1/* $Id: semevent-r0drv-linux.c 48935 2013-10-07 21:19:37Z vboxsync $ */
2/** @file
3 * IPRT - Single Release Event Semaphores, Ring-0 Driver, Linux.
4 */
5
6/*
7 * Copyright (C) 2006-2011 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 RTSEMEVENT_WITHOUT_REMAPPING
32#include "the-linux-kernel.h"
33#include "internal/iprt.h"
34#include <iprt/semaphore.h>
35
36#include <iprt/asm.h>
37#include <iprt/assert.h>
38#include <iprt/err.h>
39#include <iprt/lockvalidator.h>
40#include <iprt/mem.h>
41
42#include "waitqueue-r0drv-linux.h"
43#include "internal/magics.h"
44
45
46/*******************************************************************************
47* Structures and Typedefs *
48*******************************************************************************/
49/**
50 * Linux event semaphore.
51 */
52typedef struct RTSEMEVENTINTERNAL
53{
54 /** Magic value (RTSEMEVENT_MAGIC). */
55 uint32_t volatile u32Magic;
56 /** The object status - !0 when signaled and 0 when reset. */
57 uint32_t volatile fState;
58 /** Reference counter. */
59 uint32_t volatile cRefs;
60 /** The wait queue. */
61 wait_queue_head_t Head;
62} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
63
64
65
66RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
67{
68 return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
69}
70
71
72RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
73{
74 PRTSEMEVENTINTERNAL pThis;
75
76 AssertReturn(!(fFlags & ~(RTSEMEVENT_FLAGS_NO_LOCK_VAL | RTSEMEVENT_FLAGS_BOOTSTRAP_HACK)), VERR_INVALID_PARAMETER);
77 Assert(!(fFlags & RTSEMEVENT_FLAGS_BOOTSTRAP_HACK) || (fFlags & RTSEMEVENT_FLAGS_NO_LOCK_VAL));
78
79 pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
80 if (!pThis)
81 return VERR_NO_MEMORY;
82
83 pThis->u32Magic = RTSEMEVENT_MAGIC;
84 pThis->fState = 0;
85 pThis->cRefs = 1;
86 init_waitqueue_head(&pThis->Head);
87
88 *phEventSem = pThis;
89 return VINF_SUCCESS;
90}
91RT_EXPORT_SYMBOL(RTSemEventCreate);
92
93
94/**
95 * Retains a reference to the event semaphore.
96 *
97 * @param pThis The event semaphore.
98 */
99DECLINLINE(void) rtR0SemEventLnxRetain(PRTSEMEVENTINTERNAL pThis)
100{
101 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
102 Assert(cRefs < 100000); NOREF(cRefs);
103}
104
105
106/**
107 * Releases a reference to the event semaphore.
108 *
109 * @param pThis The event semaphore.
110 */
111DECLINLINE(void) rtR0SemEventLnxRelease(PRTSEMEVENTINTERNAL pThis)
112{
113 if (RT_UNLIKELY(ASMAtomicDecU32(&pThis->cRefs) == 0))
114 RTMemFree(pThis);
115}
116
117
118RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
119{
120 /*
121 * Validate input.
122 */
123 PRTSEMEVENTINTERNAL pThis = hEventSem;
124 if (pThis == NIL_RTSEMEVENT)
125 return VINF_SUCCESS;
126 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
127 Assert(pThis->cRefs > 0);
128
129 /*
130 * Invalidate it and signal the object just in case.
131 */
132 ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMEVENT_MAGIC);
133 ASMAtomicWriteU32(&pThis->fState, 0);
134 Assert(!waitqueue_active(&pThis->Head));
135 wake_up_all(&pThis->Head);
136 rtR0SemEventLnxRelease(pThis);
137 return VINF_SUCCESS;
138}
139RT_EXPORT_SYMBOL(RTSemEventDestroy);
140
141
142RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
143{
144 /*
145 * Validate input.
146 */
147 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
148 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
149 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
150 rtR0SemEventLnxRetain(pThis);
151
152 /*
153 * Signal the event object.
154 */
155 ASMAtomicWriteU32(&pThis->fState, 1);
156 wake_up(&pThis->Head);
157
158 rtR0SemEventLnxRelease(pThis);
159 return VINF_SUCCESS;
160}
161RT_EXPORT_SYMBOL(RTSemEventSignal);
162
163
164/**
165 * Worker for RTSemEventWaitEx and RTSemEventWaitExDebug.
166 *
167 * @returns VBox status code.
168 * @param pThis The event semaphore.
169 * @param fFlags See RTSemEventWaitEx.
170 * @param uTimeout See RTSemEventWaitEx.
171 * @param pSrcPos The source code position of the wait.
172 */
173static int rtR0SemEventLnxWait(PRTSEMEVENTINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
174 PCRTLOCKVALSRCPOS pSrcPos)
175{
176 int rc;
177
178 /*
179 * Validate the input.
180 */
181 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
182 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
183 AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
184 rtR0SemEventLnxRetain(pThis);
185
186 /*
187 * Try grab the event without setting up the wait.
188 */
189 if ( 1 /** @todo check if there are someone waiting already - waitqueue_active, but then what do we do below? */
190 && ASMAtomicCmpXchgU32(&pThis->fState, 0, 1))
191 rc = VINF_SUCCESS;
192 else
193 {
194 /*
195 * We have to wait.
196 */
197 RTR0SEMLNXWAIT Wait;
198 rc = rtR0SemLnxWaitInit(&Wait, fFlags, uTimeout, &pThis->Head);
199 if (RT_SUCCESS(rc))
200 {
201 IPRT_DEBUG_SEMS_STATE(pThis, 'E');
202 for (;;)
203 {
204 /* The destruction test. */
205 if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENT_MAGIC))
206 rc = VERR_SEM_DESTROYED;
207 else
208 {
209 rtR0SemLnxWaitPrepare(&Wait);
210
211 /* Check the exit conditions. */
212 if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENT_MAGIC))
213 rc = VERR_SEM_DESTROYED;
214 else if (ASMAtomicCmpXchgU32(&pThis->fState, 0, 1))
215 rc = VINF_SUCCESS;
216 else if (rtR0SemLnxWaitHasTimedOut(&Wait))
217 rc = VERR_TIMEOUT;
218 else if (rtR0SemLnxWaitWasInterrupted(&Wait))
219 rc = VERR_INTERRUPTED;
220 else
221 {
222 /* Do the wait and then recheck the conditions. */
223 rtR0SemLnxWaitDoIt(&Wait);
224 continue;
225 }
226 }
227 break;
228 }
229
230 rtR0SemLnxWaitDelete(&Wait);
231 IPRT_DEBUG_SEMS_STATE_RC(pThis, 'E', rc);
232 }
233 }
234
235 rtR0SemEventLnxRelease(pThis);
236 return rc;
237}
238
239
240RTDECL(int) RTSemEventWaitEx(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout)
241{
242#ifndef RTSEMEVENT_STRICT
243 return rtR0SemEventLnxWait(hEventSem, fFlags, uTimeout, NULL);
244#else
245 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
246 return rtR0SemEventLnxWait(hEventSem, fFlags, uTimeout, &SrcPos);
247#endif
248}
249RT_EXPORT_SYMBOL(RTSemEventWaitEx);
250
251
252RTDECL(int) RTSemEventWaitExDebug(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout,
253 RTHCUINTPTR uId, RT_SRC_POS_DECL)
254{
255 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
256 return rtR0SemEventLnxWait(hEventSem, fFlags, uTimeout, &SrcPos);
257}
258RT_EXPORT_SYMBOL(RTSemEventWaitExDebug);
259
260
261RTDECL(uint32_t) RTSemEventGetResolution(void)
262{
263 return rtR0SemLnxWaitGetResolution();
264}
265RT_EXPORT_SYMBOL(RTSemEventGetResolution);
266
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