]>
code.delx.au - pulseaudio/blob - src/pulsecore/fdsem.c
4 This file is part of PulseAudio.
6 Copyright 2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as
10 published by the Free Software Foundation; either version 2.1 of the
11 License, or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <pulsecore/atomic.h>
32 #include <pulsecore/log.h>
33 #include <pulsecore/thread.h>
34 #include <pulsecore/macro.h>
35 #include <pulsecore/core-util.h>
36 #include <pulse/xmalloc.h>
43 pa_atomic_t signalled
;
47 pa_fdsem
*pa_fdsem_new(void) {
50 f
= pa_xnew(pa_fdsem
, 1);
52 if (pipe(f
->fds
) < 0) {
57 pa_atomic_store(&f
->waiting
, 0);
58 pa_atomic_store(&f
->signalled
, 0);
59 pa_atomic_store(&f
->in_pipe
, 0);
64 void pa_fdsem_free(pa_fdsem
*f
) {
73 static void flush(pa_fdsem
*f
) {
77 if (pa_atomic_load(&f
->in_pipe
) <= 0)
83 if ((r
= read(f
->fds
[0], &x
, sizeof(x
))) <= 0) {
84 pa_assert(r
< 0 && errno
== EINTR
);
88 } while (pa_atomic_sub(&f
->in_pipe
, r
) > r
);
91 void pa_fdsem_post(pa_fdsem
*f
) {
94 if (pa_atomic_cmpxchg(&f
->signalled
, 0, 1)) {
96 if (pa_atomic_load(&f
->waiting
)) {
100 pa_atomic_inc(&f
->in_pipe
);
104 if ((r
= write(f
->fds
[1], &x
, 1)) != 1) {
105 pa_assert(r
< 0 && errno
== EINTR
);
115 void pa_fdsem_wait(pa_fdsem
*f
) {
120 if (pa_atomic_cmpxchg(&f
->signalled
, 1, 0))
123 pa_atomic_inc(&f
->waiting
);
125 while (!pa_atomic_cmpxchg(&f
->signalled
, 1, 0)) {
129 if ((r
= read(f
->fds
[0], &x
, sizeof(x
))) <= 0) {
130 pa_assert(r
< 0 && errno
== EINTR
);
134 pa_atomic_sub(&f
->in_pipe
, r
);
137 pa_atomic_dec(&f
->waiting
);
140 int pa_fdsem_try(pa_fdsem
*f
) {
145 if (pa_atomic_cmpxchg(&f
->signalled
, 1, 0))
152 int pa_fdsem_get(pa_fdsem
*f
) {
158 int pa_fdsem_before_poll(pa_fdsem
*f
) {
163 if (pa_atomic_cmpxchg(&f
->signalled
, 1, 0))
166 pa_atomic_inc(&f
->waiting
);
168 if (pa_atomic_cmpxchg(&f
->signalled
, 1, 0)) {
169 pa_assert_se(pa_atomic_dec(&f
->waiting
) >= 1);
175 int pa_fdsem_after_poll(pa_fdsem
*f
) {
178 pa_assert_se(pa_atomic_dec(&f
->waiting
) >= 1);
182 if (pa_atomic_cmpxchg(&f
->signalled
, 1, 0))