EventWaitHandle.Windows.cs 3.8 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 System.Runtime.InteropServices;
  6. using Microsoft.Win32.SafeHandles;
  7. namespace System.Threading
  8. {
  9. public partial class EventWaitHandle
  10. {
  11. private const uint AccessRights = (uint)Interop.Kernel32.MAXIMUM_ALLOWED | Interop.Kernel32.SYNCHRONIZE | Interop.Kernel32.EVENT_MODIFY_STATE;
  12. private EventWaitHandle(SafeWaitHandle handle)
  13. {
  14. SafeWaitHandle = handle;
  15. }
  16. private void CreateEventCore(bool initialState, EventResetMode mode, string? name, out bool createdNew)
  17. {
  18. #if !PLATFORM_WINDOWS
  19. if (name != null)
  20. throw new PlatformNotSupportedException(SR.PlatformNotSupported_NamedSynchronizationPrimitives);
  21. #endif
  22. uint eventFlags = initialState ? Interop.Kernel32.CREATE_EVENT_INITIAL_SET : 0;
  23. if (mode == EventResetMode.ManualReset)
  24. eventFlags |= (uint)Interop.Kernel32.CREATE_EVENT_MANUAL_RESET;
  25. SafeWaitHandle handle = Interop.Kernel32.CreateEventEx(IntPtr.Zero, name, eventFlags, AccessRights);
  26. int errorCode = Marshal.GetLastWin32Error();
  27. if (handle.IsInvalid)
  28. {
  29. handle.SetHandleAsInvalid();
  30. if (!string.IsNullOrEmpty(name) && errorCode == Interop.Errors.ERROR_INVALID_HANDLE)
  31. throw new WaitHandleCannotBeOpenedException(SR.Format(SR.Threading_WaitHandleCannotBeOpenedException_InvalidHandle, name));
  32. throw Win32Marshal.GetExceptionForWin32Error(errorCode, name);
  33. }
  34. createdNew = errorCode != Interop.Errors.ERROR_ALREADY_EXISTS;
  35. SafeWaitHandle = handle;
  36. }
  37. private static OpenExistingResult OpenExistingWorker(string name, out EventWaitHandle? result)
  38. {
  39. #if PLATFORM_WINDOWS
  40. if (name == null)
  41. throw new ArgumentNullException(nameof(name));
  42. if (name.Length == 0)
  43. throw new ArgumentException(SR.Argument_EmptyName, nameof(name));
  44. result = null;
  45. SafeWaitHandle myHandle = Interop.Kernel32.OpenEvent(AccessRights, false, name);
  46. if (myHandle.IsInvalid)
  47. {
  48. int errorCode = Marshal.GetLastWin32Error();
  49. if (errorCode == Interop.Errors.ERROR_FILE_NOT_FOUND || errorCode == Interop.Errors.ERROR_INVALID_NAME)
  50. return OpenExistingResult.NameNotFound;
  51. if (errorCode == Interop.Errors.ERROR_PATH_NOT_FOUND)
  52. return OpenExistingResult.PathNotFound;
  53. if (!string.IsNullOrEmpty(name) && errorCode == Interop.Errors.ERROR_INVALID_HANDLE)
  54. return OpenExistingResult.NameInvalid;
  55. throw Win32Marshal.GetExceptionForWin32Error(errorCode, name);
  56. }
  57. result = new EventWaitHandle(myHandle);
  58. return OpenExistingResult.Success;
  59. #else
  60. throw new PlatformNotSupportedException(SR.PlatformNotSupported_NamedSynchronizationPrimitives);
  61. #endif
  62. }
  63. public bool Reset()
  64. {
  65. bool res = Interop.Kernel32.ResetEvent(SafeWaitHandle);
  66. if (!res)
  67. throw Win32Marshal.GetExceptionForLastWin32Error();
  68. return res;
  69. }
  70. public bool Set()
  71. {
  72. bool res = Interop.Kernel32.SetEvent(SafeWaitHandle);
  73. if (!res)
  74. throw Win32Marshal.GetExceptionForLastWin32Error();
  75. return res;
  76. }
  77. internal static bool Set(SafeWaitHandle waitHandle)
  78. {
  79. return Interop.Kernel32.SetEvent(waitHandle);
  80. }
  81. }
  82. }