VirtualBox

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

最後變更 在這個檔案從30976是 28800,由 vboxsync 提交於 15 年 前

Automated rebranding to Oracle copyright/license strings via filemuncher

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

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