xref: /third_party/pulseaudio/src/pulsecore/once.h (revision 53a5a1b3)
1#ifndef foopulseoncehfoo
2#define foopulseoncehfoo
3
4/***
5  This file is part of PulseAudio.
6
7  Copyright 2006 Lennart Poettering
8
9  PulseAudio is free software; you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as
11  published by the Free Software Foundation; either version 2.1 of the
12  License, or (at your option) any later version.
13
14  PulseAudio is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU Lesser General Public
20  License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
21***/
22
23#include <pulsecore/atomic.h>
24#include <pulsecore/mutex.h>
25
26typedef struct pa_once {
27    pa_static_mutex mutex;
28    pa_atomic_t done;
29} pa_once;
30
31#define PA_ONCE_INIT                                                    \
32    {                                                                   \
33        .mutex = PA_STATIC_MUTEX_INIT,                                  \
34        .done = PA_ATOMIC_INIT(0)                                       \
35    }
36
37/* Not to be called directly, use the macros defined below instead */
38bool pa_once_begin(pa_once *o);
39void pa_once_end(pa_once *o);
40
41#define PA_ONCE_BEGIN                                                   \
42    do {                                                                \
43        static pa_once _once = PA_ONCE_INIT;                            \
44        if (pa_once_begin(&_once)) {{
45
46#define PA_ONCE_END                                                     \
47            }                                                           \
48            pa_once_end(&_once);                                        \
49        }                                                               \
50    } while(0)
51
52/*
53
54  Usage of these macros is like this:
55
56  void foo() {
57
58      PA_ONCE_BEGIN {
59
60          ... stuff to be called just once ...
61
62      } PA_ONCE_END;
63  }
64
65*/
66
67/* Same API but calls a function */
68typedef void (*pa_once_func_t) (void);
69void pa_run_once(pa_once *o, pa_once_func_t f);
70
71#endif
72