]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[monojob] Check for keypresses only once per timer tick
authorMichael Brown <mcb30@ipxe.org>
Thu, 28 Jun 2012 11:27:43 +0000 (12:27 +0100)
committerMichael Brown <mcb30@ipxe.org>
Thu, 28 Jun 2012 15:02:37 +0000 (16:02 +0100)
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>
src/core/monojob.c

index 7917b4272087b531ede36192c067a3bd761230bc..d2161b3cd426db0cea8c807e79ae9f79218e08d9 100644 (file)
@@ -60,7 +60,9 @@ int monojob_wait ( const char *string ) {
        struct job_progress progress;
        int key;
        int rc;
+       unsigned long last_keycheck;
        unsigned long last_progress;
+       unsigned long now;
        unsigned long elapsed;
        unsigned long completed;
        unsigned long total;
@@ -70,20 +72,32 @@ int monojob_wait ( const char *string ) {
        if ( string )
                printf ( "%s...", string );
        monojob_rc = -EINPROGRESS;
-       last_progress = currticks();
+       last_keycheck = last_progress = currticks();
        while ( monojob_rc == -EINPROGRESS ) {
+
+               /* Allow job to progress */
                step();
-               if ( iskey() ) {
-                       key = getchar();
-                       switch ( key ) {
-                       case CTRL_C:
-                               monojob_close ( &monojob, -ECANCELED );
-                               break;
-                       default:
-                               break;
+               now = currticks();
+
+               /* Check for keypresses.  This can be time-consuming,
+                * so check only once per clock tick.
+                */
+               if ( now != last_keycheck ) {
+                       if ( iskey() ) {
+                               key = getchar();
+                               switch ( key ) {
+                               case CTRL_C:
+                                       monojob_close ( &monojob, -ECANCELED );
+                                       break;
+                               default:
+                                       break;
+                               }
                        }
+                       last_keycheck = now;
                }
-               elapsed = ( currticks() - last_progress );
+
+               /* Display progress, if applicable */
+               elapsed = ( now - last_progress );
                if ( string && ( elapsed >= TICKS_PER_SEC ) ) {
                        if ( shown_percentage )
                                printf ( "\b\b\b\b    \b\b\b\b" );
@@ -99,7 +113,7 @@ int monojob_wait ( const char *string ) {
                                printf ( "." );
                                shown_percentage = 0;
                        }
-                       last_progress = currticks();
+                       last_progress = now;
                }
        }
        rc = monojob_rc;