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