0010-Replace-thread-local-with-pthread-TSD.patch 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. From c7ef2e5b008b7a91559a202f3a27c570aa0e7fa5 Mon Sep 17 00:00:00 2001
  2. From: Thomas Guillem <thomas@gllm.fr>
  3. Date: Mon, 17 Jul 2017 17:03:24 +0200
  4. Subject: [PATCH 10/17] Replace thread local with pthread TSD
  5. ---
  6. src/misc/interrupt.c | 59 ++++++++++++++++++++++++++++++++++++++++++++
  7. src/misc/sort.c | 4 +++
  8. src/misc/variables.c | 19 ++++++++++++++
  9. 3 files changed, 82 insertions(+)
  10. diff --git a/src/misc/interrupt.c b/src/misc/interrupt.c
  11. index d2a04a72b3..75edb2d7c1 100644
  12. --- a/src/misc/interrupt.c
  13. +++ b/src/misc/interrupt.c
  14. @@ -43,13 +43,26 @@
  15. #include "interrupt.h"
  16. #include "libvlc.h"
  17. +#ifdef I_CAN_HAZ_TSD
  18. static thread_local vlc_interrupt_t *vlc_interrupt_var;
  19. +#else
  20. +#include <pthread.h>
  21. +static pthread_key_t vlc_interrupt_var_key;
  22. +static pthread_once_t vlc_interrupt_var_key_once = PTHREAD_ONCE_INIT;
  23. +static void vlc_interrupt_var_key_create()
  24. +{
  25. + pthread_key_create(&vlc_interrupt_var_key, NULL);
  26. +}
  27. +#endif
  28. /**
  29. * Initializes an interruption context.
  30. */
  31. void vlc_interrupt_init(vlc_interrupt_t *ctx)
  32. {
  33. +#ifndef I_CAN_HAZ_TSD
  34. + pthread_once(&vlc_interrupt_var_key_once, vlc_interrupt_var_key_create);
  35. +#endif
  36. vlc_mutex_init(&ctx->lock);
  37. ctx->interrupted = false;
  38. atomic_init(&ctx->killed, false);
  39. @@ -98,9 +111,15 @@ void vlc_interrupt_raise(vlc_interrupt_t *ctx)
  40. vlc_interrupt_t *vlc_interrupt_set(vlc_interrupt_t *newctx)
  41. {
  42. +#ifdef I_CAN_HAZ_TSD
  43. vlc_interrupt_t *oldctx = vlc_interrupt_var;
  44. vlc_interrupt_var = newctx;
  45. +#else
  46. + vlc_interrupt_t *oldctx = pthread_getspecific(vlc_interrupt_var_key);
  47. +
  48. + pthread_setspecific(vlc_interrupt_var_key, newctx);
  49. +#endif
  50. return oldctx;
  51. }
  52. @@ -115,7 +134,11 @@ static void vlc_interrupt_prepare(vlc_interrupt_t *ctx,
  53. void (*cb)(void *), void *data)
  54. {
  55. assert(ctx != NULL);
  56. +#ifdef I_CAN_HAZ_TSD
  57. assert(ctx == vlc_interrupt_var);
  58. +#else
  59. + assert(ctx == pthread_getspecific(vlc_interrupt_var_key));
  60. +#endif
  61. vlc_mutex_lock(&ctx->lock);
  62. assert(ctx->callback == NULL);
  63. @@ -143,7 +166,11 @@ static int vlc_interrupt_finish(vlc_interrupt_t *ctx)
  64. int ret = 0;
  65. assert(ctx != NULL);
  66. +#ifdef I_CAN_HAZ_TSD
  67. assert(ctx == vlc_interrupt_var);
  68. +#else
  69. + assert(ctx == pthread_getspecific(vlc_interrupt_var_key));
  70. +#endif
  71. /* Wait for pending callbacks to prevent access by other threads. */
  72. vlc_mutex_lock(&ctx->lock);
  73. @@ -159,14 +186,22 @@ static int vlc_interrupt_finish(vlc_interrupt_t *ctx)
  74. void vlc_interrupt_register(void (*cb)(void *), void *opaque)
  75. {
  76. +#ifdef I_CAN_HAZ_TSD
  77. vlc_interrupt_t *ctx = vlc_interrupt_var;
  78. +#else
  79. + vlc_interrupt_t *ctx = pthread_getspecific(vlc_interrupt_var_key);
  80. +#endif
  81. if (ctx != NULL)
  82. vlc_interrupt_prepare(ctx, cb, opaque);
  83. }
  84. int vlc_interrupt_unregister(void)
  85. {
  86. +#ifdef I_CAN_HAZ_TSD
  87. vlc_interrupt_t *ctx = vlc_interrupt_var;
  88. +#else
  89. + vlc_interrupt_t *ctx = pthread_getspecific(vlc_interrupt_var_key);
  90. +#endif
  91. return (ctx != NULL) ? vlc_interrupt_finish(ctx) : 0;
  92. }
  93. @@ -185,7 +220,11 @@ void vlc_interrupt_kill(vlc_interrupt_t *ctx)
  94. bool vlc_killed(void)
  95. {
  96. +#ifdef I_CAN_HAZ_TSD
  97. vlc_interrupt_t *ctx = vlc_interrupt_var;
  98. +#else
  99. + vlc_interrupt_t *ctx = pthread_getspecific(vlc_interrupt_var_key);
  100. +#endif
  101. return (ctx != NULL) && atomic_load(&ctx->killed);
  102. }
  103. @@ -197,7 +236,11 @@ static void vlc_interrupt_sem(void *opaque)
  104. int vlc_sem_wait_i11e(vlc_sem_t *sem)
  105. {
  106. +#ifdef I_CAN_HAZ_TSD
  107. vlc_interrupt_t *ctx = vlc_interrupt_var;
  108. +#else
  109. + vlc_interrupt_t *ctx = pthread_getspecific(vlc_interrupt_var_key);
  110. +#endif
  111. if (ctx == NULL)
  112. return vlc_sem_wait(sem), 0;
  113. @@ -227,7 +270,11 @@ static void vlc_mwait_i11e_cleanup(void *opaque)
  114. int vlc_mwait_i11e(vlc_tick_t deadline)
  115. {
  116. +#ifdef I_CAN_HAZ_TSD
  117. vlc_interrupt_t *ctx = vlc_interrupt_var;
  118. +#else
  119. + vlc_interrupt_t *ctx = pthread_getspecific(vlc_interrupt_var_key);
  120. +#endif
  121. if (ctx == NULL)
  122. return vlc_tick_wait(deadline), 0;
  123. @@ -262,7 +309,11 @@ void vlc_interrupt_forward_start(vlc_interrupt_t *to, void *data[2])
  124. {
  125. data[0] = data[1] = NULL;
  126. +#ifdef I_CAN_HAZ_TSD
  127. vlc_interrupt_t *from = vlc_interrupt_var;
  128. +#else
  129. + vlc_interrupt_t *from = pthread_getspecific(vlc_interrupt_var_key);
  130. +#endif
  131. if (from == NULL)
  132. return;
  133. @@ -371,7 +422,11 @@ static int vlc_poll_i11e_inner(struct pollfd *restrict fds, unsigned nfds,
  134. int vlc_poll_i11e(struct pollfd *fds, unsigned nfds, int timeout)
  135. {
  136. +#ifdef I_CAN_HAZ_TSD
  137. vlc_interrupt_t *ctx = vlc_interrupt_var;
  138. +#else
  139. + vlc_interrupt_t *ctx = pthread_getspecific(vlc_interrupt_var_key);
  140. +#endif
  141. if (ctx == NULL)
  142. return poll(fds, nfds, timeout);
  143. @@ -570,7 +625,11 @@ static void vlc_poll_i11e_cleanup(void *opaque)
  144. int vlc_poll_i11e(struct pollfd *fds, unsigned nfds, int timeout)
  145. {
  146. +#ifdef I_CAN_HAZ_TSD
  147. vlc_interrupt_t *ctx = vlc_interrupt_var;
  148. +#else
  149. + vlc_interrupt_t *ctx = pthread_getspecific(vlc_interrupt_var_key);
  150. +#endif
  151. if (ctx == NULL)
  152. return vlc_poll(fds, nfds, timeout);
  153. diff --git a/src/misc/sort.c b/src/misc/sort.c
  154. index fc62eb7fff..a96fb0104c 100644
  155. --- a/src/misc/sort.c
  156. +++ b/src/misc/sort.c
  157. @@ -26,7 +26,11 @@
  158. #include <vlc_common.h>
  159. #include <vlc_sort.h>
  160. +#ifdef I_CAN_HAZ_TSD
  161. static thread_local struct
  162. +#else
  163. +static struct
  164. +#endif
  165. {
  166. int (*compar)(const void *, const void *, void *);
  167. void *arg;
  168. diff --git a/src/misc/variables.c b/src/misc/variables.c
  169. index ed284cf4f4..15f6dfe096 100644
  170. --- a/src/misc/variables.c
  171. +++ b/src/misc/variables.c
  172. @@ -1135,7 +1135,17 @@ error:
  173. return VLC_EGENERIC;
  174. }
  175. +#ifdef I_CAN_HAZ_TSD
  176. static thread_local void *twalk_ctx;
  177. +#else
  178. +#include <pthread.h>
  179. +static pthread_key_t twalk_ctx_key;
  180. +static pthread_once_t twalk_ctx_key_once = PTHREAD_ONCE_INIT;
  181. +static void twalk_ctx_key_create()
  182. +{
  183. + pthread_key_create(&twalk_ctx_key, NULL);
  184. +}
  185. +#endif
  186. static void TwalkGetNames(const void *data, const VISIT which, const int depth)
  187. {
  188. @@ -1144,7 +1154,11 @@ static void TwalkGetNames(const void *data, const VISIT which, const int depth)
  189. (void) depth;
  190. const variable_t *var = *(const variable_t **)data;
  191. +#ifdef I_CAN_HAZ_TSD
  192. DECL_ARRAY(char *) *names = twalk_ctx;
  193. +#else
  194. + DECL_ARRAY(char *) *names = pthread_getspecific(twalk_ctx_key);
  195. +#endif
  196. char *dup = strdup(var->psz_name);
  197. if (dup != NULL)
  198. ARRAY_APPEND(*names, dup);
  199. @@ -1157,7 +1171,12 @@ char **var_GetAllNames(vlc_object_t *obj)
  200. DECL_ARRAY(char *) names;
  201. ARRAY_INIT(names);
  202. +#ifdef I_CAN_HAZ_TSD
  203. twalk_ctx = &names;
  204. +#else
  205. + pthread_once(&twalk_ctx_key_once, twalk_ctx_key_create);
  206. + pthread_setspecific(twalk_ctx_key, &names);
  207. +#endif
  208. vlc_mutex_lock(&priv->var_lock);
  209. twalk(priv->var_root, TwalkGetNames);
  210. vlc_mutex_unlock(&priv->var_lock);
  211. --
  212. 2.21.1 (Apple Git-122.3)