4 This file is part of PulseAudio.
6 Copyright 2006 Lennart Poettering
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 published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
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 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <pulsecore/macro.h>
31 #include <pulsecore/mutex.h>
35 /* Not reentrant -- how could it be? */
36 void pa_once(pa_once_t *control, pa_once_func_t func) {
42 if (pa_atomic_load(&control->done))
45 pa_atomic_inc(&control->ref);
49 if ((m = pa_atomic_ptr_load(&control->mutex))) {
51 /* The mutex is stored in locked state, hence let's just
52 * wait until it is unlocked */
58 pa_assert_se(m = pa_mutex_new(0));
61 if (pa_atomic_ptr_cmpxchg(&control->mutex, NULL, m)) {
63 pa_atomic_store(&control->done, 1);
73 pa_assert(pa_atomic_load(&control->done));
75 if (pa_atomic_dec(&control->ref) <= 1) {
76 pa_assert(pa_atomic_ptr_cmpxchg(&control->mutex, m, NULL));
80 /* Caveat: We have to make sure that the once func has completed
81 * before returning, even if the once func is not actually
82 * executed by us. Hence the awkward locking. */