1 | /* $Id: semevent-r0drv-linux.c 19888 2009-05-21 15:25:42Z 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 <iprt/semaphore.h>
|
---|
37 | #include <iprt/alloc.h>
|
---|
38 | #include <iprt/assert.h>
|
---|
39 | #include <iprt/asm.h>
|
---|
40 | #include <iprt/err.h>
|
---|
41 |
|
---|
42 | #include "internal/magics.h"
|
---|
43 |
|
---|
44 |
|
---|
45 | /*******************************************************************************
|
---|
46 | * Structures and Typedefs *
|
---|
47 | *******************************************************************************/
|
---|
48 | /**
|
---|
49 | * Linux event semaphore.
|
---|
50 | */
|
---|
51 | typedef struct RTSEMEVENTINTERNAL
|
---|
52 | {
|
---|
53 | /** Magic value (RTSEMEVENT_MAGIC). */
|
---|
54 | uint32_t volatile u32Magic;
|
---|
55 | /** The object status - !0 when signaled and 0 when reset. */
|
---|
56 | uint32_t volatile fState;
|
---|
57 | /** The wait queue. */
|
---|
58 | wait_queue_head_t Head;
|
---|
59 | } RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
|
---|
60 |
|
---|
61 |
|
---|
62 |
|
---|
63 | RTDECL(int) RTSemEventCreate(PRTSEMEVENT pEventSem)
|
---|
64 | {
|
---|
65 | PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pEventInt));
|
---|
66 | if (pEventInt)
|
---|
67 | {
|
---|
68 | pEventInt->u32Magic = RTSEMEVENT_MAGIC;
|
---|
69 | pEventInt->fState = 0;
|
---|
70 | init_waitqueue_head(&pEventInt->Head);
|
---|
71 | *pEventSem = pEventInt;
|
---|
72 | return VINF_SUCCESS;
|
---|
73 | }
|
---|
74 | return VERR_NO_MEMORY;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | RTDECL(int) RTSemEventDestroy(RTSEMEVENT EventSem)
|
---|
79 | {
|
---|
80 | /*
|
---|
81 | * Validate input.
|
---|
82 | */
|
---|
83 | PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
|
---|
84 | if (!pEventInt)
|
---|
85 | return VERR_INVALID_PARAMETER;
|
---|
86 | if (pEventInt->u32Magic != RTSEMEVENT_MAGIC)
|
---|
87 | {
|
---|
88 | AssertMsgFailed(("pEventInt->u32Magic=%RX32 pEventInt=%p\n", pEventInt->u32Magic, pEventInt));
|
---|
89 | return VERR_INVALID_PARAMETER;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * Invalidate it and signal the object just in case.
|
---|
94 | */
|
---|
95 | ASMAtomicWriteU32(&pEventInt->u32Magic, ~RTSEMEVENT_MAGIC);
|
---|
96 | ASMAtomicWriteU32(&pEventInt->fState, 0);
|
---|
97 | Assert(!waitqueue_active(&pEventInt->Head));
|
---|
98 | wake_up_all(&pEventInt->Head);
|
---|
99 | RTMemFree(pEventInt);
|
---|
100 | return VINF_SUCCESS;
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | RTDECL(int) RTSemEventSignal(RTSEMEVENT EventSem)
|
---|
105 | {
|
---|
106 | /*
|
---|
107 | * Validate input.
|
---|
108 | */
|
---|
109 | PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
|
---|
110 | if (!pEventInt)
|
---|
111 | return VERR_INVALID_PARAMETER;
|
---|
112 | if ( !pEventInt
|
---|
113 | || pEventInt->u32Magic != RTSEMEVENT_MAGIC)
|
---|
114 | {
|
---|
115 | AssertMsgFailed(("pEventInt->u32Magic=%RX32 pEventInt=%p\n", pEventInt ? pEventInt->u32Magic : 0, pEventInt));
|
---|
116 | return VERR_INVALID_PARAMETER;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /*
|
---|
120 | * Signal the event object.
|
---|
121 | */
|
---|
122 | ASMAtomicWriteU32(&pEventInt->fState, 1);
|
---|
123 | wake_up(&pEventInt->Head);
|
---|
124 |
|
---|
125 | return VINF_SUCCESS;
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Worker for RTSemEvent and RTSemEventNoResume.
|
---|
131 | *
|
---|
132 | * @returns VBox status code.
|
---|
133 | * @param pEventInt The event semaphore.
|
---|
134 | * @param cMillies The number of milliseconds to wait.
|
---|
135 | * @param fInterruptible Whether it's an interruptible wait or not.
|
---|
136 | */
|
---|
137 | static int rtSemEventWait(PRTSEMEVENTINTERNAL pEventInt, unsigned cMillies, bool fInterruptible)
|
---|
138 | {
|
---|
139 | /*
|
---|
140 | * Ok wait for it.
|
---|
141 | */
|
---|
142 | DEFINE_WAIT(Wait);
|
---|
143 | int rc = VINF_SUCCESS;
|
---|
144 | long lTimeout = cMillies == RT_INDEFINITE_WAIT ? MAX_SCHEDULE_TIMEOUT : msecs_to_jiffies(cMillies);
|
---|
145 | #ifdef IPRT_DEBUG_SEMS
|
---|
146 | snprintf(current->comm, TASK_COMM_LEN, "e%lx", IPRT_DEBUG_SEMS_ADDRESS(pEventInt));
|
---|
147 | #endif
|
---|
148 | for (;;)
|
---|
149 | {
|
---|
150 | /* make everything thru schedule() atomic scheduling wise. */
|
---|
151 | prepare_to_wait(&pEventInt->Head, &Wait, fInterruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
|
---|
152 |
|
---|
153 | /* check the condition. */
|
---|
154 | if (ASMAtomicCmpXchgU32(&pEventInt->fState, 0, 1))
|
---|
155 | break;
|
---|
156 |
|
---|
157 | /* check for pending signals. */
|
---|
158 | if (fInterruptible && signal_pending(current))
|
---|
159 | {
|
---|
160 | rc = VERR_INTERRUPTED;
|
---|
161 | break;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /* wait */
|
---|
165 | lTimeout = schedule_timeout(lTimeout);
|
---|
166 |
|
---|
167 | /* Check if someone destroyed the semaphore while we were waiting. */
|
---|
168 | if (pEventInt->u32Magic != RTSEMEVENT_MAGIC)
|
---|
169 | {
|
---|
170 | rc = VERR_SEM_DESTROYED;
|
---|
171 | break;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /* check for timeout. */
|
---|
175 | if (!lTimeout)
|
---|
176 | {
|
---|
177 | rc = VERR_TIMEOUT;
|
---|
178 | break;
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | finish_wait(&pEventInt->Head, &Wait);
|
---|
183 | #ifdef IPRT_DEBUG_SEMS
|
---|
184 | snprintf(current->comm, TASK_COMM_LEN, "e%lx:%d", IPRT_DEBUG_SEMS_ADDRESS(pEventInt), rc);
|
---|
185 | #endif
|
---|
186 | return rc;
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 | RTDECL(int) RTSemEventWait(RTSEMEVENT EventSem, unsigned cMillies)
|
---|
191 | {
|
---|
192 | PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
|
---|
193 | if (!pEventInt)
|
---|
194 | return VERR_INVALID_PARAMETER;
|
---|
195 | if ( !pEventInt
|
---|
196 | || pEventInt->u32Magic != RTSEMEVENT_MAGIC)
|
---|
197 | {
|
---|
198 | AssertMsgFailed(("pEventInt->u32Magic=%RX32 pEventInt=%p\n", pEventInt ? pEventInt->u32Magic : 0, pEventInt));
|
---|
199 | return VERR_INVALID_PARAMETER;
|
---|
200 | }
|
---|
201 |
|
---|
202 | if (ASMAtomicCmpXchgU32(&pEventInt->fState, 0, 1))
|
---|
203 | return VINF_SUCCESS;
|
---|
204 | return rtSemEventWait(pEventInt, cMillies, false /* fInterruptible */);
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)
|
---|
209 | {
|
---|
210 | PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
|
---|
211 | if (!pEventInt)
|
---|
212 | return VERR_INVALID_PARAMETER;
|
---|
213 | if ( !pEventInt
|
---|
214 | || pEventInt->u32Magic != RTSEMEVENT_MAGIC)
|
---|
215 | {
|
---|
216 | AssertMsgFailed(("pEventInt->u32Magic=%RX32 pEventInt=%p\n", pEventInt ? pEventInt->u32Magic : 0, pEventInt));
|
---|
217 | return VERR_INVALID_PARAMETER;
|
---|
218 | }
|
---|
219 |
|
---|
220 | if (ASMAtomicCmpXchgU32(&pEventInt->fState, 0, 1))
|
---|
221 | return VINF_SUCCESS;
|
---|
222 | return rtSemEventWait(pEventInt, cMillies, true /* fInterruptible */);
|
---|
223 | }
|
---|
224 |
|
---|