async.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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/osdep.h"
  25. #include "qemu-common.h"
  26. #include "block/aio.h"
  27. #include "block/thread-pool.h"
  28. #include "qemu/main-loop.h"
  29. #include "qemu/atomic.h"
  30. /***********************************************************/
  31. /* bottom halves (can be seen as timers which expire ASAP) */
  32. struct QEMUBH {
  33. AioContext *ctx;
  34. QEMUBHFunc *cb;
  35. void *opaque;
  36. QEMUBH *next;
  37. bool scheduled;
  38. bool idle;
  39. bool deleted;
  40. };
  41. QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque)
  42. {
  43. QEMUBH *bh;
  44. bh = g_new(QEMUBH, 1);
  45. *bh = (QEMUBH){
  46. .ctx = ctx,
  47. .cb = cb,
  48. .opaque = opaque,
  49. };
  50. qemu_mutex_lock(&ctx->bh_lock);
  51. bh->next = ctx->first_bh;
  52. /* Make sure that the members are ready before putting bh into list */
  53. smp_wmb();
  54. ctx->first_bh = bh;
  55. qemu_mutex_unlock(&ctx->bh_lock);
  56. return bh;
  57. }
  58. void aio_bh_call(QEMUBH *bh)
  59. {
  60. bh->cb(bh->opaque);
  61. }
  62. /* Multiple occurrences of aio_bh_poll cannot be called concurrently */
  63. int aio_bh_poll(AioContext *ctx)
  64. {
  65. QEMUBH *bh, **bhp, *next;
  66. int ret;
  67. ctx->walking_bh++;
  68. ret = 0;
  69. for (bh = ctx->first_bh; bh; bh = next) {
  70. /* Make sure that fetching bh happens before accessing its members */
  71. smp_read_barrier_depends();
  72. next = bh->next;
  73. /* The atomic_xchg is paired with the one in qemu_bh_schedule. The
  74. * implicit memory barrier ensures that the callback sees all writes
  75. * done by the scheduling thread. It also ensures that the scheduling
  76. * thread sees the zero before bh->cb has run, and thus will call
  77. * aio_notify again if necessary.
  78. */
  79. if (!bh->deleted && atomic_xchg(&bh->scheduled, 0)) {
  80. /* Idle BHs and the notify BH don't count as progress */
  81. if (!bh->idle && bh != ctx->notify_dummy_bh) {
  82. ret = 1;
  83. }
  84. bh->idle = 0;
  85. aio_bh_call(bh);
  86. }
  87. }
  88. ctx->walking_bh--;
  89. /* remove deleted bhs */
  90. if (!ctx->walking_bh) {
  91. qemu_mutex_lock(&ctx->bh_lock);
  92. bhp = &ctx->first_bh;
  93. while (*bhp) {
  94. bh = *bhp;
  95. if (bh->deleted) {
  96. *bhp = bh->next;
  97. g_free(bh);
  98. } else {
  99. bhp = &bh->next;
  100. }
  101. }
  102. qemu_mutex_unlock(&ctx->bh_lock);
  103. }
  104. return ret;
  105. }
  106. void qemu_bh_schedule_idle(QEMUBH *bh)
  107. {
  108. bh->idle = 1;
  109. /* Make sure that idle & any writes needed by the callback are done
  110. * before the locations are read in the aio_bh_poll.
  111. */
  112. atomic_mb_set(&bh->scheduled, 1);
  113. }
  114. void qemu_bh_schedule(QEMUBH *bh)
  115. {
  116. AioContext *ctx;
  117. ctx = bh->ctx;
  118. bh->idle = 0;
  119. /* The memory barrier implicit in atomic_xchg makes sure that:
  120. * 1. idle & any writes needed by the callback are done before the
  121. * locations are read in the aio_bh_poll.
  122. * 2. ctx is loaded before scheduled is set and the callback has a chance
  123. * to execute.
  124. */
  125. if (atomic_xchg(&bh->scheduled, 1) == 0) {
  126. aio_notify(ctx);
  127. }
  128. }
  129. /* This func is async.
  130. */
  131. void qemu_bh_cancel(QEMUBH *bh)
  132. {
  133. bh->scheduled = 0;
  134. }
  135. /* This func is async.The bottom half will do the delete action at the finial
  136. * end.
  137. */
  138. void qemu_bh_delete(QEMUBH *bh)
  139. {
  140. bh->scheduled = 0;
  141. bh->deleted = 1;
  142. }
  143. int64_t
  144. aio_compute_timeout(AioContext *ctx)
  145. {
  146. int64_t deadline;
  147. int timeout = -1;
  148. QEMUBH *bh;
  149. for (bh = ctx->first_bh; bh; bh = bh->next) {
  150. if (!bh->deleted && bh->scheduled) {
  151. if (bh->idle) {
  152. /* idle bottom halves will be polled at least
  153. * every 10ms */
  154. timeout = 10000000;
  155. } else {
  156. /* non-idle bottom halves will be executed
  157. * immediately */
  158. return 0;
  159. }
  160. }
  161. }
  162. deadline = timerlistgroup_deadline_ns(&ctx->tlg);
  163. if (deadline == 0) {
  164. return 0;
  165. } else {
  166. return qemu_soonest_timeout(timeout, deadline);
  167. }
  168. }
  169. static gboolean
  170. aio_ctx_prepare(GSource *source, gint *timeout)
  171. {
  172. AioContext *ctx = (AioContext *) source;
  173. atomic_or(&ctx->notify_me, 1);
  174. /* We assume there is no timeout already supplied */
  175. *timeout = qemu_timeout_ns_to_ms(aio_compute_timeout(ctx));
  176. if (aio_prepare(ctx)) {
  177. *timeout = 0;
  178. }
  179. return *timeout == 0;
  180. }
  181. static gboolean
  182. aio_ctx_check(GSource *source)
  183. {
  184. AioContext *ctx = (AioContext *) source;
  185. QEMUBH *bh;
  186. atomic_and(&ctx->notify_me, ~1);
  187. aio_notify_accept(ctx);
  188. for (bh = ctx->first_bh; bh; bh = bh->next) {
  189. if (!bh->deleted && bh->scheduled) {
  190. return true;
  191. }
  192. }
  193. return aio_pending(ctx) || (timerlistgroup_deadline_ns(&ctx->tlg) == 0);
  194. }
  195. static gboolean
  196. aio_ctx_dispatch(GSource *source,
  197. GSourceFunc callback,
  198. gpointer user_data)
  199. {
  200. AioContext *ctx = (AioContext *) source;
  201. assert(callback == NULL);
  202. aio_dispatch(ctx);
  203. return true;
  204. }
  205. static void
  206. aio_ctx_finalize(GSource *source)
  207. {
  208. AioContext *ctx = (AioContext *) source;
  209. qemu_bh_delete(ctx->notify_dummy_bh);
  210. thread_pool_free(ctx->thread_pool);
  211. qemu_mutex_lock(&ctx->bh_lock);
  212. while (ctx->first_bh) {
  213. QEMUBH *next = ctx->first_bh->next;
  214. /* qemu_bh_delete() must have been called on BHs in this AioContext */
  215. assert(ctx->first_bh->deleted);
  216. g_free(ctx->first_bh);
  217. ctx->first_bh = next;
  218. }
  219. qemu_mutex_unlock(&ctx->bh_lock);
  220. aio_set_event_notifier(ctx, &ctx->notifier, false, NULL);
  221. event_notifier_cleanup(&ctx->notifier);
  222. rfifolock_destroy(&ctx->lock);
  223. qemu_mutex_destroy(&ctx->bh_lock);
  224. timerlistgroup_deinit(&ctx->tlg);
  225. }
  226. static GSourceFuncs aio_source_funcs = {
  227. aio_ctx_prepare,
  228. aio_ctx_check,
  229. aio_ctx_dispatch,
  230. aio_ctx_finalize
  231. };
  232. GSource *aio_get_g_source(AioContext *ctx)
  233. {
  234. g_source_ref(&ctx->source);
  235. return &ctx->source;
  236. }
  237. ThreadPool *aio_get_thread_pool(AioContext *ctx)
  238. {
  239. if (!ctx->thread_pool) {
  240. ctx->thread_pool = thread_pool_new(ctx);
  241. }
  242. return ctx->thread_pool;
  243. }
  244. void aio_notify(AioContext *ctx)
  245. {
  246. /* Write e.g. bh->scheduled before reading ctx->notify_me. Pairs
  247. * with atomic_or in aio_ctx_prepare or atomic_add in aio_poll.
  248. */
  249. smp_mb();
  250. if (ctx->notify_me) {
  251. event_notifier_set(&ctx->notifier);
  252. atomic_mb_set(&ctx->notified, true);
  253. }
  254. }
  255. void aio_notify_accept(AioContext *ctx)
  256. {
  257. if (atomic_xchg(&ctx->notified, false)) {
  258. event_notifier_test_and_clear(&ctx->notifier);
  259. }
  260. }
  261. static void aio_timerlist_notify(void *opaque)
  262. {
  263. aio_notify(opaque);
  264. }
  265. static void aio_rfifolock_cb(void *opaque)
  266. {
  267. AioContext *ctx = opaque;
  268. /* Kick owner thread in case they are blocked in aio_poll() */
  269. qemu_bh_schedule(ctx->notify_dummy_bh);
  270. }
  271. static void notify_dummy_bh(void *opaque)
  272. {
  273. /* Do nothing, we were invoked just to force the event loop to iterate */
  274. }
  275. static void event_notifier_dummy_cb(EventNotifier *e)
  276. {
  277. }
  278. AioContext *aio_context_new(Error **errp)
  279. {
  280. int ret;
  281. AioContext *ctx;
  282. Error *local_err = NULL;
  283. ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext));
  284. aio_context_setup(ctx, &local_err);
  285. if (local_err) {
  286. error_propagate(errp, local_err);
  287. goto fail;
  288. }
  289. ret = event_notifier_init(&ctx->notifier, false);
  290. if (ret < 0) {
  291. error_setg_errno(errp, -ret, "Failed to initialize event notifier");
  292. goto fail;
  293. }
  294. g_source_set_can_recurse(&ctx->source, true);
  295. aio_set_event_notifier(ctx, &ctx->notifier,
  296. false,
  297. (EventNotifierHandler *)
  298. event_notifier_dummy_cb);
  299. ctx->thread_pool = NULL;
  300. qemu_mutex_init(&ctx->bh_lock);
  301. rfifolock_init(&ctx->lock, aio_rfifolock_cb, ctx);
  302. timerlistgroup_init(&ctx->tlg, aio_timerlist_notify, ctx);
  303. ctx->notify_dummy_bh = aio_bh_new(ctx, notify_dummy_bh, NULL);
  304. return ctx;
  305. fail:
  306. g_source_destroy(&ctx->source);
  307. return NULL;
  308. }
  309. void aio_context_ref(AioContext *ctx)
  310. {
  311. g_source_ref(&ctx->source);
  312. }
  313. void aio_context_unref(AioContext *ctx)
  314. {
  315. g_source_unref(&ctx->source);
  316. }
  317. void aio_context_acquire(AioContext *ctx)
  318. {
  319. rfifolock_lock(&ctx->lock);
  320. }
  321. void aio_context_release(AioContext *ctx)
  322. {
  323. rfifolock_unlock(&ctx->lock);
  324. }