1 | /* $Id: semeventmulti-linux.cpp 14468 2008-11-21 15:44:27Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Multiple Release Event Semaphore, Linux (2.6.x+).
|
---|
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 | #include <features.h>
|
---|
33 | #if __GLIBC_PREREQ(2,6)
|
---|
34 |
|
---|
35 | /*
|
---|
36 | * glibc 2.6 fixed a serious bug in the mutex implementation. We wrote this
|
---|
37 | * linux specific event semaphores code in order to work around the bug. As it
|
---|
38 | * turns out, this code seems to have an unresolved issue (#2599), so we'll
|
---|
39 | * fall back on the pthread based implementation if glibc is known to contain
|
---|
40 | * the bug fix.
|
---|
41 | *
|
---|
42 | * The external refernce to epoll_pwait is a hack which prevents that we link
|
---|
43 | * against glibc < 2.6.
|
---|
44 | */
|
---|
45 | #include "../posix/semeventmulti-posix.cpp"
|
---|
46 | asm volatile (".global epoll_pwait");
|
---|
47 |
|
---|
48 | #else /* glibc < 2.6 */
|
---|
49 |
|
---|
50 | /*******************************************************************************
|
---|
51 | * Header Files *
|
---|
52 | *******************************************************************************/
|
---|
53 | #include <iprt/semaphore.h>
|
---|
54 | #include <iprt/assert.h>
|
---|
55 | #include <iprt/alloc.h>
|
---|
56 | #include <iprt/asm.h>
|
---|
57 | #include <iprt/err.h>
|
---|
58 | #include "internal/magics.h"
|
---|
59 |
|
---|
60 | #include <errno.h>
|
---|
61 | #include <limits.h>
|
---|
62 | #include <pthread.h>
|
---|
63 | #include <unistd.h>
|
---|
64 | #include <sys/time.h>
|
---|
65 | #include <sys/syscall.h>
|
---|
66 | #if 0 /* With 2.6.17 futex.h has become C++ unfriendly. */
|
---|
67 | # include <linux/futex.h>
|
---|
68 | #else
|
---|
69 | # define FUTEX_WAIT 0
|
---|
70 | # define FUTEX_WAKE 1
|
---|
71 | #endif
|
---|
72 |
|
---|
73 |
|
---|
74 | /*******************************************************************************
|
---|
75 | * Structures and Typedefs *
|
---|
76 | *******************************************************************************/
|
---|
77 | /**
|
---|
78 | * Linux multiple wakup event semaphore.
|
---|
79 | */
|
---|
80 | struct RTSEMEVENTMULTIINTERNAL
|
---|
81 | {
|
---|
82 | /** Magic value. */
|
---|
83 | intptr_t volatile iMagic;
|
---|
84 | /** The futex state variable.
|
---|
85 | * -1 means signaled.
|
---|
86 | * 0 means not signaled, no waiters.
|
---|
87 | * >0 means not signaled, and the value gives the number of waiters.
|
---|
88 | */
|
---|
89 | int32_t volatile iState;
|
---|
90 | };
|
---|
91 |
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Wrapper for the futex syscall.
|
---|
95 | */
|
---|
96 | static long sys_futex(int32_t volatile *uaddr, int op, int val, struct timespec *utime, int32_t *uaddr2, int val3)
|
---|
97 | {
|
---|
98 | errno = 0;
|
---|
99 | long rc = syscall(__NR_futex, uaddr, op, val, utime, uaddr2, val3);
|
---|
100 | if (rc < 0)
|
---|
101 | {
|
---|
102 | Assert(rc == -1);
|
---|
103 | rc = -errno;
|
---|
104 | }
|
---|
105 | return rc;
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI pEventMultiSem)
|
---|
110 | {
|
---|
111 | /*
|
---|
112 | * Allocate semaphore handle.
|
---|
113 | */
|
---|
114 | struct RTSEMEVENTMULTIINTERNAL *pThis = (struct RTSEMEVENTMULTIINTERNAL *)RTMemAlloc(sizeof(struct RTSEMEVENTMULTIINTERNAL));
|
---|
115 | if (pThis)
|
---|
116 | {
|
---|
117 | pThis->iMagic = RTSEMEVENTMULTI_MAGIC;
|
---|
118 | pThis->iState = 0;
|
---|
119 | *pEventMultiSem = pThis;
|
---|
120 | return VINF_SUCCESS;
|
---|
121 | }
|
---|
122 | return VERR_NO_MEMORY;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI EventMultiSem)
|
---|
127 | {
|
---|
128 | /*
|
---|
129 | * Validate input.
|
---|
130 | */
|
---|
131 | struct RTSEMEVENTMULTIINTERNAL *pThis = EventMultiSem;
|
---|
132 | AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENTMULTI_MAGIC,
|
---|
133 | VERR_INVALID_HANDLE);
|
---|
134 |
|
---|
135 | /*
|
---|
136 | * Invalidate the semaphore and wake up anyone waiting on it.
|
---|
137 | */
|
---|
138 | ASMAtomicWriteSize(&pThis->iMagic, RTSEMEVENTMULTI_MAGIC + 1);
|
---|
139 | if (ASMAtomicXchgS32(&pThis->iState, -1) == 1)
|
---|
140 | {
|
---|
141 | sys_futex(&pThis->iState, FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
|
---|
142 | usleep(1000);
|
---|
143 | }
|
---|
144 |
|
---|
145 | /*
|
---|
146 | * Free the semaphore memory and be gone.
|
---|
147 | */
|
---|
148 | RTMemFree(pThis);
|
---|
149 | return VINF_SUCCESS;
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI EventMultiSem)
|
---|
154 | {
|
---|
155 | /*
|
---|
156 | * Validate input.
|
---|
157 | */
|
---|
158 | struct RTSEMEVENTMULTIINTERNAL *pThis = EventMultiSem;
|
---|
159 | AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENTMULTI_MAGIC,
|
---|
160 | VERR_INVALID_HANDLE);
|
---|
161 | /*
|
---|
162 | * Signal it.
|
---|
163 | */
|
---|
164 | int32_t iOld = ASMAtomicXchgS32(&pThis->iState, -1);
|
---|
165 | if (iOld > 0)
|
---|
166 | {
|
---|
167 | /* wake up sleeping threads. */
|
---|
168 | long cWoken = sys_futex(&pThis->iState, FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
|
---|
169 | AssertMsg(cWoken >= 0, ("%ld\n", cWoken)); NOREF(cWoken);
|
---|
170 | }
|
---|
171 | Assert(iOld == 0 || iOld == -1 || iOld == 1);
|
---|
172 | return VINF_SUCCESS;
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI EventMultiSem)
|
---|
177 | {
|
---|
178 | /*
|
---|
179 | * Validate input.
|
---|
180 | */
|
---|
181 | struct RTSEMEVENTMULTIINTERNAL *pThis = EventMultiSem;
|
---|
182 | AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENTMULTI_MAGIC,
|
---|
183 | VERR_INVALID_HANDLE);
|
---|
184 | #ifdef RT_STRICT
|
---|
185 | int32_t i = pThis->iState;
|
---|
186 | Assert(i == 0 || i == -1 || i == 1);
|
---|
187 | #endif
|
---|
188 |
|
---|
189 | /*
|
---|
190 | * Reset it.
|
---|
191 | */
|
---|
192 | ASMAtomicCmpXchgS32(&pThis->iState, 0, -1);
|
---|
193 | return VINF_SUCCESS;
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | static int rtSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies, bool fAutoResume)
|
---|
198 | {
|
---|
199 | /*
|
---|
200 | * Validate input.
|
---|
201 | */
|
---|
202 | struct RTSEMEVENTMULTIINTERNAL *pThis = EventMultiSem;
|
---|
203 | AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENTMULTI_MAGIC,
|
---|
204 | VERR_INVALID_HANDLE);
|
---|
205 |
|
---|
206 | /*
|
---|
207 | * Quickly check whether it's signaled.
|
---|
208 | */
|
---|
209 | int32_t iCur = ASMAtomicUoReadS32(&pThis->iState);
|
---|
210 | Assert(iCur == 0 || iCur == -1 || iCur == 1);
|
---|
211 | if (iCur == -1)
|
---|
212 | return VINF_SUCCESS;
|
---|
213 | if (!cMillies)
|
---|
214 | return VERR_TIMEOUT;
|
---|
215 |
|
---|
216 | /*
|
---|
217 | * Convert timeout value.
|
---|
218 | */
|
---|
219 | struct timespec ts;
|
---|
220 | struct timespec *pTimeout = NULL;
|
---|
221 | if (cMillies != RT_INDEFINITE_WAIT)
|
---|
222 | {
|
---|
223 | ts.tv_sec = cMillies / 1000;
|
---|
224 | ts.tv_nsec = (cMillies % 1000) * 1000000;
|
---|
225 | pTimeout = &ts;
|
---|
226 | }
|
---|
227 |
|
---|
228 | /*
|
---|
229 | * The wait loop.
|
---|
230 | */
|
---|
231 | for (unsigned i = 0;; i++)
|
---|
232 | {
|
---|
233 | /*
|
---|
234 | * Start waiting. We only account for there being or having been
|
---|
235 | * threads waiting on the semaphore to keep things simple.
|
---|
236 | */
|
---|
237 | iCur = ASMAtomicUoReadS32(&pThis->iState);
|
---|
238 | Assert(iCur == 0 || iCur == -1 || iCur == 1);
|
---|
239 | if ( iCur == 1
|
---|
240 | || ASMAtomicCmpXchgS32(&pThis->iState, 1, 0))
|
---|
241 | {
|
---|
242 | long rc = sys_futex(&pThis->iState, FUTEX_WAIT, 1, pTimeout, NULL, 0);
|
---|
243 | if (RT_UNLIKELY(pThis->iMagic != RTSEMEVENTMULTI_MAGIC))
|
---|
244 | return VERR_SEM_DESTROYED;
|
---|
245 | if (rc == 0)
|
---|
246 | return VINF_SUCCESS;
|
---|
247 |
|
---|
248 | /*
|
---|
249 | * Act on the wakup code.
|
---|
250 | */
|
---|
251 | if (rc == -ETIMEDOUT)
|
---|
252 | {
|
---|
253 | /** @something is broken here. shows up every now and again in the ata code. Should try to run the timeout against RTTimeMilliTS to check that it's doing the right thing... */
|
---|
254 | Assert(pTimeout);
|
---|
255 | return VERR_TIMEOUT;
|
---|
256 | }
|
---|
257 | if (rc == -EWOULDBLOCK)
|
---|
258 | /* retry, the value changed. */;
|
---|
259 | else if (rc == -EINTR)
|
---|
260 | {
|
---|
261 | if (!fAutoResume)
|
---|
262 | return VERR_INTERRUPTED;
|
---|
263 | }
|
---|
264 | else
|
---|
265 | {
|
---|
266 | /* this shouldn't happen! */
|
---|
267 | AssertMsgFailed(("rc=%ld errno=%d\n", rc, errno));
|
---|
268 | return RTErrConvertFromErrno(rc);
|
---|
269 | }
|
---|
270 | }
|
---|
271 | else if (iCur == -1)
|
---|
272 | return VINF_SUCCESS;
|
---|
273 | }
|
---|
274 | }
|
---|
275 |
|
---|
276 |
|
---|
277 | RTDECL(int) RTSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
|
---|
278 | {
|
---|
279 | int rc = rtSemEventMultiWait(EventMultiSem, cMillies, true);
|
---|
280 | Assert(rc != VERR_INTERRUPTED);
|
---|
281 | return rc;
|
---|
282 | }
|
---|
283 |
|
---|
284 |
|
---|
285 | RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
|
---|
286 | {
|
---|
287 | return rtSemEventMultiWait(EventMultiSem, cMillies, false);
|
---|
288 | }
|
---|
289 |
|
---|
290 | #endif /* glibc < 2.6 */
|
---|