]>
code.delx.au - pulseaudio/blob - src/pulsecore/once.c
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
28 #include <pulsecore/macro.h>
29 #include <pulsecore/mutex.h>
33 int pa_once_begin(pa_once
*control
) {
38 if (pa_atomic_load(&control
->done
))
41 pa_atomic_inc(&control
->ref
);
43 /* Caveat: We have to make sure that the once func has completed
44 * before returning, even if the once func is not actually
45 * executed by us. Hence the awkward locking. */
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 */
59 pa_assert_se(m
= pa_mutex_new(FALSE
, FALSE
));
62 if (pa_atomic_ptr_cmpxchg(&control
->mutex
, NULL
, m
))
70 void pa_once_end(pa_once
*control
) {
75 pa_atomic_store(&control
->done
, 1);
77 pa_assert_se(m
= pa_atomic_ptr_load(&control
->mutex
));
80 if (pa_atomic_dec(&control
->ref
) <= 1) {
81 pa_assert_se(pa_atomic_ptr_cmpxchg(&control
->mutex
, m
, NULL
));
86 /* Not reentrant -- how could it be? */
87 void pa_run_once(pa_once
*control
, pa_once_func_t func
) {
91 if (pa_once_begin(control
)) {