]>
code.delx.au - pulseaudio/blob - src/pulse/fork-detect.c
2 This file is part of PulseAudio.
4 Copyright 2009 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
28 #include <pulsecore/atomic.h>
29 #include <pulsecore/macro.h>
31 #include "fork-detect.h"
33 int pa_detect_fork(void) {
34 static pa_atomic_t pid
= PA_ATOMIC_INIT((int) -1);
36 /* Some really stupid applications (Hey, vim, that means you!)
37 * love to fork after initializing
38 * gtk/libcanberra/pulseaudio. This is really bad style. We
39 * however have to deal with this cleanly, so we try to detect the
40 * forks making sure all our calls fail cleanly after the fork. */
42 pa_assert_cc(sizeof(pa_atomic_t
) >= sizeof(pid_t
));
45 pid_t stored_pid
= (pid_t
) pa_atomic_load(&pid
);
47 /* First let's check whether the current pid matches the stored one */
48 if (stored_pid
== getpid())
51 /* Does it contain a different PID than ours? Then the process got forked. */
52 if ((int) stored_pid
!= (int) -1)
55 /* Ok, it still contains no PID, then store it */
56 if (pa_atomic_cmpxchg(&pid
, (int) -1, (int) getpid()))