From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id 7388B385DC1A; Tue, 7 Apr 2020 16:24:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7388B385DC1A Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Corinna Vinschen To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: threads: use extended memory API if available X-Act-Checkin: newlib-cygwin X-Git-Author: Corinna Vinschen X-Git-Refname: refs/heads/master X-Git-Oldrev: 8d0a7701aa9e98bda511ba55f5f2dc210722d3e9 X-Git-Newrev: b8ecbaaac0f9ff9682a310cc78616d421470335e Message-Id: <20200407162448.7388B385DC1A@sourceware.org> Date: Tue, 7 Apr 2020 16:24:48 +0000 (GMT) X-BeenThere: cygwin-cvs@cygwin.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Cygwin core component git logs List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 07 Apr 2020 16:24:48 -0000 https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=b8ecbaaac0f9ff9682a310cc78616d421470335e commit b8ecbaaac0f9ff9682a310cc78616d421470335e Author: Corinna Vinschen 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 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;