]> code.delx.au - pulseaudio/commitdiff
add macro for creating static TLS objects
authorLennart Poettering <lennart@poettering.net>
Wed, 22 Aug 2007 00:19:33 +0000 (00:19 +0000)
committerLennart Poettering <lennart@poettering.net>
Wed, 22 Aug 2007 00:19:33 +0000 (00:19 +0000)
git-svn-id: file:///home/lennart/svn/public/pulseaudio/branches/lennart@1683 fefdeb5f-60dc-0310-8127-8f9354f1896f

src/pulsecore/thread.h

index ca1fe4da84f4373ee82add93f94eba830c45093e..44b80a06520dcc1b2bff28c716e99cdf50ba771e 100644 (file)
@@ -48,4 +48,30 @@ void pa_tls_free(pa_tls *t);
 void * pa_tls_get(pa_tls *t);
 void *pa_tls_set(pa_tls *t, void *userdata);
 
+/* To make use of the static TLS stuff you have to include once.h, as well */
+
+#define PA_STATIC_TLS_DECLARE(name, free_cb)                            \
+    static struct {                                                     \
+        pa_once once;                                                   \
+        pa_tls *tls;                                                    \
+    } name##_tls = {                                                    \
+        .once = PA_ONCE_INIT,                                           \
+        .tls = NULL                                                     \
+    };                                                                  \
+    static void name##_tls_init(void) {                                 \
+        name##_tls.tls = pa_tls_new(free_cb);                           \
+    }                                                                   \
+    static inline pa_tls* name##_tls_get(void) {                        \
+        pa_run_once(&name##_tls.once, name##_tls_init);                 \
+        return name##_tls.tls;                                          \
+    }                                                                   \
+    static void name##_tls_destructor(void) PA_GCC_DESTRUCTOR;          \
+    static void name##_tls_destructor(void) {                           \
+        if (name##_tls.tls)                                             \
+            pa_tls_free(name##_tls.tls);                                \
+    }                                                                   \
+    struct __stupid_useless_struct_to_allow_trailing_semicolon
+
+#define PA_STATIC_TLS_GET(name) (name##_tls_get())
+
 #endif