build-sys: bump soname
[pulseaudio-mirror.git] / src / pulsecore / fdsem.c
blob00836f9f59194db583b23da9e259b92987a87762
1 /***
2 This file is part of PulseAudio.
4 Copyright 2006 Lennart Poettering
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.
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.
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 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #ifdef HAVE_SYS_SYSCALL_H
27 #include <sys/syscall.h>
28 #endif
30 #include <unistd.h>
31 #include <errno.h>
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>
41 #ifndef HAVE_PIPE
42 #include <pulsecore/pipe.h>
43 #endif
45 #ifdef HAVE_SYS_EVENTFD_H
46 #include <sys/eventfd.h>
47 #endif
49 #include "fdsem.h"
51 struct pa_fdsem {
52 int fds[2];
53 #ifdef HAVE_SYS_EVENTFD_H
54 int efd;
55 #endif
57 pa_fdsem_data *data;
60 pa_fdsem *pa_fdsem_new(void) {
61 pa_fdsem *f;
63 f = pa_xmalloc(PA_ALIGN(sizeof(pa_fdsem)) + PA_ALIGN(sizeof(pa_fdsem_data)));
65 #ifdef HAVE_SYS_EVENTFD_H
66 if ((f->efd = eventfd(0, 0)) >= 0) {
67 pa_make_fd_cloexec(f->efd);
68 f->fds[0] = f->fds[1] = -1;
69 } else
70 #endif
72 if (pipe(f->fds) < 0) {
73 pa_xfree(f);
74 return NULL;
77 pa_make_fd_cloexec(f->fds[0]);
78 pa_make_fd_cloexec(f->fds[1]);
81 f->data = (pa_fdsem_data*) ((uint8_t*) f + PA_ALIGN(sizeof(pa_fdsem)));
83 pa_atomic_store(&f->data->waiting, 0);
84 pa_atomic_store(&f->data->signalled, 0);
85 pa_atomic_store(&f->data->in_pipe, 0);
87 return f;
90 pa_fdsem *pa_fdsem_open_shm(pa_fdsem_data *data, int event_fd) {
91 pa_fdsem *f = NULL;
93 pa_assert(data);
94 pa_assert(event_fd >= 0);
96 #ifdef HAVE_SYS_EVENTFD_H
97 f = pa_xnew(pa_fdsem, 1);
99 f->efd = event_fd;
100 pa_make_fd_cloexec(f->efd);
101 f->fds[0] = f->fds[1] = -1;
102 f->data = data;
103 #endif
105 return f;
108 pa_fdsem *pa_fdsem_new_shm(pa_fdsem_data *data, int* event_fd) {
109 pa_fdsem *f = NULL;
111 pa_assert(data);
112 pa_assert(event_fd);
114 #ifdef HAVE_SYS_EVENTFD_H
116 f = pa_xnew(pa_fdsem, 1);
118 if ((f->efd = eventfd(0, 0)) < 0) {
119 pa_xfree(f);
120 return NULL;
123 pa_make_fd_cloexec(f->efd);
124 f->fds[0] = f->fds[1] = -1;
125 f->data = data;
127 pa_atomic_store(&f->data->waiting, 0);
128 pa_atomic_store(&f->data->signalled, 0);
129 pa_atomic_store(&f->data->in_pipe, 0);
131 #endif
133 return f;
136 void pa_fdsem_free(pa_fdsem *f) {
137 pa_assert(f);
139 #ifdef HAVE_SYS_EVENTFD_H
140 if (f->efd >= 0)
141 pa_close(f->efd);
142 #endif
143 pa_close_pipe(f->fds);
145 pa_xfree(f);
148 static void flush(pa_fdsem *f) {
149 ssize_t r;
150 pa_assert(f);
152 if (pa_atomic_load(&f->data->in_pipe) <= 0)
153 return;
155 do {
156 char x[10];
158 #ifdef HAVE_SYS_EVENTFD_H
159 if (f->efd >= 0) {
160 uint64_t u;
162 if ((r = read(f->efd, &u, sizeof(u))) != sizeof(u)) {
164 if (r >= 0 || errno != EINTR) {
165 pa_log_error("Invalid read from eventfd: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
166 pa_assert_not_reached();
169 continue;
171 r = (ssize_t) u;
172 } else
173 #endif
175 if ((r = read(f->fds[0], &x, sizeof(x))) <= 0) {
177 if (r >= 0 || errno != EINTR) {
178 pa_log_error("Invalid read from pipe: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
179 pa_assert_not_reached();
182 continue;
185 } while (pa_atomic_sub(&f->data->in_pipe, (int) r) > (int) r);
188 void pa_fdsem_post(pa_fdsem *f) {
189 pa_assert(f);
191 if (pa_atomic_cmpxchg(&f->data->signalled, 0, 1)) {
193 if (pa_atomic_load(&f->data->waiting)) {
194 ssize_t r;
195 char x = 'x';
197 pa_atomic_inc(&f->data->in_pipe);
199 for (;;) {
201 #ifdef HAVE_SYS_EVENTFD_H
202 if (f->efd >= 0) {
203 uint64_t u = 1;
205 if ((r = write(f->efd, &u, sizeof(u))) != sizeof(u)) {
206 if (r >= 0 || errno != EINTR) {
207 pa_log_error("Invalid read from pipe: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
208 pa_assert_not_reached();
211 continue;
213 } else
214 #endif
216 if ((r = write(f->fds[1], &x, 1)) != 1) {
217 if (r >= 0 || errno != EINTR) {
218 pa_log_error("Invalid read from pipe: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
219 pa_assert_not_reached();
222 continue;
225 break;
231 void pa_fdsem_wait(pa_fdsem *f) {
232 pa_assert(f);
234 flush(f);
236 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
237 return;
239 pa_atomic_inc(&f->data->waiting);
241 while (!pa_atomic_cmpxchg(&f->data->signalled, 1, 0)) {
242 char x[10];
243 ssize_t r;
245 #ifdef HAVE_SYS_EVENTFD_H
246 if (f->efd >= 0) {
247 uint64_t u;
249 if ((r = read(f->efd, &u, sizeof(u))) != sizeof(u)) {
251 if (r >= 0 || errno != EINTR) {
252 pa_log_error("Invalid read from pipe: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
253 pa_assert_not_reached();
256 continue;
259 r = (ssize_t) u;
260 } else
261 #endif
263 if ((r = read(f->fds[0], &x, sizeof(x))) <= 0) {
265 if (r >= 0 || errno != EINTR) {
266 pa_log_error("Invalid read from pipe: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
267 pa_assert_not_reached();
270 continue;
273 pa_atomic_sub(&f->data->in_pipe, (int) r);
276 pa_assert_se(pa_atomic_dec(&f->data->waiting) >= 1);
279 int pa_fdsem_try(pa_fdsem *f) {
280 pa_assert(f);
282 flush(f);
284 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
285 return 1;
287 return 0;
290 int pa_fdsem_get(pa_fdsem *f) {
291 pa_assert(f);
293 #ifdef HAVE_SYS_EVENTFD_H
294 if (f->efd >= 0)
295 return f->efd;
296 #endif
298 return f->fds[0];
301 int pa_fdsem_before_poll(pa_fdsem *f) {
302 pa_assert(f);
304 flush(f);
306 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
307 return -1;
309 pa_atomic_inc(&f->data->waiting);
311 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0)) {
312 pa_assert_se(pa_atomic_dec(&f->data->waiting) >= 1);
313 return -1;
315 return 0;
318 int pa_fdsem_after_poll(pa_fdsem *f) {
319 pa_assert(f);
321 pa_assert_se(pa_atomic_dec(&f->data->waiting) >= 1);
323 flush(f);
325 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
326 return 1;
328 return 0;