-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
EventWaitHandle.Windows.cs
95 lines (79 loc) · 3.59 KB
/
EventWaitHandle.Windows.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
namespace System.Threading
{
public partial class EventWaitHandle
{
private const uint AccessRights = (uint)Interop.Kernel32.MAXIMUM_ALLOWED | Interop.Kernel32.SYNCHRONIZE | Interop.Kernel32.EVENT_MODIFY_STATE;
private EventWaitHandle(SafeWaitHandle handle)
{
SafeWaitHandle = handle;
}
private void CreateEventCore(bool initialState, EventResetMode mode, string? name, out bool createdNew)
{
#if TARGET_UNIX || TARGET_BROWSER || TARGET_WASI
if (name != null)
throw new PlatformNotSupportedException(SR.PlatformNotSupported_NamedSynchronizationPrimitives);
#endif
uint eventFlags = initialState ? Interop.Kernel32.CREATE_EVENT_INITIAL_SET : 0;
if (mode == EventResetMode.ManualReset)
eventFlags |= (uint)Interop.Kernel32.CREATE_EVENT_MANUAL_RESET;
SafeWaitHandle handle = Interop.Kernel32.CreateEventEx(IntPtr.Zero, name, eventFlags, AccessRights);
int errorCode = Marshal.GetLastPInvokeError();
if (handle.IsInvalid)
{
handle.SetHandleAsInvalid();
if (!string.IsNullOrEmpty(name) && errorCode == Interop.Errors.ERROR_INVALID_HANDLE)
throw new WaitHandleCannotBeOpenedException(SR.Format(SR.Threading_WaitHandleCannotBeOpenedException_InvalidHandle, name));
throw Win32Marshal.GetExceptionForWin32Error(errorCode, name);
}
createdNew = errorCode != Interop.Errors.ERROR_ALREADY_EXISTS;
SafeWaitHandle = handle;
}
private static OpenExistingResult OpenExistingWorker(string name, out EventWaitHandle? result)
{
#if TARGET_WINDOWS
ArgumentException.ThrowIfNullOrEmpty(name);
result = null;
SafeWaitHandle myHandle = Interop.Kernel32.OpenEvent(AccessRights, false, name);
if (myHandle.IsInvalid)
{
int errorCode = Marshal.GetLastPInvokeError();
myHandle.Dispose();
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 (errorCode == Interop.Errors.ERROR_INVALID_HANDLE)
return OpenExistingResult.NameInvalid;
throw Win32Marshal.GetExceptionForWin32Error(errorCode, name);
}
result = new EventWaitHandle(myHandle);
return OpenExistingResult.Success;
#else
throw new PlatformNotSupportedException(SR.PlatformNotSupported_NamedSynchronizationPrimitives);
#endif
}
public bool Reset()
{
bool res = Interop.Kernel32.ResetEvent(SafeWaitHandle);
if (!res)
throw Win32Marshal.GetExceptionForLastWin32Error();
return res;
}
public bool Set()
{
bool res = Interop.Kernel32.SetEvent(SafeWaitHandle);
if (!res)
throw Win32Marshal.GetExceptionForLastWin32Error();
return res;
}
internal static bool Set(SafeWaitHandle waitHandle)
{
return Interop.Kernel32.SetEvent(waitHandle);
}
}
}