]> code.delx.au - pulseaudio/blob - src/pulsecore/thread.h
explicitly destory TLS data before destroying TLS
[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 return; \
75 if (free_cb) { \
76 void *p; \
77 if ((p = pa_tls_get(name##_tls.tls))) \
78 free_cb(p); \
79 } \
80 pa_tls_free(name##_tls.tls); \
81 } \
82 static inline void* name##_tls_get(void) { \
83 return pa_tls_get(name##_tls_obj()); \
84 } \
85 static inline void* name##_tls_set(void *p) { \
86 return pa_tls_set(name##_tls_obj(), p); \
87 } \
88 struct __stupid_useless_struct_to_allow_trailing_semicolon
89
90 #ifdef HAVE_TLS_BUILTIN
91 /* An optimized version of the above that requires no dynamic
92 * allocation if the compiler supports __thread */
93 #define PA_STATIC_TLS_DECLARE_NO_FREE(name) \
94 static __thread void *name##_tls = NULL; \
95 static inline void* name##_tls_get(void) { \
96 return name##_tls; \
97 } \
98 static inline void* name##_tls_set(void *p) { \
99 void *r = name##_tls; \
100 name##_tls = p; \
101 return r; \
102 } \
103 struct __stupid_useless_struct_to_allow_trailing_semicolon
104 #else
105 #define PA_STATIC_TLS_DECLARE_NO_FREE(name) PA_STATIC_TLS_DECLARE(name, NULL)
106 #endif
107
108 #define PA_STATIC_TLS_GET(name) (name##_tls_get())
109 #define PA_STATIC_TLS_SET(name, p) (name##_tls_set(p))
110
111 #endif