From: Keir Fraser Subject: Re: [PATCH] x86/hpet: simplify hpet_get_channel() Date: Fri, 23 Mar 2012 10:24:11 +0000 Message-ID: References: <4F6C4E6D020000780007A799@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <4F6C4E6D020000780007A799@nat28.tlf.novell.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Jan Beulich , xen-devel Cc: Gang Wei List-Id: xen-devel@lists.xenproject.org On 23/03/2012 09:20, "Jan Beulich" wrote: > There's no need for a lock here, elimination of which makes the > function a leaf one, thus allowing for better (and smaller) code. > > Further, use the variable next_channel according to its name - so far > it represented the most recently used channel rather than the next one > to use. Why is the following for-loop changed into do-while? It looks like it just makes the code longer, especially as you replace a % operator with open-coded equivalent for no reason I can see. -- Keir > Signed-off-by: Jan Beulich > > --- a/xen/arch/x86/hpet.c > +++ b/xen/arch/x86/hpet.c > @@ -402,30 +402,35 @@ static void __init hpet_fsb_cap_lookup(v > static struct hpet_event_channel *hpet_get_channel(unsigned int cpu) > { > static unsigned int next_channel; > - static spinlock_t next_lock = SPIN_LOCK_UNLOCKED; > unsigned int i, next; > struct hpet_event_channel *ch; > > if ( num_hpets_used == 0 ) > return hpet_events; > > - spin_lock(&next_lock); > - next = next_channel = (next_channel + 1) % num_hpets_used; > - spin_unlock(&next_lock); > + do { > + next = next_channel; > + if ( (i = next + 1) == num_hpets_used ) > + i = 0; > + } while ( cmpxchg(&next_channel, next, i) != next ); > > /* try unused channel first */ > - for ( i = next; i < next + num_hpets_used; i++ ) > - { > - ch = &hpet_events[i % num_hpets_used]; > + ch = &hpet_events[i = next]; > + do { > if ( !test_and_set_bit(HPET_EVT_USED_BIT, &ch->flags) ) > { > ch->cpu = cpu; > return ch; > } > - } > + ++ch; > + if ( ++i == num_hpets_used ) > + { > + i = 0; > + ch = hpet_events; > + } > + } while ( i != next ); > > - /* share a in-use channel */ > - ch = &hpet_events[next]; > + /* Share an in-use channel. */ > if ( !test_and_set_bit(HPET_EVT_USED_BIT, &ch->flags) ) > ch->cpu = cpu; > > > > > _______________________________________________ > Xen-devel mailing list > Xen-devel@lists.xen.org > http://lists.xen.org/xen-devel