From patchwork Thu Nov 18 18:06:55 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arun Bharadwaj X-Patchwork-Id: 72126 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 30A00B7194 for ; Fri, 19 Nov 2010 05:13:44 +1100 (EST) Received: from localhost ([127.0.0.1]:58746 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PJ8yj-00077g-Jp for incoming@patchwork.ozlabs.org; Thu, 18 Nov 2010 13:13:02 -0500 Received: from [140.186.70.92] (port=37041 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PJ8sy-0004tS-Lq for qemu-devel@nongnu.org; Thu, 18 Nov 2010 13:07:06 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PJ8sw-0003lJ-Sw for qemu-devel@nongnu.org; Thu, 18 Nov 2010 13:07:03 -0500 Received: from e28smtp09.in.ibm.com ([122.248.162.9]:48200) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PJ8sw-0003ku-7Y for qemu-devel@nongnu.org; Thu, 18 Nov 2010 13:07:02 -0500 Received: from d28relay03.in.ibm.com (d28relay03.in.ibm.com [9.184.220.60]) by e28smtp09.in.ibm.com (8.14.4/8.13.1) with ESMTP id oAIHZc2p002442 for ; Thu, 18 Nov 2010 23:05:38 +0530 Received: from d28av05.in.ibm.com (d28av05.in.ibm.com [9.184.220.67]) by d28relay03.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id oAII6vCU4272358 for ; Thu, 18 Nov 2010 23:36:57 +0530 Received: from d28av05.in.ibm.com (loopback [127.0.0.1]) by d28av05.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id oAII6v6P001377 for ; Fri, 19 Nov 2010 05:06:57 +1100 Received: from localhost6.localdomain6 ([9.77.206.31]) by d28av05.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id oAII6uUt001338; Fri, 19 Nov 2010 05:06:56 +1100 To: qemu-devel@nongnu.org From: Arun R Bharadwaj Date: Thu, 18 Nov 2010 23:36:55 +0530 Message-ID: <20101118180655.4434.23045.stgit@localhost6.localdomain6> In-Reply-To: <20101118180547.4434.95904.stgit@localhost6.localdomain6> References: <20101118180547.4434.95904.stgit@localhost6.localdomain6> User-Agent: StGit/0.15 MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: kwolf@redhat.com Subject: [Qemu-devel] [PATCH 2/6] Move paio_cancel() to new infrastructure. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Move paio_cancel() to new infrastructure and introduce the necessary APIs for this. Signed-off-by: Arun R Bharadwaj --- posix-aio-compat.c | 92 ++++++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 74 insertions(+), 18 deletions(-) diff --git a/posix-aio-compat.c b/posix-aio-compat.c index 7b862b5..eb1e2db 100644 --- a/posix-aio-compat.c +++ b/posix-aio-compat.c @@ -27,9 +27,31 @@ #include "qemu-common.h" #include "trace.h" #include "block_int.h" +#include "qemu-thread.h" #include "block/raw-posix-aio.h" +static QemuMutex aiocb_mutex; +static QemuCond aiocb_completion; + +typedef struct ThreadletQueue +{ + QemuMutex lock; + QemuCond cond; + int max_threads; + int min_threads; + int cur_threads; + int idle_threads; + QTAILQ_HEAD(, ThreadletWork) request_list; +} ThreadletQueue; + +typedef struct ThreadletWork +{ + QTAILQ_ENTRY(ThreadletWork) node; + void (*func)(struct ThreadletWork *work); +} ThreadletWork; + +static ThreadletQueue globalqueue; struct qemu_paiocb { BlockDriverAIOCB common; @@ -51,6 +73,7 @@ struct qemu_paiocb { struct qemu_paiocb *next; int async_context_id; + ThreadletWork work; }; typedef struct PosixAioState { @@ -58,6 +81,45 @@ typedef struct PosixAioState { struct qemu_paiocb *first_aio; } PosixAioState; +/** + * dequeue_work_on_queue: Cancel a task queued on a Queue. + * @queue: The queue containing the task to be cancelled. + * @work: Contains the information of the task that needs to be cancelled. + * + * Returns: 0 if the task is successfully cancelled. + * 1 otherwise. + */ + +static int dequeue_work_on_queue(ThreadletQueue *queue, ThreadletWork *work) +{ + ThreadletWork *ret_work; + int ret = 1; + + qemu_mutex_lock(&queue->lock); + QTAILQ_FOREACH(ret_work, &(queue->request_list), node) { + if (ret_work == work) { + QTAILQ_REMOVE(&queue->request_list, ret_work, node); + ret = 0; + break; + } + } + qemu_mutex_unlock(&queue->lock); + + return ret; +} + +/** + * dequeue_work: Cancel a task queued on the global queue. + * @work: Contains the information of the task that needs to be cancelled. + * + * Returns: 0 if the task is successfully cancelled. + * 1 otherwise. + */ + +static int dequeue_work(ThreadletWork *work) +{ + return dequeue_work_on_queue(&globalqueue, work); +} static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; @@ -397,9 +459,9 @@ static ssize_t qemu_paio_return(struct qemu_paiocb *aiocb) { ssize_t ret; - mutex_lock(&lock); + qemu_mutex_lock(&aiocb_mutex); ret = aiocb->ret; - mutex_unlock(&lock); + qemu_mutex_unlock(&aiocb_mutex); return ret; } @@ -534,22 +596,13 @@ static void paio_remove(struct qemu_paiocb *acb) static void paio_cancel(BlockDriverAIOCB *blockacb) { struct qemu_paiocb *acb = (struct qemu_paiocb *)blockacb; - int active = 0; - - mutex_lock(&lock); - if (!acb->active) { - QTAILQ_REMOVE(&request_list, acb, node); - acb->ret = -ECANCELED; - } else if (acb->ret == -EINPROGRESS) { - active = 1; - } - mutex_unlock(&lock); - - if (active) { - /* fail safe: if the aio could not be canceled, we wait for - it */ - while (qemu_paio_error(acb) == EINPROGRESS) - ; + if (dequeue_work(&acb->work) != 0) { + /* Wait for running work item to complete */ + qemu_mutex_lock(&aiocb_mutex); + while (acb->ret == -EINPROGRESS) { + qemu_cond_wait(&aiocb_completion, &aiocb_mutex); + } + qemu_mutex_unlock(&aiocb_mutex); } paio_remove(acb); @@ -623,6 +676,9 @@ int paio_init(void) if (posix_aio_state) return 0; + qemu_mutex_init(&aiocb_mutex); + qemu_cond_init(&aiocb_completion); + s = qemu_malloc(sizeof(PosixAioState)); sigfillset(&act.sa_mask);