4af7b36e6695da43469049f645537bdd2d1cb3b5
[profile/ivi/pulseaudio-panda.git] / src / pulsecore / once-posix.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2006 Lennart Poettering
7
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.
12
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.
17
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
21   USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <pthread.h>
29 #include <assert.h>
30
31 #include <pulsecore/mutex.h>
32
33 #include "once.h"
34
35 #define ASSERT_SUCCESS(x) do { \
36     int _r = (x); \
37     assert(_r == 0); \
38 } while(0)
39
40 static pa_mutex *global_mutex;
41 static pthread_once_t global_mutex_once = PTHREAD_ONCE_INIT;
42
43 static void global_mutex_once_func(void) {
44     global_mutex = pa_mutex_new(0);
45 }
46
47 void pa_once(pa_once_t *control, pa_once_func_t func) {
48     assert(control);
49     assert(func);
50
51     /* Create the global mutex */
52     ASSERT_SUCCESS(pthread_once(&global_mutex_once, global_mutex_once_func));
53
54     /* Create the local mutex */
55     pa_mutex_lock(global_mutex);
56     if (!control->mutex)
57         control->mutex = pa_mutex_new(1);
58     pa_mutex_unlock(global_mutex);
59
60     /* Execute function */
61     pa_mutex_lock(control->mutex);
62     if (!control->once_value) {
63         control->once_value = 1;
64         func();
65     }
66     pa_mutex_unlock(control->mutex);
67
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. */
71 }