Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Semaphore.cs
Go to the documentation of this file.
2using System.IO;
6
7namespace System.Threading;
8
9public sealed class Semaphore : WaitHandle
10{
11 public Semaphore(int initialCount, int maximumCount)
12 : this(initialCount, maximumCount, null)
13 {
14 }
15
16 public Semaphore(int initialCount, int maximumCount, string? name)
17 : this(initialCount, maximumCount, name, out var _)
18 {
19 }
20
21 public Semaphore(int initialCount, int maximumCount, string? name, out bool createdNew)
22 {
23 if (initialCount < 0)
24 {
26 }
27 if (maximumCount < 1)
28 {
30 }
31 if (initialCount > maximumCount)
32 {
34 }
35 CreateSemaphoreCore(initialCount, maximumCount, name, out createdNew);
36 }
37
38 [SupportedOSPlatform("windows")]
39 public static Semaphore OpenExisting(string name)
40 {
41 Semaphore result;
42 return OpenExistingWorker(name, out result) switch
43 {
44 OpenExistingResult.NameNotFound => throw new WaitHandleCannotBeOpenedException(),
46 OpenExistingResult.PathNotFound => throw new IOException(SR.Format(SR.IO_PathNotFound_Path, name)),
47 _ => result,
48 };
49 }
50
51 [SupportedOSPlatform("windows")]
52 public static bool TryOpenExisting(string name, [NotNullWhen(true)] out Semaphore? result)
53 {
54 return OpenExistingWorker(name, out result) == OpenExistingResult.Success;
55 }
56
57 public int Release()
58 {
59 return ReleaseCore(1);
60 }
61
62 public int Release(int releaseCount)
63 {
64 if (releaseCount < 1)
65 {
67 }
68 return ReleaseCore(releaseCount);
69 }
70
72 {
73 base.SafeWaitHandle = handle;
74 }
75
76 private void CreateSemaphoreCore(int initialCount, int maximumCount, string name, out bool createdNew)
77 {
78 SafeWaitHandle safeWaitHandle = Interop.Kernel32.CreateSemaphoreEx(IntPtr.Zero, initialCount, maximumCount, name, 0u, 34603010u);
79 int lastPInvokeError = Marshal.GetLastPInvokeError();
80 if (safeWaitHandle.IsInvalid)
81 {
82 if (!string.IsNullOrEmpty(name) && lastPInvokeError == 6)
83 {
85 }
87 }
88 createdNew = lastPInvokeError != 183;
89 base.SafeWaitHandle = safeWaitHandle;
90 }
91
92 private static OpenExistingResult OpenExistingWorker(string name, out Semaphore result)
93 {
94 if (name == null)
95 {
96 throw new ArgumentNullException("name");
97 }
98 if (name.Length == 0)
99 {
100 throw new ArgumentException(SR.Argument_EmptyName, "name");
101 }
102 SafeWaitHandle safeWaitHandle = Interop.Kernel32.OpenSemaphore(34603010u, inheritHandle: false, name);
103 if (safeWaitHandle.IsInvalid)
104 {
105 result = null;
107 {
108 case 2:
109 case 123:
110 return OpenExistingResult.NameNotFound;
111 case 3:
112 return OpenExistingResult.PathNotFound;
113 case 6:
114 return OpenExistingResult.NameInvalid;
115 default:
117 }
118 }
119 result = new Semaphore(safeWaitHandle);
120 return OpenExistingResult.Success;
121 }
122
123 private int ReleaseCore(int releaseCount)
124 {
125 if (!Interop.Kernel32.ReleaseSemaphore(base.SafeWaitHandle, releaseCount, out var previousCount))
126 {
127 throw new SemaphoreFullException();
128 }
129 return previousCount;
130 }
131}
static SafeWaitHandle CreateSemaphoreEx(IntPtr lpSecurityAttributes, int initialCount, int maximumCount, string name, uint flags, uint desiredAccess)
static bool ReleaseSemaphore(SafeWaitHandle handle, int releaseCount, out int previousCount)
static SafeWaitHandle OpenSemaphore(uint desiredAccess, bool inheritHandle, string name)
static Exception GetExceptionForLastWin32Error(string path="")
static string ArgumentOutOfRange_NeedPosNum
Definition SR.cs:20
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Threading_WaitHandleCannotBeOpenedException_InvalidHandle
Definition SR.cs:1996
static string Argument_SemaphoreInitialMaximum
Definition SR.cs:868
static string Argument_EmptyName
Definition SR.cs:584
static string ArgumentOutOfRange_NeedNonNegNum
Definition SR.cs:32
static string IO_PathNotFound_Path
Definition SR.cs:44
Definition SR.cs:7
Semaphore(SafeWaitHandle handle)
Definition Semaphore.cs:71
int Release(int releaseCount)
Definition Semaphore.cs:62
static Semaphore OpenExisting(string name)
Definition Semaphore.cs:39
Semaphore(int initialCount, int maximumCount, string? name, out bool createdNew)
Definition Semaphore.cs:21
Semaphore(int initialCount, int maximumCount)
Definition Semaphore.cs:11
Semaphore(int initialCount, int maximumCount, string? name)
Definition Semaphore.cs:16
static bool TryOpenExisting(string name, [NotNullWhen(true)] out Semaphore? result)
Definition Semaphore.cs:52
void CreateSemaphoreCore(int initialCount, int maximumCount, string name, out bool createdNew)
Definition Semaphore.cs:76
static OpenExistingResult OpenExistingWorker(string name, out Semaphore result)
Definition Semaphore.cs:92
int ReleaseCore(int releaseCount)
Definition Semaphore.cs:123
static readonly IntPtr Zero
Definition IntPtr.cs:18