async.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 "block/thread-pool.h"
  27. #include "qemu/main-loop.h"
  28. /***********************************************************/
  29. /* bottom halves (can be seen as timers which expire ASAP) */
  30. struct QEMUBH {
  31. AioContext *ctx;
  32. QEMUBHFunc *cb;
  33. void *opaque;
  34. QEMUBH *next;
  35. bool scheduled;
  36. bool idle;
  37. bool deleted;
  38. };
  39. QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque)
  40. {
  41. QEMUBH *bh;
  42. bh = g_malloc0(sizeof(QEMUBH));
  43. bh->ctx = ctx;
  44. bh->cb = cb;
  45. bh->opaque = opaque;
  46. qemu_mutex_lock(&ctx->bh_lock);
  47. bh->next = ctx->first_bh;
  48. /* Make sure that the members are ready before putting bh into list */
  49. smp_wmb();
  50. ctx->first_bh = bh;
  51. qemu_mutex_unlock(&ctx->bh_lock);
  52. return bh;
  53. }
  54. /* Multiple occurrences of aio_bh_poll cannot be called concurrently */
  55. int aio_bh_poll(AioContext *ctx)
  56. {
  57. QEMUBH *bh, **bhp, *next;
  58. int ret;
  59. ctx->walking_bh++;
  60. ret = 0;
  61. for (bh = ctx->first_bh; bh; bh = next) {
  62. /* Make sure that fetching bh happens before accessing its members */
  63. smp_read_barrier_depends();
  64. next = bh->next;
  65. if (!bh->deleted && bh->scheduled) {
  66. bh->scheduled = 0;
  67. /* Paired with write barrier in bh schedule to ensure reading for
  68. * idle & callbacks coming after bh's scheduling.
  69. */
  70. smp_rmb();
  71. if (!bh->idle)
  72. ret = 1;
  73. bh->idle = 0;
  74. bh->cb(bh->opaque);
  75. }
  76. }
  77. ctx->walking_bh--;
  78. /* remove deleted bhs */
  79. if (!ctx->walking_bh) {
  80. qemu_mutex_lock(&ctx->bh_lock);
  81. bhp = &ctx->first_bh;
  82. while (*bhp) {
  83. bh = *bhp;
  84. if (bh->deleted) {
  85. *bhp = bh->next;
  86. g_free(bh);
  87. } else {
  88. bhp = &bh->next;
  89. }
  90. }
  91. qemu_mutex_unlock(&ctx->bh_lock);
  92. }
  93. return ret;
  94. }
  95. void qemu_bh_schedule_idle(QEMUBH *bh)
  96. {
  97. if (bh->scheduled)
  98. return;
  99. bh->idle = 1;
  100. /* Make sure that idle & any writes needed by the callback are done
  101. * before the locations are read in the aio_bh_poll.
  102. */
  103. smp_wmb();
  104. bh->scheduled = 1;
  105. }
  106. void qemu_bh_schedule(QEMUBH *bh)
  107. {
  108. AioContext *ctx;
  109. if (bh->scheduled)
  110. return;
  111. ctx = bh->ctx;
  112. bh->idle = 0;
  113. /* Make sure that:
  114. * 1. idle & any writes needed by the callback are done before the
  115. * locations are read in the aio_bh_poll.
  116. * 2. ctx is loaded before scheduled is set and the callback has a chance
  117. * to execute.
  118. */
  119. smp_mb();
  120. bh->scheduled = 1;
  121. aio_notify(ctx);
  122. }
  123. /* This func is async.
  124. */
  125. void qemu_bh_cancel(QEMUBH *bh)
  126. {
  127. bh->scheduled = 0;
  128. }
  129. /* This func is async.The bottom half will do the delete action at the finial
  130. * end.
  131. */
  132. void qemu_bh_delete(QEMUBH *bh)
  133. {
  134. bh->scheduled = 0;
  135. bh->deleted = 1;
  136. }
  137. static gboolean
  138. aio_ctx_prepare(GSource *source, gint *timeout)
  139. {
  140. AioContext *ctx = (AioContext *) source;
  141. QEMUBH *bh;
  142. int deadline;
  143. /* We assume there is no timeout already supplied */
  144. *timeout = -1;
  145. for (bh = ctx->first_bh; bh; bh = bh->next) {
  146. if (!bh->deleted && bh->scheduled) {
  147. if (bh->idle) {
  148. /* idle bottom halves will be polled at least
  149. * every 10ms */
  150. *timeout = 10;
  151. } else {
  152. /* non-idle bottom halves will be executed
  153. * immediately */
  154. *timeout = 0;
  155. return true;
  156. }
  157. }
  158. }
  159. deadline = qemu_timeout_ns_to_ms(timerlistgroup_deadline_ns(&ctx->tlg));
  160. if (deadline == 0) {
  161. *timeout = 0;
  162. return true;
  163. } else {
  164. *timeout = qemu_soonest_timeout(*timeout, deadline);
  165. }
  166. return false;
  167. }
  168. static gboolean
  169. aio_ctx_check(GSource *source)
  170. {
  171. AioContext *ctx = (AioContext *) source;
  172. QEMUBH *bh;
  173. for (bh = ctx->first_bh; bh; bh = bh->next) {
  174. if (!bh->deleted && bh->scheduled) {
  175. return true;
  176. }
  177. }
  178. return aio_pending(ctx) || (timerlistgroup_deadline_ns(&ctx->tlg) == 0);
  179. }
  180. static gboolean
  181. aio_ctx_dispatch(GSource *source,
  182. GSourceFunc callback,
  183. gpointer user_data)
  184. {
  185. AioContext *ctx = (AioContext *) source;
  186. assert(callback == NULL);
  187. aio_poll(ctx, false);
  188. return true;
  189. }
  190. static void
  191. aio_ctx_finalize(GSource *source)
  192. {
  193. AioContext *ctx = (AioContext *) source;
  194. thread_pool_free(ctx->thread_pool);
  195. aio_set_event_notifier(ctx, &ctx->notifier, NULL);
  196. event_notifier_cleanup(&ctx->notifier);
  197. rfifolock_destroy(&ctx->lock);
  198. qemu_mutex_destroy(&ctx->bh_lock);
  199. g_array_free(ctx->pollfds, TRUE);
  200. timerlistgroup_deinit(&ctx->tlg);
  201. }
  202. static GSourceFuncs aio_source_funcs = {
  203. aio_ctx_prepare,
  204. aio_ctx_check,
  205. aio_ctx_dispatch,
  206. aio_ctx_finalize
  207. };
  208. GSource *aio_get_g_source(AioContext *ctx)
  209. {
  210. g_source_ref(&ctx->source);
  211. return &ctx->source;
  212. }
  213. ThreadPool *aio_get_thread_pool(AioContext *ctx)
  214. {
  215. if (!ctx->thread_pool) {
  216. ctx->thread_pool = thread_pool_new(ctx);
  217. }
  218. return ctx->thread_pool;
  219. }
  220. void aio_notify(AioContext *ctx)
  221. {
  222. event_notifier_set(&ctx->notifier);
  223. }
  224. static void aio_timerlist_notify(void *opaque)
  225. {
  226. aio_notify(opaque);
  227. }
  228. static void aio_rfifolock_cb(void *opaque)
  229. {
  230. /* Kick owner thread in case they are blocked in aio_poll() */
  231. aio_notify(opaque);
  232. }
  233. AioContext *aio_context_new(void)
  234. {
  235. AioContext *ctx;
  236. ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext));
  237. ctx->pollfds = g_array_new(FALSE, FALSE, sizeof(GPollFD));
  238. ctx->thread_pool = NULL;
  239. qemu_mutex_init(&ctx->bh_lock);
  240. rfifolock_init(&ctx->lock, aio_rfifolock_cb, ctx);
  241. event_notifier_init(&ctx->notifier, false);
  242. aio_set_event_notifier(ctx, &ctx->notifier,
  243. (EventNotifierHandler *)
  244. event_notifier_test_and_clear);
  245. timerlistgroup_init(&ctx->tlg, aio_timerlist_notify, ctx);
  246. return ctx;
  247. }
  248. void aio_context_ref(AioContext *ctx)
  249. {
  250. g_source_ref(&ctx->source);
  251. }
  252. void aio_context_unref(AioContext *ctx)
  253. {
  254. g_source_unref(&ctx->source);
  255. }
  256. void aio_context_acquire(AioContext *ctx)
  257. {
  258. rfifolock_lock(&ctx->lock);
  259. }
  260. void aio_context_release(AioContext *ctx)
  261. {
  262. rfifolock_unlock(&ctx->lock);
  263. }