async.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * QEMU System Emulator
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu-common.h"
  25. #include "block/aio.h"
  26. #include "qemu/main-loop.h"
  27. /***********************************************************/
  28. /* bottom halves (can be seen as timers which expire ASAP) */
  29. struct QEMUBH {
  30. AioContext *ctx;
  31. QEMUBHFunc *cb;
  32. void *opaque;
  33. QEMUBH *next;
  34. bool scheduled;
  35. bool idle;
  36. bool deleted;
  37. };
  38. QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque)
  39. {
  40. QEMUBH *bh;
  41. bh = g_malloc0(sizeof(QEMUBH));
  42. bh->ctx = ctx;
  43. bh->cb = cb;
  44. bh->opaque = opaque;
  45. bh->next = ctx->first_bh;
  46. ctx->first_bh = bh;
  47. return bh;
  48. }
  49. int aio_bh_poll(AioContext *ctx)
  50. {
  51. QEMUBH *bh, **bhp, *next;
  52. int ret;
  53. ctx->walking_bh++;
  54. ret = 0;
  55. for (bh = ctx->first_bh; bh; bh = next) {
  56. next = bh->next;
  57. if (!bh->deleted && bh->scheduled) {
  58. bh->scheduled = 0;
  59. if (!bh->idle)
  60. ret = 1;
  61. bh->idle = 0;
  62. bh->cb(bh->opaque);
  63. }
  64. }
  65. ctx->walking_bh--;
  66. /* remove deleted bhs */
  67. if (!ctx->walking_bh) {
  68. bhp = &ctx->first_bh;
  69. while (*bhp) {
  70. bh = *bhp;
  71. if (bh->deleted) {
  72. *bhp = bh->next;
  73. g_free(bh);
  74. } else {
  75. bhp = &bh->next;
  76. }
  77. }
  78. }
  79. return ret;
  80. }
  81. void qemu_bh_schedule_idle(QEMUBH *bh)
  82. {
  83. if (bh->scheduled)
  84. return;
  85. bh->scheduled = 1;
  86. bh->idle = 1;
  87. }
  88. void qemu_bh_schedule(QEMUBH *bh)
  89. {
  90. if (bh->scheduled)
  91. return;
  92. bh->scheduled = 1;
  93. bh->idle = 0;
  94. aio_notify(bh->ctx);
  95. }
  96. void qemu_bh_cancel(QEMUBH *bh)
  97. {
  98. bh->scheduled = 0;
  99. }
  100. void qemu_bh_delete(QEMUBH *bh)
  101. {
  102. bh->scheduled = 0;
  103. bh->deleted = 1;
  104. }
  105. static gboolean
  106. aio_ctx_prepare(GSource *source, gint *timeout)
  107. {
  108. AioContext *ctx = (AioContext *) source;
  109. QEMUBH *bh;
  110. for (bh = ctx->first_bh; bh; bh = bh->next) {
  111. if (!bh->deleted && bh->scheduled) {
  112. if (bh->idle) {
  113. /* idle bottom halves will be polled at least
  114. * every 10ms */
  115. *timeout = 10;
  116. } else {
  117. /* non-idle bottom halves will be executed
  118. * immediately */
  119. *timeout = 0;
  120. return true;
  121. }
  122. }
  123. }
  124. return false;
  125. }
  126. static gboolean
  127. aio_ctx_check(GSource *source)
  128. {
  129. AioContext *ctx = (AioContext *) source;
  130. QEMUBH *bh;
  131. for (bh = ctx->first_bh; bh; bh = bh->next) {
  132. if (!bh->deleted && bh->scheduled) {
  133. return true;
  134. }
  135. }
  136. return aio_pending(ctx);
  137. }
  138. static gboolean
  139. aio_ctx_dispatch(GSource *source,
  140. GSourceFunc callback,
  141. gpointer user_data)
  142. {
  143. AioContext *ctx = (AioContext *) source;
  144. assert(callback == NULL);
  145. aio_poll(ctx, false);
  146. return true;
  147. }
  148. static void
  149. aio_ctx_finalize(GSource *source)
  150. {
  151. AioContext *ctx = (AioContext *) source;
  152. aio_set_event_notifier(ctx, &ctx->notifier, NULL, NULL);
  153. event_notifier_cleanup(&ctx->notifier);
  154. }
  155. static GSourceFuncs aio_source_funcs = {
  156. aio_ctx_prepare,
  157. aio_ctx_check,
  158. aio_ctx_dispatch,
  159. aio_ctx_finalize
  160. };
  161. GSource *aio_get_g_source(AioContext *ctx)
  162. {
  163. g_source_ref(&ctx->source);
  164. return &ctx->source;
  165. }
  166. void aio_notify(AioContext *ctx)
  167. {
  168. event_notifier_set(&ctx->notifier);
  169. }
  170. AioContext *aio_context_new(void)
  171. {
  172. AioContext *ctx;
  173. ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext));
  174. event_notifier_init(&ctx->notifier, false);
  175. aio_set_event_notifier(ctx, &ctx->notifier,
  176. (EventNotifierHandler *)
  177. event_notifier_test_and_clear, NULL);
  178. return ctx;
  179. }
  180. void aio_context_ref(AioContext *ctx)
  181. {
  182. g_source_ref(&ctx->source);
  183. }
  184. void aio_context_unref(AioContext *ctx)
  185. {
  186. g_source_unref(&ctx->source);
  187. }