MINOR: threads/checks: Add a lock to protect the pid list used by external checks
diff --git a/include/common/hathreads.h b/include/common/hathreads.h
index 242bece..f68dc7e 100644
--- a/include/common/hathreads.h
+++ b/include/common/hathreads.h
@@ -169,6 +169,7 @@
 	NOTIF_LOCK,
 	SPOE_APPLET_LOCK,
 	DNS_LOCK,
+	PID_LIST_LOCK,
 	LOCK_LABELS
 };
 struct lock_stat {
@@ -257,7 +258,7 @@
 					   "UPDATED_SERVERS", "LBPRM", "SIGNALS", "STK_TABLE", "STK_SESS",
 					   "APPLETS", "PEER", "BUF_WQ", "STREAMS", "SSL", "SSL_GEN_CERTS",
 					   "PATREF", "PATEXP", "PATLRU", "VARS", "COMP_POOL", "LUA",
-					   "NOTIF", "SPOE_APPLET", "DNS" };
+					   "NOTIF", "SPOE_APPLET", "DNS", "PID_LIST" };
 	int lbl;
 
 	for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
diff --git a/src/checks.c b/src/checks.c
index ed99bb5..cf1c59e 100644
--- a/src/checks.c
+++ b/src/checks.c
@@ -35,6 +35,7 @@
 #include <common/mini-clist.h>
 #include <common/standard.h>
 #include <common/time.h>
+#include <common/hathreads.h>
 
 #include <types/global.h>
 #include <types/dns.h>
@@ -1582,6 +1583,9 @@
 
 static struct list pid_list = LIST_HEAD_INIT(pid_list);
 static struct pool_head *pool2_pid_list;
+#ifdef USE_THREAD
+HA_SPINLOCK_T pid_list_lock;
+#endif
 
 void block_sigchld(void)
 {
@@ -1612,7 +1616,11 @@
 	elem->exited = 0;
 	check->curpid = elem;
 	LIST_INIT(&elem->list);
+
+	SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock);
 	LIST_ADD(&pid_list, &elem->list);
+	SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock);
+
 	return elem;
 }
 
@@ -1623,7 +1631,10 @@
 	if (!elem)
 		return;
 
+	SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock);
 	LIST_DEL(&elem->list);
+	SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock);
+
 	if (!elem->exited)
 		kill(elem->pid, SIGTERM);
 
@@ -1637,15 +1648,17 @@
 {
 	struct pid_list *elem;
 
+	SPIN_LOCK(PID_LIST_LOCK, &pid_list_lock);
 	list_for_each_entry(elem, &pid_list, list) {
 		if (elem->pid == pid) {
 			elem->t->expire = now_ms;
 			elem->status = status;
 			elem->exited = 1;
 			task_wakeup(elem->t, TASK_WOKEN_IO);
-			return;
+			break;
 		}
 	}
+	SPIN_UNLOCK(PID_LIST_LOCK, &pid_list_lock);
 }
 
 static void sigchld_handler(struct sig_handler *sh)
@@ -1676,6 +1689,8 @@
 		return 1;
 	}
 
+	SPIN_INIT(&pid_list_lock);
+
 	return 0;
 }