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, see <http://www.gnu.org/licenses/>.
24 #ifdef HAVE_SYS_SYSCALL_H
25 #include <sys/syscall.h>
31 #include <pulsecore/atomic.h>
32 #include <pulsecore/log.h>
33 #include <pulsecore/macro.h>
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/core-error.h>
36 #include <pulse/xmalloc.h>
39 #include <pulsecore/pipe.h>
42 #ifdef HAVE_SYS_EVENTFD_H
43 #include <sys/eventfd.h>
50 #ifdef HAVE_SYS_EVENTFD_H
57 pa_fdsem *pa_fdsem_new(void) {
60 f = pa_xmalloc0(PA_ALIGN(sizeof(pa_fdsem)) + PA_ALIGN(sizeof(pa_fdsem_data)));
62 #ifdef HAVE_SYS_EVENTFD_H
63 if ((f->efd = eventfd(0, EFD_CLOEXEC)) >= 0)
64 f->fds[0] = f->fds[1] = -1;
68 if (pa_pipe_cloexec(f->fds) < 0) {
74 f->data = (pa_fdsem_data*) ((uint8_t*) f + PA_ALIGN(sizeof(pa_fdsem)));
76 pa_atomic_store(&f->data->waiting, 0);
77 pa_atomic_store(&f->data->signalled, 0);
78 pa_atomic_store(&f->data->in_pipe, 0);
83 pa_fdsem *pa_fdsem_open_shm(pa_fdsem_data *data, int event_fd) {
87 pa_assert(event_fd >= 0);
89 #ifdef HAVE_SYS_EVENTFD_H
90 f = pa_xnew0(pa_fdsem, 1);
93 pa_make_fd_cloexec(f->efd);
94 f->fds[0] = f->fds[1] = -1;
101 pa_fdsem *pa_fdsem_new_shm(pa_fdsem_data *data) {
106 #ifdef HAVE_SYS_EVENTFD_H
108 f = pa_xnew0(pa_fdsem, 1);
110 if ((f->efd = eventfd(0, EFD_CLOEXEC)) < 0) {
115 f->fds[0] = f->fds[1] = -1;
118 pa_atomic_store(&f->data->waiting, 0);
119 pa_atomic_store(&f->data->signalled, 0);
120 pa_atomic_store(&f->data->in_pipe, 0);
127 void pa_fdsem_free(pa_fdsem *f) {
130 #ifdef HAVE_SYS_EVENTFD_H
134 pa_close_pipe(f->fds);
139 static void flush(pa_fdsem *f) {
143 if (pa_atomic_load(&f->data->in_pipe) <= 0)
149 #ifdef HAVE_SYS_EVENTFD_H
153 if ((r = pa_read(f->efd, &u, sizeof(u), NULL)) != sizeof(u)) {
154 pa_log_error("Invalid read from eventfd: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
155 pa_assert_not_reached();
161 if ((r = pa_read(f->fds[0], &x, sizeof(x), NULL)) <= 0) {
162 pa_log_error("Invalid read from pipe: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
163 pa_assert_not_reached();
166 } while (pa_atomic_sub(&f->data->in_pipe, (int) r) > (int) r);
169 void pa_fdsem_post(pa_fdsem *f) {
172 if (pa_atomic_cmpxchg(&f->data->signalled, 0, 1)) {
174 if (pa_atomic_load(&f->data->waiting)) {
178 pa_atomic_inc(&f->data->in_pipe);
182 #ifdef HAVE_SYS_EVENTFD_H
186 if ((r = pa_write(f->efd, &u, sizeof(u), &f->write_type)) != sizeof(u)) {
187 pa_log_error("Invalid write to eventfd: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
188 pa_assert_not_reached();
193 if ((r = pa_write(f->fds[1], &x, 1, &f->write_type)) != 1) {
194 pa_log_error("Invalid write to pipe: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
195 pa_assert_not_reached();
204 void pa_fdsem_wait(pa_fdsem *f) {
209 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
212 pa_atomic_inc(&f->data->waiting);
214 while (!pa_atomic_cmpxchg(&f->data->signalled, 1, 0)) {
218 #ifdef HAVE_SYS_EVENTFD_H
222 if ((r = pa_read(f->efd, &u, sizeof(u), NULL)) != sizeof(u)) {
223 pa_log_error("Invalid read from eventfd: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
224 pa_assert_not_reached();
231 if ((r = pa_read(f->fds[0], &x, sizeof(x), NULL)) <= 0) {
232 pa_log_error("Invalid read from pipe: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
233 pa_assert_not_reached();
236 pa_atomic_sub(&f->data->in_pipe, (int) r);
239 pa_assert_se(pa_atomic_dec(&f->data->waiting) >= 1);
242 int pa_fdsem_try(pa_fdsem *f) {
247 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
253 int pa_fdsem_get(pa_fdsem *f) {
256 #ifdef HAVE_SYS_EVENTFD_H
264 int pa_fdsem_before_poll(pa_fdsem *f) {
269 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
272 pa_atomic_inc(&f->data->waiting);
274 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0)) {
275 pa_assert_se(pa_atomic_dec(&f->data->waiting) >= 1);
281 int pa_fdsem_after_poll(pa_fdsem *f) {
284 pa_assert_se(pa_atomic_dec(&f->data->waiting) >= 1);
288 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))