]> code.delx.au - pulseaudio/blob - src/pulsecore/thread.h
Instead of including config.h from header files, check whether PACKAGE is defined...
[pulseaudio] / src / pulsecore / thread.h
1 #ifndef foopulsethreadhfoo
2 #define foopulsethreadhfoo
3
4 /* $Id$ */
5
6 /***
7 This file is part of PulseAudio.
8
9 Copyright 2006 Lennart Poettering
10 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
11
12 PulseAudio is free software; you can redistribute it and/or modify
13 it under the terms of the GNU Lesser General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
16
17 PulseAudio is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
21
22 You should have received a copy of the GNU Lesser General Public
23 License along with PulseAudio; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 USA.
26 ***/
27
28 #include <pulse/def.h>
29 #include <pulsecore/once.h>
30
31 #ifndef PACKAGE
32 #error "Please include config.h before including this file!"
33 #endif
34
35 typedef struct pa_thread pa_thread;
36
37 typedef void (*pa_thread_func_t) (void *userdata);
38
39 pa_thread* pa_thread_new(pa_thread_func_t thread_func, void *userdata);
40 void pa_thread_free(pa_thread *t);
41 int pa_thread_join(pa_thread *t);
42 int pa_thread_is_running(pa_thread *t);
43 pa_thread *pa_thread_self(void);
44 void pa_thread_yield(void);
45
46 void* pa_thread_get_data(pa_thread *t);
47 void pa_thread_set_data(pa_thread *t, void *userdata);
48
49 typedef struct pa_tls pa_tls;
50
51 pa_tls* pa_tls_new(pa_free_cb_t free_cb);
52 void pa_tls_free(pa_tls *t);
53 void * pa_tls_get(pa_tls *t);
54 void *pa_tls_set(pa_tls *t, void *userdata);
55
56 #define PA_STATIC_TLS_DECLARE(name, free_cb) \
57 static struct { \
58 pa_once once; \
59 pa_tls *tls; \
60 } name##_tls = { \
61 .once = PA_ONCE_INIT, \
62 .tls = NULL \
63 }; \
64 static void name##_tls_init(void) { \
65 name##_tls.tls = pa_tls_new(free_cb); \
66 } \
67 static inline pa_tls* name##_tls_obj(void) { \
68 pa_run_once(&name##_tls.once, name##_tls_init); \
69 return name##_tls.tls; \
70 } \
71 static void name##_tls_destructor(void) PA_GCC_DESTRUCTOR; \
72 static void name##_tls_destructor(void) { \
73 if (name##_tls.tls) \
74 pa_tls_free(name##_tls.tls); \
75 } \
76 static inline void* name##_tls_get(void) { \
77 return pa_tls_get(name##_tls_obj()); \
78 } \
79 static inline void* name##_tls_set(void *p) { \
80 return pa_tls_set(name##_tls_obj(), p); \
81 } \
82 struct __stupid_useless_struct_to_allow_trailing_semicolon
83
84 #ifdef HAVE_TLS_BUILTIN
85 /* An optimized version of the above that requires no dynamic
86 * allocation if the compiler supports __thread */
87 #define PA_STATIC_TLS_DECLARE_NO_FREE(name) \
88 static __thread void *name##_tls; \
89 static inline void* name##_tls_get(void) { \
90 return name##_tls; \
91 } \
92 static inline void* name##_tls_set(void *p) { \
93 void *r = name##_tls; \
94 name##_tls = p; \
95 return r; \
96 } \
97 struct __stupid_useless_struct_to_allow_trailing_semicolon
98 #else
99 #define PA_STATIC_TLS_DECLARE_NO_FREE(name) PA_STATIC_TLS_DECLARE(name, NULL)
100 #endif
101
102 #define PA_STATIC_TLS_GET(name) (name##_tls_get())
103 #define PA_STATIC_TLS_SET(name, p) (name##_tls_set(p))
104
105 #endif