From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41968) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VEqkm-0006WA-8I for qemu-devel@nongnu.org; Wed, 28 Aug 2013 21:10:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VEqkd-00052N-B6 for qemu-devel@nongnu.org; Wed, 28 Aug 2013 21:10:28 -0400 Received: from e23smtp02.au.ibm.com ([202.81.31.144]:37344) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VEqkc-0004za-GB for qemu-devel@nongnu.org; Wed, 28 Aug 2013 21:10:19 -0400 Received: from /spool/local by e23smtp02.au.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 29 Aug 2013 10:58:48 +1000 Received: from d23relay05.au.ibm.com (d23relay05.au.ibm.com [9.190.235.152]) by d23dlp03.au.ibm.com (Postfix) with ESMTP id C41A13578052 for ; Thu, 29 Aug 2013 11:10:11 +1000 (EST) Received: from d23av03.au.ibm.com (d23av03.au.ibm.com [9.190.234.97]) by d23relay05.au.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r7T0rokH60489828 for ; Thu, 29 Aug 2013 10:53:56 +1000 Received: from d23av03.au.ibm.com (localhost [127.0.0.1]) by d23av03.au.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id r7T1A5au005057 for ; Thu, 29 Aug 2013 11:10:05 +1000 Message-ID: <521E9F59.6060709@linux.vnet.ibm.com> Date: Thu, 29 Aug 2013 09:09:45 +0800 From: Wenchao Xia MIME-Version: 1.0 References: <1377614385-20466-1-git-send-email-stefanha@redhat.com> <521D6DAD.6080309@linux.vnet.ibm.com> <20130828084936.GF4696@stefanha-thinkpad.muc.redhat.com> In-Reply-To: <20130828084936.GF4696@stefanha-thinkpad.muc.redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [Qemu-devel] [RFC] aio: add aio_context_acquire() and aio_context_release() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Stefan Hajnoczi Cc: Kevin Wolf , Paolo Bonzini , qemu-devel@nongnu.org, Stefan Hajnoczi 于 2013-8-28 16:49, Stefan Hajnoczi 写道: > On Wed, Aug 28, 2013 at 11:25:33AM +0800, Wenchao Xia wrote: >>> +void aio_context_release(AioContext *ctx) >>> +{ >>> + qemu_mutex_lock(&ctx->acquire_lock); >>> + assert(ctx->owner && qemu_thread_is_self(ctx->owner)); >>> + ctx->owner = NULL; >>> + qemu_cond_signal(&ctx->acquire_cond); >>> + qemu_mutex_unlock(&ctx->acquire_lock); >>> +} >> if main thread have call bdrv_aio_readv(cb *bdrv_cb), now it >> is possible bdrv_cb will be executed in another thread which >> aio_context_acquire() it. I think there are some ways to solve, >> but leave a comments here now to tip better? > > Callbacks, BHs, and timers are executed in the thread that calls > aio_poll(). This is safe since other threads cannot run aio_poll() or > submit new block I/O requests at the same time. > > In other words: code should only care which AioContext it runs under, > not which thread ID it runs under. (I think we talked about this on IRC > a few weeks ago.) > > Are there any situations you are worried about? > > Stefan > Yes, we have discussed it before and think it may be safe for block driver caller. Still, here I mean to add some in-code comment to tip how to use it safely. for example: static int glob_test = 0; int aio_cb(void *opaque) { glob_test++; } Thread A: bdrv_aio_read(bs, aio_cb...); ..... glob_test++; Normally glob_test have no race condition since they supposed to work in one thread, but it need to be considered when aio_context_acquire() is involved. How about: /* Note that callback can run in different thread which acquired the AioContext and do a poll() call. */ -- Best Regards Wenchao Xia