The timer poll loop in lcore_mainloop() never terminates, so the
rte_eal_cleanup() and return in main() are unreachable. Clang
reports this with -Wunreachable-code.

Add a signal handler setting force_quit, as done by l2fwd and
others, and test it in the loop. The function is no longer
__rte_noreturn and now returns zero.

lcore_mainloop() also runs on the worker lcores, so main() must
wait for them to leave the loop before tearing down the EAL.

Signed-off-by: Stephen Hemminger <[email protected]>
---
 examples/timer/main.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/examples/timer/main.c b/examples/timer/main.c
index a349846c7c..170c15ca19 100644
--- a/examples/timer/main.c
+++ b/examples/timer/main.c
@@ -2,6 +2,8 @@
  * Copyright(c) 2010-2014 Intel Corporation
  */
 
+#include <signal.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdint.h>
@@ -56,7 +58,16 @@ timer1_cb(__rte_unused struct rte_timer *tim,
 }
 /* >8 End of timer1 callback. */
 
-static __rte_noreturn int
+static volatile bool force_quit;
+
+static void
+signal_handler(int signum)
+{
+       if (signum == SIGINT || signum == SIGTERM)
+               force_quit = true;
+}
+
+static int
 lcore_mainloop(__rte_unused void *arg)
 {
        uint64_t prev_tsc = 0, cur_tsc, diff_tsc;
@@ -66,7 +77,7 @@ lcore_mainloop(__rte_unused void *arg)
        printf("Starting mainloop on core %u\n", lcore_id);
 
        /* Main loop. 8< */
-       while (1) {
+       while (!force_quit) {
                /*
                 * Call the timer handler on each core: as we don't need a
                 * very precise timer, so only call rte_timer_manage()
@@ -81,6 +92,8 @@ lcore_mainloop(__rte_unused void *arg)
                }
        }
        /* >8 End of main loop. */
+
+       return 0;
 }
 
 int
@@ -95,6 +108,9 @@ main(int argc, char **argv)
        if (ret < 0)
                rte_panic("Cannot init EAL\n");
 
+       signal(SIGINT, signal_handler);
+       signal(SIGTERM, signal_handler);
+
        /* init RTE timer library */
        rte_timer_subsystem_init();
        /* >8 End of init EAL. */
@@ -126,6 +142,9 @@ main(int argc, char **argv)
        (void) lcore_mainloop(NULL);
        /* >8 End of call lcore_mainloop() on every worker lcore. */
 
+       /* wait for the workers to stop polling the timers */
+       rte_eal_mp_wait_lcore();
+
        /* clean up the EAL */
        rte_eal_cleanup();
 
-- 
2.53.0

Reply via email to