2
0

aio-win32.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * QEMU aio implementation
  3. *
  4. * Copyright IBM Corp., 2008
  5. * Copyright Red Hat Inc., 2012
  6. *
  7. * Authors:
  8. * Anthony Liguori <aliguori@us.ibm.com>
  9. * Paolo Bonzini <pbonzini@redhat.com>
  10. *
  11. * This work is licensed under the terms of the GNU GPL, version 2. See
  12. * the COPYING file in the top-level directory.
  13. *
  14. * Contributions after 2012-01-13 are licensed under the terms of the
  15. * GNU GPL, version 2 or (at your option) any later version.
  16. */
  17. #include "qemu-common.h"
  18. #include "block/block.h"
  19. #include "qemu/queue.h"
  20. #include "qemu/sockets.h"
  21. struct AioHandler {
  22. EventNotifier *e;
  23. IOHandler *io_read;
  24. IOHandler *io_write;
  25. EventNotifierHandler *io_notify;
  26. GPollFD pfd;
  27. int deleted;
  28. void *opaque;
  29. QLIST_ENTRY(AioHandler) node;
  30. };
  31. void aio_set_fd_handler(AioContext *ctx,
  32. int fd,
  33. IOHandler *io_read,
  34. IOHandler *io_write,
  35. void *opaque)
  36. {
  37. /* fd is a SOCKET in our case */
  38. AioHandler *node;
  39. QLIST_FOREACH(node, &ctx->aio_handlers, node) {
  40. if (node->pfd.fd == fd && !node->deleted) {
  41. break;
  42. }
  43. }
  44. /* Are we deleting the fd handler? */
  45. if (!io_read && !io_write) {
  46. if (node) {
  47. /* If the lock is held, just mark the node as deleted */
  48. if (ctx->walking_handlers) {
  49. node->deleted = 1;
  50. node->pfd.revents = 0;
  51. } else {
  52. /* Otherwise, delete it for real. We can't just mark it as
  53. * deleted because deleted nodes are only cleaned up after
  54. * releasing the walking_handlers lock.
  55. */
  56. QLIST_REMOVE(node, node);
  57. g_free(node);
  58. }
  59. }
  60. } else {
  61. HANDLE event;
  62. if (node == NULL) {
  63. /* Alloc and insert if it's not already there */
  64. node = g_malloc0(sizeof(AioHandler));
  65. node->pfd.fd = fd;
  66. QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
  67. }
  68. node->pfd.events = 0;
  69. if (node->io_read) {
  70. node->pfd.events |= G_IO_IN;
  71. }
  72. if (node->io_write) {
  73. node->pfd.events |= G_IO_OUT;
  74. }
  75. node->e = &ctx->notifier;
  76. /* Update handler with latest information */
  77. node->opaque = opaque;
  78. node->io_read = io_read;
  79. node->io_write = io_write;
  80. event = event_notifier_get_handle(&ctx->notifier);
  81. WSAEventSelect(node->pfd.fd, event,
  82. FD_READ | FD_ACCEPT | FD_CLOSE |
  83. FD_CONNECT | FD_WRITE | FD_OOB);
  84. }
  85. aio_notify(ctx);
  86. }
  87. void aio_set_event_notifier(AioContext *ctx,
  88. EventNotifier *e,
  89. EventNotifierHandler *io_notify)
  90. {
  91. AioHandler *node;
  92. QLIST_FOREACH(node, &ctx->aio_handlers, node) {
  93. if (node->e == e && !node->deleted) {
  94. break;
  95. }
  96. }
  97. /* Are we deleting the fd handler? */
  98. if (!io_notify) {
  99. if (node) {
  100. g_source_remove_poll(&ctx->source, &node->pfd);
  101. /* If the lock is held, just mark the node as deleted */
  102. if (ctx->walking_handlers) {
  103. node->deleted = 1;
  104. node->pfd.revents = 0;
  105. } else {
  106. /* Otherwise, delete it for real. We can't just mark it as
  107. * deleted because deleted nodes are only cleaned up after
  108. * releasing the walking_handlers lock.
  109. */
  110. QLIST_REMOVE(node, node);
  111. g_free(node);
  112. }
  113. }
  114. } else {
  115. if (node == NULL) {
  116. /* Alloc and insert if it's not already there */
  117. node = g_malloc0(sizeof(AioHandler));
  118. node->e = e;
  119. node->pfd.fd = (uintptr_t)event_notifier_get_handle(e);
  120. node->pfd.events = G_IO_IN;
  121. QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
  122. g_source_add_poll(&ctx->source, &node->pfd);
  123. }
  124. /* Update handler with latest information */
  125. node->io_notify = io_notify;
  126. }
  127. aio_notify(ctx);
  128. }
  129. bool aio_prepare(AioContext *ctx)
  130. {
  131. static struct timeval tv0;
  132. AioHandler *node;
  133. bool have_select_revents = false;
  134. fd_set rfds, wfds;
  135. /* fill fd sets */
  136. FD_ZERO(&rfds);
  137. FD_ZERO(&wfds);
  138. QLIST_FOREACH(node, &ctx->aio_handlers, node) {
  139. if (node->io_read) {
  140. FD_SET ((SOCKET)node->pfd.fd, &rfds);
  141. }
  142. if (node->io_write) {
  143. FD_SET ((SOCKET)node->pfd.fd, &wfds);
  144. }
  145. }
  146. if (select(0, &rfds, &wfds, NULL, &tv0) > 0) {
  147. QLIST_FOREACH(node, &ctx->aio_handlers, node) {
  148. node->pfd.revents = 0;
  149. if (FD_ISSET(node->pfd.fd, &rfds)) {
  150. node->pfd.revents |= G_IO_IN;
  151. have_select_revents = true;
  152. }
  153. if (FD_ISSET(node->pfd.fd, &wfds)) {
  154. node->pfd.revents |= G_IO_OUT;
  155. have_select_revents = true;
  156. }
  157. }
  158. }
  159. return have_select_revents;
  160. }
  161. bool aio_pending(AioContext *ctx)
  162. {
  163. AioHandler *node;
  164. QLIST_FOREACH(node, &ctx->aio_handlers, node) {
  165. if (node->pfd.revents && node->io_notify) {
  166. return true;
  167. }
  168. if ((node->pfd.revents & G_IO_IN) && node->io_read) {
  169. return true;
  170. }
  171. if ((node->pfd.revents & G_IO_OUT) && node->io_write) {
  172. return true;
  173. }
  174. }
  175. return false;
  176. }
  177. static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event)
  178. {
  179. AioHandler *node;
  180. bool progress = false;
  181. /*
  182. * We have to walk very carefully in case aio_set_fd_handler is
  183. * called while we're walking.
  184. */
  185. node = QLIST_FIRST(&ctx->aio_handlers);
  186. while (node) {
  187. AioHandler *tmp;
  188. int revents = node->pfd.revents;
  189. ctx->walking_handlers++;
  190. if (!node->deleted &&
  191. (revents || event_notifier_get_handle(node->e) == event) &&
  192. node->io_notify) {
  193. node->pfd.revents = 0;
  194. node->io_notify(node->e);
  195. /* aio_notify() does not count as progress */
  196. if (node->e != &ctx->notifier) {
  197. progress = true;
  198. }
  199. }
  200. if (!node->deleted &&
  201. (node->io_read || node->io_write)) {
  202. node->pfd.revents = 0;
  203. if ((revents & G_IO_IN) && node->io_read) {
  204. node->io_read(node->opaque);
  205. progress = true;
  206. }
  207. if ((revents & G_IO_OUT) && node->io_write) {
  208. node->io_write(node->opaque);
  209. progress = true;
  210. }
  211. /* if the next select() will return an event, we have progressed */
  212. if (event == event_notifier_get_handle(&ctx->notifier)) {
  213. WSANETWORKEVENTS ev;
  214. WSAEnumNetworkEvents(node->pfd.fd, event, &ev);
  215. if (ev.lNetworkEvents) {
  216. progress = true;
  217. }
  218. }
  219. }
  220. tmp = node;
  221. node = QLIST_NEXT(node, node);
  222. ctx->walking_handlers--;
  223. if (!ctx->walking_handlers && tmp->deleted) {
  224. QLIST_REMOVE(tmp, node);
  225. g_free(tmp);
  226. }
  227. }
  228. return progress;
  229. }
  230. bool aio_dispatch(AioContext *ctx)
  231. {
  232. bool progress;
  233. progress = aio_bh_poll(ctx);
  234. progress |= aio_dispatch_handlers(ctx, INVALID_HANDLE_VALUE);
  235. progress |= timerlistgroup_run_timers(&ctx->tlg);
  236. return progress;
  237. }
  238. bool aio_poll(AioContext *ctx, bool blocking)
  239. {
  240. AioHandler *node;
  241. HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
  242. bool was_dispatching, progress, have_select_revents, first;
  243. int count;
  244. int timeout;
  245. have_select_revents = aio_prepare(ctx);
  246. if (have_select_revents) {
  247. blocking = false;
  248. }
  249. was_dispatching = ctx->dispatching;
  250. progress = false;
  251. /* aio_notify can avoid the expensive event_notifier_set if
  252. * everything (file descriptors, bottom halves, timers) will
  253. * be re-evaluated before the next blocking poll(). This is
  254. * already true when aio_poll is called with blocking == false;
  255. * if blocking == true, it is only true after poll() returns.
  256. *
  257. * If we're in a nested event loop, ctx->dispatching might be true.
  258. * In that case we can restore it just before returning, but we
  259. * have to clear it now.
  260. */
  261. aio_set_dispatching(ctx, !blocking);
  262. ctx->walking_handlers++;
  263. /* fill fd sets */
  264. count = 0;
  265. QLIST_FOREACH(node, &ctx->aio_handlers, node) {
  266. if (!node->deleted && node->io_notify) {
  267. events[count++] = event_notifier_get_handle(node->e);
  268. }
  269. }
  270. ctx->walking_handlers--;
  271. first = true;
  272. /* wait until next event */
  273. while (count > 0) {
  274. HANDLE event;
  275. int ret;
  276. timeout = blocking
  277. ? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx)) : 0;
  278. ret = WaitForMultipleObjects(count, events, FALSE, timeout);
  279. aio_set_dispatching(ctx, true);
  280. if (first && aio_bh_poll(ctx)) {
  281. progress = true;
  282. }
  283. first = false;
  284. /* if we have any signaled events, dispatch event */
  285. event = NULL;
  286. if ((DWORD) (ret - WAIT_OBJECT_0) < count) {
  287. event = events[ret - WAIT_OBJECT_0];
  288. events[ret - WAIT_OBJECT_0] = events[--count];
  289. } else if (!have_select_revents) {
  290. break;
  291. }
  292. have_select_revents = false;
  293. blocking = false;
  294. progress |= aio_dispatch_handlers(ctx, event);
  295. }
  296. progress |= timerlistgroup_run_timers(&ctx->tlg);
  297. aio_set_dispatching(ctx, was_dispatching);
  298. return progress;
  299. }