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
31 #include <pulsecore/mutex.h>
35 #define ASSERT_SUCCESS(x) do { \
40 static pa_mutex
*global_mutex
;
41 static pthread_once_t global_mutex_once
= PTHREAD_ONCE_INIT
;
43 static void global_mutex_once_func(void) {
44 global_mutex
= pa_mutex_new(0);
47 void pa_once(pa_once_t
*control
, pa_once_func_t func
) {
51 /* Create the global mutex */
52 ASSERT_SUCCESS(pthread_once(&global_mutex_once
, global_mutex_once_func
));
54 /* Create the local mutex */
55 pa_mutex_lock(global_mutex
);
57 control
->mutex
= pa_mutex_new(1);
58 pa_mutex_unlock(global_mutex
);
60 /* Execute function */
61 pa_mutex_lock(control
->mutex
);
62 if (!control
->once_value
) {
63 control
->once_value
= 1;
66 pa_mutex_unlock(control
->mutex
);
68 /* Caveat: We have to make sure that the once func has completed
69 * before returning, even if the once func is not actually
70 * executed by us. Hence the awkward locking. */