2 using Microsoft.Win32.SafeHandles;
14 [global::__DynamicallyInvokable]
15 [HostProtection(
SecurityAction.LinkDemand, Synchronization =
true, ExternalThreading =
true)]
18 private new enum OpenExistingResult
26 private const int MAX_PATH = 260;
36 [SecuritySafeCritical]
37 [global::__DynamicallyInvokable]
38 public Semaphore(
int initialCount,
int maximumCount)
39 : this(initialCount, maximumCount, null)
56 [global::__DynamicallyInvokable]
58 public Semaphore(
int initialCount,
int maximumCount,
string name)
68 if (initialCount > maximumCount)
72 if (name !=
null && 260 < name.Length)
76 SafeWaitHandle safeWaitHandle = Microsoft.Win32.SafeNativeMethods.CreateSemaphore(
null, initialCount, maximumCount, name);
77 if (safeWaitHandle.IsInvalid)
80 if (name !=
null && name.Length != 0 && 6 == lastWin32Error)
84 InternalResources.WinIOError();
86 base.SafeWaitHandle = safeWaitHandle;
103 [global::__DynamicallyInvokable]
105 public Semaphore(
int initialCount,
int maximumCount,
string name, out
bool createdNew)
106 : this(initialCount, maximumCount, name, out createdNew, null)
128 if (initialCount < 0)
132 if (maximumCount < 1)
136 if (initialCount > maximumCount)
140 if (name !=
null && 260 < name.Length)
145 if (semaphoreSecurity !=
null)
147 Microsoft.Win32.NativeMethods.SECURITY_ATTRIBUTES sECURITY_ATTRIBUTES =
null;
148 sECURITY_ATTRIBUTES =
new Microsoft.Win32.NativeMethods.SECURITY_ATTRIBUTES();
149 sECURITY_ATTRIBUTES.nLength =
Marshal.
SizeOf((
object)sECURITY_ATTRIBUTES);
150 byte[] securityDescriptorBinaryForm = semaphoreSecurity.GetSecurityDescriptorBinaryForm();
151 byte[] array = securityDescriptorBinaryForm;
152 fixed (
byte* value = array)
154 sECURITY_ATTRIBUTES.lpSecurityDescriptor =
new SafeLocalMemHandle((
IntPtr)(
void*)value, ownsHandle:
false);
155 safeWaitHandle = Microsoft.Win32.SafeNativeMethods.CreateSemaphore(sECURITY_ATTRIBUTES, initialCount, maximumCount, name);
160 safeWaitHandle = Microsoft.Win32.SafeNativeMethods.CreateSemaphore(
null, initialCount, maximumCount, name);
163 if (safeWaitHandle.IsInvalid)
165 if (name !=
null && name.Length != 0 && 6 == lastWin32Error)
169 InternalResources.WinIOError();
171 createdNew = (lastWin32Error != 183);
172 base.SafeWaitHandle = safeWaitHandle;
177 base.SafeWaitHandle = handle;
191 [global::__DynamicallyInvokable]
214 switch (OpenExistingWorker(name, rights, out result))
216 case OpenExistingResult.NameNotFound:
218 case OpenExistingResult.NameInvalid:
220 case OpenExistingResult.PathNotFound:
221 InternalResources.WinIOError(3,
string.Empty);
240 [global::__DynamicallyInvokable]
263 return OpenExistingWorker(name, rights, out result) == OpenExistingResult.Success;
273 if (name.Length == 0)
275 throw new ArgumentException(SR.GetString(
"InvalidNullEmptyArgument",
"name"),
"name");
277 if (name !=
null && 260 < name.Length)
279 throw new ArgumentException(SR.GetString(
"Argument_WaitHandleNameTooLong"));
282 SafeWaitHandle safeWaitHandle = Microsoft.Win32.SafeNativeMethods.OpenSemaphore((
int)rights, inheritHandle:
false, name);
283 if (safeWaitHandle.IsInvalid)
286 if (2 == lastWin32Error || 123 == lastWin32Error)
288 return OpenExistingResult.NameNotFound;
290 if (3 == lastWin32Error)
292 return OpenExistingResult.PathNotFound;
294 if (name !=
null && name.Length != 0 && 6 == lastWin32Error)
296 return OpenExistingResult.NameInvalid;
298 InternalResources.WinIOError();
301 return OpenExistingResult.Success;
309 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
311 [global::__DynamicallyInvokable]
325 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
326 [global::__DynamicallyInvokable]
329 if (releaseCount < 1)
333 if (!Microsoft.Win32.SafeNativeMethods.ReleaseSemaphore(base.SafeWaitHandle, releaseCount, out
int previousCount))
337 return previousCount;
357 if (semaphoreSecurity ==
null)
361 semaphoreSecurity.Persist(base.SafeWaitHandle);
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Describes a set of security permissions applied to code. This class cannot be inherited.
static Semaphore OpenExisting(string name, SemaphoreRights rights)
Opens the specified named semaphore, if it already exists, with the desired security access.
Encapsulates operating system–specific objects that wait for exclusive access to shared resources.
The exception that is thrown when an attempt is made to open a system mutex, semaphore,...
SafeWaitHandle SafeWaitHandle
Gets or sets the native operating system handle.
void SetAccessControl(SemaphoreSecurity semaphoreSecurity)
Sets the access control security for a named system semaphore.
The exception that is thrown when the value of an argument is outside the allowable range of values a...
static Semaphore OpenExisting(string name)
Opens the specified named semaphore, if it already exists.
static int SizeOf(object structure)
Returns the unmanaged size of an object in bytes.
int Release(int releaseCount)
Exits the semaphore a specified number of times and returns the previous count.
Semaphore(int initialCount, int maximumCount)
Initializes a new instance of the T:System.Threading.Semaphore class, specifying the initial number o...
unsafe Semaphore(int initialCount, int maximumCount, string name, out bool createdNew, SemaphoreSecurity semaphoreSecurity)
Initializes a new instance of the T:System.Threading.Semaphore class, specifying the initial number o...
Cer
Specifies a method's behavior when called within a constrained execution region.
SecurityAction
Specifies the security actions that can be performed using declarative security.
static bool TryOpenExisting(string name, out Semaphore result)
Opens the specified named semaphore, if it already exists, and returns a value that indicates whether...
SemaphoreRights
Specifies the access control rights that can be applied to named system semaphore objects.
Semaphore(int initialCount, int maximumCount, string name, out bool createdNew)
Initializes a new instance of the T:System.Threading.Semaphore class, specifying the initial number o...
Semaphore(int initialCount, int maximumCount, string name)
Initializes a new instance of the T:System.Threading.Semaphore class, specifying the initial number o...
A platform-specific type that is used to represent a pointer or a handle.
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
SemaphoreSecurity GetAccessControl()
Gets the access control security for a named system semaphore.
The exception that is thrown when the Overload:System.Threading.Semaphore.Release method is called on...
Limits the number of threads that can access a resource or pool of resources concurrently.
Represents the Windows access control security for a named semaphore. This class cannot be inherited.
int Release()
Exits the semaphore and returns the previous count.
The exception that is thrown when one of the arguments provided to a method is not valid.
static bool TryOpenExisting(string name, SemaphoreRights rights, out Semaphore result)
Opens the specified named semaphore, if it already exists, with the desired security access,...
Consistency
Specifies a reliability contract.
static int GetLastWin32Error()
Returns the error code returned by the last unmanaged function that was called using platform invoke ...
SecurityPermissionFlag
Specifies access flags for the security permission object.
AccessControlSections
Specifies which sections of a security descriptor to save or load.