From 0b8b8753e4d94901627b3e86431230f2319215c4 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Mon, 4 Jul 2016 19:10:01 +0200 Subject: [PATCH] coroutine: move entry argument to qemu_coroutine_create In practice the entry argument is always known at creation time, and it is confusing that sometimes qemu_coroutine_enter is used with a non-NULL argument to re-enter a coroutine (this happens in block/sheepdog.c and tests/test-coroutine.c). So pass the opaque value at creation time, for consistency with e.g. aio_bh_new. Mostly done with the following semantic patch: @ entry1 @ expression entry, arg, co; @@ - co = qemu_coroutine_create(entry); + co = qemu_coroutine_create(entry, arg); ... - qemu_coroutine_enter(co, arg); + qemu_coroutine_enter(co); @ entry2 @ expression entry, arg; identifier co; @@ - Coroutine *co = qemu_coroutine_create(entry); + Coroutine *co = qemu_coroutine_create(entry, arg); ... - qemu_coroutine_enter(co, arg); + qemu_coroutine_enter(co); @ entry3 @ expression entry, arg; @@ - qemu_coroutine_enter(qemu_coroutine_create(entry), arg); + qemu_coroutine_enter(qemu_coroutine_create(entry, arg)); @ reentry @ expression co; @@ - qemu_coroutine_enter(co, NULL); + qemu_coroutine_enter(co); except for the aforementioned few places where the semantic patch stumbled (as expected) and for test_co_queue, which would otherwise produce an uninitialized variable warning. Signed-off-by: Paolo Bonzini Reviewed-by: Fam Zheng Signed-off-by: Kevin Wolf --- block.c | 4 +-- block/backup.c | 4 +-- block/blkdebug.c | 4 +-- block/blkreplay.c | 2 +- block/block-backend.c | 8 ++--- block/commit.c | 4 +-- block/gluster.c | 2 +- block/io.c | 45 ++++++++++++++------------- block/iscsi.c | 4 +-- block/linux-aio.c | 2 +- block/mirror.c | 6 ++-- block/nbd-client.c | 6 ++-- block/nfs.c | 2 +- block/qcow.c | 4 +-- block/qcow2.c | 4 +-- block/qed.c | 4 +-- block/sheepdog.c | 14 ++++----- block/ssh.c | 2 +- block/stream.c | 4 +-- block/vmdk.c | 4 +-- blockjob.c | 2 +- hw/9pfs/9p.c | 4 +-- hw/9pfs/coth.c | 4 +-- include/qemu/coroutine.h | 8 ++--- include/qemu/main-loop.h | 4 +-- io/channel.c | 2 +- migration/migration.c | 4 +-- nbd/server.c | 12 +++---- qemu-io-cmds.c | 4 +-- tests/test-blockjob-txn.c | 4 +-- tests/test-coroutine.c | 62 ++++++++++++++++++------------------- tests/test-thread-pool.c | 4 +-- thread-pool.c | 2 +- util/qemu-coroutine-io.c | 2 +- util/qemu-coroutine-lock.c | 4 +-- util/qemu-coroutine-sleep.c | 2 +- util/qemu-coroutine.c | 8 ++--- 37 files changed, 130 insertions(+), 131 deletions(-) diff --git a/block.c b/block.c index 823ff1d7ab..67894e0719 100644 --- a/block.c +++ b/block.c @@ -329,8 +329,8 @@ int bdrv_create(BlockDriver *drv, const char* filename, /* Fast-path if already in coroutine context */ bdrv_create_co_entry(&cco); } else { - co = qemu_coroutine_create(bdrv_create_co_entry); - qemu_coroutine_enter(co, &cco); + co = qemu_coroutine_create(bdrv_create_co_entry, &cco); + qemu_coroutine_enter(co); while (cco.ret == NOT_DONE) { aio_poll(qemu_get_aio_context(), true); } diff --git a/block/backup.c b/block/backup.c index dce66142f3..2c0532314f 100644 --- a/block/backup.c +++ b/block/backup.c @@ -576,9 +576,9 @@ void backup_start(const char *job_id, BlockDriverState *bs, bdrv_op_block_all(target, job->common.blocker); job->common.len = len; - job->common.co = qemu_coroutine_create(backup_run); + job->common.co = qemu_coroutine_create(backup_run, job); block_job_txn_add_job(txn, &job->common); - qemu_coroutine_enter(job->common.co, job); + qemu_coroutine_enter(job->common.co); return; error: diff --git a/block/blkdebug.c b/block/blkdebug.c index bbaa33fdd8..fb29283f80 100644 --- a/block/blkdebug.c +++ b/block/blkdebug.c @@ -621,7 +621,7 @@ static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag) QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, next) { if (!strcmp(r->tag, tag)) { - qemu_coroutine_enter(r->co, NULL); + qemu_coroutine_enter(r->co); return 0; } } @@ -647,7 +647,7 @@ static int blkdebug_debug_remove_breakpoint(BlockDriverState *bs, } QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, r_next) { if (!strcmp(r->tag, tag)) { - qemu_coroutine_enter(r->co, NULL); + qemu_coroutine_enter(r->co); ret = 0; } } diff --git a/block/blkreplay.c b/block/blkreplay.c index 70650e4be1..3368c8c98d 100755 --- a/block/blkreplay.c +++ b/block/blkreplay.c @@ -65,7 +65,7 @@ static int64_t blkreplay_getlength(BlockDriverState *bs) static void blkreplay_bh_cb(void *opaque) { Request *req = opaque; - qemu_coroutine_enter(req->co, NULL); + qemu_coroutine_enter(req->co); qemu_bh_delete(req->bh); g_free(req); } diff --git a/block/block-backend.c b/block/block-backend.c index a862f6577b..0d7b801107 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -836,8 +836,8 @@ static int blk_prw(BlockBackend *blk, int64_t offset, uint8_t *buf, .ret = NOT_DONE, }; - co = qemu_coroutine_create(co_entry); - qemu_coroutine_enter(co, &rwco); + co = qemu_coroutine_create(co_entry, &rwco); + qemu_coroutine_enter(co); aio_context = blk_get_aio_context(blk); while (rwco.ret == NOT_DONE) { @@ -950,8 +950,8 @@ static BlockAIOCB *blk_aio_prwv(BlockBackend *blk, int64_t offset, int bytes, acb->bh = NULL; acb->has_returned = false; - co = qemu_coroutine_create(co_entry); - qemu_coroutine_enter(co, acb); + co = qemu_coroutine_create(co_entry, acb); + qemu_coroutine_enter(co); acb->has_returned = true; if (acb->rwco.ret != NOT_DONE) { diff --git a/block/commit.c b/block/commit.c index 23368fa65b..8b534d7c95 100644 --- a/block/commit.c +++ b/block/commit.c @@ -278,10 +278,10 @@ void commit_start(const char *job_id, BlockDriverState *bs, s->backing_file_str = g_strdup(backing_file_str); s->on_error = on_error; - s->common.co = qemu_coroutine_create(commit_run); + s->common.co = qemu_coroutine_create(commit_run, s); trace_commit_start(bs, base, top, s, s->common.co, opaque); - qemu_coroutine_enter(s->common.co, s); + qemu_coroutine_enter(s->common.co); } diff --git a/block/gluster.c b/block/gluster.c index 16f7778a50..406c1e6357 100644 --- a/block/gluster.c +++ b/block/gluster.c @@ -233,7 +233,7 @@ static void qemu_gluster_complete_aio(void *opaque) qemu_bh_delete(acb->bh); acb->bh = NULL; - qemu_coroutine_enter(acb->coroutine, NULL); + qemu_coroutine_enter(acb->coroutine); } /* diff --git a/block/io.c b/block/io.c index 708690898f..2887394633 100644 --- a/block/io.c +++ b/block/io.c @@ -195,7 +195,7 @@ static void bdrv_co_drain_bh_cb(void *opaque) qemu_bh_delete(data->bh); bdrv_drain_poll(data->bs); data->done = true; - qemu_coroutine_enter(co, NULL); + qemu_coroutine_enter(co); } static void coroutine_fn bdrv_co_yield_to_drain(BlockDriverState *bs) @@ -599,8 +599,8 @@ static int bdrv_prwv_co(BdrvChild *child, int64_t offset, } else { AioContext *aio_context = bdrv_get_aio_context(child->bs); - co = qemu_coroutine_create(bdrv_rw_co_entry); - qemu_coroutine_enter(co, &rwco); + co = qemu_coroutine_create(bdrv_rw_co_entry, &rwco); + qemu_coroutine_enter(co); while (rwco.ret == NOT_DONE) { aio_poll(aio_context, true); } @@ -799,7 +799,7 @@ static void bdrv_co_io_em_complete(void *opaque, int ret) CoroutineIOCompletion *co = opaque; co->ret = ret; - qemu_coroutine_enter(co->coroutine, NULL); + qemu_coroutine_enter(co->coroutine); } static int coroutine_fn bdrv_driver_preadv(BlockDriverState *bs, @@ -1752,8 +1752,9 @@ int64_t bdrv_get_block_status_above(BlockDriverState *bs, } else { AioContext *aio_context = bdrv_get_aio_context(bs); - co = qemu_coroutine_create(bdrv_get_block_status_above_co_entry); - qemu_coroutine_enter(co, &data); + co = qemu_coroutine_create(bdrv_get_block_status_above_co_entry, + &data); + qemu_coroutine_enter(co); while (!data.done) { aio_poll(aio_context, true); } @@ -1901,9 +1902,9 @@ bdrv_rw_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos, .is_read = is_read, .ret = -EINPROGRESS, }; - Coroutine *co = qemu_coroutine_create(bdrv_co_rw_vmstate_entry); + Coroutine *co = qemu_coroutine_create(bdrv_co_rw_vmstate_entry, &data); - qemu_coroutine_enter(co, &data); + qemu_coroutine_enter(co); while (data.ret == -EINPROGRESS) { aio_poll(bdrv_get_aio_context(bs), true); } @@ -2113,8 +2114,8 @@ static BlockAIOCB *bdrv_co_aio_rw_vector(BdrvChild *child, acb->req.flags = flags; acb->is_write = is_write; - co = qemu_coroutine_create(bdrv_co_do_rw); - qemu_coroutine_enter(co, acb); + co = qemu_coroutine_create(bdrv_co_do_rw, acb); + qemu_coroutine_enter(co); bdrv_co_maybe_schedule_bh(acb); return &acb->common; @@ -2141,8 +2142,8 @@ BlockAIOCB *bdrv_aio_flush(BlockDriverState *bs, acb->need_bh = true; acb->req.error = -EINPROGRESS; - co = qemu_coroutine_create(bdrv_aio_flush_co_entry); - qemu_coroutine_enter(co, acb); + co = qemu_coroutine_create(bdrv_aio_flush_co_entry, acb); + qemu_coroutine_enter(co); bdrv_co_maybe_schedule_bh(acb); return &acb->common; @@ -2171,8 +2172,8 @@ BlockAIOCB *bdrv_aio_discard(BlockDriverState *bs, acb->req.error = -EINPROGRESS; acb->req.sector = sector_num; acb->req.nb_sectors = nb_sectors; - co = qemu_coroutine_create(bdrv_aio_discard_co_entry); - qemu_coroutine_enter(co, acb); + co = qemu_coroutine_create(bdrv_aio_discard_co_entry, acb); + qemu_coroutine_enter(co); bdrv_co_maybe_schedule_bh(acb); return &acb->common; @@ -2313,8 +2314,8 @@ int bdrv_flush(BlockDriverState *bs) } else { AioContext *aio_context = bdrv_get_aio_context(bs); - co = qemu_coroutine_create(bdrv_flush_co_entry); - qemu_coroutine_enter(co, &flush_co); + co = qemu_coroutine_create(bdrv_flush_co_entry, &flush_co); + qemu_coroutine_enter(co); while (flush_co.ret == NOT_DONE) { aio_poll(aio_context, true); } @@ -2442,8 +2443,8 @@ int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) } else { AioContext *aio_context = bdrv_get_aio_context(bs); - co = qemu_coroutine_create(bdrv_discard_co_entry); - qemu_coroutine_enter(co, &rwco); + co = qemu_coroutine_create(bdrv_discard_co_entry, &rwco); + qemu_coroutine_enter(co); while (rwco.ret == NOT_DONE) { aio_poll(aio_context, true); } @@ -2505,9 +2506,9 @@ int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) /* Fast-path if already in coroutine context */ bdrv_co_ioctl_entry(&data); } else { - Coroutine *co = qemu_coroutine_create(bdrv_co_ioctl_entry); + Coroutine *co = qemu_coroutine_create(bdrv_co_ioctl_entry, &data); - qemu_coroutine_enter(co, &data); + qemu_coroutine_enter(co); while (data.ret == -EINPROGRESS) { aio_poll(bdrv_get_aio_context(bs), true); } @@ -2535,8 +2536,8 @@ BlockAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, acb->req.error = -EINPROGRESS; acb->req.req = req; acb->req.buf = buf; - co = qemu_coroutine_create(bdrv_co_aio_ioctl_entry); - qemu_coroutine_enter(co, acb); + co = qemu_coroutine_create(bdrv_co_aio_ioctl_entry, acb); + qemu_coroutine_enter(co); bdrv_co_maybe_schedule_bh(acb); return &acb->common; diff --git a/block/iscsi.c b/block/iscsi.c index 434cb37107..cf1e9e7f66 100644 --- a/block/iscsi.c +++ b/block/iscsi.c @@ -152,7 +152,7 @@ static void iscsi_co_generic_bh_cb(void *opaque) struct IscsiTask *iTask = opaque; iTask->complete = 1; qemu_bh_delete(iTask->bh); - qemu_coroutine_enter(iTask->co, NULL); + qemu_coroutine_enter(iTask->co); } static void iscsi_retry_timer_expired(void *opaque) @@ -160,7 +160,7 @@ static void iscsi_retry_timer_expired(void *opaque) struct IscsiTask *iTask = opaque; iTask->complete = 1; if (iTask->co) { - qemu_coroutine_enter(iTask->co, NULL); + qemu_coroutine_enter(iTask->co); } } diff --git a/block/linux-aio.c b/block/linux-aio.c index 7df8651581..5c104bd3cd 100644 --- a/block/linux-aio.c +++ b/block/linux-aio.c @@ -94,7 +94,7 @@ static void qemu_laio_process_completion(struct qemu_laiocb *laiocb) laiocb->ret = ret; if (laiocb->co) { - qemu_coroutine_enter(laiocb->co, NULL); + qemu_coroutine_enter(laiocb->co); } else { laiocb->common.cb(laiocb->common.opaque, ret); qemu_aio_unref(laiocb); diff --git a/block/mirror.c b/block/mirror.c index 705fbc0052..71e5ad4377 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -121,7 +121,7 @@ static void mirror_iteration_done(MirrorOp *op, int ret) g_free(op); if (s->waiting_for_io) { - qemu_coroutine_enter(s->common.co, NULL); + qemu_coroutine_enter(s->common.co); } } @@ -901,9 +901,9 @@ static void mirror_start_job(const char *job_id, BlockDriverState *bs, bdrv_op_block_all(target, s->common.blocker); - s->common.co = qemu_coroutine_create(mirror_run); + s->common.co = qemu_coroutine_create(mirror_run, s); trace_mirror_start(bs, s, s->common.co, opaque); - qemu_coroutine_enter(s->common.co, s); + qemu_coroutine_enter(s->common.co); } void mirror_start(const char *job_id, BlockDriverState *bs, diff --git a/block/nbd-client.c b/block/nbd-client.c index 420bce89f3..4cc408d206 100644 --- a/block/nbd-client.c +++ b/block/nbd-client.c @@ -38,7 +38,7 @@ static void nbd_recv_coroutines_enter_all(NbdClientSession *s) for (i = 0; i < MAX_NBD_REQUESTS; i++) { if (s->recv_coroutine[i]) { - qemu_coroutine_enter(s->recv_coroutine[i], NULL); + qemu_coroutine_enter(s->recv_coroutine[i]); } } } @@ -99,7 +99,7 @@ static void nbd_reply_ready(void *opaque) } if (s->recv_coroutine[i]) { - qemu_coroutine_enter(s->recv_coroutine[i], NULL); + qemu_coroutine_enter(s->recv_coroutine[i]); return; } @@ -111,7 +111,7 @@ static void nbd_restart_write(void *opaque) { BlockDriverState *bs = opaque; - qemu_coroutine_enter(nbd_get_client_session(bs)->send_coroutine, NULL); + qemu_coroutine_enter(nbd_get_client_session(bs)->send_coroutine); } static int nbd_co_send_request(BlockDriverState *bs, diff --git a/block/nfs.c b/block/nfs.c index 15d6832c4c..8602a44211 100644 --- a/block/nfs.c +++ b/block/nfs.c @@ -104,7 +104,7 @@ static void nfs_co_generic_bh_cb(void *opaque) NFSRPC *task = opaque; task->complete = 1; qemu_bh_delete(task->bh); - qemu_coroutine_enter(task->co, NULL); + qemu_coroutine_enter(task->co); } static void diff --git a/block/qcow.c b/block/qcow.c index ac849bd47c..0c7b75bc76 100644 --- a/block/qcow.c +++ b/block/qcow.c @@ -948,8 +948,8 @@ static int qcow_write(BlockDriverState *bs, int64_t sector_num, .nb_sectors = nb_sectors, .ret = -EINPROGRESS, }; - co = qemu_coroutine_create(qcow_write_co_entry); - qemu_coroutine_enter(co, &data); + co = qemu_coroutine_create(qcow_write_co_entry, &data); + qemu_coroutine_enter(co); while (data.ret == -EINPROGRESS) { aio_poll(aio_context, true); } diff --git a/block/qcow2.c b/block/qcow2.c index a5ea19b0b6..a6bca735e5 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -2570,8 +2570,8 @@ static int qcow2_write(BlockDriverState *bs, int64_t sector_num, .nb_sectors = nb_sectors, .ret = -EINPROGRESS, }; - co = qemu_coroutine_create(qcow2_write_co_entry); - qemu_coroutine_enter(co, &data); + co = qemu_coroutine_create(qcow2_write_co_entry, &data); + qemu_coroutine_enter(co); while (data.ret == -EINPROGRESS) { aio_poll(aio_context, true); } diff --git a/block/qed.c b/block/qed.c index f619d82c6e..426f3cb447 100644 --- a/block/qed.c +++ b/block/qed.c @@ -708,7 +708,7 @@ static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t l } if (cb->co) { - qemu_coroutine_enter(cb->co, NULL); + qemu_coroutine_enter(cb->co); } } @@ -1425,7 +1425,7 @@ static void coroutine_fn qed_co_pwrite_zeroes_cb(void *opaque, int ret) cb->done = true; cb->ret = ret; if (cb->co) { - qemu_coroutine_enter(cb->co, NULL); + qemu_coroutine_enter(cb->co); } } diff --git a/block/sheepdog.c b/block/sheepdog.c index ef5d044ab9..e739c56f08 100644 --- a/block/sheepdog.c +++ b/block/sheepdog.c @@ -495,7 +495,7 @@ static inline void free_aio_req(BDRVSheepdogState *s, AIOReq *aio_req) static void coroutine_fn sd_finish_aiocb(SheepdogAIOCB *acb) { - qemu_coroutine_enter(acb->coroutine, NULL); + qemu_coroutine_enter(acb->coroutine); qemu_aio_unref(acb); } @@ -636,7 +636,7 @@ static void restart_co_req(void *opaque) { Coroutine *co = opaque; - qemu_coroutine_enter(co, NULL); + qemu_coroutine_enter(co); } typedef struct SheepdogReqCo { @@ -726,8 +726,8 @@ static int do_req(int sockfd, AioContext *aio_context, SheepdogReq *hdr, if (qemu_in_coroutine()) { do_co_req(&srco); } else { - co = qemu_coroutine_create(do_co_req); - qemu_coroutine_enter(co, &srco); + co = qemu_coroutine_create(do_co_req, &srco); + qemu_coroutine_enter(co); while (!srco.finished) { aio_poll(aio_context, true); } @@ -925,17 +925,17 @@ static void co_read_response(void *opaque) BDRVSheepdogState *s = opaque; if (!s->co_recv) { - s->co_recv = qemu_coroutine_create(aio_read_response); + s->co_recv = qemu_coroutine_create(aio_read_response, opaque); } - qemu_coroutine_enter(s->co_recv, opaque); + qemu_coroutine_enter(s->co_recv); } static void co_write_request(void *opaque) { BDRVSheepdogState *s = opaque; - qemu_coroutine_enter(s->co_send, NULL); + qemu_coroutine_enter(s->co_send); } /* diff --git a/block/ssh.c b/block/ssh.c index 06928ed939..bcbb0e4223 100644 --- a/block/ssh.c +++ b/block/ssh.c @@ -777,7 +777,7 @@ static void restart_coroutine(void *opaque) DPRINTF("co=%p", co); - qemu_coroutine_enter(co, NULL); + qemu_coroutine_enter(co); } static coroutine_fn void set_fd_handler(BDRVSSHState *s, BlockDriverState *bs) diff --git a/block/stream.c b/block/stream.c index 54c8cd8f57..2e7c6547d2 100644 --- a/block/stream.c +++ b/block/stream.c @@ -235,7 +235,7 @@ void stream_start(const char *job_id, BlockDriverState *bs, s->backing_file_str = g_strdup(backing_file_str); s->on_error = on_error; - s->common.co = qemu_coroutine_create(stream_run); + s->common.co = qemu_coroutine_create(stream_run, s); trace_stream_start(bs, base, s, s->common.co, opaque); - qemu_coroutine_enter(s->common.co, s); + qemu_coroutine_enter(s->common.co); } diff --git a/block/vmdk.c b/block/vmdk.c index d73f4314ba..c9914f718f 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -1686,8 +1686,8 @@ static int vmdk_write_compressed(BlockDriverState *bs, .nb_sectors = nb_sectors, .ret = -EINPROGRESS, }; - co = qemu_coroutine_create(vmdk_co_write_compressed); - qemu_coroutine_enter(co, &data); + co = qemu_coroutine_create(vmdk_co_write_compressed, &data); + qemu_coroutine_enter(co); while (data.ret == -EINPROGRESS) { aio_poll(aio_context, true); } diff --git a/blockjob.c b/blockjob.c index 3b9cec7440..6816b78fcb 100644 --- a/blockjob.c +++ b/blockjob.c @@ -375,7 +375,7 @@ void block_job_resume(BlockJob *job) void block_job_enter(BlockJob *job) { if (job->co && !job->busy) { - qemu_coroutine_enter(job->co, NULL); + qemu_coroutine_enter(job->co); } } diff --git a/hw/9pfs/9p.c b/hw/9pfs/9p.c index 9acff9293c..b6b02b46a9 100644 --- a/hw/9pfs/9p.c +++ b/hw/9pfs/9p.c @@ -3278,8 +3278,8 @@ void pdu_submit(V9fsPDU *pdu) if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) { handler = v9fs_fs_ro; } - co = qemu_coroutine_create(handler); - qemu_coroutine_enter(co, pdu); + co = qemu_coroutine_create(handler, pdu); + qemu_coroutine_enter(co); } /* Returns 0 on success, 1 on failure. */ diff --git a/hw/9pfs/coth.c b/hw/9pfs/coth.c index 9b1151b60e..89018de6bf 100644 --- a/hw/9pfs/coth.c +++ b/hw/9pfs/coth.c @@ -22,14 +22,14 @@ static void coroutine_enter_cb(void *opaque, int ret) { Coroutine *co = opaque; - qemu_coroutine_enter(co, NULL); + qemu_coroutine_enter(co); } /* Called from worker thread. */ static int coroutine_enter_func(void *arg) { Coroutine *co = arg; - qemu_coroutine_enter(co, NULL); + qemu_coroutine_enter(co); return 0; } diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h index 63ae7fee75..ac8d4c9cc8 100644 --- a/include/qemu/coroutine.h +++ b/include/qemu/coroutine.h @@ -61,16 +61,14 @@ typedef void coroutine_fn CoroutineEntry(void *opaque); * Create a new coroutine * * Use qemu_coroutine_enter() to actually transfer control to the coroutine. + * The opaque argument is passed as the argument to the entry point. */ -Coroutine *qemu_coroutine_create(CoroutineEntry *entry); +Coroutine *qemu_coroutine_create(CoroutineEntry *entry, void *opaque); /** * Transfer control to a coroutine - * - * The opaque argument is passed as the argument to the entry point when - * entering the coroutine for the first time. It is subsequently ignored. */ -void qemu_coroutine_enter(Coroutine *coroutine, void *opaque); +void qemu_coroutine_enter(Coroutine *coroutine); /** * Transfer control back to a coroutine's caller diff --git a/include/qemu/main-loop.h b/include/qemu/main-loop.h index 3fa7cfe574..470f600bbc 100644 --- a/include/qemu/main-loop.h +++ b/include/qemu/main-loop.h @@ -64,11 +64,11 @@ int qemu_init_main_loop(Error **errp); * * void enter_co_bh(void *opaque) { * QEMUCoroutine *co = opaque; - * qemu_coroutine_enter(co, NULL); + * qemu_coroutine_enter(co); * } * * ... - * QEMUCoroutine *co = qemu_coroutine_create(coroutine_entry); + * QEMUCoroutine *co = qemu_coroutine_create(coroutine_entry, NULL); * QEMUBH *start_bh = qemu_bh_new(enter_co_bh, co); * qemu_bh_schedule(start_bh); * while (...) { diff --git a/io/channel.c b/io/channel.c index 692eb179b3..923c4651ca 100644 --- a/io/channel.c +++ b/io/channel.c @@ -218,7 +218,7 @@ static gboolean qio_channel_yield_enter(QIOChannel *ioc, gpointer opaque) { QIOChannelYieldData *data = opaque; - qemu_coroutine_enter(data->co, NULL); + qemu_coroutine_enter(data->co); return FALSE; } diff --git a/migration/migration.c b/migration/migration.c index a56013662d..c4e019305c 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -418,11 +418,11 @@ static void process_incoming_migration_co(void *opaque) void migration_fd_process_incoming(QEMUFile *f) { - Coroutine *co = qemu_coroutine_create(process_incoming_migration_co); + Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, f); migrate_decompress_threads_create(); qemu_file_set_blocking(f, false); - qemu_coroutine_enter(co, f); + qemu_coroutine_enter(co); } diff --git a/nbd/server.c b/nbd/server.c index a677e266ff..fbc82de932 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -106,7 +106,7 @@ static gboolean nbd_negotiate_continue(QIOChannel *ioc, GIOCondition condition, void *opaque) { - qemu_coroutine_enter(opaque, NULL); + qemu_coroutine_enter(opaque); return TRUE; } @@ -1230,9 +1230,9 @@ static void nbd_read(void *opaque) NBDClient *client = opaque; if (client->recv_coroutine) { - qemu_coroutine_enter(client->recv_coroutine, NULL); + qemu_coroutine_enter(client->recv_coroutine); } else { - qemu_coroutine_enter(qemu_coroutine_create(nbd_trip), client); + qemu_coroutine_enter(qemu_coroutine_create(nbd_trip, client)); } } @@ -1240,7 +1240,7 @@ static void nbd_restart_write(void *opaque) { NBDClient *client = opaque; - qemu_coroutine_enter(client->send_coroutine, NULL); + qemu_coroutine_enter(client->send_coroutine); } static void nbd_set_handlers(NBDClient *client) @@ -1324,6 +1324,6 @@ void nbd_client_new(NBDExport *exp, client->close = close_fn; data->client = client; - data->co = qemu_coroutine_create(nbd_co_client_start); - qemu_coroutine_enter(data->co, data); + data->co = qemu_coroutine_create(nbd_co_client_start, data); + qemu_coroutine_enter(data->co); } diff --git a/qemu-io-cmds.c b/qemu-io-cmds.c index 09e879f872..40fe2ebf7d 100644 --- a/qemu-io-cmds.c +++ b/qemu-io-cmds.c @@ -483,8 +483,8 @@ static int do_co_pwrite_zeroes(BlockBackend *blk, int64_t offset, return -ERANGE; } - co = qemu_coroutine_create(co_pwrite_zeroes_entry); - qemu_coroutine_enter(co, &data); + co = qemu_coroutine_create(co_pwrite_zeroes_entry, &data); + qemu_coroutine_enter(co); while (!data.done) { aio_poll(blk_get_aio_context(blk), true); } diff --git a/tests/test-blockjob-txn.c b/tests/test-blockjob-txn.c index 50e232a709..d049cba8a3 100644 --- a/tests/test-blockjob-txn.c +++ b/tests/test-blockjob-txn.c @@ -103,10 +103,10 @@ static BlockJob *test_block_job_start(unsigned int iterations, s->use_timer = use_timer; s->rc = rc; s->result = result; - s->common.co = qemu_coroutine_create(test_block_job_run); + s->common.co = qemu_coroutine_create(test_block_job_run, s); data->job = s; data->result = result; - qemu_coroutine_enter(s->common.co, s); + qemu_coroutine_enter(s->common.co); return &s->common; } diff --git a/tests/test-coroutine.c b/tests/test-coroutine.c index 51711744c8..ee5e06d327 100644 --- a/tests/test-coroutine.c +++ b/tests/test-coroutine.c @@ -30,8 +30,8 @@ static void test_in_coroutine(void) g_assert(!qemu_in_coroutine()); - coroutine = qemu_coroutine_create(verify_in_coroutine); - qemu_coroutine_enter(coroutine, NULL); + coroutine = qemu_coroutine_create(verify_in_coroutine, NULL); + qemu_coroutine_enter(coroutine); } /* @@ -48,8 +48,8 @@ static void test_self(void) { Coroutine *coroutine; - coroutine = qemu_coroutine_create(verify_self); - qemu_coroutine_enter(coroutine, &coroutine); + coroutine = qemu_coroutine_create(verify_self, &coroutine); + qemu_coroutine_enter(coroutine); } /* @@ -71,8 +71,8 @@ static void coroutine_fn nest(void *opaque) if (nd->n_enter < nd->max) { Coroutine *child; - child = qemu_coroutine_create(nest); - qemu_coroutine_enter(child, nd); + child = qemu_coroutine_create(nest, nd); + qemu_coroutine_enter(child); } nd->n_return++; @@ -87,8 +87,8 @@ static void test_nesting(void) .max = 128, }; - root = qemu_coroutine_create(nest); - qemu_coroutine_enter(root, &nd); + root = qemu_coroutine_create(nest, &nd); + qemu_coroutine_enter(root); /* Must enter and return from max nesting level */ g_assert_cmpint(nd.n_enter, ==, nd.max); @@ -116,9 +116,9 @@ static void test_yield(void) bool done = false; int i = -1; /* one extra time to return from coroutine */ - coroutine = qemu_coroutine_create(yield_5_times); + coroutine = qemu_coroutine_create(yield_5_times, &done); while (!done) { - qemu_coroutine_enter(coroutine, &done); + qemu_coroutine_enter(coroutine); i++; } g_assert_cmpint(i, ==, 5); /* coroutine must yield 5 times */ @@ -132,7 +132,7 @@ static void coroutine_fn c2_fn(void *opaque) static void coroutine_fn c1_fn(void *opaque) { Coroutine *c2 = opaque; - qemu_coroutine_enter(c2, NULL); + qemu_coroutine_enter(c2); } static void test_co_queue(void) @@ -140,12 +140,12 @@ static void test_co_queue(void) Coroutine *c1; Coroutine *c2; - c1 = qemu_coroutine_create(c1_fn); - c2 = qemu_coroutine_create(c2_fn); + c2 = qemu_coroutine_create(c2_fn, NULL); + c1 = qemu_coroutine_create(c1_fn, c2); - qemu_coroutine_enter(c1, c2); + qemu_coroutine_enter(c1); memset(c1, 0xff, sizeof(Coroutine)); - qemu_coroutine_enter(c2, NULL); + qemu_coroutine_enter(c2); } /* @@ -165,14 +165,14 @@ static void test_lifecycle(void) bool done = false; /* Create, enter, and return from coroutine */ - coroutine = qemu_coroutine_create(set_and_exit); - qemu_coroutine_enter(coroutine, &done); + coroutine = qemu_coroutine_create(set_and_exit, &done); + qemu_coroutine_enter(coroutine); g_assert(done); /* expect done to be true (first time) */ /* Repeat to check that no state affects this test */ done = false; - coroutine = qemu_coroutine_create(set_and_exit); - qemu_coroutine_enter(coroutine, &done); + coroutine = qemu_coroutine_create(set_and_exit, &done); + qemu_coroutine_enter(coroutine); g_assert(done); /* expect done to be true (second time) */ } @@ -206,12 +206,12 @@ static void do_order_test(void) { Coroutine *co; - co = qemu_coroutine_create(co_order_test); + co = qemu_coroutine_create(co_order_test, NULL); record_push(1, 1); - qemu_coroutine_enter(co, NULL); + qemu_coroutine_enter(co); record_push(1, 2); g_assert(!qemu_in_coroutine()); - qemu_coroutine_enter(co, NULL); + qemu_coroutine_enter(co); record_push(1, 3); g_assert(!qemu_in_coroutine()); } @@ -248,8 +248,8 @@ static void perf_lifecycle(void) g_test_timer_start(); for (i = 0; i < max; i++) { - coroutine = qemu_coroutine_create(empty_coroutine); - qemu_coroutine_enter(coroutine, NULL); + coroutine = qemu_coroutine_create(empty_coroutine, NULL); + qemu_coroutine_enter(coroutine); } duration = g_test_timer_elapsed(); @@ -272,8 +272,8 @@ static void perf_nesting(void) .n_return = 0, .max = maxnesting, }; - root = qemu_coroutine_create(nest); - qemu_coroutine_enter(root, &nd); + root = qemu_coroutine_create(nest, &nd); + qemu_coroutine_enter(root); } duration = g_test_timer_elapsed(); @@ -302,11 +302,11 @@ static void perf_yield(void) maxcycles = 100000000; i = maxcycles; - Coroutine *coroutine = qemu_coroutine_create(yield_loop); + Coroutine *coroutine = qemu_coroutine_create(yield_loop, &i); g_test_timer_start(); while (i > 0) { - qemu_coroutine_enter(coroutine, &i); + qemu_coroutine_enter(coroutine); } duration = g_test_timer_elapsed(); @@ -352,9 +352,9 @@ static void perf_cost(void) g_test_timer_start(); while (i++ < maxcycles) { - co = qemu_coroutine_create(perf_cost_func); - qemu_coroutine_enter(co, &i); - qemu_coroutine_enter(co, NULL); + co = qemu_coroutine_create(perf_cost_func, &i); + qemu_coroutine_enter(co); + qemu_coroutine_enter(co); } duration = g_test_timer_elapsed(); ops = (long)(maxcycles / (duration * 1000)); diff --git a/tests/test-thread-pool.c b/tests/test-thread-pool.c index b0e1f3290f..8dbf66a44a 100644 --- a/tests/test-thread-pool.c +++ b/tests/test-thread-pool.c @@ -91,9 +91,9 @@ static void co_test_cb(void *opaque) static void test_submit_co(void) { WorkerTestData data; - Coroutine *co = qemu_coroutine_create(co_test_cb); + Coroutine *co = qemu_coroutine_create(co_test_cb, &data); - qemu_coroutine_enter(co, &data); + qemu_coroutine_enter(co); /* Back here once the worker has started. */ diff --git a/thread-pool.c b/thread-pool.c index 03ba0b02a4..6fba913529 100644 --- a/thread-pool.c +++ b/thread-pool.c @@ -267,7 +267,7 @@ static void thread_pool_co_cb(void *opaque, int ret) ThreadPoolCo *co = opaque; co->ret = ret; - qemu_coroutine_enter(co->co, NULL); + qemu_coroutine_enter(co->co); } int coroutine_fn thread_pool_submit_co(ThreadPool *pool, ThreadPoolFunc *func, diff --git a/util/qemu-coroutine-io.c b/util/qemu-coroutine-io.c index 91b9357d4a..44a8969a69 100644 --- a/util/qemu-coroutine-io.c +++ b/util/qemu-coroutine-io.c @@ -75,7 +75,7 @@ static void fd_coroutine_enter(void *opaque) { FDYieldUntilData *data = opaque; qemu_set_fd_handler(data->fd, NULL, NULL, NULL); - qemu_coroutine_enter(data->co, NULL); + qemu_coroutine_enter(data->co); } void coroutine_fn yield_until_fd_readable(int fd) diff --git a/util/qemu-coroutine-lock.c b/util/qemu-coroutine-lock.c index cf53693fe2..22aa9abb30 100644 --- a/util/qemu-coroutine-lock.c +++ b/util/qemu-coroutine-lock.c @@ -57,7 +57,7 @@ void qemu_co_queue_run_restart(Coroutine *co) trace_qemu_co_queue_run_restart(co); while ((next = QSIMPLEQ_FIRST(&co->co_queue_wakeup))) { QSIMPLEQ_REMOVE_HEAD(&co->co_queue_wakeup, co_queue_next); - qemu_coroutine_enter(next, NULL); + qemu_coroutine_enter(next); } } @@ -103,7 +103,7 @@ bool qemu_co_enter_next(CoQueue *queue) } QSIMPLEQ_REMOVE_HEAD(&queue->entries, co_queue_next); - qemu_coroutine_enter(next, NULL); + qemu_coroutine_enter(next); return true; } diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c index 6966831d37..25de3ed3dd 100644 --- a/util/qemu-coroutine-sleep.c +++ b/util/qemu-coroutine-sleep.c @@ -25,7 +25,7 @@ static void co_sleep_cb(void *opaque) { CoSleepCB *sleep_cb = opaque; - qemu_coroutine_enter(sleep_cb->co, NULL); + qemu_coroutine_enter(sleep_cb->co); } void coroutine_fn co_aio_sleep_ns(AioContext *ctx, QEMUClockType type, diff --git a/util/qemu-coroutine.c b/util/qemu-coroutine.c index b7cb636e4d..89f21a9cec 100644 --- a/util/qemu-coroutine.c +++ b/util/qemu-coroutine.c @@ -42,7 +42,7 @@ static void coroutine_pool_cleanup(Notifier *n, void *value) } } -Coroutine *qemu_coroutine_create(CoroutineEntry *entry) +Coroutine *qemu_coroutine_create(CoroutineEntry *entry, void *opaque) { Coroutine *co = NULL; @@ -76,6 +76,7 @@ Coroutine *qemu_coroutine_create(CoroutineEntry *entry) } co->entry = entry; + co->entry_arg = opaque; QSIMPLEQ_INIT(&co->co_queue_wakeup); return co; } @@ -100,12 +101,12 @@ static void coroutine_delete(Coroutine *co) qemu_coroutine_delete(co); } -void qemu_coroutine_enter(Coroutine *co, void *opaque) +void qemu_coroutine_enter(Coroutine *co) { Coroutine *self = qemu_coroutine_self(); CoroutineAction ret; - trace_qemu_coroutine_enter(self, co, opaque); + trace_qemu_coroutine_enter(self, co, co->entry_arg); if (co->caller) { fprintf(stderr, "Co-routine re-entered recursively\n"); @@ -113,7 +114,6 @@ void qemu_coroutine_enter(Coroutine *co, void *opaque) } co->caller = self; - co->entry_arg = opaque; ret = qemu_coroutine_switch(self, co, COROUTINE_ENTER); qemu_co_queue_run_restart(co);