VirtualBox

Ticket #5501: semeventmulti-r0drv-linux.c

File semeventmulti-r0drv-linux.c, 8.2 KB (added by aeichner, 15 years ago)

hrtimers

Line 
1/* $Id: semeventmulti-r0drv-linux.c 55251 2009-11-25 14:26:50Z fmehnert $ */
2/** @file
3 * IPRT - Multiple 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/* We use the API of Linux 2.6.28+ (schedule_hrtimeout()) */
46#if !defined(RT_USE_LINUX_HRTIMER) \
47 && LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28) \
48 && 1 /* currently disabled */
49# define RT_USE_LINUX_HRTIMER
50#endif
51
52/*******************************************************************************
53* Structures and Typedefs *
54*******************************************************************************/
55/**
56 * Linux event semaphore.
57 */
58typedef struct RTSEMEVENTMULTIINTERNAL
59{
60 /** Magic value (RTSEMEVENTMULTI_MAGIC). */
61 uint32_t volatile u32Magic;
62 /** The object status - !0 when signaled and 0 when reset. */
63 uint32_t volatile fState;
64 /** The wait queue. */
65 wait_queue_head_t Head;
66} RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
67
68
69
70RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI pEventMultiSem)
71{
72 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis));
73 if (pThis)
74 {
75 pThis->u32Magic = RTSEMEVENTMULTI_MAGIC;
76 pThis->fState = 0;
77 init_waitqueue_head(&pThis->Head);
78 *pEventMultiSem = pThis;
79 return VINF_SUCCESS;
80 }
81 return VERR_NO_MEMORY;
82}
83RT_EXPORT_SYMBOL(RTSemEventMultiCreate);
84
85
86RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI EventMultiSem)
87{
88 /*
89 * Validate input.
90 */
91 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
92 if (!pThis)
93 return VERR_INVALID_PARAMETER;
94 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
95 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
96
97 /*
98 * Invalidate it and signal the object just in case.
99 */
100 ASMAtomicIncU32(&pThis->u32Magic);
101 ASMAtomicXchgU32(&pThis->fState, 0);
102 Assert(!waitqueue_active(&pThis->Head));
103 wake_up_all(&pThis->Head);
104 RTMemFree(pThis);
105 return VINF_SUCCESS;
106}
107RT_EXPORT_SYMBOL(RTSemEventMultiDestroy);
108
109
110RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI EventMultiSem)
111{
112 /*
113 * Validate input.
114 */
115 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
116 if (!pThis)
117 return VERR_INVALID_PARAMETER;
118 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
119 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
120
121 /*
122 * Signal the event object.
123 */
124 ASMAtomicXchgU32(&pThis->fState, 1);
125 wake_up_all(&pThis->Head);
126 return VINF_SUCCESS;
127}
128RT_EXPORT_SYMBOL(RTSemEventMultiSignal);
129
130
131RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI EventMultiSem)
132{
133 /*
134 * Validate input.
135 */
136 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
137 if (!pThis)
138 return VERR_INVALID_PARAMETER;
139 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
140 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
141
142 /*
143 * Reset it.
144 */
145 ASMAtomicXchgU32(&pThis->fState, 0);
146 return VINF_SUCCESS;
147}
148RT_EXPORT_SYMBOL(RTSemEventMultiReset);
149
150
151/**
152 * Worker for RTSemEventMulti and RTSemEventMultiNoResume.
153 *
154 * @returns VBox status code.
155 * @param pThis The event semaphore.
156 * @param cMillies The number of milliseconds to wait.
157 * @param fInterruptible Whether it's an interruptible wait or not.
158 */
159static int rtSemEventMultiWait(PRTSEMEVENTMULTIINTERNAL pThis, unsigned cMillies, bool fInterruptible)
160{
161 /*
162 * Ok wait for it.
163 */
164 DEFINE_WAIT(Wait);
165 int rc = VINF_SUCCESS;
166#ifndef RT_USE_LINUX_HRTIMER
167 long lTimeout = cMillies == RT_INDEFINITE_WAIT ? MAX_SCHEDULE_TIMEOUT : msecs_to_jiffies(cMillies);
168#else
169 ktime_t Kt = cMillies == RT_INDEFINITE_WAIT ? ktime_set(KTIME_SEC_MAX, 0) : ktime_add_ns(ktime_get(), cMillies * UINT64_C(1000000));
170 int rcHr;
171#endif
172#ifdef IPRT_DEBUG_SEMS
173 snprintf(current->comm, TASK_COMM_LEN, "E%lx", IPRT_DEBUG_SEMS_ADDRESS(pThis));
174#endif
175 for (;;)
176 {
177 /* make everything thru schedule() atomic scheduling wise. */
178 prepare_to_wait(&pThis->Head, &Wait, fInterruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
179
180 /* check the condition. */
181 if (pThis->fState)
182 break;
183
184 /* check for pending signals. */
185 if (fInterruptible && signal_pending(current))
186 {
187 rc = VERR_INTERRUPTED;
188 break;
189 }
190
191#ifdef RT_USE_LINUX_HRTIMER
192 rcHr = schedule_hrtimeout(&Kt, cMillies == RT_INDEFINITE_WAIT ? HRTIMER_MODE_REL : HRTIMER_MODE_ABS);
193#else
194 /* wait */
195 lTimeout = schedule_timeout(lTimeout);
196#endif
197
198 after_wait(&Wait);
199
200 /* Check if someone destroyed the semaphore while we were waiting. */
201 if (pThis->u32Magic != RTSEMEVENTMULTI_MAGIC)
202 {
203 rc = VERR_SEM_DESTROYED;
204 break;
205 }
206
207#ifdef RT_USE_LINUX_HRTIMER
208 if (!rcHr)
209 {
210 rc = VERR_TIMEOUT;
211 break;
212 }
213#else
214 /* check for timeout. */
215 if (!lTimeout)
216 {
217 rc = VERR_TIMEOUT;
218 break;
219 }
220#endif
221 }
222
223 finish_wait(&pThis->Head, &Wait);
224#ifdef IPRT_DEBUG_SEMS
225 snprintf(current->comm, TASK_COMM_LEN, "E%lx:%d", IPRT_DEBUG_SEMS_ADDRESS(pThis), rc);
226#endif
227 return rc;
228}
229
230
231RTDECL(int) RTSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
232{
233 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
234 if (!pThis)
235 return VERR_INVALID_PARAMETER;
236 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
237 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
238
239 if (pThis->fState)
240 return VINF_SUCCESS;
241 return rtSemEventMultiWait(pThis, cMillies, false /* fInterruptible */);
242}
243RT_EXPORT_SYMBOL(RTSemEventMultiWait);
244
245
246RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
247{
248 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
249 if (!pThis)
250 return VERR_INVALID_PARAMETER;
251 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
252 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
253
254 if (pThis->fState)
255 return VINF_SUCCESS;
256 return rtSemEventMultiWait(pThis, cMillies, true /* fInterruptible */);
257}
258RT_EXPORT_SYMBOL(RTSemEventMultiWaitNoResume);
259

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy