block-coroutine-wrapper.rst 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. =======================
  2. block-coroutine-wrapper
  3. =======================
  4. A lot of functions in QEMU block layer (see ``block/*``) can only be
  5. called in coroutine context. Such functions are normally marked by the
  6. coroutine_fn specifier. Still, sometimes we need to call them from
  7. non-coroutine context; for this we need to start a coroutine, run the
  8. needed function from it and wait for the coroutine to finish in a
  9. BDRV_POLL_WHILE() loop. To run a coroutine we need a function with one
  10. void* argument. So for each coroutine_fn function which needs a
  11. non-coroutine interface, we should define a structure to pack the
  12. parameters, define a separate function to unpack the parameters and
  13. call the original function and finally define a new interface function
  14. with same list of arguments as original one, which will pack the
  15. parameters into a struct, create a coroutine, run it and wait in
  16. BDRV_POLL_WHILE() loop. It's boring to create such wrappers by hand,
  17. so we have a script to generate them.
  18. Usage
  19. =====
  20. Assume we have defined the ``coroutine_fn`` function
  21. ``bdrv_co_foo(<some args>)`` and need a non-coroutine interface for it,
  22. called ``bdrv_foo(<same args>)``. In this case the script can help. To
  23. trigger the generation:
  24. 1. You need ``bdrv_foo`` declaration somewhere (for example, in
  25. ``block/coroutines.h``) with the ``co_wrapper`` mark,
  26. like this:
  27. .. code-block:: c
  28. int co_wrapper bdrv_foo(<some args>);
  29. 2. You need to feed this declaration to block-coroutine-wrapper script.
  30. For this, add the .h (or .c) file with the declaration to the
  31. ``input: files(...)`` list of ``block_gen_c`` target declaration in
  32. ``block/meson.build``
  33. You are done. During the build, coroutine wrappers will be generated in
  34. ``<BUILD_DIR>/block/block-gen.c``.
  35. Links
  36. =====
  37. 1. The script location is ``scripts/block-coroutine-wrapper.py``.
  38. 2. Generic place for private ``co_wrapper`` declarations is
  39. ``block/coroutines.h``, for public declarations:
  40. ``include/block/block.h``
  41. 3. The core API of generated coroutine wrappers is placed in
  42. (not generated) ``block/block-gen.h``