public inbox for libc-ports@sourceware.org
 help / color / mirror / Atom feed
From: Maxim Kuvyrkov <maxim@codesourcery.com>
To: Roland McGrath <roland@hack.frob.com>
Cc: Andrew Haley <aph@redhat.com>, David Miller <davem@davemloft.net>,
	<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 22:16:00 -0000	[thread overview]
Message-ID: <7FBB4F87-9FF3-4239-818F-5A38C8094011@codesourcery.com> (raw)
In-Reply-To: <20120711112235.B28CA2C099@topped-with-meat.com>

On 11/07/2012, at 11:22 PM, Roland McGrath wrote:

>> OK, but this will be a change on its own in a follow up patch.  I want to
>> keep this patch just a mechanical change.
> 
> I don't want to put anything in the generic code that isn't clearly correct
> or isn't clearly justified or doesn't have thorough comments explaining any
> nonobvious issues.  So get it right first before the generic code goes in.

This code _is_ right.  It may not be optimal for all machines, but it is correct.  I agree that the code is not obvious and the attached patch clears it up.

> Each machine dropping its old code in favor of the new generic code can
> wait as long as each machine maintainer wants.

This is not new code.  It is the exactly same code that ARM, MIPS, HPPA and M68K have all been using for several years, it is just moved into the generic directory.

Attached is the patch based on Andrew's and Chris's comments.  It bounds the wait loop by a reasonable constant and makes atomic_compare_and_exchange update memory on the local processor.  Assuming that cmpxchg is about 100 times more expensive than a simple load, we spend 90% of the wait cycle in "while (*lock != 0)" loop and 10% in atomic_compare_and_exchange to update the memory.

--
Maxim Kuvyrkov
CodeSourcery / Mentor Graphics


Bound wait loop in pthread_spin_lock.

	2012-07-12  Maxim Kuvyrkov  <maxim@codesourcery.com>

	* nptl/pthread_spin_lock.c (PTHREAD_SPIN_LOCK_WAIT): New macro.
	(pthread_spin_lock): Bound wait cycle with PTHREAD_SPIN_LOCK_WAIT.
---
 nptl/pthread_spin_lock.c |   27 +++++++++++++++++++++++++--
 1 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/nptl/pthread_spin_lock.c b/nptl/pthread_spin_lock.c
index 1daa43d..ac59117 100644
--- a/nptl/pthread_spin_lock.c
+++ b/nptl/pthread_spin_lock.c
@@ -19,12 +19,35 @@
 #include <atomic.h>
 #include "pthreadP.h"
 
+#ifndef PTHREAD_SPIN_LOCK_WAIT
+# define PTHREAD_SPIN_LOCK_WAIT 1000
+#endif
+
 int
 pthread_spin_lock (pthread_spinlock_t *lock)
 {
   while (atomic_compare_and_exchange_val_acq (lock, 1, 0) != 0)
-    while (*lock != 0)
-      ;
+    /* The lock is contended and we need to wait.  Going straight back
+       to cmpxchg is not a good idea on many targets as that will force
+       expensive memory synchronizations among processors and penalize other
+       running threads.
+       On the other hand, we do want to update memory state on the local core
+       once in a while to avoid spinning indefinitely until some event that
+       will happen to update local memory as a side-effect.  */
+    {
+      if (PTHREAD_SPIN_LOCK_WAIT)
+	{
+	  int wait = PTHREAD_SPIN_LOCK_WAIT;
+
+	  while (*lock != 0 && --wait)
+	    ;
+	}
+      else
+	{
+	  while (*lock != 0)
+	    ;
+	}
+    }
 
   return 0;
 }
-- 
1.7.4.1


  parent reply	other threads:[~2012-07-11 22:16 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 [this message]
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=7FBB4F87-9FF3-4239-818F-5A38C8094011@codesourcery.com \
    --to=maxim@codesourcery.com \
    --cc=aph@redhat.com \
    --cc=davem@davemloft.net \
    --cc=joseph@codesourcery.com \
    --cc=libc-alpha@sourceware.org \
    --cc=libc-ports@sourceware.org \
    --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).