Ver código fonte

[monojob] Check for keypresses only once per timer tick

Checking for keypresses takes a non-negligible amount of time, and
measurably affects our RTT.  Minimise the impact by checking for
keypresses only once per timer tick.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 anos atrás
pai
commit
85917ba8dd
1 arquivos alterados com 25 adições e 11 exclusões
  1. 25
    11
      src/core/monojob.c

+ 25
- 11
src/core/monojob.c Ver arquivo

@@ -60,7 +60,9 @@ int monojob_wait ( const char *string ) {
60 60
 	struct job_progress progress;
61 61
 	int key;
62 62
 	int rc;
63
+	unsigned long last_keycheck;
63 64
 	unsigned long last_progress;
65
+	unsigned long now;
64 66
 	unsigned long elapsed;
65 67
 	unsigned long completed;
66 68
 	unsigned long total;
@@ -70,20 +72,32 @@ int monojob_wait ( const char *string ) {
70 72
 	if ( string )
71 73
 		printf ( "%s...", string );
72 74
 	monojob_rc = -EINPROGRESS;
73
-	last_progress = currticks();
75
+	last_keycheck = last_progress = currticks();
74 76
 	while ( monojob_rc == -EINPROGRESS ) {
77
+
78
+		/* Allow job to progress */
75 79
 		step();
76
-		if ( iskey() ) {
77
-			key = getchar();
78
-			switch ( key ) {
79
-			case CTRL_C:
80
-				monojob_close ( &monojob, -ECANCELED );
81
-				break;
82
-			default:
83
-				break;
80
+		now = currticks();
81
+
82
+		/* Check for keypresses.  This can be time-consuming,
83
+		 * so check only once per clock tick.
84
+		 */
85
+		if ( now != last_keycheck ) {
86
+			if ( iskey() ) {
87
+				key = getchar();
88
+				switch ( key ) {
89
+				case CTRL_C:
90
+					monojob_close ( &monojob, -ECANCELED );
91
+					break;
92
+				default:
93
+					break;
94
+				}
84 95
 			}
96
+			last_keycheck = now;
85 97
 		}
86
-		elapsed = ( currticks() - last_progress );
98
+
99
+		/* Display progress, if applicable */
100
+		elapsed = ( now - last_progress );
87 101
 		if ( string && ( elapsed >= TICKS_PER_SEC ) ) {
88 102
 			if ( shown_percentage )
89 103
 				printf ( "\b\b\b\b    \b\b\b\b" );
@@ -99,7 +113,7 @@ int monojob_wait ( const char *string ) {
99 113
 				printf ( "." );
100 114
 				shown_percentage = 0;
101 115
 			}
102
-			last_progress = currticks();
116
+			last_progress = now;
103 117
 		}
104 118
 	}
105 119
 	rc = monojob_rc;

Carregando…
Cancelar
Salvar