This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Unify pthread_spin_[try]lock implementations.


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]