]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
REORG: clock/wdt: move wdt timer initialization to clock.c
authorWilly Tarreau <w@1wt.eu>
Fri, 8 Oct 2021 12:48:30 +0000 (14:48 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 8 Oct 2021 15:22:26 +0000 (17:22 +0200)
The code that deals with timer creation for the WDT was moved to clock.c
and is called with the few relevant arguments. This removes the need for
awareness of clock_id from wdt.c and as such saves us from having to
share it outside. The timer_t is also known only from both ends but not
from the public API so that we don't have to create a fake timer_t
anymore on systems which do not support it (e.g. macos).

include/haproxy/clock.h
src/clock.c
src/wdt.c

index 77fd7ec7889ae531ddef312c1c53ae10c1bb1872..2cee76da4e69f401c7e86cf8e1db465bcef4472a 100644 (file)
@@ -39,6 +39,7 @@ void clock_set_local_source(void);
 void clock_update_date(int max_wait, int interrupted);
 void clock_init_process_date(void);
 void clock_init_thread_date(void);
+int clock_setup_signal_timer(void *timer, int sig, int val);
 char *timeofday_as_iso_us(int pad);
 uint clock_report_idle(void);
 void clock_leaving_poll(int timeout, int interrupted);
index 5a3447f2be79f2ae2c489be07d24dae257d2b2d2..517f0a3a53341ffdde874749a70d1f8f8704eece 100644 (file)
@@ -11,6 +11,7 @@
  */
 
 #include <sys/time.h>
+#include <signal.h>
 #include <time.h>
 
 #ifdef USE_THREAD
@@ -20,6 +21,7 @@
 #include <haproxy/api.h>
 #include <haproxy/activity.h>
 #include <haproxy/clock.h>
+#include <haproxy/signal-t.h>
 #include <haproxy/time.h>
 #include <haproxy/tinfo-t.h>
 #include <haproxy/tools.h>
@@ -45,7 +47,7 @@ static THREAD_LOCAL char         iso_time_str[34]; /* ISO time representation of
 uint64_t now_mono_time(void)
 {
        uint64_t ret = 0;
-#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && defined(_POSIX_MONOTONIC_CLOCK)
+#if defined(_POSIX_TIMERS) && defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && defined(_POSIX_MONOTONIC_CLOCK)
        struct timespec ts;
        clock_gettime(CLOCK_MONOTONIC, &ts);
        ret = ts.tv_sec * 1000000000ULL + ts.tv_nsec;
@@ -89,6 +91,37 @@ void clock_set_local_source(void)
 #endif
 }
 
+/* registers a timer <tmr> of type timer_t delivering signal <sig> with value
+ * <val>. It tries on the current thread's clock ID first and falls back to
+ * CLOCK_REALTIME. Returns non-zero on success, 1 on failure.
+ */
+int clock_setup_signal_timer(void *tmr, int sig, int val)
+{
+       int ret = 0;
+
+#if defined(USE_RT) && (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME)
+       struct sigevent sev = { };
+       timer_t *timer = tmr;
+       sigset_t set;
+
+       /* unblock the WDTSIG signal we intend to use */
+       sigemptyset(&set);
+       sigaddset(&set, WDTSIG);
+       ha_sigmask(SIG_UNBLOCK, &set, NULL);
+
+       /* this timer will signal WDTSIG when it fires, with tid in the si_int
+        * field (important since any thread will receive the signal).
+        */
+       sev.sigev_notify          = SIGEV_SIGNAL;
+       sev.sigev_signo           = sig;
+       sev.sigev_value.sival_int = val;
+       if (timer_create(ti->clock_id, &sev, timer) != -1 ||
+           timer_create(CLOCK_REALTIME, &sev, timer) != -1)
+               ret = 1;
+#endif
+       return ret;
+}
+
 /* clock_update_date: sets <date> to system time, and sets <now> to something as
  * close as possible to real time, following a monotonic function. The main
  * principle consists in detecting backwards and forwards time jumps and adjust
index 066f109ab7ef3fa2bb943f76294f75ec709fe376..a893b5d89659d67eb92f30db35fca9cab114795b 100644 (file)
--- a/src/wdt.c
+++ b/src/wdt.c
@@ -127,22 +127,7 @@ void wdt_handler(int sig, siginfo_t *si, void *arg)
 
 int init_wdt_per_thread()
 {
-       struct sigevent sev = { };
-       sigset_t set;
-
-       /* unblock the WDTSIG signal we intend to use */
-       sigemptyset(&set);
-       sigaddset(&set, WDTSIG);
-       ha_sigmask(SIG_UNBLOCK, &set, NULL);
-
-       /* this timer will signal WDTSIG when it fires, with tid in the si_int
-        * field (important since any thread will receive the signal).
-        */
-       sev.sigev_notify          = SIGEV_SIGNAL;
-       sev.sigev_signo           = WDTSIG;
-       sev.sigev_value.sival_int = tid;
-       if (timer_create(ti->clock_id, &sev, &ti->wd_timer) == -1 &&
-           timer_create(CLOCK_REALTIME, &sev, &ti->wd_timer) == -1)
+       if (!clock_setup_signal_timer(&ti->wd_timer, WDTSIG, tid))
                goto fail1;
 
        if (!wdt_ping(tid))