1 | /* $Id: semeventmulti-linux.cpp 8245 2008-04-21 17:24:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Multiple Release 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 multiple wakup event semaphore.
|
---|
60 | */
|
---|
61 | struct RTSEMEVENTMULTIINTERNAL
|
---|
62 | {
|
---|
63 | /** Magic value. */
|
---|
64 | intptr_t volatile iMagic;
|
---|
65 | /** The futex state variable.
|
---|
66 | * -1 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 iState;
|
---|
71 | };
|
---|
72 |
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Wrapper for the futex syscall.
|
---|
76 | */
|
---|
77 | static 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 | RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI pEventMultiSem)
|
---|
91 | {
|
---|
92 | /*
|
---|
93 | * Allocate semaphore handle.
|
---|
94 | */
|
---|
95 | struct RTSEMEVENTMULTIINTERNAL *pThis = (struct RTSEMEVENTMULTIINTERNAL *)RTMemAlloc(sizeof(struct RTSEMEVENTMULTIINTERNAL));
|
---|
96 | if (pThis)
|
---|
97 | {
|
---|
98 | pThis->iMagic = RTSEMEVENTMULTI_MAGIC;
|
---|
99 | pThis->iState = 0;
|
---|
100 | *pEventMultiSem = pThis;
|
---|
101 | return VINF_SUCCESS;
|
---|
102 | }
|
---|
103 | return VERR_NO_MEMORY;
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI EventMultiSem)
|
---|
108 | {
|
---|
109 | /*
|
---|
110 | * Validate input.
|
---|
111 | */
|
---|
112 | struct RTSEMEVENTMULTIINTERNAL *pThis = EventMultiSem;
|
---|
113 | AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENTMULTI_MAGIC,
|
---|
114 | VERR_INVALID_HANDLE);
|
---|
115 |
|
---|
116 | /*
|
---|
117 | * Invalidate the semaphore and wake up anyone waiting on it.
|
---|
118 | */
|
---|
119 | ASMAtomicWriteSize(&pThis->iMagic, RTSEMEVENTMULTI_MAGIC + 1);
|
---|
120 | if (ASMAtomicXchgS32(&pThis->iState, -1) == 1)
|
---|
121 | {
|
---|
122 | sys_futex(&pThis->iState, FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
|
---|
123 | usleep(1000);
|
---|
124 | }
|
---|
125 |
|
---|
126 | /*
|
---|
127 | * Free the semaphore memory and be gone.
|
---|
128 | */
|
---|
129 | RTMemFree(pThis);
|
---|
130 | return VINF_SUCCESS;
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI EventMultiSem)
|
---|
135 | {
|
---|
136 | /*
|
---|
137 | * Validate input.
|
---|
138 | */
|
---|
139 | struct RTSEMEVENTMULTIINTERNAL *pThis = EventMultiSem;
|
---|
140 | AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENTMULTI_MAGIC,
|
---|
141 | VERR_INVALID_HANDLE);
|
---|
142 | /*
|
---|
143 | * Signal it.
|
---|
144 | */
|
---|
145 | int32_t iOld = ASMAtomicXchgS32(&pThis->iState, -1);
|
---|
146 | if (iOld > 0)
|
---|
147 | {
|
---|
148 | /* wake up sleeping threads. */
|
---|
149 | long cWoken = sys_futex(&pThis->iState, FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
|
---|
150 | AssertMsg(cWoken >= 0, ("%ld\n", cWoken)); NOREF(cWoken);
|
---|
151 | }
|
---|
152 | Assert(iOld == 0 || iOld == -1 || iOld == 1);
|
---|
153 | return VINF_SUCCESS;
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI EventMultiSem)
|
---|
158 | {
|
---|
159 | /*
|
---|
160 | * Validate input.
|
---|
161 | */
|
---|
162 | struct RTSEMEVENTMULTIINTERNAL *pThis = EventMultiSem;
|
---|
163 | AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENTMULTI_MAGIC,
|
---|
164 | VERR_INVALID_HANDLE);
|
---|
165 | #ifdef RT_STRICT
|
---|
166 | int32_t i = pThis->iState;
|
---|
167 | Assert(i == 0 || i == -1 || i == 1);
|
---|
168 | #endif
|
---|
169 |
|
---|
170 | /*
|
---|
171 | * Reset it.
|
---|
172 | */
|
---|
173 | ASMAtomicCmpXchgS32(&pThis->iState, 0, -1);
|
---|
174 | return VINF_SUCCESS;
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | static int rtSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies, bool fAutoResume)
|
---|
179 | {
|
---|
180 | /*
|
---|
181 | * Validate input.
|
---|
182 | */
|
---|
183 | struct RTSEMEVENTMULTIINTERNAL *pThis = EventMultiSem;
|
---|
184 | AssertReturn(VALID_PTR(pThis) && pThis->iMagic == RTSEMEVENTMULTI_MAGIC,
|
---|
185 | VERR_INVALID_HANDLE);
|
---|
186 |
|
---|
187 | /*
|
---|
188 | * Quickly check whether it's signaled.
|
---|
189 | */
|
---|
190 | int32_t iCur = ASMAtomicUoReadS32(&pThis->iState);
|
---|
191 | Assert(iCur == 0 || iCur == -1 || iCur == 1);
|
---|
192 | if (iCur == -1)
|
---|
193 | return VINF_SUCCESS;
|
---|
194 | if (!cMillies)
|
---|
195 | return VERR_TIMEOUT;
|
---|
196 |
|
---|
197 | /*
|
---|
198 | * Convert timeout value.
|
---|
199 | */
|
---|
200 | struct timespec ts;
|
---|
201 | struct timespec *pTimeout = NULL;
|
---|
202 | if (cMillies != RT_INDEFINITE_WAIT)
|
---|
203 | {
|
---|
204 | ts.tv_sec = cMillies / 1000;
|
---|
205 | ts.tv_nsec = (cMillies % 1000) * 1000000;
|
---|
206 | pTimeout = &ts;
|
---|
207 | }
|
---|
208 |
|
---|
209 | /*
|
---|
210 | * The wait loop.
|
---|
211 | */
|
---|
212 | for (unsigned i = 0;; i++)
|
---|
213 | {
|
---|
214 | /*
|
---|
215 | * Start waiting. We only account for there being or having been
|
---|
216 | * threads waiting on the semaphore to keep things simple.
|
---|
217 | */
|
---|
218 | iCur = ASMAtomicUoReadS32(&pThis->iState);
|
---|
219 | Assert(iCur == 0 || iCur == -1 || iCur == 1);
|
---|
220 | if ( iCur == 1
|
---|
221 | || ASMAtomicCmpXchgS32(&pThis->iState, 1, 0))
|
---|
222 | {
|
---|
223 | long rc = sys_futex(&pThis->iState, FUTEX_WAIT, 1, pTimeout, NULL, 0);
|
---|
224 | if (RT_UNLIKELY(pThis->iMagic != RTSEMEVENTMULTI_MAGIC))
|
---|
225 | return VERR_SEM_DESTROYED;
|
---|
226 | if (rc == 0)
|
---|
227 | return VINF_SUCCESS;
|
---|
228 |
|
---|
229 | /*
|
---|
230 | * Act on the wakup code.
|
---|
231 | */
|
---|
232 | if (rc == -ETIMEDOUT)
|
---|
233 | {
|
---|
234 | Assert(pTimeout);
|
---|
235 | return VERR_TIMEOUT;
|
---|
236 | }
|
---|
237 | if (rc == -EWOULDBLOCK)
|
---|
238 | /* retry, the value changed. */;
|
---|
239 | else if (rc == -EINTR)
|
---|
240 | {
|
---|
241 | if (!fAutoResume)
|
---|
242 | return VERR_INTERRUPTED;
|
---|
243 | }
|
---|
244 | else
|
---|
245 | {
|
---|
246 | /* this shouldn't happen! */
|
---|
247 | AssertMsgFailed(("rc=%ld errno=%d\n", rc, errno));
|
---|
248 | return RTErrConvertFromErrno(rc);
|
---|
249 | }
|
---|
250 | }
|
---|
251 | else if (iCur == -1)
|
---|
252 | return VINF_SUCCESS;
|
---|
253 | }
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 | RTDECL(int) RTSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
|
---|
258 | {
|
---|
259 | int rc = rtSemEventMultiWait(EventMultiSem, cMillies, true);
|
---|
260 | Assert(rc != VERR_INTERRUPTED);
|
---|
261 | return rc;
|
---|
262 | }
|
---|
263 |
|
---|
264 |
|
---|
265 | RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
|
---|
266 | {
|
---|
267 | return rtSemEventMultiWait(EventMultiSem, cMillies, false);
|
---|
268 | }
|
---|
269 |
|
---|