Merge branch 'master' of ssh://rootserver/home/lennart/git/public/pulseaudio
[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, 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_xnew(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, int* event_fd) {
104     pa_fdsem *f = NULL;
105
106     pa_assert(data);
107     pa_assert(event_fd);
108
109 #ifdef HAVE_SYS_EVENTFD_H
110
111     f = pa_xnew(pa_fdsem, 1);
112
113     if ((f->efd = eventfd(0, EFD_CLOEXEC)) < 0) {
114         pa_xfree(f);
115         return NULL;
116     }
117
118     f->fds[0] = f->fds[1] = -1;
119     f->data = data;
120
121     pa_atomic_store(&f->data->waiting, 0);
122     pa_atomic_store(&f->data->signalled, 0);
123     pa_atomic_store(&f->data->in_pipe, 0);
124
125 #endif
126
127     return f;
128 }
129
130 void pa_fdsem_free(pa_fdsem *f) {
131     pa_assert(f);
132
133 #ifdef HAVE_SYS_EVENTFD_H
134     if (f->efd >= 0)
135         pa_close(f->efd);
136 #endif
137     pa_close_pipe(f->fds);
138
139     pa_xfree(f);
140 }
141
142 static void flush(pa_fdsem *f) {
143     ssize_t r;
144     pa_assert(f);
145
146     if (pa_atomic_load(&f->data->in_pipe) <= 0)
147         return;
148
149     do {
150         char x[10];
151
152 #ifdef HAVE_SYS_EVENTFD_H
153         if (f->efd >= 0) {
154             uint64_t u;
155
156             if ((r = read(f->efd, &u, sizeof(u))) != sizeof(u)) {
157                 pa_assert(r < 0 && errno == EINTR);
158                 continue;
159             }
160             r = (ssize_t) u;
161         } else
162 #endif
163
164         if ((r = read(f->fds[0], &x, sizeof(x))) <= 0) {
165             pa_assert(r < 0 && errno == EINTR);
166             continue;
167         }
168
169     } while (pa_atomic_sub(&f->data->in_pipe, (int) r) > (int) r);
170 }
171
172 void pa_fdsem_post(pa_fdsem *f) {
173     pa_assert(f);
174
175     if (pa_atomic_cmpxchg(&f->data->signalled, 0, 1)) {
176
177         if (pa_atomic_load(&f->data->waiting)) {
178             ssize_t r;
179             char x = 'x';
180
181             pa_atomic_inc(&f->data->in_pipe);
182
183             for (;;) {
184
185 #ifdef HAVE_SYS_EVENTFD_H
186                 if (f->efd >= 0) {
187                     uint64_t u = 1;
188
189                     if ((r = write(f->efd, &u, sizeof(u))) != sizeof(u)) {
190                         pa_assert(r < 0 && errno == EINTR);
191                         continue;
192                     }
193                 } else
194 #endif
195
196                 if ((r = write(f->fds[1], &x, 1)) != 1) {
197                     pa_assert(r < 0 && errno == EINTR);
198                     continue;
199                 }
200
201                 break;
202             }
203         }
204     }
205 }
206
207 void pa_fdsem_wait(pa_fdsem *f) {
208     pa_assert(f);
209
210     flush(f);
211
212     if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
213         return;
214
215     pa_atomic_inc(&f->data->waiting);
216
217     while (!pa_atomic_cmpxchg(&f->data->signalled, 1, 0)) {
218         char x[10];
219         ssize_t r;
220
221 #ifdef HAVE_SYS_EVENTFD_H
222         if (f->efd >= 0) {
223             uint64_t u;
224
225             if ((r = read(f->efd, &u, sizeof(u))) != sizeof(u)) {
226                 pa_assert(r < 0 && errno == EINTR);
227                 continue;
228             }
229
230             r = (ssize_t) u;
231         } else
232 #endif
233
234         if ((r = read(f->fds[0], &x, sizeof(x))) <= 0) {
235             pa_assert(r < 0 && errno == EINTR);
236             continue;
237         }
238
239         pa_atomic_sub(&f->data->in_pipe, (int) r);
240     }
241
242     pa_assert_se(pa_atomic_dec(&f->data->waiting) >= 1);
243 }
244
245 int pa_fdsem_try(pa_fdsem *f) {
246     pa_assert(f);
247
248     flush(f);
249
250     if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
251         return 1;
252
253     return 0;
254 }
255
256 int pa_fdsem_get(pa_fdsem *f) {
257     pa_assert(f);
258
259 #ifdef HAVE_SYS_EVENTFD_H
260     if (f->efd >= 0)
261         return f->efd;
262 #endif
263
264     return f->fds[0];
265 }
266
267 int pa_fdsem_before_poll(pa_fdsem *f) {
268     pa_assert(f);
269
270     flush(f);
271
272     if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
273         return -1;
274
275     pa_atomic_inc(&f->data->waiting);
276
277     if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0)) {
278         pa_assert_se(pa_atomic_dec(&f->data->waiting) >= 1);
279         return -1;
280     }
281     return 0;
282 }
283
284 int pa_fdsem_after_poll(pa_fdsem *f) {
285     pa_assert(f);
286
287     pa_assert_se(pa_atomic_dec(&f->data->waiting) >= 1);
288
289     flush(f);
290
291     if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
292         return 1;
293
294     return 0;
295 }