BUG/MINOR: signals/poller: ensure wakeup from signals

Add self-wake in signal_handler() to fix a race condition with a signal
coming in between checking signal_queue_len and entering polling sleep.

The changes in commit 43c891dda ("BUG/MINOR: signals/poller: set the
poller timeout to 0 when there are signals") were insufficient.

Move the signal_queue_len check from the poll implementations to
run_poll_loop() to keep that logic in one place.

The poll loops are terminated either by the parameter wake being set or
wake up due to a write to their poller_wr_pipe by wake_thread() in
signal_handler().

This fixes issue #1841.

Must be backported in every stable version.
diff --git a/src/haproxy.c b/src/haproxy.c
index 2eb84bd..8d6f587 100644
--- a/src/haproxy.c
+++ b/src/haproxy.c
@@ -2810,7 +2810,7 @@
 		if (killed > 1)
 			break;
 
-		/* expire immediately if events are pending */
+		/* expire immediately if events or signals are pending */
 		wake = 1;
 		if (thread_has_tasks())
 			activity[tid].wake_tasks++;
@@ -2821,6 +2821,10 @@
 			if (thread_has_tasks()) {
 				activity[tid].wake_tasks++;
 				_HA_ATOMIC_AND(&th_ctx->flags, ~TH_FL_SLEEPING);
+			} else if (signal_queue_len) {
+				/* this check is required after setting TH_FL_SLEEPING to avoid
+				 * a race with wakeup on signals using wake_threads() */
+				_HA_ATOMIC_AND(&th_ctx->flags, ~TH_FL_SLEEPING);
 			} else
 				wake = 0;
 		}