public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
* Fwd: [PATCH] spinlock spin with pause instruction
       [not found] <CAKw7uVgrjqZVznRMoCbsjyz4YXast5YtAAmpWQorOw7YXqbOhw@mail.gmail.com>
@ 2016-03-11 10:28 ` Václav Haisman
  2016-03-11 13:04   ` Corinna Vinschen
  2016-03-12  6:25   ` Fwd: " Mark Geisert
  0 siblings, 2 replies; 4+ messages in thread
From: Václav Haisman @ 2016-03-11 10:28 UTC (permalink / raw)
  To: cygwin-patches

[-- Attachment #1: Type: text/plain, Size: 283 bytes --]

Hi.

I have noticed that Cygwin's spinlock goes into heavy sleeping code
for each spin. It seems it would be a good idea to actually try to
spin a bit first. There is this 'pause' instruction which let's the
CPU make such busy loops be less busy. Here is a patch to do this.

-- 
VH

[-- Attachment #2: spinlock-pause.patch.txt --]
[-- Type: text/plain, Size: 1258 bytes --]

diff --git a/winsup/cygwin/thread.cc b/winsup/cygwin/thread.cc
index 01cfd5b..56e66f1 100644
--- a/winsup/cygwin/thread.cc
+++ b/winsup/cygwin/thread.cc
@@ -1917,6 +1917,19 @@ pthread_spinlock::lock ()
 {
   pthread_t self = ::pthread_self ();
   int result = -1;
+  unsigned spins = 0;
+
+  /*
+    We want to spin using 'pause' instruction on multi-core system but we
+    want to avoid this on single-core systems.
+
+    The limit of 1000 spins is semi-arbitrary. Microsoft suggests (in their
+    InitializeCriticalSectionAndSpinCount documentation on MSDN) they are
+    using spin count limit 4000 for their heap manager critical
+    sections. Other source suggest spin count as small as 200 for fast path
+    of mutex locking.
+   */
+  unsigned const FAST_SPINS_LIMIT = wincap.cpu_count () != 1 ? 1000 : 0;
 
   do
     {
@@ -1925,8 +1938,13 @@ pthread_spinlock::lock ()
 	  set_owner (self);
 	  result = 0;
 	}
-      else if (pthread::equal (owner, self))
+      else if (unlikely(pthread::equal (owner, self)))
 	result = EDEADLK;
+      else if (spins < FAST_SPINS_LIMIT)
+        {
+          ++spins;
+          __asm__ volatile ("pause":::);
+        }
       else
 	{
 	  /* Minimal timeout to minimize CPU usage while still spinning. */

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] spinlock spin with pause instruction
  2016-03-11 10:28 ` Fwd: [PATCH] spinlock spin with pause instruction Václav Haisman
@ 2016-03-11 13:04   ` Corinna Vinschen
  2016-03-12  6:25   ` Fwd: " Mark Geisert
  1 sibling, 0 replies; 4+ messages in thread
From: Corinna Vinschen @ 2016-03-11 13:04 UTC (permalink / raw)
  To: cygwin-patches

[-- Attachment #1: Type: text/plain, Size: 518 bytes --]

On Mar 11 11:28, Václav Haisman wrote:
> Hi.
> 
> I have noticed that Cygwin's spinlock goes into heavy sleeping code
> for each spin. It seems it would be a good idea to actually try to
> spin a bit first. There is this 'pause' instruction which let's the
> CPU make such busy loops be less busy. Here is a patch to do this.

Applied.


Thanks,
Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Fwd: [PATCH] spinlock spin with pause instruction
  2016-03-11 10:28 ` Fwd: [PATCH] spinlock spin with pause instruction Václav Haisman
  2016-03-11 13:04   ` Corinna Vinschen
@ 2016-03-12  6:25   ` Mark Geisert
  2016-03-12  8:14     ` Václav Haisman
  1 sibling, 1 reply; 4+ messages in thread
From: Mark Geisert @ 2016-03-12  6:25 UTC (permalink / raw)
  To: cygwin-patches

Václav Haisman wrote:
> Hi.
>
> I have noticed that Cygwin's spinlock goes into heavy sleeping code
> for each spin. It seems it would be a good idea to actually try to
> spin a bit first. There is this 'pause' instruction which let's the
> CPU make such busy loops be less busy. Here is a patch to do this.

I wanted to try out this patch but compilation is failing on the "unlikely" 
call.  Is that a macro that needs defining or something else?
Thanks,

..mark

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Fwd: [PATCH] spinlock spin with pause instruction
  2016-03-12  6:25   ` Fwd: " Mark Geisert
@ 2016-03-12  8:14     ` Václav Haisman
  0 siblings, 0 replies; 4+ messages in thread
From: Václav Haisman @ 2016-03-12  8:14 UTC (permalink / raw)
  To: cygwin-patches

[-- Attachment #1: Type: text/plain, Size: 777 bytes --]

On 12.3.2016 07:25, Mark Geisert wrote:
> Václav Haisman wrote:
>> Hi.
>>
>> I have noticed that Cygwin's spinlock goes into heavy sleeping code
>> for each spin. It seems it would be a good idea to actually try to
>> spin a bit first. There is this 'pause' instruction which let's the
>> CPU make such busy loops be less busy. Here is a patch to do this.
> 
> I wanted to try out this patch but compilation is failing on the
> "unlikely" call.  Is that a macro that needs defining or something else?
> Thanks,
> 
> ..mark
> 

`unlikely()` is a macro defined in `miscfuncs.h`, right below header
guard:
<https://cygwin.com/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=winsup/cygwin/miscfuncs.h;h=3a9f0258c6cbb62b8d51e96c0c5542b70659e17b;hb=HEAD>

-- 
VH


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 213 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-03-12  8:14 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CAKw7uVgrjqZVznRMoCbsjyz4YXast5YtAAmpWQorOw7YXqbOhw@mail.gmail.com>
2016-03-11 10:28 ` Fwd: [PATCH] spinlock spin with pause instruction Václav Haisman
2016-03-11 13:04   ` Corinna Vinschen
2016-03-12  6:25   ` Fwd: " Mark Geisert
2016-03-12  8:14     ` Václav Haisman

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).