]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[libc] Make sleep() interruptible
authorMichael Brown <mcb30@ipxe.org>
Tue, 22 Mar 2016 16:12:32 +0000 (16:12 +0000)
committerMichael Brown <mcb30@ipxe.org>
Tue, 22 Mar 2016 16:18:42 +0000 (16:18 +0000)
Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/core/exec.c
src/core/timer.c

index 2c2ade0a55a11994850b8a1394558993369784dd..a13884b68255a9433cdf70796f9919bea853f702 100644 (file)
@@ -36,10 +36,6 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
 #include <ipxe/command.h>
 #include <ipxe/parseopt.h>
 #include <ipxe/settings.h>
-#include <ipxe/console.h>
-#include <ipxe/keys.h>
-#include <ipxe/process.h>
-#include <ipxe/nap.h>
 #include <ipxe/shell.h>
 
 /** @file
@@ -573,8 +569,6 @@ static struct command_descriptor sleep_cmd =
 static int sleep_exec ( int argc, char **argv ) {
        struct sleep_options opts;
        unsigned int seconds;
-       unsigned long start;
-       unsigned long delay;
        int rc;
 
        /* Parse options */
@@ -586,14 +580,8 @@ static int sleep_exec ( int argc, char **argv ) {
                return rc;
 
        /* Delay for specified number of seconds */
-       start = currticks();
-       delay = ( seconds * TICKS_PER_SEC );
-       while ( ( currticks() - start ) <= delay ) {
-               step();
-               if ( iskey() && ( getchar() == CTRL_C ) )
-                       return -ECANCELED;
-               cpu_nap();
-       }
+       if ( sleep ( seconds ) != 0 )
+               return -ECANCELED;
 
        return 0;
 }
index dbd89f12bc30310c440f13124d613b36b37311e7..ca945cfba3d418a16da470924a6d43cec7fb8e7d 100644 (file)
 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
 
 #include <unistd.h>
+#include <ipxe/process.h>
+#include <ipxe/console.h>
+#include <ipxe/keys.h>
+#include <ipxe/nap.h>
 
 /**
  * Delay for a fixed number of milliseconds
@@ -36,12 +40,24 @@ void mdelay ( unsigned long msecs ) {
 }
 
 /**
- * Delay for a fixed number of seconds
+ * Sleep (interruptibly) for a fixed number of seconds
  *
  * @v secs             Number of seconds for which to delay
+ * @ret secs           Number of seconds remaining, if interrupted
  */
 unsigned int sleep ( unsigned int secs ) {
-       while ( secs-- )
-               mdelay ( 1000 );
+       unsigned long start = currticks();
+       unsigned long now;
+
+       for ( ; secs ; secs-- ) {
+               while ( ( ( now = currticks() ) - start ) < TICKS_PER_SEC ) {
+                       step();
+                       if ( iskey() && ( getchar() == CTRL_C ) )
+                               return secs;
+                       cpu_nap();
+               }
+               start = now;
+       }
+
        return 0;
 }