From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id A6638385AC2E; Wed, 24 Aug 2022 09:20:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A6638385AC2E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1661332851; bh=ZfoWaFBljmxNSwUFkltP7fYyy/6PXBkAoZOtFrTP7sk=; h=From:To:Subject:Date:From; b=khT8S8ZFkmaoItwaU9vOfUBgMK2S0ht7Zoe6EnSvkLYX7Y9jqAPw3rYjG7bTff5pX tzMiCGa5dEJG56oWajbGld1gmUzQqEgtPf7byh7NyuSVmGiaaEbeXNQiKx8oC2fzuU o6L4mEqEoEyX2esLaB5474pxtPNbA1yLs8CINVlQ= 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: cwd: use SRWLOCK instead of muto X-Act-Checkin: newlib-cygwin X-Git-Author: Corinna Vinschen X-Git-Refname: refs/heads/master X-Git-Oldrev: ee54cabad9c9fcc45275551dae7bd9fc9da78f83 X-Git-Newrev: 0819679a7a2101b7384b82f2449f56994359aa3a Message-Id: <20220824092051.A6638385AC2E@sourceware.org> Date: Wed, 24 Aug 2022 09:20:51 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3D0819679a7a2= 101b7384b82f2449f56994359aa3a commit 0819679a7a2101b7384b82f2449f56994359aa3a Author: Corinna Vinschen Date: Tue Aug 23 11:58:38 2022 +0200 Cygwin: cwd: 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/local_includes/cygheap.h | 15 ++++++++++----- winsup/cygwin/nlsfuncs.cc | 4 ++-- winsup/cygwin/path.cc | 20 +++++++++----------- winsup/cygwin/syscalls.cc | 12 ++++++------ 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/winsup/cygwin/local_includes/cygheap.h b/winsup/cygwin/local_i= ncludes/cygheap.h index 7f36c5837..6a844babd 100644 --- a/winsup/cygwin/local_includes/cygheap.h +++ b/winsup/cygwin/local_includes/cygheap.h @@ -303,25 +303,30 @@ private: =20 public: UNICODE_STRING win32; - static muto cwd_lock; + static SRWLOCK NO_COPY cwd_lock; + + static void acquire_read () { AcquireSRWLockShared (&cwd_lock); } + static void release_read () { ReleaseSRWLockShared (&cwd_lock); } + static void acquire_write () { AcquireSRWLockExclusive (&cwd_lock); } + static void release_write () { ReleaseSRWLockExclusive (&cwd_lock); } const char *get_posix () const { return posix; }; void reset_posix (wchar_t *w_cwd); char *get (char *buf, int need_posix =3D 1, int with_chroot =3D 0, unsigned ulen =3D NT_MAX_PATH); PWCHAR get (PWCHAR buf, unsigned buflen =3D NT_MAX_PATH) { - cwd_lock.acquire (); + acquire_read (); buf[0] =3D L'\0'; wcsncat (buf, win32.Buffer, buflen - 1); - cwd_lock.release (); + release_read (); return buf; } HANDLE get_handle () { return dir; } DWORD get_drive (char * dst) { - cwd_lock.acquire (); + acquire_read (); DWORD ret =3D sys_wcstombs (dst, NT_MAX_PATH, win32.Buffer, drive_leng= th); - cwd_lock.release (); + release_read (); return ret; } int get_error () const { return error; } diff --git a/winsup/cygwin/nlsfuncs.cc b/winsup/cygwin/nlsfuncs.cc index 5edc0c0e6..ddd85bea1 100644 --- a/winsup/cygwin/nlsfuncs.cc +++ b/winsup/cygwin/nlsfuncs.cc @@ -1583,7 +1583,7 @@ internal_setlocale () _sys_mbstowcs (cygheap->locale.mbtowc, w_path, 32768, path); } w_cwd =3D tp.w_get (); - cwdstuff::cwd_lock.acquire (); + cwdstuff::acquire_write (); _sys_mbstowcs (cygheap->locale.mbtowc, w_cwd, 32768, cygheap->cwd.get_posix ()); /* Set charset for internal conversion functions. */ @@ -1592,7 +1592,7 @@ internal_setlocale () cygheap->locale.mbtowc =3D __utf8_mbtowc; /* Restore CWD and PATH in new charset. */ cygheap->cwd.reset_posix (w_cwd); - cwdstuff::cwd_lock.release (); + cwdstuff::release_write (); if (w_path) { char *c_path =3D tp.c_get (); diff --git a/winsup/cygwin/path.cc b/winsup/cygwin/path.cc index 227b99d0f..4babd10b8 100644 --- a/winsup/cygwin/path.cc +++ b/winsup/cygwin/path.cc @@ -103,7 +103,7 @@ struct symlink_info bool set_error (int); }; =20 -muto NO_COPY cwdstuff::cwd_lock; +SRWLOCK NO_COPY cwdstuff::cwd_lock; =20 static const GUID GUID_shortcut =3D { 0x00021401L, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46}}; @@ -4737,12 +4737,10 @@ cwdstuff::override_win32_cwd (bool init, ULONG old_= dismount_count) } } =20 -/* Initialize cygcwd 'muto' for serializing access to cwd info. */ +/* Initialize cwdstuff */ void cwdstuff::init () { - cwd_lock.init ("cwd_lock"); - /* Cygwin processes inherit the cwd from their parent. If the win32 path buffer is not NULL, the cwd struct is already set up, and we only have to override the Win32 CWD with ours. */ @@ -4800,8 +4798,6 @@ cwdstuff::set (path_conv *nat_cwd, const char *posix_= cwd) Win32 CWD to a "weird" directory in which all relative filesystem-rel= ated calls fail. */ =20 - cwd_lock.acquire (); - if (nat_cwd) { upath =3D *nat_cwd->get_nt_native_path (); @@ -4825,6 +4821,8 @@ cwdstuff::set (path_conv *nat_cwd, const char *posix_= cwd) } } =20 + acquire_write (); + /* Memorize old DismountCount before opening the dir. This value is stored in the FAST_CWD structure. It would be simpler to fetch the old DismountCount in override_win32_cwd, but Windows also fetches @@ -4888,7 +4886,7 @@ cwdstuff::set (path_conv *nat_cwd, const char *posix_= cwd) /* Called from chdir? Just fail. */ if (nat_cwd) { - cwd_lock.release (); + release_write (); __seterrno_from_nt_status (status); return -1; } @@ -4905,7 +4903,7 @@ cwdstuff::set (path_conv *nat_cwd, const char *posix_= cwd) peb.ProcessParameters->CurrentDirectoryHandle, GetCurrentProcess (), &h, 0, TRUE, 0)) { - cwd_lock.release (); + release_write (); if (peb.ProcessParameters->CurrentDirectoryHandle) debug_printf ("...and DuplicateHandle failed with %E."); dir =3D NULL; @@ -5027,7 +5025,7 @@ cwdstuff::set (path_conv *nat_cwd, const char *posix_= cwd) posix =3D (char *) crealloc_abort (posix, strlen (posix_cwd) + 1); stpcpy (posix, posix_cwd); =20 - cwd_lock.release (); + release_write (); return 0; } =20 @@ -5079,7 +5077,7 @@ cwdstuff::get (char *buf, int need_posix, int with_ch= root, unsigned ulen) goto out; } =20 - cwd_lock.acquire (); + acquire_read (); =20 char *tocopy; if (!need_posix) @@ -5106,7 +5104,7 @@ cwdstuff::get (char *buf, int need_posix, int with_ch= root, unsigned ulen) strcpy (buf, "/"); } =20 - cwd_lock.release (); + release_read (); =20 out: syscall_printf ("(%s) =3D cwdstuff::get (%p, %u, %d, %d), errno %d", diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc index 94cb57648..1338853df 100644 --- a/winsup/cygwin/syscalls.cc +++ b/winsup/cygwin/syscalls.cc @@ -4437,9 +4437,9 @@ gen_full_path_at (char *path_ret, int dirfd, const ch= ar *pathname, =20 if (dirfd =3D=3D AT_FDCWD) { - cwdstuff::cwd_lock.acquire (); + cwdstuff::acquire_read (); p =3D stpcpy (path_ret, cygheap->cwd.get_posix ()); - cwdstuff::cwd_lock.release (); + cwdstuff::release_read (); } else { @@ -4581,9 +4581,9 @@ fchownat (int dirfd, const char *pathname, uid_t uid,= gid_t gid, int flags) /* pathname is an empty string. Operate on dirfd. */ if (dirfd =3D=3D AT_FDCWD) { - cwdstuff::cwd_lock.acquire (); + cwdstuff::acquire_read (); strcpy (path, cygheap->cwd.get_posix ()); - cwdstuff::cwd_lock.release (); + cwdstuff::release_read (); } else { @@ -4626,9 +4626,9 @@ fstatat (int dirfd, const char *__restrict pathname, = struct stat *__restrict st, /* pathname is an empty string. Operate on dirfd. */ if (dirfd =3D=3D AT_FDCWD) { - cwdstuff::cwd_lock.acquire (); + cwdstuff::acquire_read (); strcpy (path, cygheap->cwd.get_posix ()); - cwdstuff::cwd_lock.release (); + cwdstuff::release_read (); } else return fstat (dirfd, st);