[PATCH] Unify pthread_spin_[try]lock implementations.

Andrew Haley aph@redhat.com
Wed Jul 11 08:44:00 GMT 2012


On 07/11/2012 09:25 AM, David Miller wrote:
> From: Roland McGrath <roland@hack.frob.com>
> Date: Wed, 11 Jul 2012 01:14:41 -0700 (PDT)
> 
>>> +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?
> 
> I guess the idea is to spin with non-atomic reads when the lock is
> contended so we don't do expensive bus cycles grabbing the cache line
> in exclusive state over and over again.
> 
> If we spun using only the atomic it would be very expensive.

Sure, but on ARM at least there's no guarantee that the local processor
will see changes to the state of the lock when another processor frees
it.

Andrew.



More information about the Libc-alpha mailing list