]>
code.delx.au - pulseaudio/blob - src/pulsecore/lock-autospawn.c
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
33 #include <pulse/i18n.h>
34 #include <pulse/xmalloc.h>
36 #include <pulsecore/mutex.h>
37 #include <pulsecore/thread.h>
38 #include <pulsecore/core-util.h>
40 #include "lock-autospawn.h"
42 /* So, why do we have this complex code here with threads and pipes
43 * and stuff? For two reasons: POSIX file locks are per-process, not
44 * per-file descriptor. That means that two contexts within the same
45 * process that try to create the autospawn lock might end up assuming
46 * they both managed to lock the file. And then, POSIX locking
47 * operations are synchronous. If two contexts run from the same event
48 * loop it must be made sure that they do not block each other, but
49 * that the locking operation can happen asynchronously. */
51 #define AUTOSPAWN_LOCK "autospawn.lock"
53 static pa_mutex
*mutex
;
55 static unsigned n_ref
= 0;
56 static int lock_fd
= -1;
57 static pa_mutex
*lock_fd_mutex
= NULL
;
58 static pa_thread
*thread
= NULL
;
59 static int pipe_fd
[2] = { -1, -1 };
68 static void destroy_mutex(void) PA_GCC_DESTRUCTOR
;
70 static int ref(void) {
74 pa_assert(pipe_fd
[0] >= 0);
75 pa_assert(pipe_fd
[1] >= 0);
76 pa_assert(lock_fd_mutex
);
83 pa_assert(!lock_fd_mutex
);
84 pa_assert(state
== STATE_IDLE
);
85 pa_assert(lock_fd
< 0);
87 pa_assert(pipe_fd
[0] < 0);
88 pa_assert(pipe_fd
[1] < 0);
90 if (pipe(pipe_fd
) < 0)
93 pa_make_fd_cloexec(pipe_fd
[0]);
94 pa_make_fd_cloexec(pipe_fd
[1]);
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
= write(pipe_fd
[1], &x
, 1)) == 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
= poll(&pfd
, 1, -1)) != 1) {
190 pa_assert(errno
== EINTR
);
191 } else if ((s
= read(pipe_fd
[0], &x
, 1)) != 1) {
193 pa_assert(errno
== EAGAIN
);
197 static void empty_pipe(void) {
201 pa_assert(pipe_fd
[0] >= 0);
203 if ((s
= read(pipe_fd
[0], &x
, sizeof(x
))) < 1) {
205 pa_assert(errno
== EAGAIN
);
209 static void thread_func(void *u
) {
214 /* No signals in this thread please */
215 sigfillset(&fullset
);
216 pthread_sigmask(SIG_BLOCK
, &fullset
, NULL
);
218 if (!(lf
= pa_runtime_path(AUTOSPAWN_LOCK
))) {
219 pa_log_warn(_("Cannot access autospawn lock."));
223 if ((fd
= pa_lock_lockfile(lf
)) < 0)
226 pa_mutex_lock(lock_fd_mutex
);
227 pa_assert(state
== STATE_IDLE
);
229 state
= STATE_OWNING
;
230 pa_mutex_unlock(lock_fd_mutex
);
235 pa_mutex_lock(lock_fd_mutex
);
236 pa_assert(state
== STATE_IDLE
);
237 state
= STATE_FAILED
;
238 pa_mutex_unlock(lock_fd_mutex
);
246 static int start_thread(void) {
249 if (!(thread
= pa_thread_new(thread_func
, NULL
)))
255 static void create_mutex(void) {
257 mutex
= pa_mutex_new(FALSE
, FALSE
);
261 static void destroy_mutex(void) {
263 pa_mutex_free(mutex
);
266 int pa_autospawn_lock_init(void) {
270 pa_mutex_lock(mutex
);
277 pa_mutex_unlock(mutex
);
282 int pa_autospawn_lock_acquire(pa_bool_t block
) {
286 pa_mutex_lock(mutex
);
287 pa_assert(n_ref
>= 1);
289 pa_mutex_lock(lock_fd_mutex
);
295 if (state
== STATE_OWNING
) {
301 if (state
== STATE_FAILED
) {
306 if (state
== STATE_IDLE
)
307 if (start_thread() < 0)
315 pa_mutex_unlock(lock_fd_mutex
);
316 pa_mutex_unlock(mutex
);
320 pa_mutex_lock(mutex
);
321 pa_mutex_lock(lock_fd_mutex
);
324 pa_mutex_unlock(lock_fd_mutex
);
326 pa_mutex_unlock(mutex
);
331 void pa_autospawn_lock_release(void) {
334 pa_mutex_lock(mutex
);
335 pa_assert(n_ref
>= 1);
337 pa_assert(state
== STATE_TAKEN
);
338 state
= STATE_OWNING
;
342 pa_mutex_unlock(mutex
);
345 void pa_autospawn_lock_done(pa_bool_t after_fork
) {
348 pa_mutex_lock(mutex
);
349 pa_assert(n_ref
>= 1);
353 pa_mutex_unlock(mutex
);