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
 #include <ipxe/command.h>
36
 #include <ipxe/command.h>
37
 #include <ipxe/parseopt.h>
37
 #include <ipxe/parseopt.h>
38
 #include <ipxe/settings.h>
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
 #include <ipxe/shell.h>
39
 #include <ipxe/shell.h>
44
 
40
 
45
 /** @file
41
 /** @file
573
 static int sleep_exec ( int argc, char **argv ) {
569
 static int sleep_exec ( int argc, char **argv ) {
574
 	struct sleep_options opts;
570
 	struct sleep_options opts;
575
 	unsigned int seconds;
571
 	unsigned int seconds;
576
-	unsigned long start;
577
-	unsigned long delay;
578
 	int rc;
572
 	int rc;
579
 
573
 
580
 	/* Parse options */
574
 	/* Parse options */
586
 		return rc;
580
 		return rc;
587
 
581
 
588
 	/* Delay for specified number of seconds */
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
 	return 0;
586
 	return 0;
599
 }
587
 }

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

24
 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
24
 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
 
25
 
26
 #include <unistd.h>
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
  * Delay for a fixed number of milliseconds
33
  * Delay for a fixed number of milliseconds
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
  * @v secs		Number of seconds for which to delay
45
  * @v secs		Number of seconds for which to delay
46
+ * @ret secs		Number of seconds remaining, if interrupted
42
  */
47
  */
43
 unsigned int sleep ( unsigned int secs ) {
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
 	return 0;
62
 	return 0;
47
 }
63
 }

Loading…
Cancel
Save