VirtualBox

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

Last change on this file since 25645 was 24956, checked in by vboxsync, 15 years ago

Runtime/r0drv/linux: final fix for the prepare_to_wait() / finish_wait() problem on Linux 2.4 kernels

  • 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 24956 2009-11-25 14:26:50Z 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 after_wait(&Wait);
172
173 /* Check if someone destroyed the semaphore while we were waiting. */
174 if (pEventInt->u32Magic != RTSEMEVENT_MAGIC)
175 {
176 rc = VERR_SEM_DESTROYED;
177 break;
178 }
179
180 /* check for timeout. */
181 if (!lTimeout)
182 {
183 rc = VERR_TIMEOUT;
184 break;
185 }
186 }
187
188 finish_wait(&pEventInt->Head, &Wait);
189#ifdef IPRT_DEBUG_SEMS
190 snprintf(current->comm, TASK_COMM_LEN, "e%lx:%d", IPRT_DEBUG_SEMS_ADDRESS(pEventInt), rc);
191#endif
192 return rc;
193}
194
195
196RTDECL(int) RTSemEventWait(RTSEMEVENT EventSem, unsigned cMillies)
197{
198 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
199 if (!pEventInt)
200 return VERR_INVALID_PARAMETER;
201 if ( !pEventInt
202 || pEventInt->u32Magic != RTSEMEVENT_MAGIC)
203 {
204 AssertMsgFailed(("pEventInt->u32Magic=%RX32 pEventInt=%p\n", pEventInt ? pEventInt->u32Magic : 0, pEventInt));
205 return VERR_INVALID_PARAMETER;
206 }
207
208 if (ASMAtomicCmpXchgU32(&pEventInt->fState, 0, 1))
209 return VINF_SUCCESS;
210 return rtSemEventWait(pEventInt, cMillies, false /* fInterruptible */);
211}
212RT_EXPORT_SYMBOL(RTSemEventWait);
213
214
215RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)
216{
217 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
218 if (!pEventInt)
219 return VERR_INVALID_PARAMETER;
220 if ( !pEventInt
221 || pEventInt->u32Magic != RTSEMEVENT_MAGIC)
222 {
223 AssertMsgFailed(("pEventInt->u32Magic=%RX32 pEventInt=%p\n", pEventInt ? pEventInt->u32Magic : 0, pEventInt));
224 return VERR_INVALID_PARAMETER;
225 }
226
227 if (ASMAtomicCmpXchgU32(&pEventInt->fState, 0, 1))
228 return VINF_SUCCESS;
229 return rtSemEventWait(pEventInt, cMillies, true /* fInterruptible */);
230}
231RT_EXPORT_SYMBOL(RTSemEventWaitNoResume);
232
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