From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41261) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VTruF-0003mW-Bg for qemu-devel@nongnu.org; Wed, 09 Oct 2013 07:26:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VTru9-0000PE-DC for qemu-devel@nongnu.org; Wed, 09 Oct 2013 07:26:19 -0400 Received: from mx1.redhat.com ([209.132.183.28]:14246) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VTru9-0000P6-5L for qemu-devel@nongnu.org; Wed, 09 Oct 2013 07:26:13 -0400 Date: Wed, 9 Oct 2013 13:26:09 +0200 From: Stefan Hajnoczi Message-ID: <20131009112609.GA29850@stefanha-thinkpad.muc.redhat.com> References: <1381312531-28723-1-git-send-email-stefanha@redhat.com> <1381312531-28723-2-git-send-email-stefanha@redhat.com> <52552A77.7030504@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <52552A77.7030504@redhat.com> 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: Paolo Bonzini Cc: Kevin Wolf , xiawenc@linux.vnet.ibm.com, qemu-devel@nongnu.org, Michael Roth 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) { ... } } There's also a shorter approach which collapses everything into a single if statement, but I find the if expression a harder to understand: if (r->nesting == 0 || !qemu_thread_is_self(&r->owner_thread)) { unsigned int ticket = r->tail++; while (ticket != r->head) { ... } } Any preferences? Stefan