async.c 8.0 KB

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