Mutex.Windows.cs 4.2 KB

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