From: Carlos O'Donell <carlos_odonell@mentor.com>
To: Maxim Kuvyrkov <maxim@codesourcery.com>
Cc: Roland McGrath <roland@hack.frob.com>,
Andrew Haley <aph@redhat.com>, David Miller <davem@davemloft.net>,
"Joseph S. Myers" <joseph@codesourcery.com>,
Richard Sandiford <rdsandiford@googlemail.com>,
<libc-ports@sourceware.org>,
GLIBC Devel <libc-alpha@sourceware.org>,
Chris Metcalf <cmetcalf@tilera.com>
Subject: Re: [PATCH] Unify pthread_spin_[try]lock implementations.
Date: Wed, 15 Aug 2012 16:43:00 -0000 [thread overview]
Message-ID: <502BD18E.2000802@mentor.com> (raw)
In-Reply-To: <36A2FFD8-0C98-4AB6-8C64-2EEC5CC67A63@codesourcery.com>
On 8/14/2012 11:16 PM, Maxim Kuvyrkov wrote:
> On 26/07/2012, at 6:13 AM, Roland McGrath wrote:
>
>> Here I think the reasonable thing to do is:
>>
>> /* A machine-specific version can define SPIN_LOCK_READS_BETWEEN_CMPXCHG
>> to the number of plain reads that it's optimal to spin on between uses
>> of atomic_compare_and_exchange_val_acq. If spinning forever is optimal
>> then use -1. If no plain reads here would ever be optimal, use 0. */
>> #ifndef SPIN_LOCK_READS_BETWEEN_CMPXCHG
>> # warning machine-dependent file should define SPIN_LOCK_READS_BETWEEN_CMPXCHG
>> # define SPIN_LOCK_READS_BETWEEN_CMPXCHG 1000
>> #endif
>>
>> Then ARM et al can do:
>>
>> /* Machine-dependent rationale about the selection of this value. */
>> #define SPIN_LOCK_READS_BETWEEN_CMPXCHG 1000
>> #include <nptl/pthread_spin_lock.c>
>>
>> while Teil will use -1.
>>
>>> + if (PTHREAD_SPIN_LOCK_WAIT)
>>
>> Don't use implicit boolean coercion.
>> Use "if (SPIN_LOCK_READS_BETWEEN_CMPXCHG >= 0)".
>>
>>> + {
>>> + int wait = PTHREAD_SPIN_LOCK_WAIT;
>>> +
>>> + while (*lock != 0 && --wait)
>>> + ;
>>
>> Write it:
>> while (wait > 0 && *lock != 0)
>> --wait;
>>
>> That handles the SPIN_LOCK_READS_BETWEEN_CMPXCHG==0 case implicitly,
>> avoids the ugly empty statement, and doesn't use implicit coercion.
>
> OK, the updated patch attached. Is this what you had in mind?
>
> As before the patch touches ARM, HPPA, M68K and MIPS.
>
> The patch also adds another optimization to use atomic_exchange_acq for the first attempt at acquiring the lock. For a free lock atomic_exchange is, generally, faster; while atomic_compare_and_exchange is better for waiting on a contended lock. Same rationale applies to pthread_spin_trylock.
>
> This patch builds on MIPS and regression testing is in progress. OK to apply is tests are fine?
>
> Thank you,
>
> --
> Maxim Kuvyrkov
> CodeSourcery / Mentor Graphics
>
>
> Add generic versions of pthread_spin_lock and pthread_spin_trylock.
>
> 2012-07-09 Maxim Kuvyrkov <maxim@codesourcery.com>
>
> * nptl/pthread_spin_lock.c: New file.
> * nptl/pthread_spin_trylock.c: New file.
>
> ports/
> * sysdeps/arm/nptl/pthread_spin_lock.c: Use generic code.
> * sysdeps/arm/nptl/pthread_spin_trylock.c: Remove, use generic version.
>
> * sysdeps/hppa/nptl/pthread_spin_lock.c: Use generic code.
> * sysdeps/hppa/nptl/pthread_spin_trylock.c: Remove, use generic version.
>
> * sysdeps/m68k/nptl/pthread_spin_lock.c: Use generic code.
> * sysdeps/m68k/nptl/pthread_spin_trylock.c: Remove, use generic version.
>
> * sysdeps/mips/nptl/pthread_spin_lock.S: Remove, use generic version.
> * sysdeps/mips/nptl/pthread_spin_lock.c: New file.
> * sysdeps/mips/nptl/pthread_spin_trylock.S: Remove, use generic version.
> ---
> nptl/pthread_spin_lock.c | 69 ++++++++++++++++++++++++
> nptl/pthread_spin_trylock.c | 27 +++++++++
> ports/sysdeps/arm/nptl/pthread_spin_lock.c | 16 +-----
> ports/sysdeps/arm/nptl/pthread_spin_trylock.c | 26 ---------
> ports/sysdeps/hppa/nptl/pthread_spin_lock.c | 24 +-------
> ports/sysdeps/hppa/nptl/pthread_spin_trylock.c | 33 -----------
> ports/sysdeps/m68k/nptl/pthread_spin_lock.c | 16 +-----
> ports/sysdeps/m68k/nptl/pthread_spin_trylock.c | 27 ---------
> ports/sysdeps/mips/nptl/pthread_spin_lock.S | 36 ------------
> ports/sysdeps/mips/nptl/pthread_spin_lock.c | 19 +++++++
> ports/sysdeps/mips/nptl/pthread_spin_trylock.S | 40 --------------
> 11 files changed, 124 insertions(+), 209 deletions(-)
> create mode 100644 nptl/pthread_spin_lock.c
> create mode 100644 nptl/pthread_spin_trylock.c
> delete mode 100644 ports/sysdeps/arm/nptl/pthread_spin_trylock.c
> delete mode 100644 ports/sysdeps/hppa/nptl/pthread_spin_trylock.c
> delete mode 100644 ports/sysdeps/m68k/nptl/pthread_spin_trylock.c
> delete mode 100644 ports/sysdeps/mips/nptl/pthread_spin_lock.S
> create mode 100644 ports/sysdeps/mips/nptl/pthread_spin_lock.c
> delete mode 100644 ports/sysdeps/mips/nptl/pthread_spin_trylock.S
>
> diff --git a/ports/sysdeps/hppa/nptl/pthread_spin_lock.c b/ports/sysdeps/hppa/nptl/pthread_spin_lock.c
> index bcf2240..7c86768 100644
> --- a/ports/sysdeps/hppa/nptl/pthread_spin_lock.c
> +++ b/ports/sysdeps/hppa/nptl/pthread_spin_lock.c
> @@ -1,4 +1,4 @@
> -/* Copyright (C) 2005 Free Software Foundation, Inc.
> +/* Copyright (C) 2005-2012 Free Software Foundation, Inc.
> This file is part of the GNU C Library.
>
> The GNU C Library is free software; you can redistribute it and/or
> @@ -15,23 +15,5 @@
> License along with the GNU C Library. If not, see
> <http://www.gnu.org/licenses/>. */
>
> -#include <atomic.h>
> -#include "pthreadP.h"
> -
> -int
> -pthread_spin_lock (pthread_spinlock_t *lock)
> -{
> -#if 0
> - volatile unsigned int *addr = __ldcw_align (lock);
> -
> - while (__ldcw (addr) == 0)
> - while (*addr == 0) ;
> -
> - return 0;
> -#endif
> -
> - while (atomic_compare_and_exchange_val_acq(lock, 1, 0) == 1)
> - while (*lock == 1);
> -
> - return 0;
> -}
> +#define SPIN_LOCK_READS_BETWEEN_CMPXCHG 1000
> +#include_next <nptl/pthread_spin_lock.c>
OK for hppa.
> diff --git a/ports/sysdeps/hppa/nptl/pthread_spin_trylock.c b/ports/sysdeps/hppa/nptl/pthread_spin_trylock.c
> deleted file mode 100644
> index a802861..0000000
> --- a/ports/sysdeps/hppa/nptl/pthread_spin_trylock.c
> +++ /dev/null
> @@ -1,33 +0,0 @@
> -/* Copyright (C) 2005 Free Software Foundation, Inc.
> - This file is part of the GNU C Library.
> -
> - The GNU C Library is free software; you can redistribute it and/or
> - modify it under the terms of the GNU Lesser General Public
> - License as published by the Free Software Foundation; either
> - version 2.1 of the License, or (at your option) any later version.
> -
> - The GNU C Library is distributed in the hope that it will be useful,
> - but WITHOUT ANY WARRANTY; without even the implied warranty of
> - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> - Lesser General Public License for more details.
> -
> - You should have received a copy of the GNU Lesser General Public
> - License along with the GNU C Library. If not, see
> - <http://www.gnu.org/licenses/>. */
> -
> -#include <errno.h>
> -#include <atomic.h>
> -#include "pthreadP.h"
> -
> -int
> -pthread_spin_trylock (pthread_spinlock_t *lock)
> -{
> -#if 0
> - volatile unsigned int *a = __ldcw_align (lock);
> -
> - return __ldcw (a) ? 0 : EBUSY;
> -#endif
> -
> - return atomic_compare_and_exchange_val_acq(lock, 1, 0) ? EBUSY : 0;
> -
> -}
OK for hppa.
Cheers,
Carlos.
--
Carlos O'Donell
Mentor Graphics / CodeSourcery
carlos_odonell@mentor.com
carlos@codesourcery.com
+1 (613) 963 1026
next prev parent reply other threads:[~2012-08-15 16:43 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
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 [this message]
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=502BD18E.2000802@mentor.com \
--to=carlos_odonell@mentor.com \
--cc=aph@redhat.com \
--cc=cmetcalf@tilera.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).