2
0
mirror of https://github.com/xcat2/xNBA.git synced 2024-12-14 23:31:39 +00:00

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.
This commit is contained in:
Michael Brown 2007-01-16 08:10:54 +00:00
parent f11da20f25
commit ff8528ea9a
5 changed files with 54 additions and 28 deletions

View File

@ -17,6 +17,7 @@
*/
#include <stddef.h>
#include <gpxe/async.h>
#include <gpxe/aoe.h>
/** @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 ) );
}
/**

View File

@ -17,6 +17,7 @@
*/
#include <stddef.h>
#include <gpxe/async.h>
#include <gpxe/iscsi.h>
/** @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;
}

View File

@ -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 */

View File

@ -20,6 +20,7 @@
#include <byteswap.h>
#include <vsprintf.h>
#include <gpxe/in.h>
#include <gpxe/ip.h>
#include <gpxe/dhcp.h>
#include <gpxe/async.h>
#include <gpxe/netdevice.h>
@ -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;
}

View File

@ -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 */