Use pa_read, pa_write and pa_poll instead of system functions
[profile/ivi/pulseaudio-panda.git] / 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 <pulsecore/core-error.h>
39 #include <pulse/xmalloc.h>
40
41 #ifndef HAVE_PIPE
42 #include <pulsecore/pipe.h>
43 #endif
44
45 #ifdef HAVE_SYS_EVENTFD_H
46 #include <sys/eventfd.h>
47 #endif
48
49 #include "fdsem.h"
50
51 struct pa_fdsem {
52     int fds[2];
53 #ifdef HAVE_SYS_EVENTFD_H
54     int efd;
55 #endif
56
57     pa_fdsem_data *data;
58 };
59
60 pa_fdsem *pa_fdsem_new(void) {
61     pa_fdsem *f;
62
63     f = pa_xmalloc(PA_ALIGN(sizeof(pa_fdsem)) + PA_ALIGN(sizeof(pa_fdsem_data)));
64
65 #ifdef HAVE_SYS_EVENTFD_H
66     if ((f->efd = eventfd(0, EFD_CLOEXEC)) >= 0)
67         f->fds[0] = f->fds[1] = -1;
68     else
69 #endif
70     {
71         if (pa_pipe_cloexec(f->fds) < 0) {
72             pa_xfree(f);
73             return NULL;
74         }
75     }
76
77     f->data = (pa_fdsem_data*) ((uint8_t*) f + PA_ALIGN(sizeof(pa_fdsem)));
78
79     pa_atomic_store(&f->data->waiting, 0);
80     pa_atomic_store(&f->data->signalled, 0);
81     pa_atomic_store(&f->data->in_pipe, 0);
82
83     return f;
84 }
85
86 pa_fdsem *pa_fdsem_open_shm(pa_fdsem_data *data, int event_fd) {
87     pa_fdsem *f = NULL;
88
89     pa_assert(data);
90     pa_assert(event_fd >= 0);
91
92 #ifdef HAVE_SYS_EVENTFD_H
93     f = pa_xnew(pa_fdsem, 1);
94
95     f->efd = event_fd;
96     pa_make_fd_cloexec(f->efd);
97     f->fds[0] = f->fds[1] = -1;
98     f->data = data;
99 #endif
100
101     return f;
102 }
103
104 pa_fdsem *pa_fdsem_new_shm(pa_fdsem_data *data, int* event_fd) {
105     pa_fdsem *f = NULL;
106
107     pa_assert(data);
108     pa_assert(event_fd);
109
110 #ifdef HAVE_SYS_EVENTFD_H
111
112     f = pa_xnew(pa_fdsem, 1);
113
114     if ((f->efd = eventfd(0, EFD_CLOEXEC)) < 0) {
115         pa_xfree(f);
116         return NULL;
117     }
118
119     f->fds[0] = f->fds[1] = -1;
120     f->data = data;
121
122     pa_atomic_store(&f->data->waiting, 0);
123     pa_atomic_store(&f->data->signalled, 0);
124     pa_atomic_store(&f->data->in_pipe, 0);
125
126 #endif
127
128     return f;
129 }
130
131 void pa_fdsem_free(pa_fdsem *f) {
132     pa_assert(f);
133
134 #ifdef HAVE_SYS_EVENTFD_H
135     if (f->efd >= 0)
136         pa_close(f->efd);
137 #endif
138     pa_close_pipe(f->fds);
139
140     pa_xfree(f);
141 }
142
143 static void flush(pa_fdsem *f) {
144     ssize_t r;
145     pa_assert(f);
146
147     if (pa_atomic_load(&f->data->in_pipe) <= 0)
148         return;
149
150     do {
151         char x[10];
152
153 #ifdef HAVE_SYS_EVENTFD_H
154         if (f->efd >= 0) {
155             uint64_t u;
156
157             if ((r = pa_read(f->efd, &u, sizeof(u), NULL)) != sizeof(u)) {
158
159                 if (r >= 0 || errno != EINTR) {
160                     pa_log_error("Invalid read from eventfd: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
161                     pa_assert_not_reached();
162                 }
163
164                 continue;
165             }
166             r = (ssize_t) u;
167         } else
168 #endif
169
170         if ((r = pa_read(f->fds[0], &x, sizeof(x), NULL)) <= 0) {
171
172             if (r >= 0 || errno != EINTR) {
173                 pa_log_error("Invalid read from pipe: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
174                 pa_assert_not_reached();
175             }
176
177             continue;
178         }
179
180     } while (pa_atomic_sub(&f->data->in_pipe, (int) r) > (int) r);
181 }
182
183 void pa_fdsem_post(pa_fdsem *f) {
184     pa_assert(f);
185
186     if (pa_atomic_cmpxchg(&f->data->signalled, 0, 1)) {
187
188         if (pa_atomic_load(&f->data->waiting)) {
189             ssize_t r;
190             char x = 'x';
191
192             pa_atomic_inc(&f->data->in_pipe);
193
194             for (;;) {
195
196 #ifdef HAVE_SYS_EVENTFD_H
197                 if (f->efd >= 0) {
198                     uint64_t u = 1;
199
200                     if ((r = pa_write(f->efd, &u, sizeof(u), NULL)) != sizeof(u)) {
201                         if (r >= 0 || errno != EINTR) {
202                             pa_log_error("Invalid write to eventfd: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
203                             pa_assert_not_reached();
204                         }
205
206                         continue;
207                     }
208                 } else
209 #endif
210
211                 if ((r = pa_write(f->fds[1], &x, 1, NULL)) != 1) {
212                     if (r >= 0 || errno != EINTR) {
213                         pa_log_error("Invalid write to pipe: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
214                         pa_assert_not_reached();
215                     }
216
217                     continue;
218                 }
219
220                 break;
221             }
222         }
223     }
224 }
225
226 void pa_fdsem_wait(pa_fdsem *f) {
227     pa_assert(f);
228
229     flush(f);
230
231     if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
232         return;
233
234     pa_atomic_inc(&f->data->waiting);
235
236     while (!pa_atomic_cmpxchg(&f->data->signalled, 1, 0)) {
237         char x[10];
238         ssize_t r;
239
240 #ifdef HAVE_SYS_EVENTFD_H
241         if (f->efd >= 0) {
242             uint64_t u;
243
244             if ((r = pa_read(f->efd, &u, sizeof(u), NULL)) != sizeof(u)) {
245
246                 if (r >= 0 || errno != EINTR) {
247                     pa_log_error("Invalid read from eventfd: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
248                     pa_assert_not_reached();
249                 }
250
251                 continue;
252             }
253
254             r = (ssize_t) u;
255         } else
256 #endif
257
258         if ((r = pa_read(f->fds[0], &x, sizeof(x), NULL)) <= 0) {
259
260             if (r >= 0 || errno != EINTR) {
261                 pa_log_error("Invalid read from pipe: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
262                 pa_assert_not_reached();
263             }
264
265             continue;
266         }
267
268         pa_atomic_sub(&f->data->in_pipe, (int) 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_SYS_EVENTFD_H
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 }