VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/linux/semevent-linux.cpp@ 11462

Last change on this file since 11462 was 10839, checked in by vboxsync, 17 years ago

RTSemEventDestroy: Don't bitch on NIL_RTSEMEVENT.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.7 KB
Line 
1/* $Id: semevent-linux.cpp 10839 2008-07-23 19:48:51Z 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 */
61struct 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 */
77static 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
91RTDECL(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
108RTDECL(int) RTSemEventDestroy(RTSEMEVENT EventSem)
109{
110 /*
111 * Validate input.
112 */
113 if (EventSem == NIL_RTSEMEVENT) /* don't bitch */
114 return VERR_INVALID_HANDLE;
115 struct RTSEMEVENTINTERNAL *pThis = EventSem;
116 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
117 AssertReturn(pThis->iMagic == RTSEMEVENT_MAGIC, VERR_INVALID_HANDLE);
118
119 /*
120 * Invalidate the semaphore and wake up anyone waiting on it.
121 */
122 ASMAtomicXchgSize(&pThis->iMagic, RTSEMEVENT_MAGIC + 1);
123 if (ASMAtomicXchgS32(&pThis->cWaiters, INT32_MIN / 2) > 0)
124 {
125 sys_futex(&pThis->cWaiters, FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
126 usleep(1000);
127 }
128
129 /*
130 * Free the semaphore memory and be gone.
131 */
132 RTMemFree(pThis);
133 return VINF_SUCCESS;
134}
135
136
137RTDECL(int) RTSemEventSignal(RTSEMEVENT EventSem)
138{
139 /*
140 * Validate input.
141 */
142 struct RTSEMEVENTINTERNAL *pThis = EventSem;
143 AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENT_MAGIC,
144 VERR_INVALID_HANDLE);
145 /*
146 * Try signal it.
147 */
148 for (unsigned i = 0;; i++)
149 {
150 int32_t iCur = pThis->cWaiters;
151 if (iCur == 0)
152 {
153 if (ASMAtomicCmpXchgS32(&pThis->cWaiters, -1, 0))
154 break; /* nobody is waiting */
155 }
156 else if (iCur < 0)
157 break; /* already signaled */
158 else
159 {
160 /* somebody is waiting, try wake up one of them. */
161 long cWoken = sys_futex(&pThis->cWaiters, FUTEX_WAKE, 1, NULL, NULL, 0);
162 if (RT_LIKELY(cWoken == 1))
163 {
164 ASMAtomicDecS32(&pThis->cWaiters);
165 break;
166 }
167 AssertMsg(cWoken == 0, ("%ld\n", cWoken));
168
169 /*
170 * This path is taken in two situations:
171 * 1) A waiting thread is returning from the sys_futex call with a
172 * non-zero return value.
173 * 2) There are two threads signaling the event at the
174 * same time and only one thread waiting.
175 *
176 * At this point we know that nobody is activly waiting on the event but
177 * at the same time, we are racing someone updating the state. The current
178 * strategy is to spin till the thread racing us is done, this is kind of
179 * brain dead and need fixing of course.
180 */
181 if (RT_UNLIKELY(i > 32))
182 {
183 if ((i % 128) == 127)
184 usleep(1000);
185 else if (!(i % 4))
186 pthread_yield();
187 else
188 AssertReleaseMsg(i < 4096, ("iCur=%#x pThis=%p\n", iCur, pThis));
189 }
190 }
191 }
192 return VINF_SUCCESS;
193}
194
195
196static int rtSemEventWait(RTSEMEVENT EventSem, unsigned cMillies, bool fAutoResume)
197{
198 /*
199 * Validate input.
200 */
201 struct RTSEMEVENTINTERNAL *pThis = EventSem;
202 AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENT_MAGIC,
203 VERR_INVALID_HANDLE);
204
205 /*
206 * Quickly check whether it's signaled.
207 */
208 if (ASMAtomicCmpXchgS32(&pThis->cWaiters, 0, -1))
209 return VINF_SUCCESS;
210
211 /*
212 * Convert timeout value.
213 */
214 struct timespec ts;
215 struct timespec *pTimeout = NULL;
216 if (cMillies != RT_INDEFINITE_WAIT)
217 {
218 ts.tv_sec = cMillies / 1000;
219 ts.tv_nsec = (cMillies % 1000) * 1000000;
220 pTimeout = &ts;
221 }
222
223 /*
224 * The wait loop.
225 */
226 for (unsigned i = 0;; i++)
227 {
228 /*
229 * Announce that we're among the waiters.
230 */
231 int32_t iNew = ASMAtomicIncS32(&pThis->cWaiters);
232 if (iNew == 0)
233 return VINF_SUCCESS;
234 if (RT_LIKELY(iNew > 0))
235 {
236 /*
237 * Go to sleep.
238 */
239 long rc = sys_futex(&pThis->cWaiters, FUTEX_WAIT, iNew, pTimeout, NULL, 0);
240 if (RT_UNLIKELY(pThis->iMagic != RTSEMEVENT_MAGIC))
241 return VERR_SEM_DESTROYED;
242
243 /* Did somebody wake us up from RTSemEventSignal()? */
244 if (rc == 0)
245 return VINF_SUCCESS;
246
247 /* No, then the kernel woke us up or we failed going to sleep. Adjust the accounting. */
248 iNew = ASMAtomicDecS32(&pThis->cWaiters);
249 Assert(iNew >= 0);
250
251 /*
252 * Act on the wakup code.
253 */
254 if (rc == -ETIMEDOUT)
255 {
256 Assert(pTimeout);
257 return VERR_TIMEOUT;
258 }
259 if (rc == -EWOULDBLOCK)
260 /* retry with new value. */;
261 else if (rc == -EINTR)
262 {
263 if (!fAutoResume)
264 return VERR_INTERRUPTED;
265 }
266 else
267 {
268 /* this shouldn't happen! */
269 AssertMsgFailed(("rc=%ld errno=%d\n", rc, errno));
270 return RTErrConvertFromErrno(rc);
271 }
272 }
273 else
274 {
275 /* this can't happen. */
276 if (RT_UNLIKELY(pThis->iMagic != RTSEMEVENT_MAGIC))
277 return VERR_SEM_DESTROYED;
278 AssertReleaseMsgFailed(("iNew=%d\n", iNew));
279 }
280 }
281}
282
283
284RTDECL(int) RTSemEventWait(RTSEMEVENT EventSem, unsigned cMillies)
285{
286 int rc = rtSemEventWait(EventSem, cMillies, true);
287 Assert(rc != VERR_INTERRUPTED);
288 Assert(rc != VERR_TIMEOUT || cMillies != RT_INDEFINITE_WAIT);
289 return rc;
290}
291
292
293RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)
294{
295 return rtSemEventWait(EventSem, cMillies, false);
296}
297
Note: See TracBrowser for help on using the repository browser.

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