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