1 #ifndef foopulsethreadhfoo
2 #define foopulsethreadhfoo
7 This file is part of PulseAudio.
9 Copyright 2006 Lennart Poettering
10 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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
28 #include <pulse/def.h>
29 #include <pulsecore/once.h>
32 #error "Please include config.h before including this file!"
35 typedef struct pa_thread pa_thread
;
37 typedef void (*pa_thread_func_t
) (void *userdata
);
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);
46 void* pa_thread_get_data(pa_thread
*t
);
47 void pa_thread_set_data(pa_thread
*t
, void *userdata
);
49 typedef struct pa_tls pa_tls
;
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
);
56 #define PA_STATIC_TLS_DECLARE(name, free_cb) \
61 .once = PA_ONCE_INIT, \
64 static void name##_tls_init(void) { \
65 name##_tls.tls = pa_tls_new(free_cb); \
67 static inline pa_tls* name##_tls_obj(void) { \
68 pa_run_once(&name##_tls.once, name##_tls_init); \
69 return name##_tls.tls; \
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) \
78 if ((p = pa_tls_get(name##_tls.tls))) \
81 pa_tls_free(name##_tls.tls); \
83 static inline void* name##_tls_get(void) { \
84 return pa_tls_get(name##_tls_obj()); \
86 static inline void* name##_tls_set(void *p) { \
87 return pa_tls_set(name##_tls_obj(), p); \
89 struct __stupid_useless_struct_to_allow_trailing_semicolon
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) { \
99 static inline void* name##_tls_set(void *p) { \
100 void *r = name##_tls; \
104 struct __stupid_useless_struct_to_allow_trailing_semicolon
106 #define PA_STATIC_TLS_DECLARE_NO_FREE(name) PA_STATIC_TLS_DECLARE(name, NULL)
109 #define PA_STATIC_TLS_GET(name) (name##_tls_get())
110 #define PA_STATIC_TLS_SET(name, p) (name##_tls_set(p))