async.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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_new(QEMUBH, 1);
  44. *bh = (QEMUBH){
  45. .ctx = ctx,
  46. .cb = cb,
  47. .opaque = opaque,
  48. };
  49. qemu_mutex_lock(&ctx->bh_lock);
  50. bh->next = ctx->first_bh;
  51. /* Make sure that the members are ready before putting bh into list */
  52. smp_wmb();
  53. ctx->first_bh = bh;
  54. qemu_mutex_unlock(&ctx->bh_lock);
  55. return bh;
  56. }
  57. /* Multiple occurrences of aio_bh_poll cannot be called concurrently */
  58. int aio_bh_poll(AioContext *ctx)
  59. {
  60. QEMUBH *bh, **bhp, *next;
  61. int ret;
  62. ctx->walking_bh++;
  63. ret = 0;
  64. for (bh = ctx->first_bh; bh; bh = next) {
  65. /* Make sure that fetching bh happens before accessing its members */
  66. smp_read_barrier_depends();
  67. next = bh->next;
  68. /* The atomic_xchg is paired with the one in qemu_bh_schedule. The
  69. * implicit memory barrier ensures that the callback sees all writes
  70. * done by the scheduling thread. It also ensures that the scheduling
  71. * thread sees the zero before bh->cb has run, and thus will call
  72. * aio_notify again if necessary.
  73. */
  74. if (!bh->deleted && atomic_xchg(&bh->scheduled, 0)) {
  75. /* Idle BHs and the notify BH don't count as progress */
  76. if (!bh->idle && bh != ctx->notify_dummy_bh) {
  77. ret = 1;
  78. }
  79. bh->idle = 0;
  80. bh->cb(bh->opaque);
  81. }
  82. }
  83. ctx->walking_bh--;
  84. /* remove deleted bhs */
  85. if (!ctx->walking_bh) {
  86. qemu_mutex_lock(&ctx->bh_lock);
  87. bhp = &ctx->first_bh;
  88. while (*bhp) {
  89. bh = *bhp;
  90. if (bh->deleted) {
  91. *bhp = bh->next;
  92. g_free(bh);
  93. } else {
  94. bhp = &bh->next;
  95. }
  96. }
  97. qemu_mutex_unlock(&ctx->bh_lock);
  98. }
  99. return ret;
  100. }
  101. void qemu_bh_schedule_idle(QEMUBH *bh)
  102. {
  103. bh->idle = 1;
  104. /* Make sure that idle & any writes needed by the callback are done
  105. * before the locations are read in the aio_bh_poll.
  106. */
  107. atomic_mb_set(&bh->scheduled, 1);
  108. }
  109. void qemu_bh_schedule(QEMUBH *bh)
  110. {
  111. AioContext *ctx;
  112. ctx = bh->ctx;
  113. bh->idle = 0;
  114. /* The memory barrier implicit in atomic_xchg makes 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. if (atomic_xchg(&bh->scheduled, 1) == 0) {
  121. aio_notify(ctx);
  122. }
  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. atomic_or(&ctx->notify_me, 1);
  169. /* We assume there is no timeout already supplied */
  170. *timeout = qemu_timeout_ns_to_ms(aio_compute_timeout(ctx));
  171. if (aio_prepare(ctx)) {
  172. *timeout = 0;
  173. }
  174. return *timeout == 0;
  175. }
  176. static gboolean
  177. aio_ctx_check(GSource *source)
  178. {
  179. AioContext *ctx = (AioContext *) source;
  180. QEMUBH *bh;
  181. atomic_and(&ctx->notify_me, ~1);
  182. aio_notify_accept(ctx);
  183. for (bh = ctx->first_bh; bh; bh = bh->next) {
  184. if (!bh->deleted && bh->scheduled) {
  185. return true;
  186. }
  187. }
  188. return aio_pending(ctx) || (timerlistgroup_deadline_ns(&ctx->tlg) == 0);
  189. }
  190. static gboolean
  191. aio_ctx_dispatch(GSource *source,
  192. GSourceFunc callback,
  193. gpointer user_data)
  194. {
  195. AioContext *ctx = (AioContext *) source;
  196. assert(callback == NULL);
  197. aio_dispatch(ctx);
  198. return true;
  199. }
  200. static void
  201. aio_ctx_finalize(GSource *source)
  202. {
  203. AioContext *ctx = (AioContext *) source;
  204. qemu_bh_delete(ctx->notify_dummy_bh);
  205. thread_pool_free(ctx->thread_pool);
  206. qemu_mutex_lock(&ctx->bh_lock);
  207. while (ctx->first_bh) {
  208. QEMUBH *next = ctx->first_bh->next;
  209. /* qemu_bh_delete() must have been called on BHs in this AioContext */
  210. assert(ctx->first_bh->deleted);
  211. g_free(ctx->first_bh);
  212. ctx->first_bh = next;
  213. }
  214. qemu_mutex_unlock(&ctx->bh_lock);
  215. aio_set_event_notifier(ctx, &ctx->notifier, NULL);
  216. event_notifier_cleanup(&ctx->notifier);
  217. rfifolock_destroy(&ctx->lock);
  218. qemu_mutex_destroy(&ctx->bh_lock);
  219. timerlistgroup_deinit(&ctx->tlg);
  220. }
  221. static GSourceFuncs aio_source_funcs = {
  222. aio_ctx_prepare,
  223. aio_ctx_check,
  224. aio_ctx_dispatch,
  225. aio_ctx_finalize
  226. };
  227. GSource *aio_get_g_source(AioContext *ctx)
  228. {
  229. g_source_ref(&ctx->source);
  230. return &ctx->source;
  231. }
  232. ThreadPool *aio_get_thread_pool(AioContext *ctx)
  233. {
  234. if (!ctx->thread_pool) {
  235. ctx->thread_pool = thread_pool_new(ctx);
  236. }
  237. return ctx->thread_pool;
  238. }
  239. void aio_notify(AioContext *ctx)
  240. {
  241. /* Write e.g. bh->scheduled before reading ctx->notify_me. Pairs
  242. * with atomic_or in aio_ctx_prepare or atomic_add in aio_poll.
  243. */
  244. smp_mb();
  245. if (ctx->notify_me) {
  246. event_notifier_set(&ctx->notifier);
  247. atomic_mb_set(&ctx->notified, true);
  248. }
  249. }
  250. void aio_notify_accept(AioContext *ctx)
  251. {
  252. if (atomic_xchg(&ctx->notified, false)) {
  253. event_notifier_test_and_clear(&ctx->notifier);
  254. }
  255. }
  256. static void aio_timerlist_notify(void *opaque)
  257. {
  258. aio_notify(opaque);
  259. }
  260. static void aio_rfifolock_cb(void *opaque)
  261. {
  262. AioContext *ctx = opaque;
  263. /* Kick owner thread in case they are blocked in aio_poll() */
  264. qemu_bh_schedule(ctx->notify_dummy_bh);
  265. }
  266. static void notify_dummy_bh(void *opaque)
  267. {
  268. /* Do nothing, we were invoked just to force the event loop to iterate */
  269. }
  270. static void event_notifier_dummy_cb(EventNotifier *e)
  271. {
  272. }
  273. AioContext *aio_context_new(Error **errp)
  274. {
  275. int ret;
  276. AioContext *ctx;
  277. ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext));
  278. ret = event_notifier_init(&ctx->notifier, false);
  279. if (ret < 0) {
  280. g_source_destroy(&ctx->source);
  281. error_setg_errno(errp, -ret, "Failed to initialize event notifier");
  282. return NULL;
  283. }
  284. g_source_set_can_recurse(&ctx->source, true);
  285. aio_set_event_notifier(ctx, &ctx->notifier,
  286. (EventNotifierHandler *)
  287. event_notifier_dummy_cb);
  288. ctx->thread_pool = NULL;
  289. qemu_mutex_init(&ctx->bh_lock);
  290. rfifolock_init(&ctx->lock, aio_rfifolock_cb, ctx);
  291. timerlistgroup_init(&ctx->tlg, aio_timerlist_notify, ctx);
  292. ctx->notify_dummy_bh = aio_bh_new(ctx, notify_dummy_bh, NULL);
  293. return ctx;
  294. }
  295. void aio_context_ref(AioContext *ctx)
  296. {
  297. g_source_ref(&ctx->source);
  298. }
  299. void aio_context_unref(AioContext *ctx)
  300. {
  301. g_source_unref(&ctx->source);
  302. }
  303. void aio_context_acquire(AioContext *ctx)
  304. {
  305. rfifolock_lock(&ctx->lock);
  306. }
  307. void aio_context_release(AioContext *ctx)
  308. {
  309. rfifolock_unlock(&ctx->lock);
  310. }