]> code.delx.au - pulseaudio/blob - src/pulsecore/thread.h
Apply #ifdefs around functionality not available on win32
[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 <pulsecore/once.h>
28 #include <pulsecore/core-util.h>
29
30 #ifndef PACKAGE
31 #error "Please include config.h before including this file!"
32 #endif
33
34 typedef struct pa_thread pa_thread;
35
36 typedef void (*pa_thread_func_t) (void *userdata);
37
38 pa_thread* pa_thread_new(const char *name, pa_thread_func_t thread_func, void *userdata);
39 void pa_thread_free(pa_thread *t);
40 int pa_thread_join(pa_thread *t);
41 int pa_thread_is_running(pa_thread *t);
42 pa_thread *pa_thread_self(void);
43 void pa_thread_yield(void);
44
45 void* pa_thread_get_data(pa_thread *t);
46 void pa_thread_set_data(pa_thread *t, void *userdata);
47
48 const char *pa_thread_get_name(pa_thread *t);
49 void pa_thread_set_name(pa_thread *t, const char *name);
50
51 typedef struct pa_tls pa_tls;
52
53 pa_tls* pa_tls_new(pa_free_cb_t free_cb);
54 void pa_tls_free(pa_tls *t);
55 void * pa_tls_get(pa_tls *t);
56 void *pa_tls_set(pa_tls *t, void *userdata);
57
58 #define PA_STATIC_TLS_DECLARE(name, free_cb) \
59 static struct { \
60 pa_once once; \
61 pa_tls *volatile tls; \
62 } name##_tls = { \
63 .once = PA_ONCE_INIT, \
64 .tls = NULL \
65 }; \
66 static void name##_tls_init(void) { \
67 name##_tls.tls = pa_tls_new(free_cb); \
68 } \
69 static inline pa_tls* name##_tls_obj(void) { \
70 pa_run_once(&name##_tls.once, name##_tls_init); \
71 return name##_tls.tls; \
72 } \
73 static void name##_tls_destructor(void) PA_GCC_DESTRUCTOR; \
74 static void name##_tls_destructor(void) { \
75 static void (*_free_cb)(void*) = free_cb; \
76 if (!pa_in_valgrind()) \
77 return; \
78 if (!name##_tls.tls) \
79 return; \
80 if (_free_cb) { \
81 void *p; \
82 if ((p = pa_tls_get(name##_tls.tls))) \
83 _free_cb(p); \
84 } \
85 pa_tls_free(name##_tls.tls); \
86 } \
87 static inline void* name##_tls_get(void) { \
88 return pa_tls_get(name##_tls_obj()); \
89 } \
90 static inline void* name##_tls_set(void *p) { \
91 return pa_tls_set(name##_tls_obj(), p); \
92 } \
93 struct __stupid_useless_struct_to_allow_trailing_semicolon
94
95 #if defined(SUPPORT_TLS___THREAD) && !defined(OS_IS_WIN32)
96 /* An optimized version of the above that requires no dynamic
97 * allocation if the compiler supports __thread */
98 #define PA_STATIC_TLS_DECLARE_NO_FREE(name) \
99 static __thread void *name##_tls = NULL; \
100 static inline void* name##_tls_get(void) { \
101 return name##_tls; \
102 } \
103 static inline void* name##_tls_set(void *p) { \
104 void *r = name##_tls; \
105 name##_tls = p; \
106 return r; \
107 } \
108 struct __stupid_useless_struct_to_allow_trailing_semicolon
109 #else
110 #define PA_STATIC_TLS_DECLARE_NO_FREE(name) PA_STATIC_TLS_DECLARE(name, NULL)
111 #endif
112
113 #define PA_STATIC_TLS_GET(name) (name##_tls_get())
114 #define PA_STATIC_TLS_SET(name, p) (name##_tls_set(p))
115
116 #endif