From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id 9F5B0385AC19; Wed, 24 Aug 2022 09:20:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9F5B0385AC19 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1661332846; bh=SCUJFqmIXKrvPYmEd/qq0qHbbwZJEFCgDXWS/qmabbk=; h=From:To:Subject:Date:From; b=YQmr+xwzwTWmlOllxMetChIjPRKxXQmLOVi4v2xhuCuMAjn+KHp1Sgsdj4wZEKzgn iEinNWq+lUQpBE8TZg5h1cUO+V2LDYmrk4JE25vXl9bBWxhiBBlLJyRtjvsJ9jQtHD NH0OCvTV7XbwnwlMWdafxmTsKQiYBYdCnf3bBAn0= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Corinna Vinschen To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: mmap: use SRWLOCK instead of muto X-Act-Checkin: newlib-cygwin X-Git-Author: Corinna Vinschen X-Git-Refname: refs/heads/master X-Git-Oldrev: 63b503916d4258cec39df09c545498e463d9a088 X-Git-Newrev: ee54cabad9c9fcc45275551dae7bd9fc9da78f83 Message-Id: <20220824092046.9F5B0385AC19@sourceware.org> Date: Wed, 24 Aug 2022 09:20:46 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3Dee54cabad9c= 9fcc45275551dae7bd9fc9da78f83 commit ee54cabad9c9fcc45275551dae7bd9fc9da78f83 Author: Corinna Vinschen Date: Tue Aug 23 11:14:12 2022 +0200 Cygwin: mmap: use SRWLOCK instead of muto =20 To reduce thread contention, use reader/writer locks as required. =20 Signed-off-by: Corinna Vinschen Diff: --- winsup/cygwin/mm/mmap.cc | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/winsup/cygwin/mm/mmap.cc b/winsup/cygwin/mm/mmap.cc index c33342bc3..332c015a7 100644 --- a/winsup/cygwin/mm/mmap.cc +++ b/winsup/cygwin/mm/mmap.cc @@ -48,9 +48,11 @@ details. */ static fhandler_dev_zero fh_anonymous; =20 /* Used for thread synchronization while accessing mmap bookkeeping lists.= */ -static NO_COPY muto mmap_guard; -#define LIST_LOCK() (mmap_guard.init ("mmap_guard")->acquire ()) -#define LIST_UNLOCK() (mmap_guard.release ()) +static NO_COPY SRWLOCK mmap_lock =3D SRWLOCK_INIT; +#define LIST_WRITE_LOCK() (AcquireSRWLockExclusive (&mmap_lock)) +#define LIST_WRITE_UNLOCK() (ReleaseSRWLockExclusive (&mmap_lock)) +#define LIST_READ_LOCK() (AcquireSRWLockShared (&mmap_lock)) +#define LIST_READ_UNLOCK() (ReleaseSRWLockShared (&mmap_lock)) =20 /* Small helpers to avoid having lots of flag bit tests in the code. */ static inline bool @@ -703,12 +705,12 @@ is_mmapped_region (caddr_t start_addr, caddr_t end_ad= dress) { size_t len =3D end_address - start_addr; =20 - LIST_LOCK (); + LIST_READ_LOCK (); mmap_list *map_list =3D mmapped_areas.get_list_by_fd (-1, NULL); =20 if (!map_list) { - LIST_UNLOCK (); + LIST_READ_UNLOCK (); return false; } =20 @@ -725,7 +727,7 @@ is_mmapped_region (caddr_t start_addr, caddr_t end_addr= ess) break; } } - LIST_UNLOCK (); + LIST_READ_UNLOCK (); return ret; } =20 @@ -753,7 +755,7 @@ mmap_is_attached_or_noreserve (void *addr, size_t len) { mmap_region_status ret =3D MMAP_NONE; =20 - LIST_LOCK (); + LIST_READ_LOCK (); mmap_list *map_list =3D mmapped_areas.get_list_by_fd (-1, NULL); =20 const size_t pagesize =3D wincap.allocation_granularity (); @@ -800,7 +802,7 @@ mmap_is_attached_or_noreserve (void *addr, size_t len) } } out: - LIST_UNLOCK (); + LIST_READ_UNLOCK (); return ret; } =20 @@ -1043,7 +1045,7 @@ go_ahead: if (noreserve (flags) && (!anonymous (flags) || !priv (flags))) flags &=3D ~MAP_NORESERVE; =20 - LIST_LOCK (); + LIST_WRITE_LOCK (); map_list =3D mmapped_areas.get_list_by_fd (fd, &st); =20 /* Test if an existing anonymous mapping can be recycled. */ @@ -1096,7 +1098,7 @@ go_ahead: ret =3D base; =20 out_with_unlock: - LIST_UNLOCK (); + LIST_WRITE_UNLOCK (); =20 out: =20 @@ -1144,7 +1146,7 @@ munmap (void *addr, size_t len) } len =3D roundup2 (len, pagesize); =20 - LIST_LOCK (); + LIST_WRITE_LOCK (); =20 /* Iterate over maps, unmap pages between addr and addr+len in all maps.= */ mmap_list *map_list, *next_map_list; @@ -1180,7 +1182,7 @@ munmap (void *addr, size_t len) } } =20 - LIST_UNLOCK (); + LIST_WRITE_UNLOCK (); syscall_printf ("0 =3D munmap(): %p", addr); return 0; } @@ -1197,19 +1199,20 @@ msync (void *addr, size_t len, int flags) =20 pthread_testcancel (); =20 - LIST_LOCK (); - if (((uintptr_t) addr % wincap.allocation_granularity ()) || (flags & ~(MS_ASYNC | MS_SYNC | MS_INVALIDATE)) || ((flags & (MS_ASYNC | MS_SYNC)) =3D=3D (MS_ASYNC | MS_SYNC))) { set_errno (EINVAL); - goto out; + syscall_printf ("%R =3D msync()", ret); + return ret; } #if 0 /* If I only knew why I did that... */ len =3D roundup2 (len, wincap.allocation_granularity ()); #endif =20 + LIST_READ_LOCK (); + /* Iterate over maps, looking for the mmapped area. Error if not found.= */ LIST_FOREACH (map_list, &mmapped_areas.lists, ml_next) { @@ -1239,7 +1242,7 @@ msync (void *addr, size_t len, int flags) set_errno (ENOMEM); =20 out: - LIST_UNLOCK (); + LIST_READ_UNLOCK (); syscall_printf ("%R =3D msync()", ret); return ret; } @@ -1265,7 +1268,7 @@ mprotect (void *addr, size_t len, int prot) } len =3D roundup2 (len, pagesize); =20 - LIST_LOCK (); + LIST_WRITE_LOCK (); =20 /* Iterate over maps, protect pages between addr and addr+len in all map= s. */ mmap_list *map_list; @@ -1300,7 +1303,7 @@ mprotect (void *addr, size_t len, int prot) } } =20 - LIST_UNLOCK (); + LIST_WRITE_UNLOCK (); =20 if (!in_mapped) {