[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-block] [PULL 4/7] block: add aio_wait_bh_oneshot()
From: |
Stefan Hajnoczi |
Subject: |
[Qemu-block] [PULL 4/7] block: add aio_wait_bh_oneshot() |
Date: |
Fri, 9 Mar 2018 13:19:46 +0000 |
Sometimes it's necessary for the main loop thread to run a BH in an
IOThread and wait for its completion. This primitive is useful during
startup/shutdown to synchronize and avoid race conditions.
Signed-off-by: Stefan Hajnoczi <address@hidden>
Reviewed-by: Fam Zheng <address@hidden>
Acked-by: Paolo Bonzini <address@hidden>
Message-id: address@hidden
Signed-off-by: Stefan Hajnoczi <address@hidden>
---
include/block/aio-wait.h | 13 +++++++++++++
util/aio-wait.c | 31 +++++++++++++++++++++++++++++++
2 files changed, 44 insertions(+)
diff --git a/include/block/aio-wait.h b/include/block/aio-wait.h
index a48c744fa8..f7a3972200 100644
--- a/include/block/aio-wait.h
+++ b/include/block/aio-wait.h
@@ -113,4 +113,17 @@ typedef struct {
*/
void aio_wait_kick(AioWait *wait);
+/**
+ * aio_wait_bh_oneshot:
+ * @ctx: the aio context
+ * @cb: the BH callback function
+ * @opaque: user data for the BH callback function
+ *
+ * Run a BH in @ctx and wait for it to complete.
+ *
+ * Must be called from the main loop thread with @ctx acquired exactly once.
+ * Note that main loop event processing may occur.
+ */
+void aio_wait_bh_oneshot(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
+
#endif /* QEMU_AIO_WAIT */
diff --git a/util/aio-wait.c b/util/aio-wait.c
index a487cdb852..975afddf4c 100644
--- a/util/aio-wait.c
+++ b/util/aio-wait.c
@@ -38,3 +38,34 @@ void aio_wait_kick(AioWait *wait)
aio_bh_schedule_oneshot(qemu_get_aio_context(), dummy_bh_cb, NULL);
}
}
+
+typedef struct {
+ AioWait wait;
+ bool done;
+ QEMUBHFunc *cb;
+ void *opaque;
+} AioWaitBHData;
+
+/* Context: BH in IOThread */
+static void aio_wait_bh(void *opaque)
+{
+ AioWaitBHData *data = opaque;
+
+ data->cb(data->opaque);
+
+ data->done = true;
+ aio_wait_kick(&data->wait);
+}
+
+void aio_wait_bh_oneshot(AioContext *ctx, QEMUBHFunc *cb, void *opaque)
+{
+ AioWaitBHData data = {
+ .cb = cb,
+ .opaque = opaque,
+ };
+
+ assert(qemu_get_current_aio_context() == qemu_get_aio_context());
+
+ aio_bh_schedule_oneshot(ctx, aio_wait_bh, &data);
+ AIO_WAIT_WHILE(&data.wait, ctx, !data.done);
+}
--
2.14.3
- [Qemu-block] [PULL 0/7] Block patches, Stefan Hajnoczi, 2018/03/09
- [Qemu-block] [PULL 1/7] block: Fix qemu crash when using scsi-block, Stefan Hajnoczi, 2018/03/09
- [Qemu-block] [PULL 2/7] README: Fix typo 'git-publish', Stefan Hajnoczi, 2018/03/09
- [Qemu-block] [PULL 4/7] block: add aio_wait_bh_oneshot(),
Stefan Hajnoczi <=
- [Qemu-block] [PULL 3/7] virtio-blk: dataplane: Don't batch notifications if EVENT_IDX is present, Stefan Hajnoczi, 2018/03/09
- [Qemu-block] [PULL 6/7] virtio-scsi: fix race between .ioeventfd_stop() and vq handler, Stefan Hajnoczi, 2018/03/09
- [Qemu-block] [PULL 7/7] vl: introduce vm_shutdown(), Stefan Hajnoczi, 2018/03/09
- [Qemu-block] [PULL 5/7] virtio-blk: fix race between .ioeventfd_stop() and vq handler, Stefan Hajnoczi, 2018/03/09
- Re: [Qemu-block] [PULL 0/7] Block patches, Peter Maydell, 2018/03/09