From: Andrew Haley <aph@redhat.com>
To: Maxim Kuvyrkov <maxim@codesourcery.com>
Cc: David Miller <davem@davemloft.net>,
roland@hack.frob.com, joseph@codesourcery.com,
rdsandiford@googlemail.com, libc-ports@sourceware.org,
libc-alpha@sourceware.org
Subject: Re: [PATCH] Unify pthread_spin_[try]lock implementations.
Date: Wed, 11 Jul 2012 09:02:00 -0000 [thread overview]
Message-ID: <4FFD4114.9000806@redhat.com> (raw)
In-Reply-To: <84304C03-6A49-4263-9016-05486EDC0E98@codesourcery.com>
On 07/11/2012 09:53 AM, Maxim Kuvyrkov wrote:
> On 11/07/2012, at 8:44 PM, Andrew Haley wrote:
>
>> 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.
>
> Hm, but this is exactly what ARM port does and did for a while. I
> guess the memory on the local processor eventually gets updated and
> the loop breaks.
Maybe the thread ends its time slice and gets interrupted. There
aren't any guarantees.
> This is an interesting point and maybe introducing a counter like
> below will improve spinlocks for ARM.
>
> + int counter = 123456;
> + while (atomic_compare_and_exchange_val_acq (lock, 1, 0) != 0)
> + while (*lock != 0 && --counter)
> + ;
This is a much better idea than an unbounded spin.
Andrew.
next prev parent reply other threads:[~2012-07-11 9:02 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-06-14 4:36 [PATCH 2/3, MIPS] Rewrite MIPS' pthread_spin_[try]lock using __atomic_* builtins Maxim Kuvyrkov
2012-06-28 23:10 ` Joseph S. Myers
2012-07-11 5:58 ` [PATCH] Unify pthread_spin_[try]lock implementations Maxim Kuvyrkov
2012-07-11 8:15 ` Roland McGrath
2012-07-11 8:25 ` David Miller
2012-07-11 8:44 ` Andrew Haley
2012-07-11 8:54 ` Maxim Kuvyrkov
2012-07-11 9:02 ` Andrew Haley [this message]
2012-07-11 9:05 ` Maxim Kuvyrkov
2012-07-11 11:22 ` Roland McGrath
2012-07-11 14:52 ` Chris Metcalf
2012-07-11 22:16 ` Maxim Kuvyrkov
2012-07-25 18:13 ` Roland McGrath
2012-07-25 19:04 ` Chris Metcalf
2012-07-25 20:22 ` Roland McGrath
2012-07-25 20:29 ` Chris Metcalf
2012-07-25 20:43 ` Roland McGrath
2012-08-15 3:17 ` Maxim Kuvyrkov
2012-08-15 16:27 ` Roland McGrath
2012-08-15 16:39 ` Maxim Kuvyrkov
2012-08-15 16:44 ` Roland McGrath
2012-08-15 16:53 ` Maxim Kuvyrkov
2012-08-15 16:56 ` Roland McGrath
2012-08-15 17:05 ` Maxim Kuvyrkov
2012-08-15 17:05 ` Jeff Law
2012-08-15 17:11 ` Roland McGrath
2012-08-15 17:24 ` Jeff Law
2012-08-15 16:40 ` Joseph S. Myers
2012-08-15 16:43 ` Carlos O'Donell
2012-08-15 22:00 ` Andreas Schwab
2012-08-16 10:21 ` Torvald Riegel
2012-08-16 12:40 ` Chris Metcalf
2012-08-22 23:16 ` Maxim Kuvyrkov
2012-07-11 8:26 ` Maxim Kuvyrkov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4FFD4114.9000806@redhat.com \
--to=aph@redhat.com \
--cc=davem@davemloft.net \
--cc=joseph@codesourcery.com \
--cc=libc-alpha@sourceware.org \
--cc=libc-ports@sourceware.org \
--cc=maxim@codesourcery.com \
--cc=rdsandiford@googlemail.com \
--cc=roland@hack.frob.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).