From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35646) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XKQz6-00018P-Ce for qemu-devel@nongnu.org; Thu, 21 Aug 2014 07:56:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XKQz0-00068H-1M for qemu-devel@nongnu.org; Thu, 21 Aug 2014 07:56:52 -0400 Received: from mx1.redhat.com ([209.132.183.28]:40896) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XKQyz-000689-QG for qemu-devel@nongnu.org; Thu, 21 Aug 2014 07:56:45 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s7LBujFT006180 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Thu, 21 Aug 2014 07:56:45 -0400 From: Fam Zheng Date: Thu, 21 Aug 2014 19:56:48 +0800 Message-Id: <1408622216-9578-2-git-send-email-famz@redhat.com> In-Reply-To: <1408622216-9578-1-git-send-email-famz@redhat.com> References: <1408622216-9578-1-git-send-email-famz@redhat.com> Subject: [Qemu-devel] [RFC PATCH 1/9] block: Add bdrv_aio_cancel_async List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Kevin Wolf , Paolo Bonzini , Stefan Hajnoczi This is the async version of bdrv_aio_cancel, which doesn't block the caller. It guarantees that the cb is called either before returning or some time later. Signed-off-by: Fam Zheng --- block.c | 35 +++++++++++++++++++++++++++++++++++ include/block/aio.h | 2 ++ include/block/block.h | 1 + 3 files changed, 38 insertions(+) diff --git a/block.c b/block.c index 6fa0201..1cc8926 100644 --- a/block.c +++ b/block.c @@ -4607,6 +4607,41 @@ void bdrv_aio_cancel(BlockDriverAIOCB *acb) acb->aiocb_info->cancel(acb); } +static void bdrv_aio_cancel_async_cb(void *opaque, int ret) +{ + BlockDriverAIOCB *acb = opaque; + + acb->cb = acb->cancel_acb_save->cb; + acb->opaque = acb->cancel_acb_save->opaque; + g_free(acb->cancel_acb_save); + acb->cancel_acb_save = NULL; + acb->cb(acb->opaque, -ECANCELED); +} + +/* Async version of aio cancel. The caller is not blocked if the acb implements + * cancel_async, otherwise fall back to bdrv_aio_cancel. In both cases, acb->cb + * is guarenteed to be called, before or after function returns. */ +void bdrv_aio_cancel_async(BlockDriverAIOCB *acb) +{ + if (acb->aiocb_info->cancel_async) { + acb->aiocb_info->cancel_async(acb); + } else { + BlockDriverAIOCB *save = g_new(BlockDriverAIOCB, 1); + save->cb = acb->cb; + save->opaque = acb->opaque; + acb->cb = bdrv_aio_cancel_async_cb; + acb->opaque = acb; + acb->cancel_acb_save = save; + + /* Use the synchronous version and hope our cb is called. */ + acb->aiocb_info->cancel(acb); + if (acb->cancel_acb_save) { + /* cb is not called yet, let's call it */ + bdrv_aio_cancel_async_cb(acb->opaque, -ECANCELED); + } + } +} + /**************************************************************/ /* async block device emulation */ diff --git a/include/block/aio.h b/include/block/aio.h index c23de3c..fcc5c87 100644 --- a/include/block/aio.h +++ b/include/block/aio.h @@ -27,6 +27,7 @@ typedef void BlockDriverCompletionFunc(void *opaque, int ret); typedef struct AIOCBInfo { void (*cancel)(BlockDriverAIOCB *acb); + void (*cancel_async)(BlockDriverAIOCB *acb); size_t aiocb_size; } AIOCBInfo; @@ -35,6 +36,7 @@ struct BlockDriverAIOCB { BlockDriverState *bs; BlockDriverCompletionFunc *cb; void *opaque; + BlockDriverAIOCB *cancel_acb_save; }; void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs, diff --git a/include/block/block.h b/include/block/block.h index e94b701..ed2a3a8 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -335,6 +335,7 @@ BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors, BlockDriverCompletionFunc *cb, void *opaque); void bdrv_aio_cancel(BlockDriverAIOCB *acb); +void bdrv_aio_cancel_async(BlockDriverAIOCB *acb); typedef struct BlockRequest { /* Fields to be filled by multiwrite caller */ -- 2.0.3