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