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 <pulse/xmalloc.h>
41 #include <pulsecore/pipe.h>
44 #ifdef HAVE_SYS_EVENTFD_H
45 #include <sys/eventfd.h>
52 #ifdef HAVE_SYS_EVENTFD_H
59 pa_fdsem *pa_fdsem_new(void) {
62 f = pa_xmalloc(PA_ALIGN(sizeof(pa_fdsem)) + PA_ALIGN(sizeof(pa_fdsem_data)));
64 #ifdef HAVE_SYS_EVENTFD_H
65 if ((f->efd = eventfd(0, EFD_CLOEXEC)) >= 0)
66 f->fds[0] = f->fds[1] = -1;
70 if (pa_pipe_cloexec(f->fds) < 0) {
76 f->data = (pa_fdsem_data*) ((uint8_t*) f + PA_ALIGN(sizeof(pa_fdsem)));
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);
85 pa_fdsem *pa_fdsem_open_shm(pa_fdsem_data *data, int event_fd) {
89 pa_assert(event_fd >= 0);
91 #ifdef HAVE_SYS_EVENTFD_H
92 f = pa_xnew(pa_fdsem, 1);
95 pa_make_fd_cloexec(f->efd);
96 f->fds[0] = f->fds[1] = -1;
103 pa_fdsem *pa_fdsem_new_shm(pa_fdsem_data *data, int* event_fd) {
109 #ifdef HAVE_SYS_EVENTFD_H
111 f = pa_xnew(pa_fdsem, 1);
113 if ((f->efd = eventfd(0, EFD_CLOEXEC)) < 0) {
118 f->fds[0] = f->fds[1] = -1;
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);
130 void pa_fdsem_free(pa_fdsem *f) {
133 #ifdef HAVE_SYS_EVENTFD_H
137 pa_close_pipe(f->fds);
142 static void flush(pa_fdsem *f) {
146 if (pa_atomic_load(&f->data->in_pipe) <= 0)
152 #ifdef HAVE_SYS_EVENTFD_H
156 if ((r = read(f->efd, &u, sizeof(u))) != sizeof(u)) {
157 pa_assert(r < 0 && errno == EINTR);
164 if ((r = read(f->fds[0], &x, sizeof(x))) <= 0) {
165 pa_assert(r < 0 && errno == EINTR);
169 } while (pa_atomic_sub(&f->data->in_pipe, (int) r) > (int) r);
172 void pa_fdsem_post(pa_fdsem *f) {
175 if (pa_atomic_cmpxchg(&f->data->signalled, 0, 1)) {
177 if (pa_atomic_load(&f->data->waiting)) {
181 pa_atomic_inc(&f->data->in_pipe);
185 #ifdef HAVE_SYS_EVENTFD_H
189 if ((r = write(f->efd, &u, sizeof(u))) != sizeof(u)) {
190 pa_assert(r < 0 && errno == EINTR);
196 if ((r = write(f->fds[1], &x, 1)) != 1) {
197 pa_assert(r < 0 && errno == EINTR);
207 void pa_fdsem_wait(pa_fdsem *f) {
212 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
215 pa_atomic_inc(&f->data->waiting);
217 while (!pa_atomic_cmpxchg(&f->data->signalled, 1, 0)) {
221 #ifdef HAVE_SYS_EVENTFD_H
225 if ((r = read(f->efd, &u, sizeof(u))) != sizeof(u)) {
226 pa_assert(r < 0 && errno == EINTR);
234 if ((r = read(f->fds[0], &x, sizeof(x))) <= 0) {
235 pa_assert(r < 0 && errno == EINTR);
239 pa_atomic_sub(&f->data->in_pipe, (int) r);
242 pa_assert_se(pa_atomic_dec(&f->data->waiting) >= 1);
245 int pa_fdsem_try(pa_fdsem *f) {
250 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
256 int pa_fdsem_get(pa_fdsem *f) {
259 #ifdef HAVE_SYS_EVENTFD_H
267 int pa_fdsem_before_poll(pa_fdsem *f) {
272 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
275 pa_atomic_inc(&f->data->waiting);
277 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0)) {
278 pa_assert_se(pa_atomic_dec(&f->data->waiting) >= 1);
284 int pa_fdsem_after_poll(pa_fdsem *f) {
287 pa_assert_se(pa_atomic_dec(&f->data->waiting) >= 1);
291 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))