summaryrefslogtreecommitdiffstats
path: root/src/core/monojob.c
diff options
context:
space:
mode:
authorMichael Brown2013-11-01 02:55:13 +0100
committerMichael Brown2013-11-01 17:26:02 +0100
commitd1be9f4acc9d4367fb6dc793495f0cc819b5a514 (patch)
tree6865cbc8e41c0b052aa704f0a41b7eaecd0f2f0a /src/core/monojob.c
parent[job] Allow job_progress() to return an ongoing job status code, if known (diff)
downloadipxe-d1be9f4acc9d4367fb6dc793495f0cc819b5a514.tar.gz
ipxe-d1be9f4acc9d4367fb6dc793495f0cc819b5a514.tar.xz
ipxe-d1be9f4acc9d4367fb6dc793495f0cc819b5a514.zip
[monojob] Add timeout parameter to monojob_wait()
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/core/monojob.c')
-rw-r--r--src/core/monojob.c28
1 files changed, 18 insertions, 10 deletions
diff --git a/src/core/monojob.c b/src/core/monojob.c
index d262f70b..94ed74c0 100644
--- a/src/core/monojob.c
+++ b/src/core/monojob.c
@@ -55,12 +55,12 @@ struct interface monojob = INTF_INIT ( monojob_intf_desc );
* Wait for single foreground job to complete
*
* @v string Job description to display, or NULL to be silent
+ * @v timeout Timeout period, in ticks (0=indefinite)
* @ret rc Job final status code
*/
-int monojob_wait ( const char *string ) {
+int monojob_wait ( const char *string, unsigned long timeout ) {
struct job_progress progress;
- int key;
- int rc;
+ unsigned long start;
unsigned long last_keycheck;
unsigned long last_progress;
unsigned long now;
@@ -69,11 +69,13 @@ int monojob_wait ( const char *string ) {
unsigned long total;
unsigned int percentage;
int shown_percentage = 0;
+ int key;
+ int rc;
if ( string )
printf ( "%s...", string );
monojob_rc = -EINPROGRESS;
- last_keycheck = last_progress = currticks();
+ last_keycheck = last_progress = start = currticks();
while ( monojob_rc == -EINPROGRESS ) {
/* Allow job to progress */
@@ -83,20 +85,25 @@ int monojob_wait ( const char *string ) {
/* Check for keypresses. This can be time-consuming,
* so check only once per clock tick.
*/
- if ( now != last_keycheck ) {
+ elapsed = ( now - last_keycheck );
+ if ( elapsed ) {
if ( iskey() ) {
key = getchar();
- switch ( key ) {
- case CTRL_C:
- monojob_close ( &monojob, -ECANCELED );
- break;
- default:
+ if ( key == CTRL_C ) {
+ monojob_rc = -ECANCELED;
break;
}
}
last_keycheck = now;
}
+ /* Check for timeout, if applicable */
+ elapsed = ( now - start );
+ if ( timeout && ( elapsed >= timeout ) ) {
+ monojob_rc = -ETIMEDOUT;
+ break;
+ }
+
/* Display progress, if applicable */
elapsed = ( now - last_progress );
if ( string && ( elapsed >= TICKS_PER_SEC ) ) {
@@ -118,6 +125,7 @@ int monojob_wait ( const char *string ) {
}
}
rc = monojob_rc;
+ monojob_close ( &monojob, rc );
if ( shown_percentage )
printf ( "\b\b\b\b \b\b\b\b" );