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: David Miller <davem at davemloft dot net>
- To: roland at hack dot frob dot com
- Cc: maxim at codesourcery dot com, joseph at codesourcery dot com,rdsandiford at googlemail dot com, libc-ports at sourceware dot org,libc-alpha at sourceware dot org
- Date: Wed, 11 Jul 2012 01:25:09 -0700 (PDT)
- Subject: Re: [PATCH] Unify pthread_spin_[try]lock implementations.
- References: <Pine.LNX.4.64.1206282306320.20312@digraph.polyomino.org.uk><65B470D2-4D01-4BA1-AEC5-A72C0006EA22@codesourcery.com><20120711081441.73BB22C093@topped-with-meat.com>
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.