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