From 923700aa9b4efe865cb1b743b69a261a456669cd Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 5 Jul 2024 13:37:15 +0000 Subject: [PATCH] make.sh: Create a timer co-process Signed-off-by: Michael Tremer --- make.sh | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/make.sh b/make.sh index 19d94445d..f709adcd7 100755 --- a/make.sh +++ b/make.sh @@ -358,6 +358,45 @@ print_build_summary() { print_status DONE } +# Launches a timer process as a co-process +launch_timer() { + # Do nothing if the timer is already running + if [ -n "${TIMER_PID}" ]; then + return 0 + fi + + # Launch the co-process + coproc TIMER { "${0}" "__timer" "$$"; } + + # Register the signal handlers + trap "__timer_event" SIGUSR1 + trap "terminate_timer" EXIT +} + +# Terminates a previously launched timer +terminate_timer() { + if [ -n "${TIMER_PID}" ]; then + kill -TERM "${TIMER_PID}" + fi +} + +# The timer main loop +__timer() { + local pid="${1}" + + # Send SIGUSR1 to the main process once a second + while sleep 1; do + kill -USR1 "${pid}" + done + + return 0 +} + +# Called when the timer triggers +__timer_event() { + : # TODO +} + exiterror() { # Dump logfile if [ -n "${LOGFILE}" ] && [ -e "${LOGFILE}" ]; then @@ -778,6 +817,9 @@ run_command() { # Store the start time local t="${SECONDS}" + # Launch the timer + launch_timer + # Run the command and pipe all output to the logfile if ! "${command[@]}" >> "${LOGFILE}" 2>&1