]> code.delx.au - pulseaudio/blob - src/pulsecore/fdsem.c
remap: Change remapping function argument type from void to int16_t / float as approp...
[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/macro.h>
36 #include <pulsecore/core-util.h>
37 #include <pulsecore/core-error.h>
38 #include <pulse/xmalloc.h>
39
40 #ifndef HAVE_PIPE
41 #include <pulsecore/pipe.h>
42 #endif
43
44 #ifdef HAVE_SYS_EVENTFD_H
45 #include <sys/eventfd.h>
46 #endif
47
48 #include "fdsem.h"
49
50 struct pa_fdsem {
51 int fds[2];
52 #ifdef HAVE_SYS_EVENTFD_H
53 int efd;
54 #endif
55 int write_type;
56 pa_fdsem_data *data;
57 };
58
59 pa_fdsem *pa_fdsem_new(void) {
60 pa_fdsem *f;
61
62 f = pa_xmalloc0(PA_ALIGN(sizeof(pa_fdsem)) + PA_ALIGN(sizeof(pa_fdsem_data)));
63
64 #ifdef HAVE_SYS_EVENTFD_H
65 if ((f->efd = eventfd(0, EFD_CLOEXEC)) >= 0)
66 f->fds[0] = f->fds[1] = -1;
67 else
68 #endif
69 {
70 if (pa_pipe_cloexec(f->fds) < 0) {
71 pa_xfree(f);
72 return NULL;
73 }
74 }
75
76 f->data = (pa_fdsem_data*) ((uint8_t*) f + PA_ALIGN(sizeof(pa_fdsem)));
77
78 pa_atomic_store(&f->data->waiting, 0);
79 pa_atomic_store(&f->data->signalled, 0);
80 pa_atomic_store(&f->data->in_pipe, 0);
81
82 return f;
83 }
84
85 pa_fdsem *pa_fdsem_open_shm(pa_fdsem_data *data, int event_fd) {
86 pa_fdsem *f = NULL;
87
88 pa_assert(data);
89 pa_assert(event_fd >= 0);
90
91 #ifdef HAVE_SYS_EVENTFD_H
92 f = pa_xnew0(pa_fdsem, 1);
93
94 f->efd = event_fd;
95 pa_make_fd_cloexec(f->efd);
96 f->fds[0] = f->fds[1] = -1;
97 f->data = data;
98 #endif
99
100 return f;
101 }
102
103 pa_fdsem *pa_fdsem_new_shm(pa_fdsem_data *data) {
104 pa_fdsem *f = NULL;
105
106 pa_assert(data);
107
108 #ifdef HAVE_SYS_EVENTFD_H
109
110 f = pa_xnew0(pa_fdsem, 1);
111
112 if ((f->efd = eventfd(0, EFD_CLOEXEC)) < 0) {
113 pa_xfree(f);
114 return NULL;
115 }
116
117 f->fds[0] = f->fds[1] = -1;
118 f->data = data;
119
120 pa_atomic_store(&f->data->waiting, 0);
121 pa_atomic_store(&f->data->signalled, 0);
122 pa_atomic_store(&f->data->in_pipe, 0);
123
124 #endif
125
126 return f;
127 }
128
129 void pa_fdsem_free(pa_fdsem *f) {
130 pa_assert(f);
131
132 #ifdef HAVE_SYS_EVENTFD_H
133 if (f->efd >= 0)
134 pa_close(f->efd);
135 #endif
136 pa_close_pipe(f->fds);
137
138 pa_xfree(f);
139 }
140
141 static void flush(pa_fdsem *f) {
142 ssize_t r;
143 pa_assert(f);
144
145 if (pa_atomic_load(&f->data->in_pipe) <= 0)
146 return;
147
148 do {
149 char x[10];
150
151 #ifdef HAVE_SYS_EVENTFD_H
152 if (f->efd >= 0) {
153 uint64_t u;
154
155 if ((r = pa_read(f->efd, &u, sizeof(u), NULL)) != sizeof(u)) {
156
157 if (r >= 0 || errno != EINTR) {
158 pa_log_error("Invalid read from eventfd: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
159 pa_assert_not_reached();
160 }
161
162 continue;
163 }
164 r = (ssize_t) u;
165 } else
166 #endif
167
168 if ((r = pa_read(f->fds[0], &x, sizeof(x), NULL)) <= 0) {
169
170 if (r >= 0 || errno != EINTR) {
171 pa_log_error("Invalid read from pipe: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
172 pa_assert_not_reached();
173 }
174
175 continue;
176 }
177
178 } while (pa_atomic_sub(&f->data->in_pipe, (int) r) > (int) r);
179 }
180
181 void pa_fdsem_post(pa_fdsem *f) {
182 pa_assert(f);
183
184 if (pa_atomic_cmpxchg(&f->data->signalled, 0, 1)) {
185
186 if (pa_atomic_load(&f->data->waiting)) {
187 ssize_t r;
188 char x = 'x';
189
190 pa_atomic_inc(&f->data->in_pipe);
191
192 for (;;) {
193
194 #ifdef HAVE_SYS_EVENTFD_H
195 if (f->efd >= 0) {
196 uint64_t u = 1;
197
198 if ((r = pa_write(f->efd, &u, sizeof(u), &f->write_type)) != sizeof(u)) {
199 if (r >= 0 || errno != EINTR) {
200 pa_log_error("Invalid write to eventfd: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
201 pa_assert_not_reached();
202 }
203
204 continue;
205 }
206 } else
207 #endif
208
209 if ((r = pa_write(f->fds[1], &x, 1, &f->write_type)) != 1) {
210 if (r >= 0 || errno != EINTR) {
211 pa_log_error("Invalid write to pipe: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
212 pa_assert_not_reached();
213 }
214
215 continue;
216 }
217
218 break;
219 }
220 }
221 }
222 }
223
224 void pa_fdsem_wait(pa_fdsem *f) {
225 pa_assert(f);
226
227 flush(f);
228
229 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
230 return;
231
232 pa_atomic_inc(&f->data->waiting);
233
234 while (!pa_atomic_cmpxchg(&f->data->signalled, 1, 0)) {
235 char x[10];
236 ssize_t r;
237
238 #ifdef HAVE_SYS_EVENTFD_H
239 if (f->efd >= 0) {
240 uint64_t u;
241
242 if ((r = pa_read(f->efd, &u, sizeof(u), NULL)) != sizeof(u)) {
243
244 if (r >= 0 || errno != EINTR) {
245 pa_log_error("Invalid read from eventfd: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
246 pa_assert_not_reached();
247 }
248
249 continue;
250 }
251
252 r = (ssize_t) u;
253 } else
254 #endif
255
256 if ((r = pa_read(f->fds[0], &x, sizeof(x), NULL)) <= 0) {
257
258 if (r >= 0 || errno != EINTR) {
259 pa_log_error("Invalid read from pipe: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
260 pa_assert_not_reached();
261 }
262
263 continue;
264 }
265
266 pa_atomic_sub(&f->data->in_pipe, (int) r);
267 }
268
269 pa_assert_se(pa_atomic_dec(&f->data->waiting) >= 1);
270 }
271
272 int pa_fdsem_try(pa_fdsem *f) {
273 pa_assert(f);
274
275 flush(f);
276
277 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
278 return 1;
279
280 return 0;
281 }
282
283 int pa_fdsem_get(pa_fdsem *f) {
284 pa_assert(f);
285
286 #ifdef HAVE_SYS_EVENTFD_H
287 if (f->efd >= 0)
288 return f->efd;
289 #endif
290
291 return f->fds[0];
292 }
293
294 int pa_fdsem_before_poll(pa_fdsem *f) {
295 pa_assert(f);
296
297 flush(f);
298
299 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
300 return -1;
301
302 pa_atomic_inc(&f->data->waiting);
303
304 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0)) {
305 pa_assert_se(pa_atomic_dec(&f->data->waiting) >= 1);
306 return -1;
307 }
308 return 0;
309 }
310
311 int pa_fdsem_after_poll(pa_fdsem *f) {
312 pa_assert(f);
313
314 pa_assert_se(pa_atomic_dec(&f->data->waiting) >= 1);
315
316 flush(f);
317
318 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
319 return 1;
320
321 return 0;
322 }