2 This file is part of PulseAudio.
4 Copyright 2008 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 published
8 by the Free Software Foundation; either version 2.1 of the License,
9 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 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 #include <pulse/gccmacro.h>
35 #include <pulse/i18n.h>
36 #include <pulse/xmalloc.h>
38 #include <pulsecore/poll.h>
39 #include <pulsecore/mutex.h>
40 #include <pulsecore/thread.h>
41 #include <pulsecore/core-util.h>
43 #include "lock-autospawn.h"
45 /* So, why do we have this complex code here with threads and pipes
46 * and stuff? For two reasons: POSIX file locks are per-process, not
47 * per-file descriptor. That means that two contexts within the same
48 * process that try to create the autospawn lock might end up assuming
49 * they both managed to lock the file. And then, POSIX locking
50 * operations are synchronous. If two contexts run from the same event
51 * loop it must be made sure that they do not block each other, but
52 * that the locking operation can happen asynchronously. */
54 #define AUTOSPAWN_LOCK "autospawn.lock"
56 static pa_mutex
*mutex
;
58 static unsigned n_ref
= 0;
59 static int lock_fd
= -1;
60 static pa_mutex
*lock_fd_mutex
= NULL
;
61 static pa_thread
*thread
= NULL
;
62 static int pipe_fd
[2] = { -1, -1 };
71 static void destroy_mutex(void) PA_GCC_DESTRUCTOR
;
73 static int ref(void) {
77 pa_assert(pipe_fd
[0] >= 0);
78 pa_assert(pipe_fd
[1] >= 0);
79 pa_assert(lock_fd_mutex
);
86 pa_assert(!lock_fd_mutex
);
87 pa_assert(state
== STATE_IDLE
);
88 pa_assert(lock_fd
< 0);
90 pa_assert(pipe_fd
[0] < 0);
91 pa_assert(pipe_fd
[1] < 0);
93 if (pa_pipe_cloexec(pipe_fd
) < 0)
96 pa_make_fd_nonblock(pipe_fd
[1]);
97 pa_make_fd_nonblock(pipe_fd
[0]);
99 lock_fd_mutex
= pa_mutex_new(FALSE
, FALSE
);
105 static void unref(pa_bool_t after_fork
) {
107 pa_assert(n_ref
> 0);
108 pa_assert(pipe_fd
[0] >= 0);
109 pa_assert(pipe_fd
[1] >= 0);
110 pa_assert(lock_fd_mutex
);
118 pa_thread_free(thread
);
122 pa_mutex_lock(lock_fd_mutex
);
124 pa_assert(state
!= STATE_TAKEN
);
126 if (state
== STATE_OWNING
) {
128 pa_assert(lock_fd
>= 0);
135 if (!(lf
= pa_runtime_path(AUTOSPAWN_LOCK
)))
136 pa_log_warn(_("Cannot access autospawn lock."));
138 pa_unlock_lockfile(lf
, lock_fd
);
146 pa_mutex_unlock(lock_fd_mutex
);
148 pa_mutex_free(lock_fd_mutex
);
149 lock_fd_mutex
= NULL
;
151 pa_close(pipe_fd
[0]);
152 pa_close(pipe_fd
[1]);
153 pipe_fd
[0] = pipe_fd
[1] = -1;
156 static void ping(void) {
159 pa_assert(pipe_fd
[1] >= 0);
164 if ((s
= pa_write(pipe_fd
[1], &x
, 1, NULL
)) == 1)
172 pa_assert(errno
== EINTR
);
176 static void wait_for_ping(void) {
182 pa_assert(pipe_fd
[0] >= 0);
184 memset(&pfd
, 0, sizeof(pfd
));
188 if ((k
= pa_poll(&pfd
, 1, -1)) != 1) {
190 pa_assert(errno
== EINTR
);
191 } else if ((s
= pa_read(pipe_fd
[0], &x
, 1, NULL
)) != 1) {
193 pa_assert(errno
== EAGAIN
);
197 static void empty_pipe(void) {
201 pa_assert(pipe_fd
[0] >= 0);
203 if ((s
= pa_read(pipe_fd
[0], &x
, sizeof(x
), NULL
)) < 1) {
205 pa_assert(errno
== EAGAIN
);
209 static void thread_func(void *u
) {
216 /* No signals in this thread please */
217 sigfillset(&fullset
);
218 pthread_sigmask(SIG_BLOCK
, &fullset
, NULL
);
221 if (!(lf
= pa_runtime_path(AUTOSPAWN_LOCK
))) {
222 pa_log_warn(_("Cannot access autospawn lock."));
226 if ((fd
= pa_lock_lockfile(lf
)) < 0)
229 pa_mutex_lock(lock_fd_mutex
);
230 pa_assert(state
== STATE_IDLE
);
232 state
= STATE_OWNING
;
233 pa_mutex_unlock(lock_fd_mutex
);
238 pa_mutex_lock(lock_fd_mutex
);
239 pa_assert(state
== STATE_IDLE
);
240 state
= STATE_FAILED
;
241 pa_mutex_unlock(lock_fd_mutex
);
249 static int start_thread(void) {
252 if (!(thread
= pa_thread_new("autospawn", thread_func
, NULL
)))
258 static void create_mutex(void) {
260 mutex
= pa_mutex_new(FALSE
, FALSE
);
264 static void destroy_mutex(void) {
266 pa_mutex_free(mutex
);
269 int pa_autospawn_lock_init(void) {
273 pa_mutex_lock(mutex
);
280 pa_mutex_unlock(mutex
);
285 int pa_autospawn_lock_acquire(pa_bool_t block
) {
289 pa_mutex_lock(mutex
);
290 pa_assert(n_ref
>= 1);
292 pa_mutex_lock(lock_fd_mutex
);
298 if (state
== STATE_OWNING
) {
304 if (state
== STATE_FAILED
) {
309 if (state
== STATE_IDLE
)
310 if (start_thread() < 0)
318 pa_mutex_unlock(lock_fd_mutex
);
319 pa_mutex_unlock(mutex
);
323 pa_mutex_lock(mutex
);
324 pa_mutex_lock(lock_fd_mutex
);
327 pa_mutex_unlock(lock_fd_mutex
);
329 pa_mutex_unlock(mutex
);
334 void pa_autospawn_lock_release(void) {
337 pa_mutex_lock(mutex
);
338 pa_assert(n_ref
>= 1);
340 pa_assert(state
== STATE_TAKEN
);
341 state
= STATE_OWNING
;
345 pa_mutex_unlock(mutex
);
348 void pa_autospawn_lock_done(pa_bool_t after_fork
) {
351 pa_mutex_lock(mutex
);
352 pa_assert(n_ref
>= 1);
356 pa_mutex_unlock(mutex
);