2
0

qemu-coroutine-io.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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-common.h"
  26. #include "qemu_socket.h"
  27. #include "qemu-coroutine.h"
  28. int coroutine_fn qemu_co_recvv(int sockfd, struct iovec *iov,
  29. int len, int iov_offset)
  30. {
  31. int total = 0;
  32. int ret;
  33. while (len) {
  34. ret = qemu_recvv(sockfd, iov, len, iov_offset + total);
  35. if (ret < 0) {
  36. if (errno == EAGAIN) {
  37. qemu_coroutine_yield();
  38. continue;
  39. }
  40. if (total == 0) {
  41. total = -1;
  42. }
  43. break;
  44. }
  45. if (ret == 0) {
  46. break;
  47. }
  48. total += ret, len -= ret;
  49. }
  50. return total;
  51. }
  52. int coroutine_fn qemu_co_sendv(int sockfd, struct iovec *iov,
  53. int len, int iov_offset)
  54. {
  55. int total = 0;
  56. int ret;
  57. while (len) {
  58. ret = qemu_sendv(sockfd, iov, len, iov_offset + total);
  59. if (ret < 0) {
  60. if (errno == EAGAIN) {
  61. qemu_coroutine_yield();
  62. continue;
  63. }
  64. if (total == 0) {
  65. total = -1;
  66. }
  67. break;
  68. }
  69. total += ret, len -= ret;
  70. }
  71. return total;
  72. }
  73. int coroutine_fn qemu_co_recv(int sockfd, void *buf, int len)
  74. {
  75. struct iovec iov;
  76. iov.iov_base = buf;
  77. iov.iov_len = len;
  78. return qemu_co_recvv(sockfd, &iov, len, 0);
  79. }
  80. int coroutine_fn qemu_co_send(int sockfd, void *buf, int len)
  81. {
  82. struct iovec iov;
  83. iov.iov_base = buf;
  84. iov.iov_len = len;
  85. return qemu_co_sendv(sockfd, &iov, len, 0);
  86. }