0009-Replace-thread-local-with-pthread-TSD.patch 6.42 KB
Newer Older
1
From 3cd38bd0a11400577995f94b10a94ac0d601ddd0 Mon Sep 17 00:00:00 2001
2 3
From: Thomas Guillem <thomas@gllm.fr>
Date: Mon, 17 Jul 2017 17:03:24 +0200
4
Subject: [PATCH 09/21] Replace thread local with pthread TSD
5 6

---
7 8
 src/misc/interrupt.c | 59 ++++++++++++++++++++++++++++++++++++++++++++
 src/misc/variables.c | 19 ++++++++++++++
9
 2 files changed, 78 insertions(+)
10 11

diff --git a/src/misc/interrupt.c b/src/misc/interrupt.c
12
index 7004ed2c3a..65607cdcd3 100644
13 14
--- a/src/misc/interrupt.c
+++ b/src/misc/interrupt.c
15
@@ -43,13 +43,26 @@
16 17 18
 #include "interrupt.h"
 #include "libvlc.h"
 
19 20 21
+#ifdef I_CAN_HAZ_TSD
 static thread_local vlc_interrupt_t *vlc_interrupt_var;
+#else
22 23 24 25 26 27 28
+#include <pthread.h>
+static pthread_key_t vlc_interrupt_var_key;
+static pthread_once_t vlc_interrupt_var_key_once = PTHREAD_ONCE_INIT;
+static void vlc_interrupt_var_key_create()
+{
+    pthread_key_create(&vlc_interrupt_var_key, NULL);
+}
29
+#endif
30 31 32 33 34 35
 
 /**
  * Initializes an interruption context.
  */
 void vlc_interrupt_init(vlc_interrupt_t *ctx)
 {
36
+#ifndef I_CAN_HAZ_TSD
37
+    pthread_once(&vlc_interrupt_var_key_once, vlc_interrupt_var_key_create);
38
+#endif
39 40 41
     vlc_mutex_init(&ctx->lock);
     ctx->interrupted = false;
     atomic_init(&ctx->killed, false);
42
@@ -98,9 +111,15 @@ void vlc_interrupt_raise(vlc_interrupt_t *ctx)
43 44 45
 
 vlc_interrupt_t *vlc_interrupt_set(vlc_interrupt_t *newctx)
 {
46 47 48 49 50
+#ifdef I_CAN_HAZ_TSD
     vlc_interrupt_t *oldctx = vlc_interrupt_var;
 
     vlc_interrupt_var = newctx;
+#else
51
+    vlc_interrupt_t *oldctx = pthread_getspecific(vlc_interrupt_var_key);
52
+
53
+    pthread_setspecific(vlc_interrupt_var_key, newctx);
54
+#endif
55 56 57
     return oldctx;
 }
 
58
@@ -115,7 +134,11 @@ static void vlc_interrupt_prepare(vlc_interrupt_t *ctx,
59 60 61
                                   void (*cb)(void *), void *data)
 {
     assert(ctx != NULL);
62 63 64
+#ifdef I_CAN_HAZ_TSD
     assert(ctx == vlc_interrupt_var);
+#else
65
+    assert(ctx == pthread_getspecific(vlc_interrupt_var_key));
66
+#endif
67 68 69
 
     vlc_mutex_lock(&ctx->lock);
     assert(ctx->callback == NULL);
70
@@ -143,7 +166,11 @@ static int vlc_interrupt_finish(vlc_interrupt_t *ctx)
71 72 73
     int ret = 0;
 
     assert(ctx != NULL);
74 75 76
+#ifdef I_CAN_HAZ_TSD
     assert(ctx == vlc_interrupt_var);
+#else
77
+    assert(ctx == pthread_getspecific(vlc_interrupt_var_key));
78
+#endif
79 80 81
 
     /* Wait for pending callbacks to prevent access by other threads. */
     vlc_mutex_lock(&ctx->lock);
82
@@ -159,14 +186,22 @@ static int vlc_interrupt_finish(vlc_interrupt_t *ctx)
83 84 85
 
 void vlc_interrupt_register(void (*cb)(void *), void *opaque)
 {
86 87 88
+#ifdef I_CAN_HAZ_TSD
     vlc_interrupt_t *ctx = vlc_interrupt_var;
+#else
89
+    vlc_interrupt_t *ctx = pthread_getspecific(vlc_interrupt_var_key);
90
+#endif
91 92 93 94 95 96
     if (ctx != NULL)
         vlc_interrupt_prepare(ctx, cb, opaque);
 }
 
 int vlc_interrupt_unregister(void)
 {
97 98 99
+#ifdef I_CAN_HAZ_TSD
     vlc_interrupt_t *ctx = vlc_interrupt_var;
+#else
100
+    vlc_interrupt_t *ctx = pthread_getspecific(vlc_interrupt_var_key);
101
+#endif
102 103 104
     return (ctx != NULL) ? vlc_interrupt_finish(ctx) : 0;
 }
 
105
@@ -185,7 +220,11 @@ void vlc_interrupt_kill(vlc_interrupt_t *ctx)
106 107 108
 
 bool vlc_killed(void)
 {
109 110 111
+#ifdef I_CAN_HAZ_TSD
     vlc_interrupt_t *ctx = vlc_interrupt_var;
+#else
112
+    vlc_interrupt_t *ctx = pthread_getspecific(vlc_interrupt_var_key);
113
+#endif
114 115 116
 
     return (ctx != NULL) && atomic_load(&ctx->killed);
 }
117
@@ -197,7 +236,11 @@ static void vlc_interrupt_sem(void *opaque)
118 119 120
 
 int vlc_sem_wait_i11e(vlc_sem_t *sem)
 {
121 122 123
+#ifdef I_CAN_HAZ_TSD
     vlc_interrupt_t *ctx = vlc_interrupt_var;
+#else
124
+    vlc_interrupt_t *ctx = pthread_getspecific(vlc_interrupt_var_key);
125
+#endif
126 127 128
     if (ctx == NULL)
         return vlc_sem_wait(sem), 0;
 
129
@@ -227,7 +270,11 @@ static void vlc_mwait_i11e_cleanup(void *opaque)
130 131 132
 
 int vlc_mwait_i11e(mtime_t deadline)
 {
133 134 135
+#ifdef I_CAN_HAZ_TSD
     vlc_interrupt_t *ctx = vlc_interrupt_var;
+#else
136
+    vlc_interrupt_t *ctx = pthread_getspecific(vlc_interrupt_var_key);
137
+#endif
138 139 140
     if (ctx == NULL)
         return mwait(deadline), 0;
 
141
@@ -262,7 +309,11 @@ void vlc_interrupt_forward_start(vlc_interrupt_t *to, void *data[2])
142 143 144
 {
     data[0] = data[1] = NULL;
 
145 146 147
+#ifdef I_CAN_HAZ_TSD
     vlc_interrupt_t *from = vlc_interrupt_var;
+#else
148
+    vlc_interrupt_t *from = pthread_getspecific(vlc_interrupt_var_key);
149
+#endif
150 151 152
     if (from == NULL)
         return;
 
153
@@ -371,7 +422,11 @@ static int vlc_poll_i11e_inner(struct pollfd *restrict fds, unsigned nfds,
154 155 156
 
 int vlc_poll_i11e(struct pollfd *fds, unsigned nfds, int timeout)
 {
157 158 159
+#ifdef I_CAN_HAZ_TSD
     vlc_interrupt_t *ctx = vlc_interrupt_var;
+#else
160
+    vlc_interrupt_t *ctx = pthread_getspecific(vlc_interrupt_var_key);
161
+#endif
162 163 164
     if (ctx == NULL)
         return poll(fds, nfds, timeout);
 
165
@@ -568,7 +623,11 @@ static void vlc_poll_i11e_cleanup(void *opaque)
166 167 168
 
 int vlc_poll_i11e(struct pollfd *fds, unsigned nfds, int timeout)
 {
169 170 171
+#ifdef I_CAN_HAZ_TSD
     vlc_interrupt_t *ctx = vlc_interrupt_var;
+#else
172
+    vlc_interrupt_t *ctx = pthread_getspecific(vlc_interrupt_var_key);
173
+#endif
174 175 176 177
     if (ctx == NULL)
         return vlc_poll(fds, nfds, timeout);
 
diff --git a/src/misc/variables.c b/src/misc/variables.c
Felix Paul Kühne's avatar
Felix Paul Kühne committed
178
index 810c8ffc81..3d2d2f1dc8 100644
179 180
--- a/src/misc/variables.c
+++ b/src/misc/variables.c
181
@@ -1378,7 +1378,17 @@ void DumpVariables(vlc_object_t *obj)
182 183 184
     vlc_mutex_unlock(&vlc_internals(obj)->var_lock);
 }
 
185 186 187
+#ifdef I_CAN_HAZ_TSD
 static thread_local void *twalk_ctx;
+#else
188 189 190 191 192 193 194
+#include <pthread.h>
+static pthread_key_t twalk_ctx_key;
+static pthread_once_t twalk_ctx_key_once = PTHREAD_ONCE_INIT;
+static void twalk_ctx_key_create()
+{
+    pthread_key_create(&twalk_ctx_key, NULL);
+}
195
+#endif
196 197 198
 
 static void TwalkGetNames(const void *data, const VISIT which, const int depth)
 {
199
@@ -1387,7 +1397,11 @@ static void TwalkGetNames(const void *data, const VISIT which, const int depth)
200 201 202
     (void) depth;
 
     const variable_t *var = *(const variable_t **)data;
203 204 205
+#ifdef I_CAN_HAZ_TSD
     DECL_ARRAY(char *) *names = twalk_ctx;
+#else
206
+    DECL_ARRAY(char *) *names = pthread_getspecific(twalk_ctx_key);
207
+#endif
208 209 210
     char *dup = strdup(var->psz_name);
     if (dup != NULL)
         ARRAY_APPEND(*names, dup);
211
@@ -1400,7 +1414,12 @@ char **var_GetAllNames(vlc_object_t *obj)
212 213 214
     DECL_ARRAY(char *) names;
     ARRAY_INIT(names);
 
215 216 217
+#ifdef I_CAN_HAZ_TSD
     twalk_ctx = &names;
+#else
218 219
+    pthread_once(&twalk_ctx_key_once, twalk_ctx_key_create);
+    pthread_setspecific(twalk_ctx_key, &names);
220
+#endif
221 222 223 224
     vlc_mutex_lock(&priv->var_lock);
     twalk(priv->var_root, TwalkGetNames);
     vlc_mutex_unlock(&priv->var_lock);
-- 
Felix Paul Kühne's avatar
Felix Paul Kühne committed
225
2.19.1
226