]> code.delx.au - pulseaudio/blob - src/pulsecore/fdsem.c
Merge dead branch 'ossman'
[pulseaudio] / src / pulsecore / fdsem.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2006 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #ifdef HAVE_SYS_SYSCALL_H
27 #include <sys/syscall.h>
28 #endif
29
30 #include <unistd.h>
31 #include <errno.h>
32
33 #include <pulsecore/atomic.h>
34 #include <pulsecore/log.h>
35 #include <pulsecore/thread.h>
36 #include <pulsecore/macro.h>
37 #include <pulsecore/core-util.h>
38 #include <pulse/xmalloc.h>
39
40 #ifndef HAVE_PIPE
41 #include <pulsecore/pipe.h>
42 #endif
43
44 #ifdef __linux__
45
46 #if !defined(__NR_eventfd) && defined(__i386__)
47 #define __NR_eventfd 323
48 #endif
49
50 #if !defined(__NR_eventfd) && defined(__x86_64__)
51 #define __NR_eventfd 284
52 #endif
53
54 #if !defined(__NR_eventfd) && defined(__arm__)
55 #define __NR_eventfd (__NR_SYSCALL_BASE+351)
56 #endif
57
58 #if !defined(SYS_eventfd) && defined(__NR_eventfd)
59 #define SYS_eventfd __NR_eventfd
60 #endif
61
62 #ifdef SYS_eventfd
63 #define HAVE_EVENTFD
64
65 static inline long eventfd(unsigned count) {
66 return syscall(SYS_eventfd, count);
67 }
68
69 #endif
70 #endif
71
72 #include "fdsem.h"
73
74 struct pa_fdsem {
75 int fds[2];
76 #ifdef HAVE_EVENTFD
77 int efd;
78 #endif
79
80 pa_fdsem_data *data;
81 };
82
83 pa_fdsem *pa_fdsem_new(void) {
84 pa_fdsem *f;
85
86 f = pa_xmalloc(PA_ALIGN(sizeof(pa_fdsem)) + PA_ALIGN(sizeof(pa_fdsem_data)));
87
88 #ifdef HAVE_EVENTFD
89 if ((f->efd = eventfd(0)) >= 0) {
90 pa_make_fd_cloexec(f->efd);
91 f->fds[0] = f->fds[1] = -1;
92 } else
93 #endif
94 {
95 if (pipe(f->fds) < 0) {
96 pa_xfree(f);
97 return NULL;
98 }
99
100 pa_make_fd_cloexec(f->fds[0]);
101 pa_make_fd_cloexec(f->fds[1]);
102 }
103
104 f->data = (pa_fdsem_data*) ((uint8_t*) f + PA_ALIGN(sizeof(pa_fdsem)));
105
106 pa_atomic_store(&f->data->waiting, 0);
107 pa_atomic_store(&f->data->signalled, 0);
108 pa_atomic_store(&f->data->in_pipe, 0);
109
110 return f;
111 }
112
113 pa_fdsem *pa_fdsem_open_shm(pa_fdsem_data *data, int event_fd) {
114 pa_fdsem *f = NULL;
115
116 pa_assert(data);
117 pa_assert(event_fd >= 0);
118
119 #ifdef HAVE_EVENTFD
120 f = pa_xnew(pa_fdsem, 1);
121
122 f->efd = event_fd;
123 pa_make_fd_cloexec(f->efd);
124 f->fds[0] = f->fds[1] = -1;
125 f->data = data;
126 #endif
127
128 return f;
129 }
130
131 pa_fdsem *pa_fdsem_new_shm(pa_fdsem_data *data, int* event_fd) {
132 pa_fdsem *f = NULL;
133
134 pa_assert(data);
135 pa_assert(event_fd);
136
137 #ifdef HAVE_EVENTFD
138
139 f = pa_xnew(pa_fdsem, 1);
140
141 if ((f->efd = eventfd(0)) < 0) {
142 pa_xfree(f);
143 return NULL;
144 }
145
146 pa_make_fd_cloexec(f->efd);
147 f->fds[0] = f->fds[1] = -1;
148 f->data = data;
149
150 pa_atomic_store(&f->data->waiting, 0);
151 pa_atomic_store(&f->data->signalled, 0);
152 pa_atomic_store(&f->data->in_pipe, 0);
153
154 #endif
155
156 return f;
157 }
158
159 void pa_fdsem_free(pa_fdsem *f) {
160 pa_assert(f);
161
162 #ifdef HAVE_EVENTFD
163 if (f->efd >= 0)
164 pa_close(f->efd);
165 #endif
166 pa_close_pipe(f->fds);
167
168 pa_xfree(f);
169 }
170
171 static void flush(pa_fdsem *f) {
172 ssize_t r;
173 pa_assert(f);
174
175 if (pa_atomic_load(&f->data->in_pipe) <= 0)
176 return;
177
178 do {
179 char x[10];
180
181 #ifdef HAVE_EVENTFD
182 if (f->efd >= 0) {
183 uint64_t u;
184
185 if ((r = read(f->efd, &u, sizeof(u))) != sizeof(u)) {
186 pa_assert(r < 0 && errno == EINTR);
187 continue;
188 }
189 r = (ssize_t) u;
190 } else
191 #endif
192
193 if ((r = read(f->fds[0], &x, sizeof(x))) <= 0) {
194 pa_assert(r < 0 && errno == EINTR);
195 continue;
196 }
197
198 } while (pa_atomic_sub(&f->data->in_pipe, r) > r);
199 }
200
201 void pa_fdsem_post(pa_fdsem *f) {
202 pa_assert(f);
203
204 if (pa_atomic_cmpxchg(&f->data->signalled, 0, 1)) {
205
206 if (pa_atomic_load(&f->data->waiting)) {
207 ssize_t r;
208 char x = 'x';
209
210 pa_atomic_inc(&f->data->in_pipe);
211
212 for (;;) {
213
214 #ifdef HAVE_EVENTFD
215 if (f->efd >= 0) {
216 uint64_t u = 1;
217
218 if ((r = write(f->efd, &u, sizeof(u))) != sizeof(u)) {
219 pa_assert(r < 0 && errno == EINTR);
220 continue;
221 }
222 } else
223 #endif
224
225 if ((r = write(f->fds[1], &x, 1)) != 1) {
226 pa_assert(r < 0 && errno == EINTR);
227 continue;
228 }
229
230 break;
231 }
232 }
233 }
234 }
235
236 void pa_fdsem_wait(pa_fdsem *f) {
237 pa_assert(f);
238
239 flush(f);
240
241 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
242 return;
243
244 pa_atomic_inc(&f->data->waiting);
245
246 while (!pa_atomic_cmpxchg(&f->data->signalled, 1, 0)) {
247 char x[10];
248 ssize_t r;
249
250 #ifdef HAVE_EVENTFD
251 if (f->efd >= 0) {
252 uint64_t u;
253
254 if ((r = read(f->efd, &u, sizeof(u))) != sizeof(u)) {
255 pa_assert(r < 0 && errno == EINTR);
256 continue;
257 }
258
259 r = (ssize_t) u;
260 } else
261 #endif
262
263 if ((r = read(f->fds[0], &x, sizeof(x))) <= 0) {
264 pa_assert(r < 0 && errno == EINTR);
265 continue;
266 }
267
268 pa_atomic_sub(&f->data->in_pipe, r);
269 }
270
271 pa_assert_se(pa_atomic_dec(&f->data->waiting) >= 1);
272 }
273
274 int pa_fdsem_try(pa_fdsem *f) {
275 pa_assert(f);
276
277 flush(f);
278
279 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
280 return 1;
281
282 return 0;
283 }
284
285 int pa_fdsem_get(pa_fdsem *f) {
286 pa_assert(f);
287
288 #ifdef HAVE_EVENTFD
289 if (f->efd >= 0)
290 return f->efd;
291 #endif
292
293 return f->fds[0];
294 }
295
296 int pa_fdsem_before_poll(pa_fdsem *f) {
297 pa_assert(f);
298
299 flush(f);
300
301 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
302 return -1;
303
304 pa_atomic_inc(&f->data->waiting);
305
306 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0)) {
307 pa_assert_se(pa_atomic_dec(&f->data->waiting) >= 1);
308 return -1;
309 }
310 return 0;
311 }
312
313 int pa_fdsem_after_poll(pa_fdsem *f) {
314 pa_assert(f);
315
316 pa_assert_se(pa_atomic_dec(&f->data->waiting) >= 1);
317
318 flush(f);
319
320 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
321 return 1;
322
323 return 0;
324 }