qemu-coroutine-io.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Coroutine-aware I/O functions
  3. *
  4. * Copyright (C) 2009-2010 Nippon Telegraph and Telephone Corporation.
  5. * Copyright (c) 2011, Red Hat, Inc.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "qemu/sockets.h"
  27. #include "qemu/coroutine.h"
  28. #include "qemu/iov.h"
  29. #include "qemu/main-loop.h"
  30. ssize_t coroutine_fn
  31. qemu_co_sendv_recvv(int sockfd, struct iovec *iov, unsigned iov_cnt,
  32. size_t offset, size_t bytes, bool do_send)
  33. {
  34. size_t done = 0;
  35. ssize_t ret;
  36. while (done < bytes) {
  37. ret = iov_send_recv(sockfd, iov, iov_cnt,
  38. offset + done, bytes - done, do_send);
  39. if (ret > 0) {
  40. done += ret;
  41. } else if (ret < 0) {
  42. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  43. qemu_coroutine_yield();
  44. } else if (done == 0) {
  45. return -errno;
  46. } else {
  47. break;
  48. }
  49. } else if (ret == 0 && !do_send) {
  50. /* write (send) should never return 0.
  51. * read (recv) returns 0 for end-of-file (-data).
  52. * In both cases there's little point retrying,
  53. * but we do for write anyway, just in case */
  54. break;
  55. }
  56. }
  57. return done;
  58. }
  59. ssize_t coroutine_fn
  60. qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send)
  61. {
  62. struct iovec iov = { .iov_base = buf, .iov_len = bytes };
  63. return qemu_co_sendv_recvv(sockfd, &iov, 1, 0, bytes, do_send);
  64. }
  65. typedef struct {
  66. AioContext *ctx;
  67. Coroutine *co;
  68. int fd;
  69. } FDYieldUntilData;
  70. static void fd_coroutine_enter(void *opaque)
  71. {
  72. FDYieldUntilData *data = opaque;
  73. aio_set_fd_handler(data->ctx, data->fd, false,
  74. NULL, NULL, NULL, NULL, NULL);
  75. qemu_coroutine_enter(data->co);
  76. }
  77. void coroutine_fn yield_until_fd_readable(int fd)
  78. {
  79. FDYieldUntilData data;
  80. assert(qemu_in_coroutine());
  81. data.ctx = qemu_get_current_aio_context();
  82. data.co = qemu_coroutine_self();
  83. data.fd = fd;
  84. aio_set_fd_handler(
  85. data.ctx, fd, false, fd_coroutine_enter, NULL, NULL, NULL, &data);
  86. qemu_coroutine_yield();
  87. }