Skip to content

Commit

Permalink
[time] Add sleep_fixed() function to sleep without checking for Ctrl-C
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Brown <mcb30@ipxe.org>
  • Loading branch information
mcb30 committed Mar 27, 2017
1 parent c73af29 commit 6bd0060
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
39 changes: 36 additions & 3 deletions src/core/timer.c
Expand Up @@ -90,19 +90,21 @@ void mdelay ( unsigned long msecs ) {
}

/**
* Sleep (interruptibly) for a fixed number of seconds
* Sleep (possibly interruptibly) for a fixed number of seconds
*
* @v secs Number of seconds for which to delay
* @v interrupted Interrupt checking method, or NULL
* @ret secs Number of seconds remaining, if interrupted
*/
unsigned int sleep ( unsigned int secs ) {
static unsigned int sleep_interruptible ( unsigned int secs,
int ( * interrupted ) ( void ) ) {
unsigned long start = currticks();
unsigned long now;

for ( ; secs ; secs-- ) {
while ( ( ( now = currticks() ) - start ) < TICKS_PER_SEC ) {
step();
if ( iskey() && ( getchar() == CTRL_C ) )
if ( interrupted && interrupted() )
return secs;
cpu_nap();
}
Expand All @@ -112,6 +114,37 @@ unsigned int sleep ( unsigned int secs ) {
return 0;
}

/**
* Check if sleep has been interrupted by keypress
*
* @ret interrupted Sleep has been interrupted
*/
static int keypress_interrupted ( void ) {

return ( iskey() && ( getchar() == CTRL_C ) );
}

/**
* 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 ) {

return sleep_interruptible ( secs, keypress_interrupted );
}

/**
* Sleep (uninterruptibly) for a fixed number of seconds
*
* @v secs Number of seconds for which to delay
*/
void sleep_fixed ( unsigned int secs ) {

sleep_interruptible ( secs, NULL );
}

/**
* Find a working timer
*
Expand Down
1 change: 1 addition & 0 deletions src/include/ipxe/timer.h
Expand Up @@ -75,5 +75,6 @@ extern void udelay ( unsigned long usecs );
extern void mdelay ( unsigned long msecs );
extern unsigned long currticks ( void );
extern unsigned int sleep ( unsigned int seconds );
extern void sleep_fixed ( unsigned int secs );

#endif /* _IPXE_TIMER_H */

0 comments on commit 6bd0060

Please sign in to comment.