VirtualBox

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

Last change on this file since 23454 was 21538, checked in by vboxsync, 16 years ago

fixed comment.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.1 KB
Line 
1/* $Id: semevent-r0drv-linux.c 21538 2009-07-13 14:50:07Z vboxsync $ */
2/** @file
3 * IPRT - Single Release Event Semaphores, Ring-0 Driver, Linux.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include "the-linux-kernel.h"
36#include "internal/iprt.h"
37#include <iprt/semaphore.h>
38#include <iprt/alloc.h>
39#include <iprt/assert.h>
40#include <iprt/asm.h>
41#include <iprt/err.h>
42
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 /** The wait queue. */
59 wait_queue_head_t Head;
60} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
61
62
63
64RTDECL(int) RTSemEventCreate(PRTSEMEVENT pEventSem)
65{
66 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pEventInt));
67 if (pEventInt)
68 {
69 pEventInt->u32Magic = RTSEMEVENT_MAGIC;
70 pEventInt->fState = 0;
71 init_waitqueue_head(&pEventInt->Head);
72 *pEventSem = pEventInt;
73 return VINF_SUCCESS;
74 }
75 return VERR_NO_MEMORY;
76}
77RT_EXPORT_SYMBOL(RTSemEventCreate);
78
79
80RTDECL(int) RTSemEventDestroy(RTSEMEVENT EventSem)
81{
82 /*
83 * Validate input.
84 */
85 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
86 if (!pEventInt)
87 return VERR_INVALID_PARAMETER;
88 if (pEventInt->u32Magic != RTSEMEVENT_MAGIC)
89 {
90 AssertMsgFailed(("pEventInt->u32Magic=%RX32 pEventInt=%p\n", pEventInt->u32Magic, pEventInt));
91 return VERR_INVALID_PARAMETER;
92 }
93
94 /*
95 * Invalidate it and signal the object just in case.
96 */
97 ASMAtomicWriteU32(&pEventInt->u32Magic, ~RTSEMEVENT_MAGIC);
98 ASMAtomicWriteU32(&pEventInt->fState, 0);
99 Assert(!waitqueue_active(&pEventInt->Head));
100 wake_up_all(&pEventInt->Head);
101 RTMemFree(pEventInt);
102 return VINF_SUCCESS;
103}
104RT_EXPORT_SYMBOL(RTSemEventDestroy);
105
106
107RTDECL(int) RTSemEventSignal(RTSEMEVENT EventSem)
108{
109 /*
110 * Validate input.
111 */
112 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
113 if (!pEventInt)
114 return VERR_INVALID_PARAMETER;
115 if ( !pEventInt
116 || pEventInt->u32Magic != RTSEMEVENT_MAGIC)
117 {
118 AssertMsgFailed(("pEventInt->u32Magic=%RX32 pEventInt=%p\n", pEventInt ? pEventInt->u32Magic : 0, pEventInt));
119 return VERR_INVALID_PARAMETER;
120 }
121
122 /*
123 * Signal the event object.
124 */
125 ASMAtomicWriteU32(&pEventInt->fState, 1);
126 wake_up(&pEventInt->Head);
127
128 return VINF_SUCCESS;
129}
130RT_EXPORT_SYMBOL(RTSemEventSignal);
131
132
133/**
134 * Worker for RTSemEvent and RTSemEventNoResume.
135 *
136 * @returns VBox status code.
137 * @param pEventInt The event semaphore.
138 * @param cMillies The number of milliseconds to wait.
139 * @param fInterruptible Whether it's an interruptible wait or not.
140 */
141static int rtSemEventWait(PRTSEMEVENTINTERNAL pEventInt, unsigned cMillies, bool fInterruptible)
142{
143 /*
144 * Ok wait for it.
145 */
146 DEFINE_WAIT(Wait);
147 int rc = VINF_SUCCESS;
148 long lTimeout = cMillies == RT_INDEFINITE_WAIT ? MAX_SCHEDULE_TIMEOUT : msecs_to_jiffies(cMillies);
149#ifdef IPRT_DEBUG_SEMS
150 snprintf(current->comm, TASK_COMM_LEN, "e%lx", IPRT_DEBUG_SEMS_ADDRESS(pEventInt));
151#endif
152 for (;;)
153 {
154 /* make everything thru schedule_timeout() atomic scheduling wise. */
155 prepare_to_wait(&pEventInt->Head, &Wait, fInterruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
156
157 /* check the condition. */
158 if (ASMAtomicCmpXchgU32(&pEventInt->fState, 0, 1))
159 break;
160
161 /* check for pending signals. */
162 if (fInterruptible && signal_pending(current))
163 {
164 rc = VERR_INTERRUPTED;
165 break;
166 }
167
168 /* wait */
169 lTimeout = schedule_timeout(lTimeout);
170
171 /* Check if someone destroyed the semaphore while we were waiting. */
172 if (pEventInt->u32Magic != RTSEMEVENT_MAGIC)
173 {
174 rc = VERR_SEM_DESTROYED;
175 break;
176 }
177
178 /* check for timeout. */
179 if (!lTimeout)
180 {
181 rc = VERR_TIMEOUT;
182 break;
183 }
184 }
185
186 finish_wait(&pEventInt->Head, &Wait);
187#ifdef IPRT_DEBUG_SEMS
188 snprintf(current->comm, TASK_COMM_LEN, "e%lx:%d", IPRT_DEBUG_SEMS_ADDRESS(pEventInt), rc);
189#endif
190 return rc;
191}
192
193
194RTDECL(int) RTSemEventWait(RTSEMEVENT EventSem, unsigned cMillies)
195{
196 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
197 if (!pEventInt)
198 return VERR_INVALID_PARAMETER;
199 if ( !pEventInt
200 || pEventInt->u32Magic != RTSEMEVENT_MAGIC)
201 {
202 AssertMsgFailed(("pEventInt->u32Magic=%RX32 pEventInt=%p\n", pEventInt ? pEventInt->u32Magic : 0, pEventInt));
203 return VERR_INVALID_PARAMETER;
204 }
205
206 if (ASMAtomicCmpXchgU32(&pEventInt->fState, 0, 1))
207 return VINF_SUCCESS;
208 return rtSemEventWait(pEventInt, cMillies, false /* fInterruptible */);
209}
210RT_EXPORT_SYMBOL(RTSemEventWait);
211
212
213RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)
214{
215 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
216 if (!pEventInt)
217 return VERR_INVALID_PARAMETER;
218 if ( !pEventInt
219 || pEventInt->u32Magic != RTSEMEVENT_MAGIC)
220 {
221 AssertMsgFailed(("pEventInt->u32Magic=%RX32 pEventInt=%p\n", pEventInt ? pEventInt->u32Magic : 0, pEventInt));
222 return VERR_INVALID_PARAMETER;
223 }
224
225 if (ASMAtomicCmpXchgU32(&pEventInt->fState, 0, 1))
226 return VINF_SUCCESS;
227 return rtSemEventWait(pEventInt, cMillies, true /* fInterruptible */);
228}
229RT_EXPORT_SYMBOL(RTSemEventWaitNoResume);
230
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