async.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. int64_t
  139. aio_compute_timeout(AioContext *ctx)
  140. {
  141. int64_t deadline;
  142. int timeout = -1;
  143. QEMUBH *bh;
  144. for (bh = ctx->first_bh; bh; bh = bh->next) {
  145. if (!bh->deleted && bh->scheduled) {
  146. if (bh->idle) {
  147. /* idle bottom halves will be polled at least
  148. * every 10ms */
  149. timeout = 10000000;
  150. } else {
  151. /* non-idle bottom halves will be executed
  152. * immediately */
  153. return 0;
  154. }
  155. }
  156. }
  157. deadline = timerlistgroup_deadline_ns(&ctx->tlg);
  158. if (deadline == 0) {
  159. return 0;
  160. } else {
  161. return qemu_soonest_timeout(timeout, deadline);
  162. }
  163. }
  164. static gboolean
  165. aio_ctx_prepare(GSource *source, gint *timeout)
  166. {
  167. AioContext *ctx = (AioContext *) source;
  168. /* We assume there is no timeout already supplied */
  169. *timeout = qemu_timeout_ns_to_ms(aio_compute_timeout(ctx));
  170. if (aio_prepare(ctx)) {
  171. *timeout = 0;
  172. }
  173. return *timeout == 0;
  174. }
  175. static gboolean
  176. aio_ctx_check(GSource *source)
  177. {
  178. AioContext *ctx = (AioContext *) source;
  179. QEMUBH *bh;
  180. for (bh = ctx->first_bh; bh; bh = bh->next) {
  181. if (!bh->deleted && bh->scheduled) {
  182. return true;
  183. }
  184. }
  185. return aio_pending(ctx) || (timerlistgroup_deadline_ns(&ctx->tlg) == 0);
  186. }
  187. static gboolean
  188. aio_ctx_dispatch(GSource *source,
  189. GSourceFunc callback,
  190. gpointer user_data)
  191. {
  192. AioContext *ctx = (AioContext *) source;
  193. assert(callback == NULL);
  194. aio_dispatch(ctx);
  195. return true;
  196. }
  197. static void
  198. aio_ctx_finalize(GSource *source)
  199. {
  200. AioContext *ctx = (AioContext *) source;
  201. thread_pool_free(ctx->thread_pool);
  202. aio_set_event_notifier(ctx, &ctx->notifier, NULL);
  203. event_notifier_cleanup(&ctx->notifier);
  204. rfifolock_destroy(&ctx->lock);
  205. qemu_mutex_destroy(&ctx->bh_lock);
  206. g_array_free(ctx->pollfds, TRUE);
  207. timerlistgroup_deinit(&ctx->tlg);
  208. }
  209. static GSourceFuncs aio_source_funcs = {
  210. aio_ctx_prepare,
  211. aio_ctx_check,
  212. aio_ctx_dispatch,
  213. aio_ctx_finalize
  214. };
  215. GSource *aio_get_g_source(AioContext *ctx)
  216. {
  217. g_source_ref(&ctx->source);
  218. return &ctx->source;
  219. }
  220. ThreadPool *aio_get_thread_pool(AioContext *ctx)
  221. {
  222. if (!ctx->thread_pool) {
  223. ctx->thread_pool = thread_pool_new(ctx);
  224. }
  225. return ctx->thread_pool;
  226. }
  227. void aio_set_dispatching(AioContext *ctx, bool dispatching)
  228. {
  229. ctx->dispatching = dispatching;
  230. if (!dispatching) {
  231. /* Write ctx->dispatching before reading e.g. bh->scheduled.
  232. * Optimization: this is only needed when we're entering the "unsafe"
  233. * phase where other threads must call event_notifier_set.
  234. */
  235. smp_mb();
  236. }
  237. }
  238. void aio_notify(AioContext *ctx)
  239. {
  240. /* Write e.g. bh->scheduled before reading ctx->dispatching. */
  241. smp_mb();
  242. if (!ctx->dispatching) {
  243. event_notifier_set(&ctx->notifier);
  244. }
  245. }
  246. static void aio_timerlist_notify(void *opaque)
  247. {
  248. aio_notify(opaque);
  249. }
  250. static void aio_rfifolock_cb(void *opaque)
  251. {
  252. /* Kick owner thread in case they are blocked in aio_poll() */
  253. aio_notify(opaque);
  254. }
  255. AioContext *aio_context_new(Error **errp)
  256. {
  257. int ret;
  258. AioContext *ctx;
  259. ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext));
  260. ret = event_notifier_init(&ctx->notifier, false);
  261. if (ret < 0) {
  262. g_source_destroy(&ctx->source);
  263. error_setg_errno(errp, -ret, "Failed to initialize event notifier");
  264. return NULL;
  265. }
  266. aio_set_event_notifier(ctx, &ctx->notifier,
  267. (EventNotifierHandler *)
  268. event_notifier_test_and_clear);
  269. ctx->pollfds = g_array_new(FALSE, FALSE, sizeof(GPollFD));
  270. ctx->thread_pool = NULL;
  271. qemu_mutex_init(&ctx->bh_lock);
  272. rfifolock_init(&ctx->lock, aio_rfifolock_cb, ctx);
  273. timerlistgroup_init(&ctx->tlg, aio_timerlist_notify, ctx);
  274. return ctx;
  275. }
  276. void aio_context_ref(AioContext *ctx)
  277. {
  278. g_source_ref(&ctx->source);
  279. }
  280. void aio_context_unref(AioContext *ctx)
  281. {
  282. g_source_unref(&ctx->source);
  283. }
  284. void aio_context_acquire(AioContext *ctx)
  285. {
  286. rfifolock_lock(&ctx->lock);
  287. }
  288. void aio_context_release(AioContext *ctx)
  289. {
  290. rfifolock_unlock(&ctx->lock);
  291. }