public inbox for libc-ports@sourceware.org
 help / color / mirror / Atom feed
From: Maxim Kuvyrkov <maxim@codesourcery.com>
To: "Joseph S. Myers" <joseph@codesourcery.com>
Cc: Richard Sandiford <rdsandiford@googlemail.com>,
	<libc-ports@sourceware.org>
Subject: [PATCH 2/3, MIPS] Rewrite MIPS' pthread_spin_[try]lock using __atomic_* builtins.
Date: Thu, 14 Jun 2012 04:36:00 -0000	[thread overview]
Message-ID: <2109EAD5-BBE8-4C8C-8D61-0AF33290F240@codesourcery.com> (raw)

This is a follow-up patch to convert assembly implementations of MIPS' pthread_spin_[try]lock routines using __atomic_* builtins.  Using the builtins the compiler can generate optimal code for a particular MIPS processor for which GLIBC is being built.

OK to apply once 2.16 branches?

Thank you,

--
Maxim Kuvyrkov
CodeSourcery / Mentor Graphics


2012-06-13  Tom de Vries  <vries@codesourcery.com>
	    Maxim Kuvyrkov  <maxim@codesourcery.com>

	* sysdeps/mips/nptl/pthread_spin_[try]lock.S: Remove.
	* sysdeps/mips/nptl/pthread_spin_[try]lock.c: New files.
---
 sysdeps/mips/nptl/pthread_spin_lock.S    |   36 -------------------------
 sysdeps/mips/nptl/pthread_spin_lock.c    |   43 ++++++++++++++++++++++++++++++
 sysdeps/mips/nptl/pthread_spin_trylock.S |   40 ---------------------------
 sysdeps/mips/nptl/pthread_spin_trylock.c |   26 ++++++++++++++++++
 4 files changed, 69 insertions(+), 76 deletions(-)
 delete mode 100644 sysdeps/mips/nptl/pthread_spin_lock.S
 create mode 100644 sysdeps/mips/nptl/pthread_spin_lock.c
 delete mode 100644 sysdeps/mips/nptl/pthread_spin_trylock.S
 create mode 100644 sysdeps/mips/nptl/pthread_spin_trylock.c

diff --git a/sysdeps/mips/nptl/pthread_spin_lock.S b/sysdeps/mips/nptl/pthread_spin_lock.S
deleted file mode 100644
index a8504f1..0000000
--- a/sysdeps/mips/nptl/pthread_spin_lock.S
+++ /dev/null
@@ -1,36 +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 <sys/asm.h>
-#include <sysdep.h>
-#include <sgidefs.h>
-
-ENTRY (pthread_spin_lock)
-	.set	push
-#if _MIPS_SIM == _ABIO32
-	.set	mips2
-#endif
-1:	ll	a2, 0(a0)
-	li	a1, 1
-	bnez	a2, 1b
-	sc	a1, 0(a0)
-	beqz	a1, 1b
-	MIPS_SYNC
-	.set	pop
-	li	v0, 0
-	ret
-PSEUDO_END (pthread_spin_lock)
diff --git a/sysdeps/mips/nptl/pthread_spin_lock.c b/sysdeps/mips/nptl/pthread_spin_lock.c
new file mode 100644
index 0000000..42dcb8d
--- /dev/null
+++ b/sysdeps/mips/nptl/pthread_spin_lock.c
@@ -0,0 +1,43 @@
+/* Copyright (C) 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
+   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; see the file COPYING.LIB.  If
+   not, see <http://www.gnu.org/licenses/>.  */
+
+#include <atomic.h>
+#include "pthreadP.h"
+
+int
+pthread_spin_lock (pthread_spinlock_t *lock)
+{
+
+  /* atomic_exchange takes less instructions than atomic_compare_and_exchange
+     for MIPS, in particular when the MIPS ISA supports an atomic exchange
+     instruction.  On the other hand, atomic_exchange potentially generates more
+     bus traffic in case the value already present is written back.
+     We assume that the first try mostly will be successful, so we use
+     atomic_exchange.  For the other tries, we use
+     atomic_compare_and_exchange.  */
+  if (atomic_exchange_acq (lock, 1) == 0)
+    return 0;
+
+  do
+    {
+      while (*lock != 0)
+	;
+    }
+  while (atomic_compare_and_exchange_val_acq (lock, 1, 0) != 0);
+
+  return 0;
+}
diff --git a/sysdeps/mips/nptl/pthread_spin_trylock.S b/sysdeps/mips/nptl/pthread_spin_trylock.S
deleted file mode 100644
index 95b55c3..0000000
--- a/sysdeps/mips/nptl/pthread_spin_trylock.S
+++ /dev/null
@@ -1,40 +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 <sys/asm.h>
-#include <sysdep.h>
-#define _ERRNO_H 1
-#include <bits/errno.h>
-#include <sgidefs.h>
-
-ENTRY (pthread_spin_trylock)
-	.set	push
-#if _MIPS_SIM == _ABIO32
-	.set	mips2
-#endif
-	ll	a2, 0(a0)
-	li	a1, 1
-	bnez	a2, 1f
-	sc	a1, 0(a0)
-	beqz	a1, 1f
-	MIPS_SYNC
-	.set	pop
-	li	v0, 0
-	ret
-1:	li	v0, EBUSY
-	ret
-PSEUDO_END (pthread_spin_trylock)
diff --git a/sysdeps/mips/nptl/pthread_spin_trylock.c b/sysdeps/mips/nptl/pthread_spin_trylock.c
new file mode 100644
index 0000000..ed75813
--- /dev/null
+++ b/sysdeps/mips/nptl/pthread_spin_trylock.c
@@ -0,0 +1,26 @@
+/* Copyright (C) 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
+   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; see the file COPYING.LIB.  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)
+{
+  return atomic_compare_and_exchange_val_acq (lock, 1, 0) ? EBUSY : 0;
+}
-- 
1.7.4.1


             reply	other threads:[~2012-06-14  4:36 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-14  4:36 Maxim Kuvyrkov [this message]
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
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=2109EAD5-BBE8-4C8C-8D61-0AF33290F240@codesourcery.com \
    --to=maxim@codesourcery.com \
    --cc=joseph@codesourcery.com \
    --cc=libc-ports@sourceware.org \
    --cc=rdsandiford@googlemail.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).