Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Mutex.cs
Go to the documentation of this file.
2using System.IO;
5
6namespace System.Threading;
7
8public sealed class Mutex : WaitHandle
9{
10 public Mutex(bool initiallyOwned, string? name, out bool createdNew)
11 {
12 CreateMutexCore(initiallyOwned, name, out createdNew);
13 }
14
15 public Mutex(bool initiallyOwned, string? name)
16 {
17 CreateMutexCore(initiallyOwned, name, out var _);
18 }
19
20 public Mutex(bool initiallyOwned)
21 {
22 CreateMutexCore(initiallyOwned, null, out var _);
23 }
24
25 public Mutex()
26 {
27 CreateMutexCore(initiallyOwned: false, null, out var _);
28 }
29
31 {
32 base.SafeWaitHandle = handle;
33 }
34
35 public static Mutex OpenExisting(string name)
36 {
37 Mutex result;
38 return OpenExistingWorker(name, out result) switch
39 {
40 OpenExistingResult.NameNotFound => throw new WaitHandleCannotBeOpenedException(),
42 OpenExistingResult.PathNotFound => throw new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, name)),
43 _ => result,
44 };
45 }
46
47 public static bool TryOpenExisting(string name, [NotNullWhen(true)] out Mutex? result)
48 {
49 return OpenExistingWorker(name, out result) == OpenExistingResult.Success;
50 }
51
52 private void CreateMutexCore(bool initiallyOwned, string name, out bool createdNew)
53 {
54 uint flags = (initiallyOwned ? 1u : 0u);
55 SafeWaitHandle safeWaitHandle = Interop.Kernel32.CreateMutexEx(IntPtr.Zero, name, flags, 34603009u);
56 int lastPInvokeError = Marshal.GetLastPInvokeError();
57 if (safeWaitHandle.IsInvalid)
58 {
59 safeWaitHandle.SetHandleAsInvalid();
60 if (lastPInvokeError == 6)
61 {
63 }
64 throw Win32Marshal.GetExceptionForWin32Error(lastPInvokeError, name);
65 }
66 createdNew = lastPInvokeError != 183;
67 base.SafeWaitHandle = safeWaitHandle;
68 }
69
70 private static OpenExistingResult OpenExistingWorker(string name, out Mutex result)
71 {
72 if (name == null)
73 {
74 throw new ArgumentNullException("name");
75 }
76 if (name.Length == 0)
77 {
78 throw new ArgumentException(SR.Argument_EmptyName, "name");
79 }
80 result = null;
81 SafeWaitHandle safeWaitHandle = Interop.Kernel32.OpenMutex(34603009u, inheritHandle: false, name);
82 if (safeWaitHandle.IsInvalid)
83 {
84 int lastPInvokeError = Marshal.GetLastPInvokeError();
85 if (2 == lastPInvokeError || 123 == lastPInvokeError)
86 {
87 return OpenExistingResult.NameNotFound;
88 }
89 if (3 == lastPInvokeError)
90 {
91 return OpenExistingResult.PathNotFound;
92 }
93 if (6 == lastPInvokeError)
94 {
95 return OpenExistingResult.NameInvalid;
96 }
97 throw Win32Marshal.GetExceptionForWin32Error(lastPInvokeError, name);
98 }
99 result = new Mutex(safeWaitHandle);
100 return OpenExistingResult.Success;
101 }
102
103 public void ReleaseMutex()
104 {
105 if (!Interop.Kernel32.ReleaseMutex(base.SafeWaitHandle))
106 {
108 }
109 }
110}
static bool ReleaseMutex(SafeWaitHandle handle)
static SafeWaitHandle CreateMutexEx(IntPtr lpMutexAttributes, string name, uint flags, uint desiredAccess)
static SafeWaitHandle OpenMutex(uint desiredAccess, bool inheritHandle, string name)
static Exception GetExceptionForWin32Error(int errorCode, string path="")
static string Arg_SynchronizationLockException
Definition SR.cs:414
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Threading_WaitHandleCannotBeOpenedException_InvalidHandle
Definition SR.cs:1996
static string Argument_EmptyName
Definition SR.cs:584
static string IO_PathNotFound_Path
Definition SR.cs:44
Definition SR.cs:7
static OpenExistingResult OpenExistingWorker(string name, out Mutex result)
Definition Mutex.cs:70
Mutex(bool initiallyOwned, string? name)
Definition Mutex.cs:15
Mutex(SafeWaitHandle handle)
Definition Mutex.cs:30
Mutex(bool initiallyOwned, string? name, out bool createdNew)
Definition Mutex.cs:10
Mutex(bool initiallyOwned)
Definition Mutex.cs:20
void CreateMutexCore(bool initiallyOwned, string name, out bool createdNew)
Definition Mutex.cs:52
static Mutex OpenExisting(string name)
Definition Mutex.cs:35
static bool TryOpenExisting(string name, [NotNullWhen(true)] out Mutex? result)
Definition Mutex.cs:47
static readonly IntPtr Zero
Definition IntPtr.cs:18