0016-Replace-thread-local-with-pthread-TSD.patch 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. From c7f8367e0e5fef23db8f9a0498cbc5834fc57af2 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 16/16] Replace thread local with pthread TSD
  5. ---
  6. src/misc/interrupt.c | 34 ++++++++++++++++++++--------------
  7. src/misc/variables.c | 13 ++++++++++---
  8. 2 files changed, 30 insertions(+), 17 deletions(-)
  9. diff --git a/src/misc/interrupt.c b/src/misc/interrupt.c
  10. index 3fafaa4183..7fdc7b8b96 100644
  11. --- a/src/misc/interrupt.c
  12. +++ b/src/misc/interrupt.c
  13. @@ -43,13 +43,20 @@
  14. #include "interrupt.h"
  15. #include "libvlc.h"
  16. -static thread_local vlc_interrupt_t *vlc_interrupt_var;
  17. +#include <pthread.h>
  18. +static pthread_key_t vlc_interrupt_var_key;
  19. +static pthread_once_t vlc_interrupt_var_key_once = PTHREAD_ONCE_INIT;
  20. +static void vlc_interrupt_var_key_create()
  21. +{
  22. + pthread_key_create(&vlc_interrupt_var_key, NULL);
  23. +}
  24. /**
  25. * Initializes an interruption context.
  26. */
  27. void vlc_interrupt_init(vlc_interrupt_t *ctx)
  28. {
  29. + pthread_once(&vlc_interrupt_var_key_once, vlc_interrupt_var_key_create);
  30. vlc_mutex_init(&ctx->lock);
  31. ctx->interrupted = false;
  32. atomic_init(&ctx->killed, false);
  33. @@ -98,9 +105,8 @@ void vlc_interrupt_raise(vlc_interrupt_t *ctx)
  34. vlc_interrupt_t *vlc_interrupt_set(vlc_interrupt_t *newctx)
  35. {
  36. - vlc_interrupt_t *oldctx = vlc_interrupt_var;
  37. -
  38. - vlc_interrupt_var = newctx;
  39. + vlc_interrupt_t *oldctx = pthread_getspecific(vlc_interrupt_var_key);
  40. + pthread_setspecific(vlc_interrupt_var_key, newctx);
  41. return oldctx;
  42. }
  43. @@ -115,7 +121,7 @@ static void vlc_interrupt_prepare(vlc_interrupt_t *ctx,
  44. void (*cb)(void *), void *data)
  45. {
  46. assert(ctx != NULL);
  47. - assert(ctx == vlc_interrupt_var);
  48. + assert(ctx == pthread_getspecific(vlc_interrupt_var_key));
  49. vlc_mutex_lock(&ctx->lock);
  50. assert(ctx->callback == NULL);
  51. @@ -143,7 +149,7 @@ static int vlc_interrupt_finish(vlc_interrupt_t *ctx)
  52. int ret = 0;
  53. assert(ctx != NULL);
  54. - assert(ctx == vlc_interrupt_var);
  55. + assert(ctx == pthread_getspecific(vlc_interrupt_var_key));
  56. /* Wait for pending callbacks to prevent access by other threads. */
  57. vlc_mutex_lock(&ctx->lock);
  58. @@ -159,14 +165,14 @@ static int vlc_interrupt_finish(vlc_interrupt_t *ctx)
  59. void vlc_interrupt_register(void (*cb)(void *), void *opaque)
  60. {
  61. - vlc_interrupt_t *ctx = vlc_interrupt_var;
  62. + vlc_interrupt_t *ctx = pthread_getspecific(vlc_interrupt_var_key);
  63. if (ctx != NULL)
  64. vlc_interrupt_prepare(ctx, cb, opaque);
  65. }
  66. int vlc_interrupt_unregister(void)
  67. {
  68. - vlc_interrupt_t *ctx = vlc_interrupt_var;
  69. + vlc_interrupt_t *ctx = pthread_getspecific(vlc_interrupt_var_key);
  70. return (ctx != NULL) ? vlc_interrupt_finish(ctx) : 0;
  71. }
  72. @@ -185,7 +191,7 @@ void vlc_interrupt_kill(vlc_interrupt_t *ctx)
  73. bool vlc_killed(void)
  74. {
  75. - vlc_interrupt_t *ctx = vlc_interrupt_var;
  76. + vlc_interrupt_t *ctx = pthread_getspecific(vlc_interrupt_var_key);
  77. return (ctx != NULL) && atomic_load(&ctx->killed);
  78. }
  79. @@ -197,7 +203,7 @@ static void vlc_interrupt_sem(void *opaque)
  80. int vlc_sem_wait_i11e(vlc_sem_t *sem)
  81. {
  82. - vlc_interrupt_t *ctx = vlc_interrupt_var;
  83. + vlc_interrupt_t *ctx = pthread_getspecific(vlc_interrupt_var_key);
  84. if (ctx == NULL)
  85. return vlc_sem_wait(sem), 0;
  86. @@ -227,7 +233,7 @@ static void vlc_mwait_i11e_cleanup(void *opaque)
  87. int vlc_mwait_i11e(mtime_t deadline)
  88. {
  89. - vlc_interrupt_t *ctx = vlc_interrupt_var;
  90. + vlc_interrupt_t *ctx = pthread_getspecific(vlc_interrupt_var_key);
  91. if (ctx == NULL)
  92. return mwait(deadline), 0;
  93. @@ -262,7 +268,7 @@ void vlc_interrupt_forward_start(vlc_interrupt_t *to, void *data[2])
  94. {
  95. data[0] = data[1] = NULL;
  96. - vlc_interrupt_t *from = vlc_interrupt_var;
  97. + vlc_interrupt_t *from = pthread_getspecific(vlc_interrupt_var_key);
  98. if (from == NULL)
  99. return;
  100. @@ -371,7 +377,7 @@ static int vlc_poll_i11e_inner(struct pollfd *restrict fds, unsigned nfds,
  101. int vlc_poll_i11e(struct pollfd *fds, unsigned nfds, int timeout)
  102. {
  103. - vlc_interrupt_t *ctx = vlc_interrupt_var;
  104. + vlc_interrupt_t *ctx = pthread_getspecific(vlc_interrupt_var_key);
  105. if (ctx == NULL)
  106. return poll(fds, nfds, timeout);
  107. @@ -568,7 +574,7 @@ static void vlc_poll_i11e_cleanup(void *opaque)
  108. int vlc_poll_i11e(struct pollfd *fds, unsigned nfds, int timeout)
  109. {
  110. - vlc_interrupt_t *ctx = vlc_interrupt_var;
  111. + vlc_interrupt_t *ctx = pthread_getspecific(vlc_interrupt_var_key);
  112. if (ctx == NULL)
  113. return vlc_poll(fds, nfds, timeout);
  114. diff --git a/src/misc/variables.c b/src/misc/variables.c
  115. index 87a9c27478..2b05526162 100644
  116. --- a/src/misc/variables.c
  117. +++ b/src/misc/variables.c
  118. @@ -1378,7 +1378,13 @@ void DumpVariables(vlc_object_t *obj)
  119. vlc_mutex_unlock(&vlc_internals(obj)->var_lock);
  120. }
  121. -static thread_local void *twalk_ctx;
  122. +#include <pthread.h>
  123. +static pthread_key_t twalk_ctx_key;
  124. +static pthread_once_t twalk_ctx_key_once = PTHREAD_ONCE_INIT;
  125. +static void twalk_ctx_key_create()
  126. +{
  127. + pthread_key_create(&twalk_ctx_key, NULL);
  128. +}
  129. static void TwalkGetNames(const void *data, const VISIT which, const int depth)
  130. {
  131. @@ -1387,7 +1393,7 @@ static void TwalkGetNames(const void *data, const VISIT which, const int depth)
  132. (void) depth;
  133. const variable_t *var = *(const variable_t **)data;
  134. - DECL_ARRAY(char *) *names = twalk_ctx;
  135. + DECL_ARRAY(char *) *names = pthread_getspecific(twalk_ctx_key);
  136. char *dup = strdup(var->psz_name);
  137. if (dup != NULL)
  138. ARRAY_APPEND(*names, dup);
  139. @@ -1400,7 +1406,8 @@ char **var_GetAllNames(vlc_object_t *obj)
  140. DECL_ARRAY(char *) names;
  141. ARRAY_INIT(names);
  142. - twalk_ctx = &names;
  143. + pthread_once(&twalk_ctx_key_once, twalk_ctx_key_create);
  144. + pthread_setspecific(twalk_ctx_key, &names);
  145. vlc_mutex_lock(&priv->var_lock);
  146. twalk(priv->var_root, TwalkGetNames);
  147. vlc_mutex_unlock(&priv->var_lock);
  148. --
  149. 2.11.0