public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin] Cygwin: threads: use extended memory API if available
Date: Tue,  7 Apr 2020 16:24:48 +0000 (GMT)	[thread overview]
Message-ID: <20200407162448.7388B385DC1A@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=b8ecbaaac0f9ff9682a310cc78616d421470335e

commit b8ecbaaac0f9ff9682a310cc78616d421470335e
Author: Corinna Vinschen <corinna@vinschen.de>
Date:   Tue Apr 7 17:41:05 2020 +0200

    Cygwin: threads: use extended memory API if available
    
    So far Cygwin was jumping through hoops to restrict memory
    allocation to specific regions.  With the advent of VirtualAlloc2
    and MapViewOfFile3 (and it's NT counterpart NtMapViewOfSectionEx),
    we can skip searching for free space in the specific regions
    and just call these functions and let the OS do the job more
    efficiently and less racy.
    
    Use VirtualAlloc2 on W10 1803 and later in thread stack allocation.
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 winsup/cygwin/miscfuncs.cc | 51 +++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 48 insertions(+), 3 deletions(-)

diff --git a/winsup/cygwin/miscfuncs.cc b/winsup/cygwin/miscfuncs.cc
index b4ab7cd7d..cba2140b6 100644
--- a/winsup/cygwin/miscfuncs.cc
+++ b/winsup/cygwin/miscfuncs.cc
@@ -508,9 +508,45 @@ pthread_wrapper (PVOID arg)
 class thread_allocator
 {
   UINT_PTR current;
-public:
-  thread_allocator () : current (THREAD_STORAGE_HIGH) {}
-  PVOID alloc (SIZE_T size)
+  PVOID (thread_allocator::*alloc_func) (SIZE_T);
+  PVOID _alloc (SIZE_T size)
+  {
+    MEM_ADDRESS_REQUIREMENTS thread_req = {
+      (PVOID) THREAD_STORAGE_LOW,
+      (PVOID) (THREAD_STORAGE_HIGH - 1),
+      THREAD_STACK_SLOT
+    };
+    MEM_EXTENDED_PARAMETER thread_ext = {
+      .Type = MemExtendedParameterAddressRequirements,
+      .Pointer = (PVOID) &thread_req
+    };
+    SIZE_T real_size = roundup2 (size, THREAD_STACK_SLOT);
+    PVOID real_stackaddr = NULL;
+
+    if (real_size <= THREAD_STACK_MAX)
+      real_stackaddr = VirtualAlloc2 (GetCurrentProcess(), NULL, real_size,
+				      MEM_RESERVE | MEM_TOP_DOWN,
+				      PAGE_READWRITE, &thread_ext, 1);
+    /* If the thread area allocation failed, or if the application requests a
+       monster stack, fulfill request from mmap area. */
+    if (!real_stackaddr)
+      {
+	MEM_ADDRESS_REQUIREMENTS mmap_req = {
+	  (PVOID) MMAP_STORAGE_LOW,
+	  (PVOID) (MMAP_STORAGE_HIGH - 1),
+	  THREAD_STACK_SLOT
+	};
+	MEM_EXTENDED_PARAMETER mmap_ext = {
+	  .Type = MemExtendedParameterAddressRequirements,
+	  .Pointer = (PVOID) &mmap_req
+	};
+	real_stackaddr = VirtualAlloc2 (GetCurrentProcess(), NULL, real_size,
+					MEM_RESERVE | MEM_TOP_DOWN,
+					PAGE_READWRITE, &mmap_ext, 1);
+      }
+    return real_stackaddr;
+  }
+  PVOID _alloc_old (SIZE_T size)
   {
     SIZE_T real_size = roundup2 (size, THREAD_STACK_SLOT);
     BOOL overflow = FALSE;
@@ -558,6 +594,15 @@ public:
       set_errno (EAGAIN);
     return real_stackaddr;
   }
+public:
+  thread_allocator () : current (THREAD_STORAGE_HIGH)
+  {
+    alloc_func = wincap.has_extended_mem_api () ? &_alloc : &_alloc_old;
+  }
+  PVOID alloc (SIZE_T size)
+  {
+    return (this->*alloc_func) (size);
+  }
 };
 
 thread_allocator thr_alloc NO_COPY;


                 reply	other threads:[~2020-04-07 16:24 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20200407162448.7388B385DC1A@sourceware.org \
    --to=corinna@sourceware.org \
    --cc=cygwin-cvs@sourceware.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).