Non Shared Changed And feedback
authorAnipik <anirudhagnihotry098@gmail.com>
Fri, 22 Jun 2018 18:32:13 +0000 (11:32 -0700)
committerJan Kotas <jkotas@microsoft.com>
Fri, 22 Jun 2018 21:20:40 +0000 (14:20 -0700)
Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.Mutex.cs
src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.Semaphore.cs
src/System.Private.CoreLib/shared/System/Threading/Semaphore.Windows.cs
src/System.Private.CoreLib/shared/System/Threading/Semaphore.cs

index f858514..cbb306e 100644 (file)
@@ -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)]
index 6d1467b..94648a7 100644 (file)
@@ -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);
     }
 }
index f101564..8a14f5e 100644 (file)
@@ -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();
index 8e4ad76..1135a1f 100644 (file)
@@ -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)