From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40885) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V7qkQ-0002jg-Mo for qemu-devel@nongnu.org; Fri, 09 Aug 2013 13:45:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1V7qkJ-0005vg-Fu for qemu-devel@nongnu.org; Fri, 09 Aug 2013 13:45:10 -0400 Received: from mail6.webfaction.com ([74.55.86.74]:50510 helo=smtp.webfaction.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V7qkJ-0005uK-7a for qemu-devel@nongnu.org; Fri, 09 Aug 2013 13:45:03 -0400 From: Charlie Shepherd Date: Fri, 9 Aug 2013 19:43:59 +0200 Message-Id: <1376070245-22557-9-git-send-email-charlie@ctshepherd.com> In-Reply-To: <1376070245-22557-1-git-send-email-charlie@ctshepherd.com> References: <1376070245-22557-1-git-send-email-charlie@ctshepherd.com> Subject: [Qemu-devel] [RFC v2 09/15] Add a synchronous wrapper bdrv_sync_rwco List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, stefanha@gmail.com, gabriel@kerneis.info, Charlie Shepherd , pbonzini@redhat.com A number of functions in block.c pass an RwCo struct to a coroutine entry point in order to synchronise an asynchronous function. This patch factors this pattern out into a function. Signed-off-by: Charlie Shepherd --- block.c | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/block.c b/block.c index 51a6649..25edb44 100644 --- a/block.c +++ b/block.c @@ -2171,6 +2171,17 @@ typedef struct RwCo { BdrvRequestFlags flags; } RwCo; +static int bdrv_sync_rwco(void coroutine_fn (*co_fn)(void *), RwCo *rwco) +{ + Coroutine *co; + co = qemu_coroutine_create(co_fn); + qemu_coroutine_enter(co, rwco); + while (rwco->ret == NOT_DONE) { + qemu_aio_wait(); + } + return rwco->ret; +} + static void coroutine_fn bdrv_rw_co_entry(void *opaque) { RwCo *rwco = opaque; @@ -2219,14 +2230,10 @@ static int bdrv_rwv_co(BlockDriverState *bs, int64_t sector_num, if (qemu_in_coroutine()) { /* Fast-path if already in coroutine context */ bdrv_rw_co_entry(&rwco); + return rwco.ret; } else { - co = qemu_coroutine_create(bdrv_rw_co_entry); - qemu_coroutine_enter(co, &rwco); - while (rwco.ret == NOT_DONE) { - qemu_aio_wait(); - } + return bdrv_sync_rwco(bdrv_rw_co_entry, &rwco); } - return rwco.ret; } /* @@ -4144,15 +4151,10 @@ int bdrv_flush(BlockDriverState *bs) if (qemu_in_coroutine()) { /* Fast-path if already in coroutine context */ bdrv_flush_co_entry(&rwco); + return rwco.ret; } else { - co = qemu_coroutine_create(bdrv_flush_co_entry); - qemu_coroutine_enter(co, &rwco); - while (rwco.ret == NOT_DONE) { - qemu_aio_wait(); - } + return bdrv_sync_rwco(bdrv_flush_co_entry, &rwco); } - - return rwco.ret; } static void coroutine_fn bdrv_discard_co_entry(void *opaque) @@ -4216,15 +4218,10 @@ int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) if (qemu_in_coroutine()) { /* Fast-path if already in coroutine context */ bdrv_discard_co_entry(&rwco); + return rwco.ret; } else { - co = qemu_coroutine_create(bdrv_discard_co_entry); - qemu_coroutine_enter(co, &rwco); - while (rwco.ret == NOT_DONE) { - qemu_aio_wait(); - } + return bdrv_sync_rwco(bdrv_discard_co_entry, &rwco); } - - return rwco.ret; } /**************************************************************/ -- 1.8.3.2