From b0a65765e92cfd879342fa85ce810596fa7ca3e4 Mon Sep 17 00:00:00 2001 From: Anipik Date: Fri, 22 Jun 2018 11:32:13 -0700 Subject: [PATCH] Non Shared Changed And feedback Signed-off-by: dotnet-bot Commit migrated from https://github.com/dotnet/coreclr/commit/db4248f0d615f6e86ed291a86adf395a59ec2e9c --- .../src/Interop/Windows/Kernel32/Interop.Mutex.cs | 4 ++-- .../src/Interop/Windows/Kernel32/Interop.Semaphore.cs | 5 ++--- .../src/System/Threading/Semaphore.Windows.cs | 11 ++++------- .../src/System/Threading/Semaphore.cs | 16 ++++------------ 4 files changed, 12 insertions(+), 24 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/Interop/Windows/Kernel32/Interop.Mutex.cs b/src/libraries/System.Private.CoreLib/src/Interop/Windows/Kernel32/Interop.Mutex.cs index f858514..cbb306e 100644 --- a/src/libraries/System.Private.CoreLib/src/Interop/Windows/Kernel32/Interop.Mutex.cs +++ b/src/libraries/System.Private.CoreLib/src/Interop/Windows/Kernel32/Interop.Mutex.cs @@ -12,10 +12,10 @@ internal static partial class Interop { internal const uint CREATE_MUTEX_INITIAL_OWNER = 0x1; - [DllImport(Interop.Libraries.Kernel32, SetLastError = true, CharSet = CharSet.Unicode)] + [DllImport(Interop.Libraries.Kernel32, EntryPoint = "OpenMutexW", SetLastError = true, CharSet = CharSet.Unicode)] internal static extern SafeWaitHandle OpenMutex(uint desiredAccess, bool inheritHandle, string name); - [DllImport(Interop.Libraries.Kernel32, SetLastError = true, CharSet = CharSet.Unicode)] + [DllImport(Interop.Libraries.Kernel32, EntryPoint = "CreateMutexExW", SetLastError = true, CharSet = CharSet.Unicode)] internal static extern SafeWaitHandle CreateMutexEx(IntPtr lpMutexAttributes, string name, uint flags, uint desiredAccess); [DllImport(Interop.Libraries.Kernel32, SetLastError = true)] diff --git a/src/libraries/System.Private.CoreLib/src/Interop/Windows/Kernel32/Interop.Semaphore.cs b/src/libraries/System.Private.CoreLib/src/Interop/Windows/Kernel32/Interop.Semaphore.cs index 6d1467b..94648a7 100644 --- a/src/libraries/System.Private.CoreLib/src/Interop/Windows/Kernel32/Interop.Semaphore.cs +++ b/src/libraries/System.Private.CoreLib/src/Interop/Windows/Kernel32/Interop.Semaphore.cs @@ -10,14 +10,13 @@ internal static partial class Interop { internal static partial class Kernel32 { - [DllImport(Interop.Libraries.Kernel32, SetLastError = true, CharSet = CharSet.Unicode)] + [DllImport(Interop.Libraries.Kernel32, EntryPoint = "OpenSemaphoreW", SetLastError = true, CharSet = CharSet.Unicode)] internal static extern SafeWaitHandle OpenSemaphore(uint desiredAccess, bool inheritHandle, string name); - [DllImport(Libraries.Kernel32, SetLastError = true, CharSet = CharSet.Unicode)] + [DllImport(Libraries.Kernel32, EntryPoint = "CreateSemaphoreExW", SetLastError = true, CharSet = CharSet.Unicode)] internal static extern SafeWaitHandle CreateSemaphoreEx(IntPtr lpSecurityAttributes, int initialCount, int maximumCount, string name, uint flags, uint desiredAccess); [DllImport(Interop.Libraries.Kernel32, SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool ReleaseSemaphore(SafeWaitHandle handle, int releaseCount, out int previousCount); } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Semaphore.Windows.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/Semaphore.Windows.cs index f101564..8a14f5e 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/Semaphore.Windows.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Semaphore.Windows.cs @@ -24,7 +24,7 @@ namespace System.Threading Debug.Assert(maximumCount >= 1); Debug.Assert(initialCount <= maximumCount); -#if PLATFORM_UNIX +#if !PLATFORM_WINDOWS if (name != null) throw new PlatformNotSupportedException(SR.PlatformNotSupported_NamedSynchronizationPrimitives); #endif @@ -51,7 +51,7 @@ namespace System.Threading if (name.Length == 0) throw new ArgumentException(SR.Argument_EmptyName, nameof(name)); - //Pass false to OpenSemaphore to prevent inheritedHandles + // Pass false to OpenSemaphore to prevent inheritedHandles SafeWaitHandle myHandle = Interop.Kernel32.OpenSemaphore(AccessRights, false, name); if (myHandle.IsInvalid) @@ -59,13 +59,13 @@ namespace System.Threading result = null; int errorCode = Marshal.GetLastWin32Error(); - if (errorCode == Interop.Errors.ERROR_FILE_NOT_FOUND || errorCode == Interop.Errors.ERROR_INVALID_NAME) + if (errorCode == Interop.Errors.ERROR_FILE_NOT_FOUND || errorCode == Interop.Errors.ERROR_INVALID_NAME) return OpenExistingResult.NameNotFound; if (errorCode == Interop.Errors.ERROR_PATH_NOT_FOUND) return OpenExistingResult.PathNotFound; if (name != null && name.Length != 0 && errorCode == Interop.Errors.ERROR_INVALID_HANDLE) return OpenExistingResult.NameInvalid; - //this is for passed through NativeMethods Errors + // this is for passed through NativeMethods Errors throw Win32Marshal.GetExceptionForLastWin32Error(); } @@ -78,9 +78,6 @@ namespace System.Threading private int ReleaseCore(int releaseCount) { - // If ReleaseSempahore returns false when the specified value would cause - // the semaphore's count to exceed the maximum count set when Semaphore was created - // Non-Zero return int previousCount; if (!Interop.Kernel32.ReleaseSemaphore(SafeWaitHandle, releaseCount, out previousCount)) throw new SemaphoreFullException(); diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Semaphore.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/Semaphore.cs index 8e4ad76..1135a1f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/Semaphore.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Semaphore.cs @@ -15,23 +15,13 @@ namespace System.Threading // Win32 only takes maximum count of Int32.MaxValue public Semaphore(int initialCount, int maximumCount) : this(initialCount, maximumCount, null) { } - public Semaphore(int initialCount, int maximumCount, string name) + public Semaphore(int initialCount, int maximumCount, string name) : + this(initialCount, maximumCount, name, out _) { - VerifyCounts(initialCount, maximumCount); - - bool createdNew; - CreateSemaphoreCore(initialCount, maximumCount, name, out createdNew); } public Semaphore(int initialCount, int maximumCount, string name, out bool createdNew) { - VerifyCounts(initialCount, maximumCount); - - CreateSemaphoreCore(initialCount, maximumCount, name, out createdNew); - } - - private static void VerifyCounts(int initialCount, int maximumCount) - { if (initialCount < 0) throw new ArgumentOutOfRangeException(nameof(initialCount), SR.ArgumentOutOfRange_NeedNonNegNum); @@ -40,6 +30,8 @@ namespace System.Threading if (initialCount > maximumCount) throw new ArgumentException(SR.Argument_SemaphoreInitialMaximum); + + CreateSemaphoreCore(initialCount, maximumCount, name, out createdNew); } public static Semaphore OpenExisting(string name) -- 2.7.4