Browse Source

[libc] Make sleep() interruptible

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 8 years ago
parent
commit
ee3122bc54
2 changed files with 21 additions and 17 deletions
  1. 2
    14
      src/core/exec.c
  2. 19
    3
      src/core/timer.c

+ 2
- 14
src/core/exec.c View File

@@ -36,10 +36,6 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
36 36
 #include <ipxe/command.h>
37 37
 #include <ipxe/parseopt.h>
38 38
 #include <ipxe/settings.h>
39
-#include <ipxe/console.h>
40
-#include <ipxe/keys.h>
41
-#include <ipxe/process.h>
42
-#include <ipxe/nap.h>
43 39
 #include <ipxe/shell.h>
44 40
 
45 41
 /** @file
@@ -573,8 +569,6 @@ static struct command_descriptor sleep_cmd =
573 569
 static int sleep_exec ( int argc, char **argv ) {
574 570
 	struct sleep_options opts;
575 571
 	unsigned int seconds;
576
-	unsigned long start;
577
-	unsigned long delay;
578 572
 	int rc;
579 573
 
580 574
 	/* Parse options */
@@ -586,14 +580,8 @@ static int sleep_exec ( int argc, char **argv ) {
586 580
 		return rc;
587 581
 
588 582
 	/* Delay for specified number of seconds */
589
-	start = currticks();
590
-	delay = ( seconds * TICKS_PER_SEC );
591
-	while ( ( currticks() - start ) <= delay ) {
592
-		step();
593
-		if ( iskey() && ( getchar() == CTRL_C ) )
594
-			return -ECANCELED;
595
-		cpu_nap();
596
-	}
583
+	if ( sleep ( seconds ) != 0 )
584
+		return -ECANCELED;
597 585
 
598 586
 	return 0;
599 587
 }

+ 19
- 3
src/core/timer.c View File

@@ -24,6 +24,10 @@
24 24
 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 25
 
26 26
 #include <unistd.h>
27
+#include <ipxe/process.h>
28
+#include <ipxe/console.h>
29
+#include <ipxe/keys.h>
30
+#include <ipxe/nap.h>
27 31
 
28 32
 /**
29 33
  * Delay for a fixed number of milliseconds
@@ -36,12 +40,24 @@ void mdelay ( unsigned long msecs ) {
36 40
 }
37 41
 
38 42
 /**
39
- * Delay for a fixed number of seconds
43
+ * Sleep (interruptibly) for a fixed number of seconds
40 44
  *
41 45
  * @v secs		Number of seconds for which to delay
46
+ * @ret secs		Number of seconds remaining, if interrupted
42 47
  */
43 48
 unsigned int sleep ( unsigned int secs ) {
44
-	while ( secs-- )
45
-		mdelay ( 1000 );
49
+	unsigned long start = currticks();
50
+	unsigned long now;
51
+
52
+	for ( ; secs ; secs-- ) {
53
+		while ( ( ( now = currticks() ) - start ) < TICKS_PER_SEC ) {
54
+			step();
55
+			if ( iskey() && ( getchar() == CTRL_C ) )
56
+				return secs;
57
+			cpu_nap();
58
+		}
59
+		start = now;
60
+	}
61
+
46 62
 	return 0;
47 63
 }

Loading…
Cancel
Save