VirtualBox

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

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

iprt/lockvalidation: give better names to anonymous locks

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.6 KB
Line 
1/* $Id: semevent-linux.cpp 25831 2010-01-14 15:12:53Z 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 if (!pszNameFmt)
140 {
141 static uint32_t volatile s_iSemEventAnon = 0;
142 RTLockValidatorRecSharedInit(&pThis->Signallers, hClass, RTLOCKVAL_SUB_CLASS_ANY, pThis,
143 true /*fSignaller*/, !(fFlags & RTSEMEVENT_FLAGS_NO_LOCK_VAL),
144 "RTSemEvent-%u", ASMAtomicIncU32(&s_iSemEventAnon) - 1);
145 }
146 else
147 {
148 va_list va;
149 va_start(va, pszNameFmt);
150 RTLockValidatorRecSharedInitV(&pThis->Signallers, hClass, RTLOCKVAL_SUB_CLASS_ANY, pThis,
151 true /*fSignaller*/, !(fFlags & RTSEMEVENT_FLAGS_NO_LOCK_VAL),
152 pszNameFmt, va);
153 va_end(va);
154 }
155 pThis->fEverHadSignallers = false;
156#endif
157
158 *phEventSem = pThis;
159 return VINF_SUCCESS;
160 }
161 return VERR_NO_MEMORY;
162}
163
164
165RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
166{
167 /*
168 * Validate input.
169 */
170 struct RTSEMEVENTINTERNAL *pThis = hEventSem;
171 if (pThis == NIL_RTSEMEVENT)
172 return VINF_SUCCESS;
173 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
174 AssertReturn(pThis->iMagic == RTSEMEVENT_MAGIC, VERR_INVALID_HANDLE);
175
176 /*
177 * Invalidate the semaphore and wake up anyone waiting on it.
178 */
179 ASMAtomicXchgSize(&pThis->iMagic, RTSEMEVENT_MAGIC | UINT32_C(0x80000000));
180 if (ASMAtomicXchgS32(&pThis->cWaiters, INT32_MIN / 2) > 0)
181 {
182 sys_futex(&pThis->fSignalled, FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
183 usleep(1000);
184 }
185
186 /*
187 * Free the semaphore memory and be gone.
188 */
189#ifdef RTSEMEVENT_STRICT
190 RTLockValidatorRecSharedDelete(&pThis->Signallers);
191#endif
192 RTMemFree(pThis);
193 return VINF_SUCCESS;
194}
195
196
197RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
198{
199 /*
200 * Validate input.
201 */
202 struct RTSEMEVENTINTERNAL *pThis = hEventSem;
203 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
204 AssertReturn(pThis->iMagic == RTSEMEVENT_MAGIC, VERR_INVALID_HANDLE);
205
206#ifdef RTSEMEVENT_STRICT
207 if (pThis->fEverHadSignallers)
208 {
209 int rc9 = RTLockValidatorRecSharedCheckSignaller(&pThis->Signallers, NIL_RTTHREAD);
210 if (RT_FAILURE(rc9))
211 return rc9;
212 }
213#endif
214
215 ASMAtomicWriteU32(&pThis->fSignalled, 1);
216 if (ASMAtomicReadS32(&pThis->cWaiters) < 1)
217 return VINF_SUCCESS;
218
219 /* somebody is waiting, try wake up one of them. */
220 long cWoken = sys_futex(&pThis->fSignalled, FUTEX_WAKE, 1, NULL, NULL, 0);
221 if (RT_LIKELY(cWoken >= 0))
222 return VINF_SUCCESS;
223
224 if (RT_UNLIKELY(pThis->iMagic != RTSEMEVENT_MAGIC))
225 return VERR_SEM_DESTROYED;
226
227 return VERR_INVALID_PARAMETER;
228}
229
230
231static int rtSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies, bool fAutoResume)
232{
233 PCRTLOCKVALSRCPOS pSrcPos = NULL;
234
235 /*
236 * Validate input.
237 */
238 struct RTSEMEVENTINTERNAL *pThis = hEventSem;
239 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
240 AssertReturn(pThis->iMagic == RTSEMEVENT_MAGIC, VERR_INVALID_HANDLE);
241
242 /*
243 * Quickly check whether it's signaled.
244 */
245 /** @todo this isn't fair if someone is already waiting on it. They should
246 * have the first go at it!
247 * (ASMAtomicReadS32(&pThis->cWaiters) == 0 || !cMillies) && ... */
248 if (ASMAtomicCmpXchgU32(&pThis->fSignalled, 0, 1))
249 return VINF_SUCCESS;
250
251 /*
252 * Convert the timeout value.
253 */
254 struct timespec ts;
255 struct timespec *pTimeout = NULL;
256 uint64_t u64End = 0; /* shut up gcc */
257 if (cMillies != RT_INDEFINITE_WAIT)
258 {
259 if (!cMillies)
260 return VERR_TIMEOUT;
261 ts.tv_sec = cMillies / 1000;
262 ts.tv_nsec = (cMillies % 1000) * UINT32_C(1000000);
263 u64End = RTTimeSystemNanoTS() + cMillies * UINT64_C(1000000);
264 pTimeout = &ts;
265 }
266
267 ASMAtomicIncS32(&pThis->cWaiters);
268
269 /*
270 * The wait loop.
271 */
272#ifdef RTSEMEVENT_STRICT
273 RTTHREAD hThreadSelf = RTThreadSelfAutoAdopt();
274#else
275 RTTHREAD hThreadSelf = RTThreadSelf();
276#endif
277 int rc = VINF_SUCCESS;
278 for (;;)
279 {
280#ifdef RTSEMEVENT_STRICT
281 if (pThis->fEverHadSignallers)
282 {
283 rc = RTLockValidatorRecSharedCheckBlocking(&pThis->Signallers, hThreadSelf, pSrcPos, false,
284 cMillies, RTTHREADSTATE_EVENT, true);
285 if (RT_FAILURE(rc))
286 break;
287 }
288#endif
289 RTThreadBlocking(hThreadSelf, RTTHREADSTATE_EVENT, true);
290 long lrc = sys_futex(&pThis->fSignalled, FUTEX_WAIT, 0, pTimeout, NULL, 0);
291 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_EVENT);
292 if (RT_UNLIKELY(pThis->iMagic != RTSEMEVENT_MAGIC))
293 {
294 rc = VERR_SEM_DESTROYED;
295 break;
296 }
297
298 if (RT_LIKELY(lrc == 0 || lrc == -EWOULDBLOCK))
299 {
300 /* successful wakeup or fSignalled > 0 in the meantime */
301 if (ASMAtomicCmpXchgU32(&pThis->fSignalled, 0, 1))
302 break;
303 }
304 else if (lrc == -ETIMEDOUT)
305 {
306 rc = VERR_TIMEOUT;
307 break;
308 }
309 else if (lrc == -EINTR)
310 {
311 if (!fAutoResume)
312 {
313 rc = VERR_INTERRUPTED;
314 break;
315 }
316 }
317 else
318 {
319 /* this shouldn't happen! */
320 AssertMsgFailed(("rc=%ld errno=%d\n", lrc, errno));
321 rc = RTErrConvertFromErrno(lrc);
322 break;
323 }
324 /* adjust the relative timeout */
325 if (pTimeout)
326 {
327 int64_t i64Diff = u64End - RTTimeSystemNanoTS();
328 if (i64Diff < 1000)
329 {
330 rc = VERR_TIMEOUT;
331 break;
332 }
333 ts.tv_sec = (uint64_t)i64Diff / UINT32_C(1000000000);
334 ts.tv_nsec = (uint64_t)i64Diff % UINT32_C(1000000000);
335 }
336 }
337
338 ASMAtomicDecS32(&pThis->cWaiters);
339 return rc;
340}
341
342
343RTDECL(int) RTSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
344{
345 int rc = rtSemEventWait(hEventSem, cMillies, true);
346 Assert(rc != VERR_INTERRUPTED);
347 Assert(rc != VERR_TIMEOUT || cMillies != RT_INDEFINITE_WAIT);
348 return rc;
349}
350
351
352RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
353{
354 return rtSemEventWait(hEventSem, cMillies, false);
355}
356
357
358RTDECL(void) RTSemEventSetSignaller(RTSEMEVENT hEventSem, RTTHREAD hThread)
359{
360#ifdef RTSEMEVENT_STRICT
361 struct RTSEMEVENTINTERNAL *pThis = hEventSem;
362 AssertPtrReturnVoid(pThis);
363 AssertReturnVoid(pThis->iMagic == RTSEMEVENT_MAGIC);
364
365 ASMAtomicWriteBool(&pThis->fEverHadSignallers, true);
366 RTLockValidatorRecSharedResetOwner(&pThis->Signallers, hThread, NULL);
367#endif
368}
369
370
371RTDECL(void) RTSemEventAddSignaller(RTSEMEVENT hEventSem, RTTHREAD hThread)
372{
373#ifdef RTSEMEVENT_STRICT
374 struct RTSEMEVENTINTERNAL *pThis = hEventSem;
375 AssertPtrReturnVoid(pThis);
376 AssertReturnVoid(pThis->iMagic == RTSEMEVENT_MAGIC);
377
378 ASMAtomicWriteBool(&pThis->fEverHadSignallers, true);
379 RTLockValidatorRecSharedAddOwner(&pThis->Signallers, hThread, NULL);
380#endif
381}
382
383
384RTDECL(void) RTSemEventRemoveSignaller(RTSEMEVENT hEventSem, RTTHREAD hThread)
385{
386#ifdef RTSEMEVENT_STRICT
387 struct RTSEMEVENTINTERNAL *pThis = hEventSem;
388 AssertPtrReturnVoid(pThis);
389 AssertReturnVoid(pThis->iMagic == RTSEMEVENT_MAGIC);
390
391 RTLockValidatorRecSharedRemoveOwner(&pThis->Signallers, hThread);
392#endif
393}
394
395#endif /* glibc < 2.6 || IPRT_WITH_FUTEX_BASED_SEMS */
396
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