From 9cea2c4ae3442f24fe883d6e5854fef749788b74 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen@gmail.com>
Date: Fri, 13 Sep 2019 05:03:12 +0100
Subject: [PATCH 2/3] BUILD/MEDIUM: threads: refactoring thread_info struct

On Darwin, thread_info is a genuine existing pointer type thus
need to rename and make the Haproxy's origin clearer.
---
 include/common/hathreads.h | 18 +++++++++---------
 include/common/time.h      |  2 +-
 src/debug.c                |  6 +++---
 src/haproxy.c              |  8 ++++----
 src/hathreads.c            |  8 ++++----
 src/wdt.c                  | 12 ++++++------
 6 files changed, 27 insertions(+), 27 deletions(-)

diff --git a/include/common/hathreads.h b/include/common/hathreads.h
index b05215bd..75809f5c 100644
--- a/include/common/hathreads.h
+++ b/include/common/hathreads.h
@@ -38,7 +38,7 @@
  *      only one thread is enabled, it equals 1.
  */
 
-/* thread info flags, for thread_info[].flags */
+/* thread info flags, for ha_thread_info[].flags */
 #define TI_FL_STUCK             0x00000001
 
 
@@ -57,7 +57,7 @@ enum { threads_sync_mask = 0 };
 enum { tid_bit = 1UL };
 enum { tid = 0 };
 
-extern struct thread_info {
+extern struct ha_thread_info {
 	clockid_t clock_id;
 	timer_t wd_timer;          /* valid timer or TIMER_INVALID if not set */
 	uint64_t prev_cpu_time;    /* previous per thread CPU time */
@@ -67,9 +67,9 @@ extern struct thread_info {
 	/* pad to cache line (64B) */
 	char __pad[0];            /* unused except to check remaining room */
 	char __end[0] __attribute__((aligned(64)));
-} thread_info[MAX_THREADS];
+} ha_thread_info[MAX_THREADS];
 
-extern THREAD_LOCAL struct thread_info *ti; /* thread_info for the current thread */
+extern THREAD_LOCAL struct ha_thread_info *ti; /* thread_info for the current thread */
 
 #define __decl_hathreads(decl)
 #define __decl_spinlock(lock)
@@ -167,7 +167,7 @@ extern THREAD_LOCAL struct thread_info *ti; /* thread_info for the current threa
 
 static inline void ha_set_tid(unsigned int tid)
 {
-	ti = &thread_info[tid];
+	ti = &ha_thread_info[tid];
 }
 
 static inline void ha_thread_relax(void)
@@ -426,7 +426,7 @@ void thread_sync_release();
 void ha_tkill(unsigned int thr, int sig);
 void ha_tkillall(int sig);
 
-extern struct thread_info {
+extern struct ha_thread_info {
 	pthread_t pthread;
 	clockid_t clock_id;
 	timer_t wd_timer;          /* valid timer or TIMER_INVALID if not set */
@@ -437,11 +437,11 @@ extern struct thread_info {
 	/* pad to cache line (64B) */
 	char __pad[0];            /* unused except to check remaining room */
 	char __end[0] __attribute__((aligned(64)));
-} thread_info[MAX_THREADS];
+} ha_thread_info[MAX_THREADS];
 
 extern THREAD_LOCAL unsigned int tid;     /* The thread id */
 extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the thread id */
-extern THREAD_LOCAL struct thread_info *ti; /* thread_info for the current thread */
+extern THREAD_LOCAL struct ha_thread_info *ti; /* thread_info for the current thread */
 extern volatile unsigned long all_threads_mask;
 extern volatile unsigned long threads_want_rdv_mask;
 extern volatile unsigned long threads_harmless_mask;
@@ -480,7 +480,7 @@ static inline void ha_set_tid(unsigned int data)
 {
 	tid     = data;
 	tid_bit = (1UL << tid);
-	ti      = &thread_info[tid];
+	ti      = &ha_thread_info[tid];
 }
 
 static inline void ha_thread_relax(void)
diff --git a/include/common/time.h b/include/common/time.h
index 2633b1c5..8826207f 100644
--- a/include/common/time.h
+++ b/include/common/time.h
@@ -541,7 +541,7 @@ static inline uint64_t now_cpu_time()
 }
 
 /* returns another thread's cumulated CPU time in nanoseconds if supported, otherwise zero */
-static inline uint64_t now_cpu_time_thread(const struct thread_info *thr)
+static inline uint64_t now_cpu_time_thread(const struct ha_thread_info *thr)
 {
 #if (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME)
 	struct timespec ts;
diff --git a/src/debug.c b/src/debug.c
index 409bd2cb..0bf63e5f 100644
--- a/src/debug.c
+++ b/src/debug.c
@@ -45,9 +45,9 @@ volatile unsigned long threads_to_dump = 0;
 void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
 {
 	unsigned long thr_bit = 1UL << thr;
-	unsigned long long p = thread_info[thr].prev_cpu_time;
-	unsigned long long n = now_cpu_time_thread(&thread_info[thr]);
-	int stuck = !!(thread_info[thr].flags & TI_FL_STUCK);
+	unsigned long long p = ha_thread_info[thr].prev_cpu_time;
+	unsigned long long n = now_cpu_time_thread(&ha_thread_info[thr]);
+	int stuck = !!(ha_thread_info[thr].flags & TI_FL_STUCK);
 
 	chunk_appendf(buf,
 	              "%c%cThread %-2u: act=%d glob=%d wq=%d rq=%d tl=%d tlsz=%d rqsz=%d\n"
diff --git a/src/haproxy.c b/src/haproxy.c
index c490cead..e867ea13 100644
--- a/src/haproxy.c
+++ b/src/haproxy.c
@@ -3231,9 +3231,9 @@ int main(int argc, char **argv)
 		pthread_sigmask(SIG_SETMASK, &blocked_sig, &old_sig);
 
 		/* Create nbthread-1 thread. The first thread is the current process */
-		thread_info[0].pthread = pthread_self();
+		ha_thread_info[0].pthread = pthread_self();
 		for (i = 1; i < global.nbthread; i++)
-			pthread_create(&thread_info[i].pthread, NULL, &run_thread_poll_loop, (void *)(long)i);
+			pthread_create(&ha_thread_info[i].pthread, NULL, &run_thread_poll_loop, (void *)(long)i);
 
 #ifdef USE_CPU_AFFINITY
 		/* Now the CPU affinity for all threads */
@@ -3260,7 +3260,7 @@ int main(int argc, char **argv)
 					CPU_SET(j - 1, &cpuset);
 					cpu_map &= ~(1UL << (j - 1));
 				}
-				pthread_setaffinity_np(thread_info[i].pthread,
+				pthread_setaffinity_np(ha_thread_info[i].pthread,
 						       sizeof(cpuset), &cpuset);
 			}
 		}
@@ -3274,7 +3274,7 @@ int main(int argc, char **argv)
 
 		/* Wait the end of other threads */
 		for (i = 1; i < global.nbthread; i++)
-			pthread_join(thread_info[i].pthread, NULL);
+			pthread_join(ha_thread_info[i].pthread, NULL);
 
 #if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
 		show_lock_stats();
diff --git a/src/hathreads.c b/src/hathreads.c
index 69385236..b01d5a3a 100644
--- a/src/hathreads.c
+++ b/src/hathreads.c
@@ -29,8 +29,8 @@
 #include <types/global.h>
 #include <proto/fd.h>
 
-struct thread_info thread_info[MAX_THREADS] = { };
-THREAD_LOCAL struct thread_info *ti = &thread_info[0];
+struct ha_thread_info ha_thread_info[MAX_THREADS] = { };
+THREAD_LOCAL struct ha_thread_info *ti = &ha_thread_info[0];
 
 #ifdef USE_THREAD
 
@@ -134,7 +134,7 @@ void thread_sync_release()
 /* send signal <sig> to thread <thr> */
 void ha_tkill(unsigned int thr, int sig)
 {
-	pthread_kill(thread_info[thr].pthread, sig);
+	pthread_kill(ha_thread_info[thr].pthread, sig);
 }
 
 /* send signal <sig> to all threads. The calling thread is signaled last in
@@ -149,7 +149,7 @@ void ha_tkillall(int sig)
 			continue;
 		if (thr == tid)
 			continue;
-		pthread_kill(thread_info[thr].pthread, sig);
+		pthread_kill(ha_thread_info[thr].pthread, sig);
 	}
 	raise(sig);
 }
diff --git a/src/wdt.c b/src/wdt.c
index aa89fd44..8f31e949 100644
--- a/src/wdt.c
+++ b/src/wdt.c
@@ -43,7 +43,7 @@ int wdt_ping(int thr)
 
 	its.it_value.tv_sec    = 1; its.it_value.tv_nsec    = 0;
 	its.it_interval.tv_sec = 0; its.it_interval.tv_nsec = 0;
-	return timer_settime(thread_info[thr].wd_timer, 0, &its, NULL) == 0;
+	return timer_settime(ha_thread_info[thr].wd_timer, 0, &its, NULL) == 0;
 }
 
 /* This is the WDTSIG signal handler */
@@ -68,8 +68,8 @@ void wdt_handler(int sig, siginfo_t *si, void *arg)
 		if (thr < 0 || thr >= global.nbthread)
 			break;
 
-		p = thread_info[thr].prev_cpu_time;
-		n = now_cpu_time_thread(&thread_info[thr]);
+		p = ha_thread_info[thr].prev_cpu_time;
+		n = now_cpu_time_thread(&ha_thread_info[thr]);
 
 		/* not yet reached the deadline of 1 sec */
 		if (n - p < 1000000000UL)
@@ -94,8 +94,8 @@ void wdt_handler(int sig, siginfo_t *si, void *arg)
 		 * If it's already set, then it's our second call with no
 		 * progress and the thread is dead.
 		 */
-		if (!(thread_info[thr].flags & TI_FL_STUCK)) {
-			_HA_ATOMIC_OR(&thread_info[thr].flags, TI_FL_STUCK);
+		if (!(ha_thread_info[thr].flags & TI_FL_STUCK)) {
+			_HA_ATOMIC_OR(&ha_thread_info[thr].flags, TI_FL_STUCK);
 			goto update_and_leave;
 		}
 
@@ -118,7 +118,7 @@ void wdt_handler(int sig, siginfo_t *si, void *arg)
 	 * the current one not involved in this.
 	 */
 	if (thr != tid)
-		pthread_kill(thread_info[thr].pthread, sig);
+		pthread_kill(ha_thread_info[thr].pthread, sig);
 	else
 		ha_panic();
 	return;
-- 
2.22.1

