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