Mutex.Windows.cs 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.IO;
  5. using Microsoft.Win32.SafeHandles;
  6. using System.Runtime.InteropServices;
  7. namespace System.Threading
  8. {
  9. /// <summary>
  10. /// Synchronization primitive that can also be used for interprocess synchronization
  11. /// </summary>
  12. public sealed partial class Mutex : WaitHandle
  13. {
  14. private const uint AccessRights =
  15. (uint)Interop.Kernel32.MAXIMUM_ALLOWED | Interop.Kernel32.SYNCHRONIZE | Interop.Kernel32.MUTEX_MODIFY_STATE;
  16. private void CreateMutexCore(bool initiallyOwned, string? name, out bool createdNew)
  17. {
  18. uint mutexFlags = initiallyOwned ? Interop.Kernel32.CREATE_MUTEX_INITIAL_OWNER : 0;
  19. SafeWaitHandle mutexHandle = Interop.Kernel32.CreateMutexEx(IntPtr.Zero, name, mutexFlags, AccessRights);
  20. int errorCode = Marshal.GetLastWin32Error();
  21. if (mutexHandle.IsInvalid)
  22. {
  23. mutexHandle.SetHandleAsInvalid();
  24. #if !PLATFORM_WINDOWS
  25. if (errorCode == Interop.Errors.ERROR_FILENAME_EXCED_RANGE)
  26. // On Unix, length validation is done by CoreCLR's PAL after converting to utf-8
  27. throw new ArgumentException(SR.Argument_WaitHandleNameTooLong, nameof(name));
  28. #endif
  29. if (errorCode == Interop.Errors.ERROR_INVALID_HANDLE)
  30. throw new WaitHandleCannotBeOpenedException(SR.Format(SR.Threading_WaitHandleCannotBeOpenedException_InvalidHandle, name));
  31. throw Win32Marshal.GetExceptionForWin32Error(errorCode, name);
  32. }
  33. createdNew = errorCode != Interop.Errors.ERROR_ALREADY_EXISTS;
  34. SafeWaitHandle = mutexHandle;
  35. }
  36. private static OpenExistingResult OpenExistingWorker(string name, out Mutex? result)
  37. {
  38. if (name == null)
  39. {
  40. throw new ArgumentNullException(nameof(name));
  41. }
  42. if (name.Length == 0)
  43. {
  44. throw new ArgumentException(SR.Argument_EmptyName, nameof(name));
  45. }
  46. result = null;
  47. // To allow users to view & edit the ACL's, call OpenMutex
  48. // with parameters to allow us to view & edit the ACL. This will
  49. // fail if we don't have permission to view or edit the ACL's.
  50. // If that happens, ask for less permissions.
  51. SafeWaitHandle myHandle = Interop.Kernel32.OpenMutex(AccessRights, false, name);
  52. if (myHandle.IsInvalid)
  53. {
  54. int errorCode = Marshal.GetLastWin32Error();
  55. #if !PLATFORM_WINDOWS
  56. if (errorCode == Interop.Errors.ERROR_FILENAME_EXCED_RANGE)
  57. {
  58. // On Unix, length validation is done by CoreCLR's PAL after converting to utf-8
  59. throw new ArgumentException(SR.Argument_WaitHandleNameTooLong, nameof(name));
  60. }
  61. #endif
  62. if (Interop.Errors.ERROR_FILE_NOT_FOUND == errorCode || Interop.Errors.ERROR_INVALID_NAME == errorCode)
  63. return OpenExistingResult.NameNotFound;
  64. if (Interop.Errors.ERROR_PATH_NOT_FOUND == errorCode)
  65. return OpenExistingResult.PathNotFound;
  66. if (Interop.Errors.ERROR_INVALID_HANDLE == errorCode)
  67. return OpenExistingResult.NameInvalid;
  68. throw Win32Marshal.GetExceptionForWin32Error(errorCode, name);
  69. }
  70. result = new Mutex(myHandle);
  71. return OpenExistingResult.Success;
  72. }
  73. // Note: To call ReleaseMutex, you must have an ACL granting you
  74. // MUTEX_MODIFY_STATE rights (0x0001). The other interesting value
  75. // in a Mutex's ACL is MUTEX_ALL_ACCESS (0x1F0001).
  76. public void ReleaseMutex()
  77. {
  78. if (!Interop.Kernel32.ReleaseMutex(SafeWaitHandle))
  79. {
  80. throw new ApplicationException(SR.Arg_SynchronizationLockException);
  81. }
  82. }
  83. }
  84. }