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