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
26 #ifdef HAVE_SYS_SYSCALL_H
27 #include <sys/syscall.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>
42 #include <pulsecore/pipe.h>
45 #ifdef HAVE_SYS_EVENTFD_H
46 #include <sys/eventfd.h>
53 #ifdef HAVE_SYS_EVENTFD_H
60 pa_fdsem *pa_fdsem_new(void) {
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, EFD_CLOEXEC)) >= 0)
67 f->fds[0] = f->fds[1] = -1;
71 if (pa_pipe_cloexec(f->fds) < 0) {
77 f->data = (pa_fdsem_data*) ((uint8_t*) f + PA_ALIGN(sizeof(pa_fdsem)));
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);
86 pa_fdsem *pa_fdsem_open_shm(pa_fdsem_data *data, int event_fd) {
90 pa_assert(event_fd >= 0);
92 #ifdef HAVE_SYS_EVENTFD_H
93 f = pa_xnew(pa_fdsem, 1);
96 pa_make_fd_cloexec(f->efd);
97 f->fds[0] = f->fds[1] = -1;
104 pa_fdsem *pa_fdsem_new_shm(pa_fdsem_data *data, int* event_fd) {
110 #ifdef HAVE_SYS_EVENTFD_H
112 f = pa_xnew(pa_fdsem, 1);
114 if ((f->efd = eventfd(0, EFD_CLOEXEC)) < 0) {
119 f->fds[0] = f->fds[1] = -1;
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);
131 void pa_fdsem_free(pa_fdsem *f) {
134 #ifdef HAVE_SYS_EVENTFD_H
138 pa_close_pipe(f->fds);
143 static void flush(pa_fdsem *f) {
147 if (pa_atomic_load(&f->data->in_pipe) <= 0)
153 #ifdef HAVE_SYS_EVENTFD_H
157 if ((r = pa_read(f->efd, &u, sizeof(u), NULL)) != sizeof(u)) {
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();
170 if ((r = pa_read(f->fds[0], &x, sizeof(x), NULL)) <= 0) {
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();
180 } while (pa_atomic_sub(&f->data->in_pipe, (int) r) > (int) r);
183 void pa_fdsem_post(pa_fdsem *f) {
186 if (pa_atomic_cmpxchg(&f->data->signalled, 0, 1)) {
188 if (pa_atomic_load(&f->data->waiting)) {
192 pa_atomic_inc(&f->data->in_pipe);
196 #ifdef HAVE_SYS_EVENTFD_H
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();
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();
226 void pa_fdsem_wait(pa_fdsem *f) {
231 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
234 pa_atomic_inc(&f->data->waiting);
236 while (!pa_atomic_cmpxchg(&f->data->signalled, 1, 0)) {
240 #ifdef HAVE_SYS_EVENTFD_H
244 if ((r = pa_read(f->efd, &u, sizeof(u), NULL)) != sizeof(u)) {
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();
258 if ((r = pa_read(f->fds[0], &x, sizeof(x), NULL)) <= 0) {
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();
268 pa_atomic_sub(&f->data->in_pipe, (int) r);
271 pa_assert_se(pa_atomic_dec(&f->data->waiting) >= 1);
274 int pa_fdsem_try(pa_fdsem *f) {
279 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
285 int pa_fdsem_get(pa_fdsem *f) {
288 #ifdef HAVE_SYS_EVENTFD_H
296 int pa_fdsem_before_poll(pa_fdsem *f) {
301 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
304 pa_atomic_inc(&f->data->waiting);
306 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0)) {
307 pa_assert_se(pa_atomic_dec(&f->data->waiting) >= 1);
313 int pa_fdsem_after_poll(pa_fdsem *f) {
316 pa_assert_se(pa_atomic_dec(&f->data->waiting) >= 1);
320 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))