From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43678) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VTs3w-00060k-8A for qemu-devel@nongnu.org; Wed, 09 Oct 2013 07:36:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VTs3q-00047w-58 for qemu-devel@nongnu.org; Wed, 09 Oct 2013 07:36:20 -0400 Received: from mx1.redhat.com ([209.132.183.28]:58629) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VTs3p-00047i-TD for qemu-devel@nongnu.org; Wed, 09 Oct 2013 07:36:14 -0400 Message-ID: <52553FA5.70108@redhat.com> Date: Wed, 09 Oct 2013 13:36:05 +0200 From: Paolo Bonzini MIME-Version: 1.0 References: <1381312531-28723-1-git-send-email-stefanha@redhat.com> <1381312531-28723-2-git-send-email-stefanha@redhat.com> <52552A77.7030504@redhat.com> <20131009112609.GA29850@stefanha-thinkpad.muc.redhat.com> In-Reply-To: <20131009112609.GA29850@stefanha-thinkpad.muc.redhat.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH 1/2] rfifolock: add recursive FIFO lock List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Stefan Hajnoczi Cc: Kevin Wolf , xiawenc@linux.vnet.ibm.com, qemu-devel@nongnu.org, Michael Roth Il 09/10/2013 13:26, Stefan Hajnoczi ha scritto: > On Wed, Oct 09, 2013 at 12:05:43PM +0200, Paolo Bonzini wrote: >> Il 09/10/2013 11:55, Stefan Hajnoczi ha scritto: >>> + /* Take a ticket */ >>> + unsigned int ticket = r->tail++; >>> + >>> + if (r->nesting > 0) { >>> + if (qemu_thread_is_self(&r->owner_thread)) { >>> + r->tail--; /* put ticket back, we're nesting */ >>> + } else { >> >> ticket is dead in the "nested" path, why not move it (and the increment) >> directly after the else? > > The increment cannot be moved because there are 3 cases: > > 1. Uncontended lock: r->tail++ > 2. Recursive lock: do not change tail > 3. Wait for contended lock: r->tail++ > > Case #1 needs r->tail++ too. > > I find this approach clearest: > > if (r->nesting == 0) { > /* Uncontended, just take a ticket */ > r->tail++; > } else if (qemu_thread_is_self(&r->owner_thread)) { > /* Recursive lock, no need to take a ticket */ > } else { > /* Contended case, take a ticket and wait for it */ > unsigned int ticket = r->tail++; > > while (ticket != r->head) { > ... > } > } I like this one too. Paolo