This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH] Unify pthread_spin_[try]lock implementations.
- From: Maxim Kuvyrkov <maxim at codesourcery dot com>
- To: Roland McGrath <roland at hack dot frob dot com>
- Cc: Joseph S.Myers <joseph at codesourcery dot com>, Richard Sandiford<rdsandiford at googlemail dot com>, <libc-ports at sourceware dot org>, libc-alpha Devel<libc-alpha at sourceware dot org>
- Date: Wed, 11 Jul 2012 20:26:06 +1200
- Subject: Re: [PATCH] Unify pthread_spin_[try]lock implementations.
- References: <2109EAD5-BBE8-4C8C-8D61-0AF33290F240@codesourcery.com> <Pine.LNX.4.64.1206282306320.20312@digraph.polyomino.org.uk> <65B470D2-4D01-4BA1-AEC5-A72C0006EA22@codesourcery.com> <20120711081441.73BB22C093@topped-with-meat.com>
On 11/07/2012, at 8:14 PM, Roland McGrath wrote:
>> +int
>> +pthread_spin_lock (pthread_spinlock_t *lock)
>> +{
>> + while (atomic_compare_and_exchange_val_acq (lock, 1, 0) != 0)
>> + while (*lock != 0)
>> + ;
>
> What's the inner loop for?
My understanding is that this is an optimization. On many architectures atomic_compare_and_exchange will synchronize memory across all CPUs, which will penalize other running threads. Spinning in a simple (*lock != 0) loop will allow those threads to finish whatever they are doing faster and release the lock.
--
Maxim Kuvyrkov
CodeSourcery / Mentor Graphics