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