public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Jangwoong Kim <6812skiii@gmail.com>
To: Maxim Kuvyrkov <maxim.kuvyrkov@linaro.org>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH] nptl: Effectively skip CAS in spinlock loop
Date: Tue, 7 Dec 2021 03:03:01 +0900	[thread overview]
Message-ID: <CAF=mnpjwqpaajLFHxoiP14X2q9EK=Qg+kuQWBCTvgQbcstw3Xw@mail.gmail.com> (raw)
In-Reply-To: <331FA4DD-8EE9-4538-A462-B68A3617E41C@linaro.org>

Hi Maxim,

Did you mean editing the commit message?

If so, I will resend a patch right away.

Or, if you meant adding a comment to source code, I'm not clear on your request.

The reason do-while loop structure causes an extra CAS is "continue"
expression does not skip the conditional expression do-while loop.

My patch replaces the do-while loop, so there isn't an extra CAS.

Thus, adding a comment why the previous code was wrong doesn't seem proper.

--
Thank you.
Jangwoong Kim

2021년 12월 6일 (월) 오후 11:26, Maxim Kuvyrkov <maxim.kuvyrkov@linaro.org>님이 작성:
>
>
> > On 5 Dec 2021, at 18:28, Jangwoong Kim via Libc-alpha <libc-alpha@sourceware.org> wrote:
> >
> > The commit:
> > "Add LLL_MUTEX_READ_LOCK [BZ #28537]"
> > SHA1: d672a98a1af106bd68deb15576710cd61363f7a6
> >
> > introduced LLL_MUTEX_READ_LOCK, to skip CAS in spinlock loop
> > if atmoic load fails. But, "continue" inside of do-while loop
> > does not skip the evaluation of escape expression, thus CAS
> > is not skipped.
> >
> > Replace do-while with while and break if LLL_MUTEX_READ_LOCK fails.
> > ---
> > nptl/pthread_mutex_lock.c | 5 +++--
> > 1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/nptl/pthread_mutex_lock.c b/nptl/pthread_mutex_lock.c
> > index 47b88a6b5b..24936c8d56 100644
> > --- a/nptl/pthread_mutex_lock.c
> > +++ b/nptl/pthread_mutex_lock.c
> > @@ -138,7 +138,7 @@ PTHREAD_MUTEX_LOCK (pthread_mutex_t *mutex)
> >         int cnt = 0;
> >         int max_cnt = MIN (max_adaptive_count (),
> >                            mutex->__data.__spins * 2 + 10);
> > -       do
> > +       while (1)
> >           {
> >             if (cnt++ >= max_cnt)
> >               {
> > @@ -148,8 +148,9 @@ PTHREAD_MUTEX_LOCK (pthread_mutex_t *mutex)
> >             atomic_spin_nop ();
> >             if (LLL_MUTEX_READ_LOCK (mutex) != 0)
> >               continue;
> > +           if (LLL_MUTEX_TRYLOCK (mutex) == 0)
> > +             break;
> >           }
> > -       while (LLL_MUTEX_TRYLOCK (mutex) != 0);
>
> Giving this a second look, would you please add a comment before “break” to explain why do-while loop structure would cause an extra compare-and-swap?
>
> --
> Maxim Kuvyrkov
> https://www.linaro.org
>
> >
> >         mutex->__data.__spins += (cnt - mutex->__data.__spins) / 8;
> >       }
> > --
> > 2.25.1
> >
>

  reply	other threads:[~2021-12-06 18:03 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-05 15:28 Jangwoong Kim
2021-12-06 14:11 ` Maxim Kuvyrkov
2021-12-06 14:26 ` Maxim Kuvyrkov
2021-12-06 18:03   ` Jangwoong Kim [this message]
2021-12-13 13:42     ` Maxim Kuvyrkov
2021-12-13 14:42 ` Andreas Schwab

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='CAF=mnpjwqpaajLFHxoiP14X2q9EK=Qg+kuQWBCTvgQbcstw3Xw@mail.gmail.com' \
    --to=6812skiii@gmail.com \
    --cc=libc-alpha@sourceware.org \
    --cc=maxim.kuvyrkov@linaro.org \
    /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).