Merge commit 'origin/master-tx'
[platform/upstream/pulseaudio.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 <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
56     pa_fdsem_data *data;
57 };
58
59 pa_fdsem *pa_fdsem_new(void) {
60     pa_fdsem *f;
61
62     f = pa_xmalloc(PA_ALIGN(sizeof(pa_fdsem)) + PA_ALIGN(sizeof(pa_fdsem_data)));
63
64 #ifdef HAVE_SYS_EVENTFD_H
65     if ((f->efd = eventfd(0, 0)) >= 0) {
66         pa_make_fd_cloexec(f->efd);
67         f->fds[0] = f->fds[1] = -1;
68     } else
69 #endif
70     {
71         if (pipe(f->fds) < 0) {
72             pa_xfree(f);
73             return NULL;
74         }
75
76         pa_make_fd_cloexec(f->fds[0]);
77         pa_make_fd_cloexec(f->fds[1]);
78     }
79
80     f->data = (pa_fdsem_data*) ((uint8_t*) f + PA_ALIGN(sizeof(pa_fdsem)));
81
82     pa_atomic_store(&f->data->waiting, 0);
83     pa_atomic_store(&f->data->signalled, 0);
84     pa_atomic_store(&f->data->in_pipe, 0);
85
86     return f;
87 }
88
89 pa_fdsem *pa_fdsem_open_shm(pa_fdsem_data *data, int event_fd) {
90     pa_fdsem *f = NULL;
91
92     pa_assert(data);
93     pa_assert(event_fd >= 0);
94
95 #ifdef HAVE_SYS_EVENTFD_H
96     f = pa_xnew(pa_fdsem, 1);
97
98     f->efd = event_fd;
99     pa_make_fd_cloexec(f->efd);
100     f->fds[0] = f->fds[1] = -1;
101     f->data = data;
102 #endif
103
104     return f;
105 }
106
107 pa_fdsem *pa_fdsem_new_shm(pa_fdsem_data *data, int* event_fd) {
108     pa_fdsem *f = NULL;
109
110     pa_assert(data);
111     pa_assert(event_fd);
112
113 #ifdef HAVE_SYS_EVENTFD_H
114
115     f = pa_xnew(pa_fdsem, 1);
116
117     if ((f->efd = eventfd(0, 0)) < 0) {
118         pa_xfree(f);
119         return NULL;
120     }
121
122     pa_make_fd_cloexec(f->efd);
123     f->fds[0] = f->fds[1] = -1;
124     f->data = data;
125
126     pa_atomic_store(&f->data->waiting, 0);
127     pa_atomic_store(&f->data->signalled, 0);
128     pa_atomic_store(&f->data->in_pipe, 0);
129
130 #endif
131
132     return f;
133 }
134
135 void pa_fdsem_free(pa_fdsem *f) {
136     pa_assert(f);
137
138 #ifdef HAVE_SYS_EVENTFD_H
139     if (f->efd >= 0)
140         pa_close(f->efd);
141 #endif
142     pa_close_pipe(f->fds);
143
144     pa_xfree(f);
145 }
146
147 static void flush(pa_fdsem *f) {
148     ssize_t r;
149     pa_assert(f);
150
151     if (pa_atomic_load(&f->data->in_pipe) <= 0)
152         return;
153
154     do {
155         char x[10];
156
157 #ifdef HAVE_SYS_EVENTFD_H
158         if (f->efd >= 0) {
159             uint64_t u;
160
161             if ((r = read(f->efd, &u, sizeof(u))) != sizeof(u)) {
162                 pa_assert(r < 0 && errno == EINTR);
163                 continue;
164             }
165             r = (ssize_t) u;
166         } else
167 #endif
168
169         if ((r = read(f->fds[0], &x, sizeof(x))) <= 0) {
170             pa_assert(r < 0 && errno == EINTR);
171             continue;
172         }
173
174     } while (pa_atomic_sub(&f->data->in_pipe, (int) r) > (int) r);
175 }
176
177 void pa_fdsem_post(pa_fdsem *f) {
178     pa_assert(f);
179
180     if (pa_atomic_cmpxchg(&f->data->signalled, 0, 1)) {
181
182         if (pa_atomic_load(&f->data->waiting)) {
183             ssize_t r;
184             char x = 'x';
185
186             pa_atomic_inc(&f->data->in_pipe);
187
188             for (;;) {
189
190 #ifdef HAVE_SYS_EVENTFD_H
191                 if (f->efd >= 0) {
192                     uint64_t u = 1;
193
194                     if ((r = write(f->efd, &u, sizeof(u))) != sizeof(u)) {
195                         pa_assert(r < 0 && errno == EINTR);
196                         continue;
197                     }
198                 } else
199 #endif
200
201                 if ((r = write(f->fds[1], &x, 1)) != 1) {
202                     pa_assert(r < 0 && errno == EINTR);
203                     continue;
204                 }
205
206                 break;
207             }
208         }
209     }
210 }
211
212 void pa_fdsem_wait(pa_fdsem *f) {
213     pa_assert(f);
214
215     flush(f);
216
217     if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
218         return;
219
220     pa_atomic_inc(&f->data->waiting);
221
222     while (!pa_atomic_cmpxchg(&f->data->signalled, 1, 0)) {
223         char x[10];
224         ssize_t r;
225
226 #ifdef HAVE_SYS_EVENTFD_H
227         if (f->efd >= 0) {
228             uint64_t u;
229
230             if ((r = read(f->efd, &u, sizeof(u))) != sizeof(u)) {
231                 pa_assert(r < 0 && errno == EINTR);
232                 continue;
233             }
234
235             r = (ssize_t) u;
236         } else
237 #endif
238
239         if ((r = read(f->fds[0], &x, sizeof(x))) <= 0) {
240             pa_assert(r < 0 && errno == EINTR);
241             continue;
242         }
243
244         pa_atomic_sub(&f->data->in_pipe, (int) r);
245     }
246
247     pa_assert_se(pa_atomic_dec(&f->data->waiting) >= 1);
248 }
249
250 int pa_fdsem_try(pa_fdsem *f) {
251     pa_assert(f);
252
253     flush(f);
254
255     if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
256         return 1;
257
258     return 0;
259 }
260
261 int pa_fdsem_get(pa_fdsem *f) {
262     pa_assert(f);
263
264 #ifdef HAVE_SYS_EVENTFD_H
265     if (f->efd >= 0)
266         return f->efd;
267 #endif
268
269     return f->fds[0];
270 }
271
272 int pa_fdsem_before_poll(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     pa_atomic_inc(&f->data->waiting);
281
282     if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0)) {
283         pa_assert_se(pa_atomic_dec(&f->data->waiting) >= 1);
284         return -1;
285     }
286     return 0;
287 }
288
289 int pa_fdsem_after_poll(pa_fdsem *f) {
290     pa_assert(f);
291
292     pa_assert_se(pa_atomic_dec(&f->data->waiting) >= 1);
293
294     flush(f);
295
296     if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
297         return 1;
298
299     return 0;
300 }