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, see <http://www.gnu.org/licenses/>.
22***/
23
24#include <pulse/def.h>
25#include <pulse/gccmacro.h>
26
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
34typedef struct pa_thread pa_thread;
35
36typedef void (*pa_thread_func_t) (void *userdata);
37
38pa_thread* pa_thread_new(const char *name, pa_thread_func_t thread_func, void *userdata);
39void pa_thread_free(pa_thread *t);
40void pa_thread_free_nojoin(pa_thread *t);
41int pa_thread_join(pa_thread *t);
42int pa_thread_is_running(pa_thread *t);
43pa_thread *pa_thread_self(void);
44void pa_thread_yield(void);
45
46void* pa_thread_get_data(pa_thread *t);
47void pa_thread_set_data(pa_thread *t, void *userdata);
48
49const char *pa_thread_get_name(pa_thread *t);
50void pa_thread_set_name(pa_thread *t, const char *name);
51
52typedef struct pa_tls pa_tls;
53
54pa_tls* pa_tls_new(pa_free_cb_t free_cb);
55void pa_tls_free(pa_tls *t);
56void * pa_tls_get(pa_tls *t);
57void *pa_tls_set(pa_tls *t, void *userdata);
58
59#define PA_STATIC_TLS_DECLARE(name, free_cb)                            \
60    static struct {                                                     \
61        pa_once once;                                                   \
62        pa_tls *volatile tls;                                           \
63    } name##_tls = {                                                    \
64        .once = PA_ONCE_INIT,                                           \
65        .tls = NULL                                                     \
66    };                                                                  \
67    static void name##_tls_init(void) {                                 \
68        name##_tls.tls = pa_tls_new(free_cb);                           \
69    }                                                                   \
70    static inline pa_tls* name##_tls_obj(void) {                        \
71        pa_run_once(&name##_tls.once, name##_tls_init);                 \
72        return name##_tls.tls;                                          \
73    }                                                                   \
74    static void name##_tls_destructor(void) PA_GCC_DESTRUCTOR;          \
75    static void name##_tls_destructor(void) {                           \
76        static void (*_free_cb)(void*) = free_cb;                       \
77        if (!pa_in_valgrind())                                          \
78            return;                                                     \
79        if (!name##_tls.tls)                                            \
80            return;                                                     \
81        if (_free_cb) {                                                 \
82            void *p;                                                    \
83            if ((p = pa_tls_get(name##_tls.tls)))                       \
84                _free_cb(p);                                            \
85        }                                                               \
86        pa_tls_free(name##_tls.tls);                                    \
87    }                                                                   \
88    static inline void* name##_tls_get(void) {                          \
89        return pa_tls_get(name##_tls_obj());                            \
90    }                                                                   \
91    static inline void* name##_tls_set(void *p) {                       \
92        return pa_tls_set(name##_tls_obj(), p);                         \
93    }                                                                   \
94    struct __stupid_useless_struct_to_allow_trailing_semicolon
95
96#if defined(SUPPORT_TLS___THREAD) && !defined(OS_IS_WIN32)
97/* An optimized version of the above that requires no dynamic
98 * allocation if the compiler supports __thread */
99#define PA_STATIC_TLS_DECLARE_NO_FREE(name)                             \
100    static __thread void *name##_tls = NULL;                            \
101    static inline void* name##_tls_get(void) {                          \
102        return name##_tls;                                              \
103    }                                                                   \
104    static inline void* name##_tls_set(void *p) {                       \
105        void *r = name##_tls;                                           \
106        name##_tls = p;                                                 \
107        return r;                                                       \
108    }                                                                   \
109    struct __stupid_useless_struct_to_allow_trailing_semicolon
110#else
111#define PA_STATIC_TLS_DECLARE_NO_FREE(name) PA_STATIC_TLS_DECLARE(name, NULL)
112#endif
113
114#define PA_STATIC_TLS_GET(name) (name##_tls_get())
115#define PA_STATIC_TLS_SET(name, p) (name##_tls_set(p))
116
117#endif
118