aio-win32.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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/osdep.h"
  18. #include "qemu-common.h"
  19. #include "block/block.h"
  20. #include "qemu/queue.h"
  21. #include "qemu/sockets.h"
  22. struct AioHandler {
  23. EventNotifier *e;
  24. IOHandler *io_read;
  25. IOHandler *io_write;
  26. EventNotifierHandler *io_notify;
  27. GPollFD pfd;
  28. int deleted;
  29. void *opaque;
  30. bool is_external;
  31. QLIST_ENTRY(AioHandler) node;
  32. };
  33. void aio_set_fd_handler(AioContext *ctx,
  34. int fd,
  35. bool is_external,
  36. IOHandler *io_read,
  37. IOHandler *io_write,
  38. void *opaque)
  39. {
  40. /* fd is a SOCKET in our case */
  41. AioHandler *node;
  42. QLIST_FOREACH(node, &ctx->aio_handlers, node) {
  43. if (node->pfd.fd == fd && !node->deleted) {
  44. break;
  45. }
  46. }
  47. /* Are we deleting the fd handler? */
  48. if (!io_read && !io_write) {
  49. if (node) {
  50. /* If the lock is held, just mark the node as deleted */
  51. if (ctx->walking_handlers) {
  52. node->deleted = 1;
  53. node->pfd.revents = 0;
  54. } else {
  55. /* Otherwise, delete it for real. We can't just mark it as
  56. * deleted because deleted nodes are only cleaned up after
  57. * releasing the walking_handlers lock.
  58. */
  59. QLIST_REMOVE(node, node);
  60. g_free(node);
  61. }
  62. }
  63. } else {
  64. HANDLE event;
  65. if (node == NULL) {
  66. /* Alloc and insert if it's not already there */
  67. node = g_new0(AioHandler, 1);
  68. node->pfd.fd = fd;
  69. QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
  70. }
  71. node->pfd.events = 0;
  72. if (node->io_read) {
  73. node->pfd.events |= G_IO_IN;
  74. }
  75. if (node->io_write) {
  76. node->pfd.events |= G_IO_OUT;
  77. }
  78. node->e = &ctx->notifier;
  79. /* Update handler with latest information */
  80. node->opaque = opaque;
  81. node->io_read = io_read;
  82. node->io_write = io_write;
  83. node->is_external = is_external;
  84. event = event_notifier_get_handle(&ctx->notifier);
  85. WSAEventSelect(node->pfd.fd, event,
  86. FD_READ | FD_ACCEPT | FD_CLOSE |
  87. FD_CONNECT | FD_WRITE | FD_OOB);
  88. }
  89. aio_notify(ctx);
  90. }
  91. void aio_set_event_notifier(AioContext *ctx,
  92. EventNotifier *e,
  93. bool is_external,
  94. EventNotifierHandler *io_notify)
  95. {
  96. AioHandler *node;
  97. QLIST_FOREACH(node, &ctx->aio_handlers, node) {
  98. if (node->e == e && !node->deleted) {
  99. break;
  100. }
  101. }
  102. /* Are we deleting the fd handler? */
  103. if (!io_notify) {
  104. if (node) {
  105. g_source_remove_poll(&ctx->source, &node->pfd);
  106. /* If the lock is held, just mark the node as deleted */
  107. if (ctx->walking_handlers) {
  108. node->deleted = 1;
  109. node->pfd.revents = 0;
  110. } else {
  111. /* Otherwise, delete it for real. We can't just mark it as
  112. * deleted because deleted nodes are only cleaned up after
  113. * releasing the walking_handlers lock.
  114. */
  115. QLIST_REMOVE(node, node);
  116. g_free(node);
  117. }
  118. }
  119. } else {
  120. if (node == NULL) {
  121. /* Alloc and insert if it's not already there */
  122. node = g_new0(AioHandler, 1);
  123. node->e = e;
  124. node->pfd.fd = (uintptr_t)event_notifier_get_handle(e);
  125. node->pfd.events = G_IO_IN;
  126. node->is_external = is_external;
  127. QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
  128. g_source_add_poll(&ctx->source, &node->pfd);
  129. }
  130. /* Update handler with latest information */
  131. node->io_notify = io_notify;
  132. }
  133. aio_notify(ctx);
  134. }
  135. bool aio_prepare(AioContext *ctx)
  136. {
  137. static struct timeval tv0;
  138. AioHandler *node;
  139. bool have_select_revents = false;
  140. fd_set rfds, wfds;
  141. /* fill fd sets */
  142. FD_ZERO(&rfds);
  143. FD_ZERO(&wfds);
  144. QLIST_FOREACH(node, &ctx->aio_handlers, node) {
  145. if (node->io_read) {
  146. FD_SET ((SOCKET)node->pfd.fd, &rfds);
  147. }
  148. if (node->io_write) {
  149. FD_SET ((SOCKET)node->pfd.fd, &wfds);
  150. }
  151. }
  152. if (select(0, &rfds, &wfds, NULL, &tv0) > 0) {
  153. QLIST_FOREACH(node, &ctx->aio_handlers, node) {
  154. node->pfd.revents = 0;
  155. if (FD_ISSET(node->pfd.fd, &rfds)) {
  156. node->pfd.revents |= G_IO_IN;
  157. have_select_revents = true;
  158. }
  159. if (FD_ISSET(node->pfd.fd, &wfds)) {
  160. node->pfd.revents |= G_IO_OUT;
  161. have_select_revents = true;
  162. }
  163. }
  164. }
  165. return have_select_revents;
  166. }
  167. bool aio_pending(AioContext *ctx)
  168. {
  169. AioHandler *node;
  170. QLIST_FOREACH(node, &ctx->aio_handlers, node) {
  171. if (node->pfd.revents && node->io_notify) {
  172. return true;
  173. }
  174. if ((node->pfd.revents & G_IO_IN) && node->io_read) {
  175. return true;
  176. }
  177. if ((node->pfd.revents & G_IO_OUT) && node->io_write) {
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event)
  184. {
  185. AioHandler *node;
  186. bool progress = false;
  187. /*
  188. * We have to walk very carefully in case aio_set_fd_handler is
  189. * called while we're walking.
  190. */
  191. node = QLIST_FIRST(&ctx->aio_handlers);
  192. while (node) {
  193. AioHandler *tmp;
  194. int revents = node->pfd.revents;
  195. ctx->walking_handlers++;
  196. if (!node->deleted &&
  197. (revents || event_notifier_get_handle(node->e) == event) &&
  198. node->io_notify) {
  199. node->pfd.revents = 0;
  200. node->io_notify(node->e);
  201. /* aio_notify() does not count as progress */
  202. if (node->e != &ctx->notifier) {
  203. progress = true;
  204. }
  205. }
  206. if (!node->deleted &&
  207. (node->io_read || node->io_write)) {
  208. node->pfd.revents = 0;
  209. if ((revents & G_IO_IN) && node->io_read) {
  210. node->io_read(node->opaque);
  211. progress = true;
  212. }
  213. if ((revents & G_IO_OUT) && node->io_write) {
  214. node->io_write(node->opaque);
  215. progress = true;
  216. }
  217. /* if the next select() will return an event, we have progressed */
  218. if (event == event_notifier_get_handle(&ctx->notifier)) {
  219. WSANETWORKEVENTS ev;
  220. WSAEnumNetworkEvents(node->pfd.fd, event, &ev);
  221. if (ev.lNetworkEvents) {
  222. progress = true;
  223. }
  224. }
  225. }
  226. tmp = node;
  227. node = QLIST_NEXT(node, node);
  228. ctx->walking_handlers--;
  229. if (!ctx->walking_handlers && tmp->deleted) {
  230. QLIST_REMOVE(tmp, node);
  231. g_free(tmp);
  232. }
  233. }
  234. return progress;
  235. }
  236. bool aio_dispatch(AioContext *ctx)
  237. {
  238. bool progress;
  239. progress = aio_bh_poll(ctx);
  240. progress |= aio_dispatch_handlers(ctx, INVALID_HANDLE_VALUE);
  241. progress |= timerlistgroup_run_timers(&ctx->tlg);
  242. return progress;
  243. }
  244. bool aio_poll(AioContext *ctx, bool blocking)
  245. {
  246. AioHandler *node;
  247. HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
  248. bool progress, have_select_revents, first;
  249. int count;
  250. int timeout;
  251. aio_context_acquire(ctx);
  252. progress = false;
  253. /* aio_notify can avoid the expensive event_notifier_set if
  254. * everything (file descriptors, bottom halves, timers) will
  255. * be re-evaluated before the next blocking poll(). This is
  256. * already true when aio_poll is called with blocking == false;
  257. * if blocking == true, it is only true after poll() returns,
  258. * so disable the optimization now.
  259. */
  260. if (blocking) {
  261. atomic_add(&ctx->notify_me, 2);
  262. }
  263. have_select_revents = aio_prepare(ctx);
  264. ctx->walking_handlers++;
  265. /* fill fd sets */
  266. count = 0;
  267. QLIST_FOREACH(node, &ctx->aio_handlers, node) {
  268. if (!node->deleted && node->io_notify
  269. && aio_node_check(ctx, node->is_external)) {
  270. events[count++] = event_notifier_get_handle(node->e);
  271. }
  272. }
  273. ctx->walking_handlers--;
  274. first = true;
  275. /* ctx->notifier is always registered. */
  276. assert(count > 0);
  277. /* Multiple iterations, all of them non-blocking except the first,
  278. * may be necessary to process all pending events. After the first
  279. * WaitForMultipleObjects call ctx->notify_me will be decremented.
  280. */
  281. do {
  282. HANDLE event;
  283. int ret;
  284. timeout = blocking && !have_select_revents
  285. ? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx)) : 0;
  286. if (timeout) {
  287. aio_context_release(ctx);
  288. }
  289. ret = WaitForMultipleObjects(count, events, FALSE, timeout);
  290. if (blocking) {
  291. assert(first);
  292. atomic_sub(&ctx->notify_me, 2);
  293. }
  294. if (timeout) {
  295. aio_context_acquire(ctx);
  296. }
  297. if (first) {
  298. aio_notify_accept(ctx);
  299. progress |= aio_bh_poll(ctx);
  300. first = false;
  301. }
  302. /* if we have any signaled events, dispatch event */
  303. event = NULL;
  304. if ((DWORD) (ret - WAIT_OBJECT_0) < count) {
  305. event = events[ret - WAIT_OBJECT_0];
  306. events[ret - WAIT_OBJECT_0] = events[--count];
  307. } else if (!have_select_revents) {
  308. break;
  309. }
  310. have_select_revents = false;
  311. blocking = false;
  312. progress |= aio_dispatch_handlers(ctx, event);
  313. } while (count > 0);
  314. progress |= timerlistgroup_run_timers(&ctx->tlg);
  315. aio_context_release(ctx);
  316. return progress;
  317. }
  318. void aio_context_setup(AioContext *ctx, Error **errp)
  319. {
  320. }