1 | /* $Id: semevent-linux.cpp 8245 2008-04-21 17:24:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - 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 | * Header Files *
|
---|
33 | *******************************************************************************/
|
---|
34 | #include <iprt/semaphore.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/alloc.h>
|
---|
37 | #include <iprt/asm.h>
|
---|
38 | #include <iprt/err.h>
|
---|
39 | #include "internal/magics.h"
|
---|
40 |
|
---|
41 | #include <errno.h>
|
---|
42 | #include <limits.h>
|
---|
43 | #include <pthread.h>
|
---|
44 | #include <unistd.h>
|
---|
45 | #include <sys/time.h>
|
---|
46 | #include <sys/syscall.h>
|
---|
47 | #if 0 /* With 2.6.17 futex.h has become C++ unfriendly. */
|
---|
48 | # include <linux/futex.h>
|
---|
49 | #else
|
---|
50 | # define FUTEX_WAIT 0
|
---|
51 | # define FUTEX_WAKE 1
|
---|
52 | #endif
|
---|
53 |
|
---|
54 |
|
---|
55 | /*******************************************************************************
|
---|
56 | * Structures and Typedefs *
|
---|
57 | *******************************************************************************/
|
---|
58 | /**
|
---|
59 | * Linux (single wakup) event semaphore.
|
---|
60 | */
|
---|
61 | struct RTSEMEVENTINTERNAL
|
---|
62 | {
|
---|
63 | /** Magic value. */
|
---|
64 | intptr_t volatile iMagic;
|
---|
65 | /** The futex state variable.
|
---|
66 | * <0 means signaled.
|
---|
67 | * 0 means not signaled, no waiters.
|
---|
68 | * >0 means not signaled, and the value gives the number of waiters.
|
---|
69 | */
|
---|
70 | int32_t volatile cWaiters;
|
---|
71 | };
|
---|
72 |
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Wrapper for the futex syscall.
|
---|
76 | */
|
---|
77 | static long sys_futex(int32_t volatile *uaddr, int op, int val, struct timespec *utime, int32_t *uaddr2, int val3)
|
---|
78 | {
|
---|
79 | errno = 0;
|
---|
80 | long rc = syscall(__NR_futex, uaddr, op, val, utime, uaddr2, val3);
|
---|
81 | if (rc < 0)
|
---|
82 | {
|
---|
83 | Assert(rc == -1);
|
---|
84 | rc = -errno;
|
---|
85 | }
|
---|
86 | return rc;
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 |
|
---|
91 | RTDECL(int) RTSemEventCreate(PRTSEMEVENT pEventSem)
|
---|
92 | {
|
---|
93 | /*
|
---|
94 | * Allocate semaphore handle.
|
---|
95 | */
|
---|
96 | struct RTSEMEVENTINTERNAL *pThis = (struct RTSEMEVENTINTERNAL *)RTMemAlloc(sizeof(struct RTSEMEVENTINTERNAL));
|
---|
97 | if (pThis)
|
---|
98 | {
|
---|
99 | pThis->iMagic = RTSEMEVENT_MAGIC;
|
---|
100 | pThis->cWaiters = 0;
|
---|
101 | *pEventSem = pThis;
|
---|
102 | return VINF_SUCCESS;
|
---|
103 | }
|
---|
104 | return VERR_NO_MEMORY;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | RTDECL(int) RTSemEventDestroy(RTSEMEVENT EventSem)
|
---|
109 | {
|
---|
110 | /*
|
---|
111 | * Validate input.
|
---|
112 | */
|
---|
113 | struct RTSEMEVENTINTERNAL *pThis = EventSem;
|
---|
114 | AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENT_MAGIC,
|
---|
115 | VERR_INVALID_HANDLE);
|
---|
116 |
|
---|
117 | /*
|
---|
118 | * Invalidate the semaphore and wake up anyone waiting on it.
|
---|
119 | */
|
---|
120 | ASMAtomicXchgSize(&pThis->iMagic, RTSEMEVENT_MAGIC + 1);
|
---|
121 | if (ASMAtomicXchgS32(&pThis->cWaiters, INT32_MIN / 2) > 0)
|
---|
122 | {
|
---|
123 | sys_futex(&pThis->cWaiters, FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
|
---|
124 | usleep(1000);
|
---|
125 | }
|
---|
126 |
|
---|
127 | /*
|
---|
128 | * Free the semaphore memory and be gone.
|
---|
129 | */
|
---|
130 | RTMemFree(pThis);
|
---|
131 | return VINF_SUCCESS;
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | RTDECL(int) RTSemEventSignal(RTSEMEVENT EventSem)
|
---|
136 | {
|
---|
137 | /*
|
---|
138 | * Validate input.
|
---|
139 | */
|
---|
140 | struct RTSEMEVENTINTERNAL *pThis = EventSem;
|
---|
141 | AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENT_MAGIC,
|
---|
142 | VERR_INVALID_HANDLE);
|
---|
143 | /*
|
---|
144 | * Try signal it.
|
---|
145 | */
|
---|
146 | for (unsigned i = 0;; i++)
|
---|
147 | {
|
---|
148 | int32_t iCur = pThis->cWaiters;
|
---|
149 | if (iCur == 0)
|
---|
150 | {
|
---|
151 | if (ASMAtomicCmpXchgS32(&pThis->cWaiters, -1, 0))
|
---|
152 | break; /* nobody is waiting */
|
---|
153 | }
|
---|
154 | else if (iCur < 0)
|
---|
155 | break; /* already signaled */
|
---|
156 | else
|
---|
157 | {
|
---|
158 | /* somebody is waiting, try wake up one of them. */
|
---|
159 | long cWoken = sys_futex(&pThis->cWaiters, FUTEX_WAKE, 1, NULL, NULL, 0);
|
---|
160 | if (RT_LIKELY(cWoken == 1))
|
---|
161 | {
|
---|
162 | ASMAtomicDecS32(&pThis->cWaiters);
|
---|
163 | break;
|
---|
164 | }
|
---|
165 | AssertMsg(cWoken == 0, ("%ld\n", cWoken));
|
---|
166 |
|
---|
167 | /*
|
---|
168 | * This path is taken in two situations:
|
---|
169 | * 1) A waiting thread is returning from the sys_futex call with a
|
---|
170 | * non-zero return value.
|
---|
171 | * 2) There are two threads signaling the event at the
|
---|
172 | * same time and only one thread waiting.
|
---|
173 | *
|
---|
174 | * At this point we know that nobody is activly waiting on the event but
|
---|
175 | * at the same time, we are racing someone updating the state. The current
|
---|
176 | * strategy is to spin till the thread racing us is done, this is kind of
|
---|
177 | * brain dead and need fixing of course.
|
---|
178 | */
|
---|
179 | if (RT_UNLIKELY(i > 32))
|
---|
180 | {
|
---|
181 | if ((i % 128) == 127)
|
---|
182 | usleep(1000);
|
---|
183 | else if (!(i % 4))
|
---|
184 | pthread_yield();
|
---|
185 | else
|
---|
186 | AssertReleaseMsg(i < 4096, ("iCur=%#x pThis=%p\n", iCur, pThis));
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|
190 | return VINF_SUCCESS;
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 | static int rtSemEventWait(RTSEMEVENT EventSem, unsigned cMillies, bool fAutoResume)
|
---|
195 | {
|
---|
196 | /*
|
---|
197 | * Validate input.
|
---|
198 | */
|
---|
199 | struct RTSEMEVENTINTERNAL *pThis = EventSem;
|
---|
200 | AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENT_MAGIC,
|
---|
201 | VERR_INVALID_HANDLE);
|
---|
202 |
|
---|
203 | /*
|
---|
204 | * Quickly check whether it's signaled.
|
---|
205 | */
|
---|
206 | if (ASMAtomicCmpXchgS32(&pThis->cWaiters, 0, -1))
|
---|
207 | return VINF_SUCCESS;
|
---|
208 |
|
---|
209 | /*
|
---|
210 | * Convert timeout value.
|
---|
211 | */
|
---|
212 | struct timespec ts;
|
---|
213 | struct timespec *pTimeout = NULL;
|
---|
214 | if (cMillies != RT_INDEFINITE_WAIT)
|
---|
215 | {
|
---|
216 | ts.tv_sec = cMillies / 1000;
|
---|
217 | ts.tv_nsec = (cMillies % 1000) * 1000000;
|
---|
218 | pTimeout = &ts;
|
---|
219 | }
|
---|
220 |
|
---|
221 | /*
|
---|
222 | * The wait loop.
|
---|
223 | */
|
---|
224 | for (unsigned i = 0;; i++)
|
---|
225 | {
|
---|
226 | /*
|
---|
227 | * Announce that we're among the waiters.
|
---|
228 | */
|
---|
229 | int32_t iNew = ASMAtomicIncS32(&pThis->cWaiters);
|
---|
230 | if (iNew == 0)
|
---|
231 | return VINF_SUCCESS;
|
---|
232 | if (RT_LIKELY(iNew > 0))
|
---|
233 | {
|
---|
234 | /*
|
---|
235 | * Go to sleep.
|
---|
236 | */
|
---|
237 | long rc = sys_futex(&pThis->cWaiters, FUTEX_WAIT, iNew, pTimeout, NULL, 0);
|
---|
238 | if (RT_UNLIKELY(pThis->iMagic != RTSEMEVENT_MAGIC))
|
---|
239 | return VERR_SEM_DESTROYED;
|
---|
240 |
|
---|
241 | /* Did somebody wake us up from RTSemEventSignal()? */
|
---|
242 | if (rc == 0)
|
---|
243 | return VINF_SUCCESS;
|
---|
244 |
|
---|
245 | /* No, then the kernel woke us up or we failed going to sleep. Adjust the accounting. */
|
---|
246 | iNew = ASMAtomicDecS32(&pThis->cWaiters);
|
---|
247 | Assert(iNew >= 0);
|
---|
248 |
|
---|
249 | /*
|
---|
250 | * Act on the wakup code.
|
---|
251 | */
|
---|
252 | if (rc == -ETIMEDOUT)
|
---|
253 | {
|
---|
254 | Assert(pTimeout);
|
---|
255 | return VERR_TIMEOUT;
|
---|
256 | }
|
---|
257 | if (rc == -EWOULDBLOCK)
|
---|
258 | /* retry with new value. */;
|
---|
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
|
---|
272 | {
|
---|
273 | /* this can't happen. */
|
---|
274 | if (RT_UNLIKELY(pThis->iMagic != RTSEMEVENT_MAGIC))
|
---|
275 | return VERR_SEM_DESTROYED;
|
---|
276 | AssertReleaseMsgFailed(("iNew=%d\n", iNew));
|
---|
277 | }
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 |
|
---|
282 | RTDECL(int) RTSemEventWait(RTSEMEVENT EventSem, unsigned cMillies)
|
---|
283 | {
|
---|
284 | int rc = rtSemEventWait(EventSem, cMillies, true);
|
---|
285 | Assert(rc != VERR_INTERRUPTED);
|
---|
286 | Assert(rc != VERR_TIMEOUT || cMillies != RT_INDEFINITE_WAIT);
|
---|
287 | return rc;
|
---|
288 | }
|
---|
289 |
|
---|
290 |
|
---|
291 | RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)
|
---|
292 | {
|
---|
293 | return rtSemEventWait(EventSem, cMillies, false);
|
---|
294 | }
|
---|
295 |
|
---|