]> code.delx.au - pulseaudio/blob - src/pulsecore/fdsem.c
add an assert()
[pulseaudio] / src / pulsecore / fdsem.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2006 Lennart Poettering
7
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.
12
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.
17
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
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <unistd.h>
29 #include <errno.h>
30
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>
37
38 #include "fdsem.h"
39
40 struct pa_fdsem {
41 int fds[2];
42 pa_atomic_t waiting;
43 pa_atomic_t signalled;
44 pa_atomic_t in_pipe;
45 };
46
47 pa_fdsem *pa_fdsem_new(void) {
48 pa_fdsem *f;
49
50 f = pa_xnew(pa_fdsem, 1);
51
52 if (pipe(f->fds) < 0) {
53 pa_xfree(f);
54 return NULL;
55 }
56
57 pa_fd_set_cloexec(f->fds[0], 1);
58 pa_fd_set_cloexec(f->fds[1], 1);
59
60 pa_atomic_store(&f->waiting, 0);
61 pa_atomic_store(&f->signalled, 0);
62 pa_atomic_store(&f->in_pipe, 0);
63
64 return f;
65 }
66
67 void pa_fdsem_free(pa_fdsem *f) {
68 pa_assert(f);
69
70 close(f->fds[0]);
71 close(f->fds[1]);
72
73 pa_xfree(f);
74 }
75
76 static void flush(pa_fdsem *f) {
77 ssize_t r;
78 pa_assert(f);
79
80 if (pa_atomic_load(&f->in_pipe) <= 0)
81 return;
82
83 do {
84 char x[10];
85
86 if ((r = read(f->fds[0], &x, sizeof(x))) <= 0) {
87 pa_assert(r < 0 && errno == EINTR);
88 continue;
89 }
90
91 } while (pa_atomic_sub(&f->in_pipe, r) > r);
92 }
93
94 void pa_fdsem_post(pa_fdsem *f) {
95 pa_assert(f);
96
97 if (pa_atomic_cmpxchg(&f->signalled, 0, 1)) {
98
99 if (pa_atomic_load(&f->waiting)) {
100 ssize_t r;
101 char x = 'x';
102
103 pa_atomic_inc(&f->in_pipe);
104
105 for (;;) {
106
107 if ((r = write(f->fds[1], &x, 1)) != 1) {
108 pa_assert(r < 0 && errno == EINTR);
109 continue;
110 }
111
112 break;
113 }
114 }
115 }
116 }
117
118 void pa_fdsem_wait(pa_fdsem *f) {
119 pa_assert(f);
120
121 flush(f);
122
123 if (pa_atomic_cmpxchg(&f->signalled, 1, 0))
124 return;
125
126 pa_atomic_inc(&f->waiting);
127
128 while (!pa_atomic_cmpxchg(&f->signalled, 1, 0)) {
129 char x[10];
130 ssize_t r;
131
132 if ((r = read(f->fds[0], &x, sizeof(x))) <= 0) {
133 pa_assert(r < 0 && errno == EINTR);
134 continue;
135 }
136
137 pa_atomic_sub(&f->in_pipe, r);
138 }
139
140 pa_assert_se(pa_atomic_dec(&f->waiting) >= 1);
141 }
142
143 int pa_fdsem_try(pa_fdsem *f) {
144 pa_assert(f);
145
146 flush(f);
147
148 if (pa_atomic_cmpxchg(&f->signalled, 1, 0))
149 return 1;
150
151 return 0;
152 }
153
154
155 int pa_fdsem_get(pa_fdsem *f) {
156 pa_assert(f);
157
158 return f->fds[0];
159 }
160
161 int pa_fdsem_before_poll(pa_fdsem *f) {
162 pa_assert(f);
163
164 flush(f);
165
166 if (pa_atomic_cmpxchg(&f->signalled, 1, 0))
167 return -1;
168
169 pa_atomic_inc(&f->waiting);
170
171 if (pa_atomic_cmpxchg(&f->signalled, 1, 0)) {
172 pa_assert_se(pa_atomic_dec(&f->waiting) >= 1);
173 return -1;
174 }
175 return 0;
176 }
177
178 int pa_fdsem_after_poll(pa_fdsem *f) {
179 pa_assert(f);
180
181 pa_assert_se(pa_atomic_dec(&f->waiting) >= 1);
182
183 flush(f);
184
185 if (pa_atomic_cmpxchg(&f->signalled, 1, 0))
186 return 1;
187
188 return 0;
189 }