l10n: Updates to Russian (ru) translation
[pulseaudio-mirror.git] / src / pulsecore / fdsem.c
blob5bf994793c78b04808505d1a5e9e5cf40192c1e8
1 /***
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
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #ifdef HAVE_SYS_SYSCALL_H
27 #include <sys/syscall.h>
28 #endif
30 #include <unistd.h>
31 #include <errno.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>
41 #ifndef HAVE_PIPE
42 #include <pulsecore/pipe.h>
43 #endif
45 #ifdef HAVE_SYS_EVENTFD_H
46 #include <sys/eventfd.h>
47 #endif
49 #include "fdsem.h"
51 struct pa_fdsem {
52 int fds[2];
53 #ifdef HAVE_SYS_EVENTFD_H
54 int efd;
55 #endif
57 pa_fdsem_data *data;
60 pa_fdsem *pa_fdsem_new(void) {
61 pa_fdsem *f;
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;
68 else
69 #endif
71 if (pa_pipe_cloexec(f->fds) < 0) {
72 pa_xfree(f);
73 return NULL;
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);
83 return f;
86 pa_fdsem *pa_fdsem_open_shm(pa_fdsem_data *data, int event_fd) {
87 pa_fdsem *f = NULL;
89 pa_assert(data);
90 pa_assert(event_fd >= 0);
92 #ifdef HAVE_SYS_EVENTFD_H
93 f = pa_xnew(pa_fdsem, 1);
95 f->efd = event_fd;
96 pa_make_fd_cloexec(f->efd);
97 f->fds[0] = f->fds[1] = -1;
98 f->data = data;
99 #endif
101 return f;
104 pa_fdsem *pa_fdsem_new_shm(pa_fdsem_data *data, int* event_fd) {
105 pa_fdsem *f = NULL;
107 pa_assert(data);
108 pa_assert(event_fd);
110 #ifdef HAVE_SYS_EVENTFD_H
112 f = pa_xnew(pa_fdsem, 1);
114 if ((f->efd = eventfd(0, EFD_CLOEXEC)) < 0) {
115 pa_xfree(f);
116 return NULL;
119 f->fds[0] = f->fds[1] = -1;
120 f->data = data;
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);
126 #endif
128 return f;
131 void pa_fdsem_free(pa_fdsem *f) {
132 pa_assert(f);
134 #ifdef HAVE_SYS_EVENTFD_H
135 if (f->efd >= 0)
136 pa_close(f->efd);
137 #endif
138 pa_close_pipe(f->fds);
140 pa_xfree(f);
143 static void flush(pa_fdsem *f) {
144 ssize_t r;
145 pa_assert(f);
147 if (pa_atomic_load(&f->data->in_pipe) <= 0)
148 return;
150 do {
151 char x[10];
153 #ifdef HAVE_SYS_EVENTFD_H
154 if (f->efd >= 0) {
155 uint64_t u;
157 if ((r = read(f->efd, &u, sizeof(u))) != 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();
164 continue;
166 r = (ssize_t) u;
167 } else
168 #endif
170 if ((r = read(f->fds[0], &x, sizeof(x))) <= 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();
177 continue;
180 } while (pa_atomic_sub(&f->data->in_pipe, (int) r) > (int) r);
183 void pa_fdsem_post(pa_fdsem *f) {
184 pa_assert(f);
186 if (pa_atomic_cmpxchg(&f->data->signalled, 0, 1)) {
188 if (pa_atomic_load(&f->data->waiting)) {
189 ssize_t r;
190 char x = 'x';
192 pa_atomic_inc(&f->data->in_pipe);
194 for (;;) {
196 #ifdef HAVE_SYS_EVENTFD_H
197 if (f->efd >= 0) {
198 uint64_t u = 1;
200 if ((r = write(f->efd, &u, sizeof(u))) != sizeof(u)) {
201 if (r >= 0 || errno != EINTR) {
202 pa_log_error("Invalid read from pipe: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
203 pa_assert_not_reached();
206 continue;
208 } else
209 #endif
211 if ((r = write(f->fds[1], &x, 1)) != 1) {
212 if (r >= 0 || errno != EINTR) {
213 pa_log_error("Invalid read from pipe: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
214 pa_assert_not_reached();
217 continue;
220 break;
226 void pa_fdsem_wait(pa_fdsem *f) {
227 pa_assert(f);
229 flush(f);
231 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
232 return;
234 pa_atomic_inc(&f->data->waiting);
236 while (!pa_atomic_cmpxchg(&f->data->signalled, 1, 0)) {
237 char x[10];
238 ssize_t r;
240 #ifdef HAVE_SYS_EVENTFD_H
241 if (f->efd >= 0) {
242 uint64_t u;
244 if ((r = read(f->efd, &u, sizeof(u))) != sizeof(u)) {
246 if (r >= 0 || errno != EINTR) {
247 pa_log_error("Invalid read from pipe: %s", r < 0 ? pa_cstrerror(errno) : "EOF");
248 pa_assert_not_reached();
251 continue;
254 r = (ssize_t) u;
255 } else
256 #endif
258 if ((r = read(f->fds[0], &x, sizeof(x))) <= 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();
265 continue;
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) {
275 pa_assert(f);
277 flush(f);
279 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
280 return 1;
282 return 0;
285 int pa_fdsem_get(pa_fdsem *f) {
286 pa_assert(f);
288 #ifdef HAVE_SYS_EVENTFD_H
289 if (f->efd >= 0)
290 return f->efd;
291 #endif
293 return f->fds[0];
296 int pa_fdsem_before_poll(pa_fdsem *f) {
297 pa_assert(f);
299 flush(f);
301 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
302 return -1;
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);
308 return -1;
310 return 0;
313 int pa_fdsem_after_poll(pa_fdsem *f) {
314 pa_assert(f);
316 pa_assert_se(pa_atomic_dec(&f->data->waiting) >= 1);
318 flush(f);
320 if (pa_atomic_cmpxchg(&f->data->signalled, 1, 0))
321 return 1;
323 return 0;