VirtualBox

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

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

i64Diff not u64Diff

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.8 KB
Line 
1/* $Id: semevent-linux.cpp 22958 2009-09-11 13:30:27Z 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#include <features.h>
32#if __GLIBC_PREREQ(2,6)
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 <iprt/assert.h>
53#include <iprt/alloc.h>
54#include <iprt/asm.h>
55#include <iprt/err.h>
56#include <iprt/time.h>
57#include "internal/magics.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};
90
91
92/**
93 * Wrapper for the futex syscall.
94 */
95static long sys_futex(uint32_t volatile *uaddr, int op, int val, struct timespec *utime, int32_t *uaddr2, int val3)
96{
97 errno = 0;
98 long rc = syscall(__NR_futex, uaddr, op, val, utime, uaddr2, val3);
99 if (rc < 0)
100 {
101 Assert(rc == -1);
102 rc = -errno;
103 }
104 return rc;
105}
106
107
108
109RTDECL(int) RTSemEventCreate(PRTSEMEVENT pEventSem)
110{
111 /*
112 * Allocate semaphore handle.
113 */
114 struct RTSEMEVENTINTERNAL *pThis = (struct RTSEMEVENTINTERNAL *)RTMemAlloc(sizeof(struct RTSEMEVENTINTERNAL));
115 if (pThis)
116 {
117 pThis->iMagic = RTSEMEVENT_MAGIC;
118 pThis->cWaiters = 0;
119 pThis->fSignalled = 0;
120 *pEventSem = pThis;
121 return VINF_SUCCESS;
122 }
123 return VERR_NO_MEMORY;
124}
125
126
127RTDECL(int) RTSemEventDestroy(RTSEMEVENT EventSem)
128{
129 /*
130 * Validate input.
131 */
132 if (EventSem == NIL_RTSEMEVENT) /* don't bitch */
133 return VERR_INVALID_HANDLE;
134 struct RTSEMEVENTINTERNAL *pThis = EventSem;
135 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
136 AssertReturn(pThis->iMagic == RTSEMEVENT_MAGIC, VERR_INVALID_HANDLE);
137
138 /*
139 * Invalidate the semaphore and wake up anyone waiting on it.
140 */
141 ASMAtomicXchgSize(&pThis->iMagic, RTSEMEVENT_MAGIC | UINT32_C(0x80000000));
142 if (ASMAtomicXchgS32(&pThis->cWaiters, INT32_MIN / 2) > 0)
143 {
144 sys_futex(&pThis->fSignalled, FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
145 usleep(1000);
146 }
147
148 /*
149 * Free the semaphore memory and be gone.
150 */
151 RTMemFree(pThis);
152 return VINF_SUCCESS;
153}
154
155
156RTDECL(int) RTSemEventSignal(RTSEMEVENT EventSem)
157{
158 /*
159 * Validate input.
160 */
161 struct RTSEMEVENTINTERNAL *pThis = EventSem;
162 AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENT_MAGIC,
163 VERR_INVALID_HANDLE);
164
165 ASMAtomicWriteU32(&pThis->fSignalled, 1);
166 if (ASMAtomicReadS32(&pThis->cWaiters) < 1)
167 return VINF_SUCCESS;
168
169 /* somebody is waiting, try wake up one of them. */
170 long cWoken = sys_futex(&pThis->fSignalled, FUTEX_WAKE, 1, NULL, NULL, 0);
171 if (RT_LIKELY(cWoken >= 0))
172 return VINF_SUCCESS;
173
174 if (RT_UNLIKELY(pThis->iMagic != RTSEMEVENT_MAGIC))
175 return VERR_SEM_DESTROYED;
176
177 return VERR_INVALID_PARAMETER;
178}
179
180
181static int rtSemEventWait(RTSEMEVENT EventSem, unsigned cMillies, bool fAutoResume)
182{
183 /*
184 * Validate input.
185 */
186 struct RTSEMEVENTINTERNAL *pThis = EventSem;
187 AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENT_MAGIC,
188 VERR_INVALID_HANDLE);
189
190 /*
191 * Quickly check whether it's signaled.
192 */
193 if (ASMAtomicCmpXchgU32(&pThis->fSignalled, 0, 1))
194 return VINF_SUCCESS;
195
196 /*
197 * Convert timeout value.
198 */
199 struct timespec ts;
200 struct timespec *pTimeout = NULL;
201 uint64_t u64End = 0; /* shut up gcc */
202 if (cMillies != RT_INDEFINITE_WAIT)
203 {
204 ts.tv_sec = cMillies / 1000;
205 ts.tv_nsec = (cMillies % 1000) * 1000000;
206 u64End = RTTimeSystemNanoTS() + cMillies * 1000000;
207 pTimeout = &ts;
208 }
209
210 ASMAtomicIncS32(&pThis->cWaiters);
211
212 /*
213 * The wait loop.
214 */
215 int rc = VINF_SUCCESS;
216 for (;;)
217 {
218 long lrc = sys_futex(&pThis->fSignalled, FUTEX_WAIT, 0, pTimeout, NULL, 0);
219 if (RT_UNLIKELY(pThis->iMagic != RTSEMEVENT_MAGIC))
220 {
221 rc = VERR_SEM_DESTROYED;
222 break;
223 }
224
225 if (RT_LIKELY(lrc == 0 || lrc == -EWOULDBLOCK))
226 {
227 /* successful wakeup or fSignalled > 0 in the meantime */
228 if (ASMAtomicCmpXchgU32(&pThis->fSignalled, 0, 1))
229 break;
230 }
231 else if (lrc == -ETIMEDOUT)
232 {
233 rc = VERR_TIMEOUT;
234 break;
235 }
236 else if (lrc == -EINTR)
237 {
238 if (!fAutoResume)
239 {
240 rc = VERR_INTERRUPTED;
241 break;
242 }
243 }
244 else
245 {
246 /* this shouldn't happen! */
247 AssertMsgFailed(("rc=%ld errno=%d\n", lrc, errno));
248 rc = RTErrConvertFromErrno(lrc);
249 break;
250 }
251 /* adjust the relative timeout */
252 if (pTimeout)
253 {
254 int64_t i64Diff = u64End - RTTimeSystemNanoTS();
255 if (i64Diff < 1000)
256 {
257 rc = VERR_TIMEOUT;
258 break;
259 }
260 ts.tv_sec = i64Diff / 1000000000;
261 ts.tv_nsec = i64Diff % 1000000000;
262 }
263 }
264
265 ASMAtomicDecS32(&pThis->cWaiters);
266 return rc;
267}
268
269
270RTDECL(int) RTSemEventWait(RTSEMEVENT EventSem, unsigned cMillies)
271{
272 int rc = rtSemEventWait(EventSem, cMillies, true);
273 Assert(rc != VERR_INTERRUPTED);
274 Assert(rc != VERR_TIMEOUT || cMillies != RT_INDEFINITE_WAIT);
275 return rc;
276}
277
278
279RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)
280{
281 return rtSemEventWait(EventSem, cMillies, false);
282}
283
284#endif /* glibc < 2.6 */
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