VirtualBox

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

Last change on this file since 25724 was 25724, checked in by vboxsync, 15 years ago

iprt: Use RTMSINTERVAL for timeouts. Fixed missing timeout underflow checks in two RTFileAioCtxWait implementations.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.2 KB
Line 
1/* $Id: semevent-linux.cpp 25724 2010-01-11 14:45:34Z vboxsync $ */
2/** @file
3 * IPRT - Event Semaphore, Linux (2.6.x+).
4 */
5
6/*
7 * Copyright (C) 2006-2010 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#include <features.h>
32#if __GLIBC_PREREQ(2,6) && !defined(IPRT_WITH_FUTEX_BASED_SEMS)
33
34/*
35 * glibc 2.6 fixed a serious bug in the mutex implementation. We wrote this
36 * linux specific event semaphores code in order to work around the bug. We
37 * will fall back on the pthread-based implementation if glibc is known to
38 * contain the bug fix.
39 *
40 * The external refernce to epoll_pwait is a hack which prevents that we link
41 * against glibc < 2.6.
42 */
43#include "../posix/semevent-posix.cpp"
44asm volatile (".global epoll_pwait");
45
46#else /* glibc < 2.6 */
47
48/*******************************************************************************
49* Header Files *
50*******************************************************************************/
51#include <iprt/semaphore.h>
52#include "internal/iprt.h"
53
54#include <iprt/asm.h>
55#include <iprt/assert.h>
56#include <iprt/err.h>
57#include <iprt/lockvalidator.h>
58#include <iprt/mem.h>
59#include <iprt/time.h>
60#include "internal/magics.h"
61#include "internal/strict.h"
62
63#include <errno.h>
64#include <limits.h>
65#include <pthread.h>
66#include <unistd.h>
67#include <sys/time.h>
68#include <sys/syscall.h>
69#if 0 /* With 2.6.17 futex.h has become C++ unfriendly. */
70# include <linux/futex.h>
71#else
72# define FUTEX_WAIT 0
73# define FUTEX_WAKE 1
74#endif
75
76
77/*******************************************************************************
78* Structures and Typedefs *
79*******************************************************************************/
80/**
81 * Linux (single wakup) event semaphore.
82 */
83struct RTSEMEVENTINTERNAL
84{
85 /** Magic value. */
86 intptr_t volatile iMagic;
87 /** The futex state variable.
88 * 0 means not signalled.
89 1 means signalled. */
90 uint32_t volatile fSignalled;
91 /** The number of waiting threads */
92 int32_t volatile cWaiters;
93#ifdef RTSEMEVENT_STRICT
94 /** Signallers. */
95 RTLOCKVALRECSHRD Signallers;
96 /** Indicates that lock validation should be performed. */
97 bool volatile fEverHadSignallers;
98#endif
99};
100
101
102/**
103 * Wrapper for the futex syscall.
104 */
105static long sys_futex(uint32_t volatile *uaddr, int op, int val, struct timespec *utime, int32_t *uaddr2, int val3)
106{
107 errno = 0;
108 long rc = syscall(__NR_futex, uaddr, op, val, utime, uaddr2, val3);
109 if (rc < 0)
110 {
111 Assert(rc == -1);
112 rc = -errno;
113 }
114 return rc;
115}
116
117
118
119RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
120{
121 return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
122}
123
124
125RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
126{
127 AssertReturn(!(fFlags & ~RTSEMEVENT_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
128
129 /*
130 * Allocate semaphore handle.
131 */
132 struct RTSEMEVENTINTERNAL *pThis = (struct RTSEMEVENTINTERNAL *)RTMemAlloc(sizeof(struct RTSEMEVENTINTERNAL));
133 if (pThis)
134 {
135 pThis->iMagic = RTSEMEVENT_MAGIC;
136 pThis->cWaiters = 0;
137 pThis->fSignalled = 0;
138#ifdef RTSEMEVENT_STRICT
139 va_list va;
140 va_start(va, pszNameFmt);
141 RTLockValidatorRecSharedInitV(&pThis->Signallers, hClass, RTLOCKVAL_SUB_CLASS_ANY, pThis,
142 true /*fSignaller*/, !(fFlags & RTSEMEVENT_FLAGS_NO_LOCK_VAL),
143 pszNameFmt, va);
144 va_end(va);
145 pThis->fEverHadSignallers = false;
146#endif
147
148 *phEventSem = pThis;
149 return VINF_SUCCESS;
150 }
151 return VERR_NO_MEMORY;
152}
153
154
155RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
156{
157 /*
158 * Validate input.
159 */
160 struct RTSEMEVENTINTERNAL *pThis = hEventSem;
161 if (pThis == NIL_RTSEMEVENT)
162 return VINF_SUCCESS;
163 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
164 AssertReturn(pThis->iMagic == RTSEMEVENT_MAGIC, VERR_INVALID_HANDLE);
165
166 /*
167 * Invalidate the semaphore and wake up anyone waiting on it.
168 */
169 ASMAtomicXchgSize(&pThis->iMagic, RTSEMEVENT_MAGIC | UINT32_C(0x80000000));
170 if (ASMAtomicXchgS32(&pThis->cWaiters, INT32_MIN / 2) > 0)
171 {
172 sys_futex(&pThis->fSignalled, FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
173 usleep(1000);
174 }
175
176 /*
177 * Free the semaphore memory and be gone.
178 */
179#ifdef RTSEMEVENT_STRICT
180 RTLockValidatorRecSharedDelete(&pThis->Signallers);
181#endif
182 RTMemFree(pThis);
183 return VINF_SUCCESS;
184}
185
186
187RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
188{
189 /*
190 * Validate input.
191 */
192 struct RTSEMEVENTINTERNAL *pThis = hEventSem;
193 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
194 AssertReturn(pThis->iMagic == RTSEMEVENT_MAGIC, VERR_INVALID_HANDLE);
195
196#ifdef RTSEMEVENT_STRICT
197 if (pThis->fEverHadSignallers)
198 {
199 int rc9 = RTLockValidatorRecSharedCheckSignaller(&pThis->Signallers, NIL_RTTHREAD);
200 if (RT_FAILURE(rc9))
201 return rc9;
202 }
203#endif
204
205 ASMAtomicWriteU32(&pThis->fSignalled, 1);
206 if (ASMAtomicReadS32(&pThis->cWaiters) < 1)
207 return VINF_SUCCESS;
208
209 /* somebody is waiting, try wake up one of them. */
210 long cWoken = sys_futex(&pThis->fSignalled, FUTEX_WAKE, 1, NULL, NULL, 0);
211 if (RT_LIKELY(cWoken >= 0))
212 return VINF_SUCCESS;
213
214 if (RT_UNLIKELY(pThis->iMagic != RTSEMEVENT_MAGIC))
215 return VERR_SEM_DESTROYED;
216
217 return VERR_INVALID_PARAMETER;
218}
219
220
221static int rtSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies, bool fAutoResume)
222{
223 PCRTLOCKVALSRCPOS pSrcPos = NULL;
224
225 /*
226 * Validate input.
227 */
228 struct RTSEMEVENTINTERNAL *pThis = hEventSem;
229 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
230 AssertReturn(pThis->iMagic == RTSEMEVENT_MAGIC, VERR_INVALID_HANDLE);
231
232 /*
233 * Quickly check whether it's signaled.
234 */
235 /** @todo this isn't fair if someone is already waiting on it. They should
236 * have the first go at it!
237 * (ASMAtomicReadS32(&pThis->cWaiters) == 0 || !cMillies) && ... */
238 if (ASMAtomicCmpXchgU32(&pThis->fSignalled, 0, 1))
239 return VINF_SUCCESS;
240
241 /*
242 * Convert the timeout value.
243 */
244 struct timespec ts;
245 struct timespec *pTimeout = NULL;
246 uint64_t u64End = 0; /* shut up gcc */
247 if (cMillies != RT_INDEFINITE_WAIT)
248 {
249 if (!cMillies)
250 return VERR_TIMEOUT;
251 ts.tv_sec = cMillies / 1000;
252 ts.tv_nsec = (cMillies % 1000) * UINT32_C(1000000);
253 u64End = RTTimeSystemNanoTS() + cMillies * UINT64_C(1000000);
254 pTimeout = &ts;
255 }
256
257 ASMAtomicIncS32(&pThis->cWaiters);
258
259 /*
260 * The wait loop.
261 */
262#ifdef RTSEMEVENT_STRICT
263 RTTHREAD hThreadSelf = RTThreadSelfAutoAdopt();
264#else
265 RTTHREAD hThreadSelf = RTThreadSelf();
266#endif
267 int rc = VINF_SUCCESS;
268 for (;;)
269 {
270#ifdef RTSEMEVENT_STRICT
271 if (pThis->fEverHadSignallers)
272 {
273 rc = RTLockValidatorRecSharedCheckBlocking(&pThis->Signallers, hThreadSelf, pSrcPos, false,
274 cMillies, RTTHREADSTATE_EVENT, true);
275 if (RT_FAILURE(rc))
276 break;
277 }
278#endif
279 RTThreadBlocking(hThreadSelf, RTTHREADSTATE_EVENT, true);
280 long lrc = sys_futex(&pThis->fSignalled, FUTEX_WAIT, 0, pTimeout, NULL, 0);
281 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_EVENT);
282 if (RT_UNLIKELY(pThis->iMagic != RTSEMEVENT_MAGIC))
283 {
284 rc = VERR_SEM_DESTROYED;
285 break;
286 }
287
288 if (RT_LIKELY(lrc == 0 || lrc == -EWOULDBLOCK))
289 {
290 /* successful wakeup or fSignalled > 0 in the meantime */
291 if (ASMAtomicCmpXchgU32(&pThis->fSignalled, 0, 1))
292 break;
293 }
294 else if (lrc == -ETIMEDOUT)
295 {
296 rc = VERR_TIMEOUT;
297 break;
298 }
299 else if (lrc == -EINTR)
300 {
301 if (!fAutoResume)
302 {
303 rc = VERR_INTERRUPTED;
304 break;
305 }
306 }
307 else
308 {
309 /* this shouldn't happen! */
310 AssertMsgFailed(("rc=%ld errno=%d\n", lrc, errno));
311 rc = RTErrConvertFromErrno(lrc);
312 break;
313 }
314 /* adjust the relative timeout */
315 if (pTimeout)
316 {
317 int64_t i64Diff = u64End - RTTimeSystemNanoTS();
318 if (i64Diff < 1000)
319 {
320 rc = VERR_TIMEOUT;
321 break;
322 }
323 ts.tv_sec = (uint64_t)i64Diff / UINT32_C(1000000000);
324 ts.tv_nsec = (uint64_t)i64Diff % UINT32_C(1000000000);
325 }
326 }
327
328 ASMAtomicDecS32(&pThis->cWaiters);
329 return rc;
330}
331
332
333RTDECL(int) RTSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
334{
335 int rc = rtSemEventWait(hEventSem, cMillies, true);
336 Assert(rc != VERR_INTERRUPTED);
337 Assert(rc != VERR_TIMEOUT || cMillies != RT_INDEFINITE_WAIT);
338 return rc;
339}
340
341
342RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
343{
344 return rtSemEventWait(hEventSem, cMillies, false);
345}
346
347
348RTDECL(void) RTSemEventSetSignaller(RTSEMEVENT hEventSem, RTTHREAD hThread)
349{
350#ifdef RTSEMEVENT_STRICT
351 struct RTSEMEVENTINTERNAL *pThis = hEventSem;
352 AssertPtrReturnVoid(pThis);
353 AssertReturnVoid(pThis->iMagic == RTSEMEVENT_MAGIC);
354
355 ASMAtomicWriteBool(&pThis->fEverHadSignallers, true);
356 RTLockValidatorRecSharedResetOwner(&pThis->Signallers, hThread, NULL);
357#endif
358}
359
360
361RTDECL(void) RTSemEventAddSignaller(RTSEMEVENT hEventSem, RTTHREAD hThread)
362{
363#ifdef RTSEMEVENT_STRICT
364 struct RTSEMEVENTINTERNAL *pThis = hEventSem;
365 AssertPtrReturnVoid(pThis);
366 AssertReturnVoid(pThis->iMagic == RTSEMEVENT_MAGIC);
367
368 ASMAtomicWriteBool(&pThis->fEverHadSignallers, true);
369 RTLockValidatorRecSharedAddOwner(&pThis->Signallers, hThread, NULL);
370#endif
371}
372
373
374RTDECL(void) RTSemEventRemoveSignaller(RTSEMEVENT hEventSem, RTTHREAD hThread)
375{
376#ifdef RTSEMEVENT_STRICT
377 struct RTSEMEVENTINTERNAL *pThis = hEventSem;
378 AssertPtrReturnVoid(pThis);
379 AssertReturnVoid(pThis->iMagic == RTSEMEVENT_MAGIC);
380
381 RTLockValidatorRecSharedRemoveOwner(&pThis->Signallers, hThread);
382#endif
383}
384
385#endif /* glibc < 2.6 || IPRT_WITH_FUTEX_BASED_SEMS */
386
Note: See TracBrowser for help on using the repository browser.

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