From b850a6be2811e9c21d2f6c02ed6c94c51a6522a6 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Mon, 10 Mar 2014 13:16:18 +0000 Subject: [PATCH] [monojob] Reset timeout when progress is made Redefine the timeout parameter from "time since start of job" to "time since progress was last made". This does not affect any existing behaviour, since all existing users of the timeout parameter do not provide progress indication. Signed-off-by: Michael Brown --- src/core/monojob.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/core/monojob.c b/src/core/monojob.c index 5dbfa4062..820fa31dc 100644 --- a/src/core/monojob.c +++ b/src/core/monojob.c @@ -60,13 +60,14 @@ struct interface monojob = INTF_INIT ( monojob_intf_desc ); */ int monojob_wait ( const char *string, unsigned long timeout ) { struct job_progress progress; - unsigned long start; unsigned long last_keycheck; unsigned long last_progress; + unsigned long last_display; unsigned long now; unsigned long elapsed; - unsigned long completed; - unsigned long total; + unsigned long completed = 0; + unsigned long scaled_completed; + unsigned long scaled_total; unsigned int percentage; int shown_percentage = 0; int ongoing_rc; @@ -76,7 +77,7 @@ int monojob_wait ( const char *string, unsigned long timeout ) { if ( string ) printf ( "%s...", string ); monojob_rc = -EINPROGRESS; - last_keycheck = last_progress = start = currticks(); + last_keycheck = last_progress = last_display = currticks(); while ( monojob_rc == -EINPROGRESS ) { /* Allow job to progress */ @@ -101,30 +102,36 @@ int monojob_wait ( const char *string, unsigned long timeout ) { /* Monitor progress */ ongoing_rc = job_progress ( &monojob, &progress ); + /* Reset timeout if progress has been made */ + if ( completed != progress.completed ) + last_progress = now; + completed = progress.completed; + /* Check for timeout, if applicable */ - elapsed = ( now - start ); + elapsed = ( now - last_progress ); if ( timeout && ( elapsed >= timeout ) ) { monojob_rc = ( ongoing_rc ? ongoing_rc : -ETIMEDOUT ); break; } /* Display progress, if applicable */ - elapsed = ( now - last_progress ); + elapsed = ( now - last_display ); if ( string && ( elapsed >= TICKS_PER_SEC ) ) { if ( shown_percentage ) printf ( "\b\b\b\b \b\b\b\b" ); /* Normalise progress figures to avoid overflow */ - completed = ( progress.completed / 128 ); - total = ( progress.total / 128 ); - if ( total ) { - percentage = ( ( 100 * completed ) / total ); + scaled_completed = ( progress.completed / 128 ); + scaled_total = ( progress.total / 128 ); + if ( scaled_total ) { + percentage = ( ( 100 * scaled_completed ) / + scaled_total ); printf ( "%3d%%", percentage ); shown_percentage = 1; } else { printf ( "." ); shown_percentage = 0; } - last_progress = now; + last_display = now; } } rc = monojob_rc;