{
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)]
{
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);
}
}
Debug.Assert(maximumCount >= 1);
Debug.Assert(initialCount <= maximumCount);
-#if PLATFORM_UNIX
+#if !PLATFORM_WINDOWS
if (name != null)
throw new PlatformNotSupportedException(SR.PlatformNotSupported_NamedSynchronizationPrimitives);
#endif
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)
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();
}
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();
// 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);
if (initialCount > maximumCount)
throw new ArgumentException(SR.Argument_SemaphoreInitialMaximum);
+
+ CreateSemaphoreCore(initialCount, maximumCount, name, out createdNew);
}
public static Semaphore OpenExisting(string name)