You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
5.8 KiB

  1. commit ece550d98e1c10017fb91ecfa0d19ae9d2dc45da
  2. Author: Willy Tarreau <w@1wt.eu>
  3. Date: Wed Aug 1 19:12:20 2018 +0200
  4. MINOR: threads: add more consistency between certain variables in no-thread case
  5. When threads are disabled, some variables such as tid and tid_bit are
  6. still checked everywhere, the MAX_THREADS_MASK macro is ~0UL while
  7. MAX_THREADS is 1, and the all_threads_mask variable is replaced with a
  8. macro forced to zero. The compiler cannot optimize away all this code
  9. involving checks on tid and tid_bit, and we end up in special cases
  10. where all_threads_mask has to be specifically tested for being zero or
  11. not. It is not even certain the code paths are always equivalent when
  12. testing without threads and with nbthread 1.
  13. Let's change this to make sure we always present a single thread when
  14. threads are disabled, and have the relevant values declared as constants
  15. so that the compiler can optimize all the tests away. Now we have
  16. MAX_THREADS_MASK set to 1, all_threads_mask set to 1, tid set to zero
  17. and tid_bit set to 1. Doing just this has removed 4 kB of code in the
  18. no-thread case.
  19. A few checks for all_threads_mask==0 have been removed since it never
  20. happens anymore.
  21. (cherry picked from commit 0c026f49e7348bce5b3c74be896ae208ae6e26a4)
  22. [wt: the thread code feels safer with this, especially with the small updates
  23. needed for the rdv point; missed one occurrence fixed by next patch]
  24. Signed-off-by: Willy Tarreau <w@1wt.eu>
  25. diff --git a/include/common/hathreads.h b/include/common/hathreads.h
  26. index 4e72848e..7eb5d127 100644
  27. --- a/include/common/hathreads.h
  28. +++ b/include/common/hathreads.h
  29. @@ -24,10 +24,6 @@
  30. #include <common/config.h>
  31. -#define MAX_THREADS_MASK ((unsigned long)-1)
  32. -extern THREAD_LOCAL unsigned int tid; /* The thread id */
  33. -extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the thread id */
  34. -
  35. /* Note about all_threads_mask :
  36. * - with threads support disabled, this symbol is defined as zero (0UL).
  37. * - with threads enabled, this variable is never zero, it contains the mask
  38. @@ -37,7 +33,14 @@ extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the threa
  39. #ifndef USE_THREAD
  40. #define MAX_THREADS 1
  41. -#define all_threads_mask 0UL
  42. +#define MAX_THREADS_MASK 1
  43. +
  44. +/* Only way found to replace variables with constants that are optimized away
  45. + * at build time.
  46. + */
  47. +enum { all_threads_mask = 1UL };
  48. +enum { tid_bit = 1UL };
  49. +enum { tid = 0 };
  50. #define __decl_hathreads(decl)
  51. @@ -98,6 +101,9 @@ extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the threa
  52. #define ha_sigmask(how, set, oldset) sigprocmask(how, set, oldset)
  53. +static inline void ha_set_tid(unsigned int tid)
  54. +{
  55. +}
  56. static inline void __ha_barrier_load(void)
  57. {
  58. @@ -120,6 +126,7 @@ static inline void __ha_barrier_full(void)
  59. #include <import/plock.h>
  60. #define MAX_THREADS LONGBITS
  61. +#define MAX_THREADS_MASK ((unsigned long)-1)
  62. #define __decl_hathreads(decl) decl
  63. @@ -223,10 +230,19 @@ void thread_exit_sync(void);
  64. int thread_no_sync(void);
  65. int thread_need_sync(void);
  66. +extern THREAD_LOCAL unsigned int tid; /* The thread id */
  67. +extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the thread id */
  68. extern volatile unsigned long all_threads_mask;
  69. #define ha_sigmask(how, set, oldset) pthread_sigmask(how, set, oldset)
  70. +/* sets the thread ID and the TID bit for the current thread */
  71. +static inline void ha_set_tid(unsigned int data)
  72. +{
  73. + tid = data;
  74. + tid_bit = (1UL << tid);
  75. +}
  76. +
  77. #if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
  78. diff --git a/src/cfgparse.c b/src/cfgparse.c
  79. index 24349a59..d1474d4b 100644
  80. --- a/src/cfgparse.c
  81. +++ b/src/cfgparse.c
  82. @@ -7652,11 +7652,11 @@ int check_config_validity()
  83. nbproc = my_ffsl(bind_conf->bind_proc);
  84. mask = bind_conf->bind_thread[nbproc - 1];
  85. - if (mask && !(mask & (all_threads_mask ? all_threads_mask : 1UL))) {
  86. + if (mask && !(mask & all_threads_mask)) {
  87. unsigned long new_mask = 0;
  88. while (mask) {
  89. - new_mask |= mask & (all_threads_mask ? all_threads_mask : 1UL);
  90. + new_mask |= mask & all_threads_mask;
  91. mask >>= global.nbthread;
  92. }
  93. diff --git a/src/haproxy.c b/src/haproxy.c
  94. index 9ba56623..e0186ff9 100644
  95. --- a/src/haproxy.c
  96. +++ b/src/haproxy.c
  97. @@ -2448,8 +2448,7 @@ static void *run_thread_poll_loop(void *data)
  98. struct per_thread_deinit_fct *ptdf;
  99. __decl_hathreads(static HA_SPINLOCK_T start_lock);
  100. - tid = *((unsigned int *)data);
  101. - tid_bit = (1UL << tid);
  102. + ha_set_tid(*((unsigned int *)data));
  103. tv_update_date(-1,-1);
  104. list_for_each_entry(ptif, &per_thread_init_list, list) {
  105. diff --git a/src/hathreads.c b/src/hathreads.c
  106. index 0d0a0509..238cbb80 100644
  107. --- a/src/hathreads.c
  108. +++ b/src/hathreads.c
  109. @@ -19,8 +19,6 @@
  110. #include <common/standard.h>
  111. #include <proto/fd.h>
  112. -THREAD_LOCAL unsigned int tid = 0;
  113. -THREAD_LOCAL unsigned long tid_bit = (1UL << 0);
  114. /* Dummy I/O handler used by the sync pipe.*/
  115. void thread_sync_io_handler(int fd)
  116. @@ -33,6 +31,9 @@ static HA_SPINLOCK_T sync_lock;
  117. static int threads_sync_pipe[2];
  118. static unsigned long threads_want_sync = 0;
  119. volatile unsigned long all_threads_mask = 1; // nbthread 1 assumed by default
  120. +THREAD_LOCAL unsigned int tid = 0;
  121. +THREAD_LOCAL unsigned long tid_bit = (1UL << 0);
  122. +
  123. #if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
  124. struct lock_stat lock_stats[LOCK_LABELS];
  125. @@ -130,7 +131,7 @@ void thread_enter_sync()
  126. {
  127. static volatile unsigned long barrier = 0;
  128. - if (!all_threads_mask)
  129. + if (!(all_threads_mask & (all_threads_mask - 1)))
  130. return;
  131. thread_sync_barrier(&barrier);
  132. @@ -146,7 +147,7 @@ void thread_exit_sync()
  133. {
  134. static volatile unsigned long barrier = 0;
  135. - if (!all_threads_mask)
  136. + if (!(all_threads_mask & (all_threads_mask - 1)))
  137. return;
  138. if (threads_want_sync & tid_bit)