瀏覽代碼

[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 <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 10 年之前
父節點
當前提交
b850a6be28
共有 1 個文件被更改,包括 18 次插入11 次删除
  1. 18
    11
      src/core/monojob.c

+ 18
- 11
src/core/monojob.c 查看文件

@@ -60,13 +60,14 @@ struct interface monojob = INTF_INIT ( monojob_intf_desc );
60 60
  */
61 61
 int monojob_wait ( const char *string, unsigned long timeout ) {
62 62
 	struct job_progress progress;
63
-	unsigned long start;
64 63
 	unsigned long last_keycheck;
65 64
 	unsigned long last_progress;
65
+	unsigned long last_display;
66 66
 	unsigned long now;
67 67
 	unsigned long elapsed;
68
-	unsigned long completed;
69
-	unsigned long total;
68
+	unsigned long completed = 0;
69
+	unsigned long scaled_completed;
70
+	unsigned long scaled_total;
70 71
 	unsigned int percentage;
71 72
 	int shown_percentage = 0;
72 73
 	int ongoing_rc;
@@ -76,7 +77,7 @@ int monojob_wait ( const char *string, unsigned long timeout ) {
76 77
 	if ( string )
77 78
 		printf ( "%s...", string );
78 79
 	monojob_rc = -EINPROGRESS;
79
-	last_keycheck = last_progress = start = currticks();
80
+	last_keycheck = last_progress = last_display = currticks();
80 81
 	while ( monojob_rc == -EINPROGRESS ) {
81 82
 
82 83
 		/* Allow job to progress */
@@ -101,30 +102,36 @@ int monojob_wait ( const char *string, unsigned long timeout ) {
101 102
 		/* Monitor progress */
102 103
 		ongoing_rc = job_progress ( &monojob, &progress );
103 104
 
105
+		/* Reset timeout if progress has been made */
106
+		if ( completed != progress.completed )
107
+			last_progress = now;
108
+		completed = progress.completed;
109
+
104 110
 		/* Check for timeout, if applicable */
105
-		elapsed = ( now - start );
111
+		elapsed = ( now - last_progress );
106 112
 		if ( timeout && ( elapsed >= timeout ) ) {
107 113
 			monojob_rc = ( ongoing_rc ? ongoing_rc : -ETIMEDOUT );
108 114
 			break;
109 115
 		}
110 116
 
111 117
 		/* Display progress, if applicable */
112
-		elapsed = ( now - last_progress );
118
+		elapsed = ( now - last_display );
113 119
 		if ( string && ( elapsed >= TICKS_PER_SEC ) ) {
114 120
 			if ( shown_percentage )
115 121
 				printf ( "\b\b\b\b    \b\b\b\b" );
116 122
 			/* Normalise progress figures to avoid overflow */
117
-			completed = ( progress.completed / 128 );
118
-			total = ( progress.total / 128 );
119
-			if ( total ) {
120
-				percentage = ( ( 100 * completed ) / total );
123
+			scaled_completed = ( progress.completed / 128 );
124
+			scaled_total = ( progress.total / 128 );
125
+			if ( scaled_total ) {
126
+				percentage = ( ( 100 * scaled_completed ) /
127
+					       scaled_total );
121 128
 				printf ( "%3d%%", percentage );
122 129
 				shown_percentage = 1;
123 130
 			} else {
124 131
 				printf ( "." );
125 132
 				shown_percentage = 0;
126 133
 			}
127
-			last_progress = now;
134
+			last_display = now;
128 135
 		}
129 136
 	}
130 137
 	rc = monojob_rc;

Loading…
取消
儲存