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: mmap: use SRWLOCK instead of muto
Date: Wed, 24 Aug 2022 09:20:46 +0000 (GMT)	[thread overview]
Message-ID: <20220824092046.9F5B0385AC19@sourceware.org> (raw)

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

commit ee54cabad9c9fcc45275551dae7bd9fc9da78f83
Author: Corinna Vinschen <corinna@vinschen.de>
Date:   Tue Aug 23 11:14:12 2022 +0200

    Cygwin: mmap: use SRWLOCK instead of muto
    
    To reduce thread contention, use reader/writer locks as required.
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

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;
 
 /* 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 = 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))
 
 /* 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_address)
 {
   size_t len = end_address - start_addr;
 
-  LIST_LOCK ();
+  LIST_READ_LOCK ();
   mmap_list *map_list = mmapped_areas.get_list_by_fd (-1, NULL);
 
   if (!map_list)
     {
-      LIST_UNLOCK ();
+      LIST_READ_UNLOCK ();
       return false;
     }
 
@@ -725,7 +727,7 @@ is_mmapped_region (caddr_t start_addr, caddr_t end_address)
 	  break;
 	}
     }
-  LIST_UNLOCK ();
+  LIST_READ_UNLOCK ();
   return ret;
 }
 
@@ -753,7 +755,7 @@ mmap_is_attached_or_noreserve (void *addr, size_t len)
 {
   mmap_region_status ret = MMAP_NONE;
 
-  LIST_LOCK ();
+  LIST_READ_LOCK ();
   mmap_list *map_list = mmapped_areas.get_list_by_fd (-1, NULL);
 
   const size_t pagesize = 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;
 }
 
@@ -1043,7 +1045,7 @@ go_ahead:
   if (noreserve (flags) && (!anonymous (flags) || !priv (flags)))
     flags &= ~MAP_NORESERVE;
 
-  LIST_LOCK ();
+  LIST_WRITE_LOCK ();
   map_list = mmapped_areas.get_list_by_fd (fd, &st);
 
   /* Test if an existing anonymous mapping can be recycled. */
@@ -1096,7 +1098,7 @@ go_ahead:
   ret = base;
 
 out_with_unlock:
-  LIST_UNLOCK ();
+  LIST_WRITE_UNLOCK ();
 
 out:
 
@@ -1144,7 +1146,7 @@ munmap (void *addr, size_t len)
     }
   len = roundup2 (len, pagesize);
 
-  LIST_LOCK ();
+  LIST_WRITE_LOCK ();
 
   /* 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)
 	}
     }
 
-  LIST_UNLOCK ();
+  LIST_WRITE_UNLOCK ();
   syscall_printf ("0 = munmap(): %p", addr);
   return 0;
 }
@@ -1197,19 +1199,20 @@ msync (void *addr, size_t len, int flags)
 
   pthread_testcancel ();
 
-  LIST_LOCK ();
-
   if (((uintptr_t) addr % wincap.allocation_granularity ())
       || (flags & ~(MS_ASYNC | MS_SYNC | MS_INVALIDATE))
       || ((flags & (MS_ASYNC | MS_SYNC)) == (MS_ASYNC | MS_SYNC)))
     {
       set_errno (EINVAL);
-      goto out;
+      syscall_printf ("%R = msync()", ret);
+      return ret;
     }
 #if 0 /* If I only knew why I did that... */
   len = roundup2 (len, wincap.allocation_granularity ());
 #endif
 
+  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);
 
 out:
-  LIST_UNLOCK ();
+  LIST_READ_UNLOCK ();
   syscall_printf ("%R = msync()", ret);
   return ret;
 }
@@ -1265,7 +1268,7 @@ mprotect (void *addr, size_t len, int prot)
     }
   len = roundup2 (len, pagesize);
 
-  LIST_LOCK ();
+  LIST_WRITE_LOCK ();
 
   /* Iterate over maps, protect pages between addr and addr+len in all maps. */
   mmap_list *map_list;
@@ -1300,7 +1303,7 @@ mprotect (void *addr, size_t len, int prot)
 	}
     }
 
-  LIST_UNLOCK ();
+  LIST_WRITE_UNLOCK ();
 
   if (!in_mapped)
     {

                 reply	other threads:[~2022-08-24  9:20 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=20220824092046.9F5B0385AC19@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).