From ff8528ea9a69ca2ef6cfbed0b7a1283e165aabe6 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Tue, 16 Jan 2007 08:10:54 +0000 Subject: [PATCH] Create and use async_block() macro; it cuts down on the visual overhead of blocking on asynchronous operations, when that isn't an important aspect of the code. --- src/drivers/ata/aoedev.c | 9 +++------ src/drivers/scsi/iscsidev.c | 20 ++++++++++---------- src/include/gpxe/async.h | 36 ++++++++++++++++++++++++++++++++++++ src/usr/dhcpmgmt.c | 10 +++------- src/usr/fetch.c | 7 ++----- 5 files changed, 54 insertions(+), 28 deletions(-) diff --git a/src/drivers/ata/aoedev.c b/src/drivers/ata/aoedev.c index 6658827c..ff047f10 100644 --- a/src/drivers/ata/aoedev.c +++ b/src/drivers/ata/aoedev.c @@ -17,6 +17,7 @@ */ #include +#include #include /** @file @@ -37,13 +38,9 @@ static int aoe_command ( struct ata_device *ata, struct aoe_device *aoedev = container_of ( ata, struct aoe_device, ata ); struct async async; - int rc; - async_init_orphan ( &async ); - if ( ( rc = aoe_issue ( &aoedev->aoe, command, &async ) ) != 0 ) - return rc; - async_wait ( &async, &rc, 1 ); - return rc; + return async_block ( &async, aoe_issue ( &aoedev->aoe, command, + &async ) ); } /** diff --git a/src/drivers/scsi/iscsidev.c b/src/drivers/scsi/iscsidev.c index 75b857e7..aab99032 100644 --- a/src/drivers/scsi/iscsidev.c +++ b/src/drivers/scsi/iscsidev.c @@ -17,6 +17,7 @@ */ #include +#include #include /** @file @@ -37,13 +38,9 @@ static int iscsi_command ( struct scsi_device *scsi, struct iscsi_device *iscsidev = container_of ( scsi, struct iscsi_device, scsi ); struct async async; - int rc; - async_init_orphan ( &async ); - if ( ( rc = iscsi_issue ( &iscsidev->iscsi, command, &async ) ) != 0 ) - return rc; - async_wait ( &async, &rc, 1 ); - return rc; + return async_block ( &async, iscsi_issue ( &iscsidev->iscsi, command, + &async ) ); } /** @@ -56,10 +53,13 @@ int init_iscsidev ( struct iscsi_device *iscsidev ) { iscsidev->scsi.command = iscsi_command; iscsidev->scsi.lun = iscsidev->iscsi.lun; - rc = init_scsidev ( &iscsidev->scsi ); - if ( rc != 0 ) { - fini_iscsidev ( iscsidev ); - } + if ( ( rc = init_scsidev ( &iscsidev->scsi ) ) != 0 ) + goto err; + + return 0; + + err: + fini_iscsidev ( iscsidev ); return rc; } diff --git a/src/include/gpxe/async.h b/src/include/gpxe/async.h index d3b075b9..b1ca1a1c 100644 --- a/src/include/gpxe/async.h +++ b/src/include/gpxe/async.h @@ -167,4 +167,40 @@ static inline aid_t async_init_orphan ( struct async *async ) { return async_init ( async, &orphan_async_operations, NULL ); } +/** + * Execute and block on an asynchronous operation + * + * @v async_temp Temporary asynchronous operation structure to use + * @v START Code used to start the asynchronous operation + * @ret rc Return status code + * + * This is a notational shorthand for writing + * + * async_init_orphan ( &async_temp ); + * if ( ( rc = START ) == 0 ) + * async_wait ( &async_temp ); + * if ( rc != 0 ) { + * ...handle failure... + * } + * + * and allows you instead to write + * + * if ( ( rc = async_block ( &async_temp, START ) ) != 0 ) { + * ...handle failure... + * } + * + * The argument START is a code snippet; it should initiate an + * asynchronous operation as a child of @c async_temp and return an + * error status code if it failed to do so (e.g. due to malloc() + * failure). + */ +#define async_block( async_temp, START ) ( { \ + int rc; \ + \ + async_init_orphan ( async_temp ); \ + if ( ( rc = START ) == 0 ) \ + async_wait ( async_temp, &rc, 1 ); \ + rc; \ + } ) + #endif /* _GPXE_ASYNC_H */ diff --git a/src/usr/dhcpmgmt.c b/src/usr/dhcpmgmt.c index 90ed62bf..6f030274 100644 --- a/src/usr/dhcpmgmt.c +++ b/src/usr/dhcpmgmt.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -65,13 +66,8 @@ int dhcp ( struct net_device *netdev ) { printf ( "DHCP (%s %s)...", netdev->name, netdev_hwaddr ( netdev ) ); memset ( &dhcp, 0, sizeof ( dhcp ) ); dhcp.netdev = netdev; - async_init_orphan ( &async ); - if ( ( rc = start_dhcp ( &dhcp, &async ) ) != 0 ) { - printf ( "could not start (%s)\n", strerror ( rc ) ); - return rc; - } - async_wait ( &async, &rc, 1 ); - if ( rc != 0 ) { + if ( ( rc = async_block ( &async, + start_dhcp ( &dhcp, &async ) ) ) != 0 ) { printf ( "failed (%s)\n", strerror ( rc ) ); return rc; } diff --git a/src/usr/fetch.c b/src/usr/fetch.c index 45031049..11197e9f 100644 --- a/src/usr/fetch.c +++ b/src/usr/fetch.c @@ -86,11 +86,8 @@ int fetch ( const char *uri_string, userptr_t *data, size_t *len ) { } } - async_init_orphan ( &async ); - if ( ( rc = download ( uri, &buffer, &async ) ) != 0 ) - goto err; - async_wait ( &async, &rc, 1 ); - if ( rc != 0 ) + if ( ( rc = async_block ( &async, + download ( uri, &buffer, &async ) ) ) != 0 ) goto err; /* Fill in buffer address and length */