Browse Source

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 years ago
parent
commit
ff8528ea9a
5 changed files with 54 additions and 28 deletions
  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 View File

17
  */
17
  */
18
 
18
 
19
 #include <stddef.h>
19
 #include <stddef.h>
20
+#include <gpxe/async.h>
20
 #include <gpxe/aoe.h>
21
 #include <gpxe/aoe.h>
21
 
22
 
22
 /** @file
23
 /** @file
37
 	struct aoe_device *aoedev
38
 	struct aoe_device *aoedev
38
 		= container_of ( ata, struct aoe_device, ata );
39
 		= container_of ( ata, struct aoe_device, ata );
39
 	struct async async;
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 View File

17
  */
17
  */
18
 
18
 
19
 #include <stddef.h>
19
 #include <stddef.h>
20
+#include <gpxe/async.h>
20
 #include <gpxe/iscsi.h>
21
 #include <gpxe/iscsi.h>
21
 
22
 
22
 /** @file
23
 /** @file
37
 	struct iscsi_device *iscsidev
38
 	struct iscsi_device *iscsidev
38
 		= container_of ( scsi, struct iscsi_device, scsi );
39
 		= container_of ( scsi, struct iscsi_device, scsi );
39
 	struct async async;
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
 
53
 
57
 	iscsidev->scsi.command = iscsi_command;
54
 	iscsidev->scsi.command = iscsi_command;
58
 	iscsidev->scsi.lun = iscsidev->iscsi.lun;
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
 	return rc;
63
 	return rc;
64
 }
64
 }
65
 
65
 

+ 36
- 0
src/include/gpxe/async.h View File

167
 	return async_init ( async, &orphan_async_operations, NULL );
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
 #endif /* _GPXE_ASYNC_H */
206
 #endif /* _GPXE_ASYNC_H */

+ 3
- 7
src/usr/dhcpmgmt.c View File

20
 #include <byteswap.h>
20
 #include <byteswap.h>
21
 #include <vsprintf.h>
21
 #include <vsprintf.h>
22
 #include <gpxe/in.h>
22
 #include <gpxe/in.h>
23
+#include <gpxe/ip.h>
23
 #include <gpxe/dhcp.h>
24
 #include <gpxe/dhcp.h>
24
 #include <gpxe/async.h>
25
 #include <gpxe/async.h>
25
 #include <gpxe/netdevice.h>
26
 #include <gpxe/netdevice.h>
65
 	printf ( "DHCP (%s %s)...", netdev->name, netdev_hwaddr ( netdev ) );
66
 	printf ( "DHCP (%s %s)...", netdev->name, netdev_hwaddr ( netdev ) );
66
 	memset ( &dhcp, 0, sizeof ( dhcp ) );
67
 	memset ( &dhcp, 0, sizeof ( dhcp ) );
67
 	dhcp.netdev = netdev;
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
 		printf ( "failed (%s)\n", strerror ( rc ) );
71
 		printf ( "failed (%s)\n", strerror ( rc ) );
76
 		return rc;
72
 		return rc;
77
 	}
73
 	}

+ 2
- 5
src/usr/fetch.c View File

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
 		goto err;
91
 		goto err;
95
 
92
 
96
 	/* Fill in buffer address and length */
93
 	/* Fill in buffer address and length */

Loading…
Cancel
Save