浏览代码

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.
tags/v0.9.3
Michael Brown 18 年前
父节点
当前提交
ff8528ea9a
共有 5 个文件被更改,包括 54 次插入28 次删除
  1. 3
    6
      src/drivers/ata/aoedev.c
  2. 10
    10
      src/drivers/scsi/iscsidev.c
  3. 36
    0
      src/include/gpxe/async.h
  4. 3
    7
      src/usr/dhcpmgmt.c
  5. 2
    5
      src/usr/fetch.c

+ 3
- 6
src/drivers/ata/aoedev.c 查看文件

@@ -17,6 +17,7 @@
17 17
  */
18 18
 
19 19
 #include <stddef.h>
20
+#include <gpxe/async.h>
20 21
 #include <gpxe/aoe.h>
21 22
 
22 23
 /** @file
@@ -37,13 +38,9 @@ static int aoe_command ( struct ata_device *ata,
37 38
 	struct aoe_device *aoedev
38 39
 		= container_of ( ata, struct aoe_device, ata );
39 40
 	struct async async;
40
-	int rc;
41 41
 
42
-	async_init_orphan ( &async );
43
-	if ( ( rc = aoe_issue ( &aoedev->aoe, command, &async ) ) != 0 )
44
-		return rc;
45
-	async_wait ( &async, &rc, 1 );
46
-	return rc;
42
+	return async_block ( &async, aoe_issue ( &aoedev->aoe, command,
43
+						 &async ) );
47 44
 }
48 45
 
49 46
 /**

+ 10
- 10
src/drivers/scsi/iscsidev.c 查看文件

@@ -17,6 +17,7 @@
17 17
  */
18 18
 
19 19
 #include <stddef.h>
20
+#include <gpxe/async.h>
20 21
 #include <gpxe/iscsi.h>
21 22
 
22 23
 /** @file
@@ -37,13 +38,9 @@ static int iscsi_command ( struct scsi_device *scsi,
37 38
 	struct iscsi_device *iscsidev
38 39
 		= container_of ( scsi, struct iscsi_device, scsi );
39 40
 	struct async async;
40
-	int rc;
41 41
 
42
-	async_init_orphan ( &async );
43
-	if ( ( rc = iscsi_issue ( &iscsidev->iscsi, command, &async ) ) != 0 )
44
-		return rc;
45
-	async_wait ( &async, &rc, 1 );
46
-	return rc;
42
+	return async_block ( &async, iscsi_issue ( &iscsidev->iscsi, command,
43
+						   &async ) );
47 44
 }
48 45
 
49 46
 /**
@@ -56,10 +53,13 @@ int init_iscsidev ( struct iscsi_device *iscsidev ) {
56 53
 
57 54
 	iscsidev->scsi.command = iscsi_command;
58 55
 	iscsidev->scsi.lun = iscsidev->iscsi.lun;
59
-	rc = init_scsidev ( &iscsidev->scsi );
60
-	if ( rc != 0 ) {
61
-		fini_iscsidev ( iscsidev );
62
-	}
56
+	if ( ( rc = init_scsidev ( &iscsidev->scsi ) ) != 0 )
57
+		goto err;
58
+
59
+	return 0;
60
+
61
+ err:
62
+	fini_iscsidev ( iscsidev );
63 63
 	return rc;
64 64
 }
65 65
 

+ 36
- 0
src/include/gpxe/async.h 查看文件

@@ -167,4 +167,40 @@ static inline aid_t async_init_orphan ( struct async *async ) {
167 167
 	return async_init ( async, &orphan_async_operations, NULL );
168 168
 }
169 169
 
170
+/**
171
+ * Execute and block on an asynchronous operation
172
+ *
173
+ * @v async_temp	Temporary asynchronous operation structure to use
174
+ * @v START		Code used to start the asynchronous operation
175
+ * @ret rc		Return status code
176
+ *
177
+ * This is a notational shorthand for writing
178
+ *
179
+ *     	async_init_orphan ( &async_temp );
180
+ *	if ( ( rc = START ) == 0 )
181
+ *		async_wait ( &async_temp );
182
+ *      if ( rc != 0 ) {
183
+ *         ...handle failure...
184
+ *      }
185
+ *
186
+ * and allows you instead to write
187
+ *
188
+ *      if ( ( rc = async_block ( &async_temp, START ) ) != 0 ) {
189
+ *         ...handle failure...
190
+ *      }
191
+ *
192
+ * The argument START is a code snippet; it should initiate an
193
+ * asynchronous operation as a child of @c async_temp and return an
194
+ * error status code if it failed to do so (e.g. due to malloc()
195
+ * failure).
196
+ */
197
+#define async_block( async_temp, START ) ( {			\
198
+		int rc;						\
199
+								\
200
+	 	async_init_orphan ( async_temp );		\
201
+		if ( ( rc = START ) == 0 )			\
202
+			async_wait ( async_temp, &rc, 1 );	\
203
+		rc;						\
204
+	} )
205
+
170 206
 #endif /* _GPXE_ASYNC_H */

+ 3
- 7
src/usr/dhcpmgmt.c 查看文件

@@ -20,6 +20,7 @@
20 20
 #include <byteswap.h>
21 21
 #include <vsprintf.h>
22 22
 #include <gpxe/in.h>
23
+#include <gpxe/ip.h>
23 24
 #include <gpxe/dhcp.h>
24 25
 #include <gpxe/async.h>
25 26
 #include <gpxe/netdevice.h>
@@ -65,13 +66,8 @@ int dhcp ( struct net_device *netdev ) {
65 66
 	printf ( "DHCP (%s %s)...", netdev->name, netdev_hwaddr ( netdev ) );
66 67
 	memset ( &dhcp, 0, sizeof ( dhcp ) );
67 68
 	dhcp.netdev = netdev;
68
-	async_init_orphan ( &async );
69
-	if ( ( rc = start_dhcp ( &dhcp, &async ) ) != 0 ) {
70
-		printf ( "could not start (%s)\n", strerror ( rc ) );
71
-		return rc;
72
-	}
73
-	async_wait ( &async, &rc, 1 );	
74
-	if ( rc != 0 ) {
69
+	if ( ( rc = async_block ( &async,
70
+				  start_dhcp ( &dhcp, &async ) ) ) != 0 ) {
75 71
 		printf ( "failed (%s)\n", strerror ( rc ) );
76 72
 		return rc;
77 73
 	}

+ 2
- 5
src/usr/fetch.c 查看文件

@@ -86,11 +86,8 @@ int fetch ( const char *uri_string, userptr_t *data, size_t *len ) {
86 86
 		}
87 87
 	}
88 88
 
89
-	async_init_orphan ( &async );
90
-	if ( ( rc = download ( uri, &buffer, &async ) ) != 0 )
91
-		goto err;
92
-	async_wait ( &async, &rc, 1 );
93
-	if ( rc != 0 )
89
+	if ( ( rc = async_block ( &async,
90
+				  download ( uri, &buffer, &async ) ) )  != 0 )
94 91
 		goto err;
95 92
 
96 93
 	/* Fill in buffer address and length */

正在加载...
取消
保存