coroutines: generate wrapper code
RFC PATCH v2 1/1 coroutines: generate wrapper code
Hi all!
We have a very frequent pattern of creating coroutine from function with several arguments:
- create structure to pack parameters
- create _entry function to call original function taking parameters from struct
- do different magic to handle completion: set ret to NOT_DONE or EINPROGRESS, use separate bool for void functions
- fill the struct and create coroutine from _entry function and this struct as a parameter
Here is a template code + example how it can be used to drop a lot of similar code.
TODO: make coroutine-wrapper template file to be header itself, or
generate header from it
---
v2: - don't generate poll-loop wrappers
- drop jinja
Makefile | 5 ++ Makefile.objs | 2 +- include/block/block.h | 3 ++ include/block/block_int.h | 8 +++ block.c | 76 +++++++--------------------- coroutine-wrapper | 2 + scripts/coroutine-wrapper.py | 98 ++++++++++++++++++++++++++++++++++++ 7 files changed, 136 insertions(+), 58 deletions(-) create mode 100644 coroutine-wrapper create mode 100755 scripts/coroutine-wrapper.py
Makefile +5 -0
@@ -122,6 +122,8 @@ endif GENERATED_FILES += module_block.h +GENERATED_FILES += block-gen.c + TRACE_HEADERS = trace-root.h $(trace-events-subdirs:%=%/trace.h) TRACE_SOURCES = trace-root.c $(trace-events-subdirs:%=%/trace.c) TRACE_DTRACE =
@@ -138,6 +140,9 @@ GENERATED_FILES += $(TRACE_SOURCES) GENERATED_FILES += $(BUILD_DIR)/trace-events-all GENERATED_FILES += .git-submodule-status +block-gen.c: coroutine-wrapper $(SRC_PATH)/scripts/coroutine-wrapper.py + $(call quiet-command, python $(SRC_PATH)/scripts/coroutine-wrapper.py < $< > $@,"GEN","$(TARGET_DIR)$@") + trace-group-name = $(shell dirname $1 | sed -e 's/[^a-zA-Z0-9]/_/g') tracetool-y = $(SRC_PATH)/scripts/tracetool.py
Makefile.objs +1 -1
@@ -23,7 +23,7 @@ slirp-obj-$(CONFIG_SLIRP) = slirp/ # block-obj-y is code used by both qemu system emulation and qemu-img block-obj-y += nbd/ -block-obj-y += block.o blockjob.o job.o +block-obj-y += block.o blockjob.o job.o block-gen.o block-obj-y += block/ scsi/ block-obj-y += qemu-io-cmds.o block-obj-$(CONFIG_REPLICATION) += replication.o
include/block/block.h +3 -0
@@ -371,6 +371,8 @@ typedef enum { } BdrvCheckMode;int bdrv_check (BlockDriverState *bs,BdrvCheckResult *res,BdrvCheckMode fix); +int coroutine_fn bdrv_co_check(BlockDriverState *bs, +BdrvCheckResult *res,BdrvCheckMode fix);/* The units of offset and total_work_size may be chosen arbitrarily by the * block driver; total_work_size may change during the course of the amendment
@@ -399,6 +401,7 @@ int bdrv_co_ioctl(BlockDriverState *bs, int req, void *buf);/* Invalidate any cached metadata used by image formats */ void bdrv_invalidate_cache (BlockDriverState *bs,Error **errp); +void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp);void bdrv_invalidate_cache_all (Error **errp);int bdrv_inactivate_all (void );
include/block/block_int.h +8 -0
@@ -1175,4 +1175,12 @@ int coroutine_fn bdrv_co_copy_range_to(BdrvChild *src, uint64_t src_offset,int refresh_total_sectors (BlockDriverState *bs,int64_t hint); + +Coroutine *bdrv_co_invalidate_cache__create_co (bool *_in_progress, +BlockDriverState *bs, +Error **errp); +Coroutine *bdrv_co_check__create_co (bool *_in_progress,int *_ret, +BlockDriverState *bs,BdrvCheckResult *res, +BdrvCheckMode fix); +#endif /* BLOCK_INT_H */
block.c +19 -57
116 lines not on this page · raw message
coroutine-wrapper new file +2 -0
@@ -0,0 +1,2 @@ +int bdrv_co_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix) +void bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp)
scripts/coroutine-wrapper.py new file +98 -0
98 lines not on this page · raw message
Signature · 1 line
2.18.0
Quoted 13 lines · Vladimir Sementsov-Ogievskiy
Hi all!
We have a very frequent pattern of creating coroutine from function with several arguments:
- create structure to pack parameters
- create_entryfunction to call original function taking parameters from struct
- do different magic to handle completion: set ret toNOT_DONEor EINPROGRESS, use separate bool for void functions
- fill the struct and create coroutine from_entryfunction and this struct as a parameter
Quoted 5 lines
Here is a template code + example how it can be used to drop a lot of similar code.
TODO: make coroutine-wrapper template file to be header itself, or
generate header from it
Yes, please. For example, block/coroutines.h:
#include "block/block.h"
int coroutine_fn bdrv_co_check(BlockDriverState *bs,
BdrvCheckResult *res, BdrvCheckMode fix);
void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp);
Now include/block/block.h doesn't need to be modified and these coroutine function prototypes stay private to the block layer (they are not in include/).
The code generator script can parse coroutines.h to generate __create_co() functions. This also makes the tool independent of the block layer - the output C file just needs to include the input header file:
/*
* File is generated by scripts/coroutine-wrapper.py */
#include "qemu/osdep.h"
#include "block/coroutines.h" <--- from coroutine-wrapper command-line
...
Patchew URL: https://patchew.org/QEMU/[email protected]/
Patchew URL: https://patchew.org/QEMU/[email protected]/
Hi,
This series failed the docker-mingw@fedora build test. Please find the testing commands and their output below. If you have Docker installed, you can probably reproduce it locally.
=== TEST SCRIPT BEGIN ===
#!/bin/bash
time make docker-test-mingw@fedora SHOW_ENV=1 J=14 NETWORK=1 === TEST SCRIPT END ===
The full log is available at
http://patchew.org/logs/[email protected]/testing.docker-mingw@fedora/?type=message.
---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to [email protected]