From ee3122bc54631711e192c934734f3f5be40c2fa9 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Tue, 22 Mar 2016 16:12:32 +0000 Subject: [PATCH] [libc] Make sleep() interruptible Signed-off-by: Michael Brown --- src/core/exec.c | 16 ++-------------- src/core/timer.c | 22 +++++++++++++++++++--- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/core/exec.c b/src/core/exec.c index 2c2ade0a..a13884b6 100644 --- a/src/core/exec.c +++ b/src/core/exec.c @@ -36,10 +36,6 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); #include #include #include -#include -#include -#include -#include #include /** @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; } diff --git a/src/core/timer.c b/src/core/timer.c index dbd89f12..ca945cfb 100644 --- a/src/core/timer.c +++ b/src/core/timer.c @@ -24,6 +24,10 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); #include +#include +#include +#include +#include /** * 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; }