We need you! We're working hard on the next version of Developer Fusion -
Let us know what you think we should be up to!
- namespace System.Threading
- {
- using System.IO;
- using System.IO.Ports;
- using Microsoft.Win32;
- using Microsoft.Win32.SafeHandles;
- using System.Runtime.InteropServices;
- using System.Threading;
- using System.Security.Permissions;
- using System.Runtime.Versioning;
- using System.Runtime.ConstrainedExecution;
-
-
- [HostProtection(Synchronization = true, ExternalThreading = true)]
- [ComVisibleAttribute(false)]
- public sealed class Semaphore : WaitHandle
- {
- private static int MAX_PATH = 260;
-
-
- [ResourceExposure(ResourceScope.None)]
- [ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
- public Semaphore(int initialCount, int maximumCount) : this(initialCount, maximumCount, null)
- {
- }
-
- [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
- [ResourceExposure(ResourceScope.Machine)]
- [ResourceConsumption(ResourceScope.Machine)]
- public Semaphore(int initialCount, int maximumCount, string name)
- {
- if (initialCount < 0) {
- throw new ArgumentOutOfRangeException("initialCount", SR.GetString(SR.ArgumentOutOfRange_NeedNonNegNumRequired));
- }
-
- if (maximumCount < 1) {
- throw new ArgumentOutOfRangeException("maximumCount", SR.GetString(SR.ArgumentOutOfRange_NeedNonNegNumRequired));
- }
-
- if (initialCount > maximumCount) {
- throw new ArgumentException(SR.GetString(SR.Argument_SemaphoreInitialMaximum));
- }
-
- if (null != name && MAX_PATH < name.Length) {
- throw new ArgumentException(SR.GetString(SR.Argument_WaitHandleNameTooLong));
- }
- SafeWaitHandle myHandle = SafeNativeMethods.CreateSemaphore(null, initialCount, maximumCount, name);
-
- if (myHandle.IsInvalid) {
- int errorCode = Marshal.GetLastWin32Error();
-
- if (null != name && 0 != name.Length && NativeMethods.ERROR_INVALID_HANDLE == errorCode)
- throw new WaitHandleCannotBeOpenedException(SR.GetString(SR.WaitHandleCannotBeOpenedException_InvalidHandle, name));
-
- InternalResources.WinIOError();
- }
- this.SafeWaitHandle = myHandle;
- }
-
- [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
- [ResourceExposure(ResourceScope.Machine)]
- [ResourceConsumption(ResourceScope.Machine)]
- public Semaphore(int initialCount, int maximumCount, string name, out bool createdNew)
- {
- if (initialCount < 0) {
- throw new ArgumentOutOfRangeException("initialCount", SR.GetString(SR.ArgumentOutOfRange_NeedNonNegNumRequired));
- }
-
- if (maximumCount < 1) {
- throw new ArgumentOutOfRangeException("maximumCount", SR.GetString(SR.ArgumentOutOfRange_NeedNonNegNumRequired));
- }
-
- if (initialCount > maximumCount) {
- throw new ArgumentException(SR.GetString(SR.Argument_SemaphoreInitialMaximum));
- }
-
- if (null != name && MAX_PATH < name.Length) {
- throw new ArgumentException(SR.GetString(SR.Argument_WaitHandleNameTooLong));
- }
- SafeWaitHandle myHandle;
- myHandle = SafeNativeMethods.CreateSemaphore(null, initialCount, maximumCount, name);
- int errorCode = Marshal.GetLastWin32Error();
- if (myHandle.IsInvalid) {
- if (null != name && 0 != name.Length && NativeMethods.ERROR_INVALID_HANDLE == errorCode)
- throw new WaitHandleCannotBeOpenedException(SR.GetString(SR.WaitHandleCannotBeOpenedException_InvalidHandle, name));
- InternalResources.WinIOError();
- }
- createdNew = errorCode != NativeMethods.ERROR_ALREADY_EXISTS;
- this.SafeWaitHandle = myHandle;
- }
-
- private Semaphore(SafeWaitHandle handle)
- {
- this.SafeWaitHandle = handle;
- }
-
- [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
- [ResourceExposure(ResourceScope.Machine)]
- [ResourceConsumption(ResourceScope.Machine)]
- public static Semaphore OpenExisting(string name)
- {
- if (name == null) {
- throw new ArgumentNullException("name");
- }
- if (name.Length == 0) {
- throw new ArgumentException(SR.GetString(SR.InvalidNullEmptyArgument, "name"), "name");
- }
- if (null != name && MAX_PATH < name.Length) {
- throw new ArgumentException(SR.GetString(SR.Argument_WaitHandleNameTooLong));
- }
-
- SafeWaitHandle myHandle = SafeNativeMethods.OpenSemaphore(NativeMethods.SEMAPHORE_MODIFY_STATE | NativeMethods.SYNCHRONIZE, false, name);
-
- if (myHandle.IsInvalid) {
- int errorCode = Marshal.GetLastWin32Error();
-
- if (NativeMethods.ERROR_FILE_NOT_FOUND == errorCode || NativeMethods.ERROR_INVALID_NAME == errorCode)
- throw new WaitHandleCannotBeOpenedException();
- if (null != name && 0 != name.Length && NativeMethods.ERROR_INVALID_HANDLE == errorCode)
- throw new WaitHandleCannotBeOpenedException(SR.GetString(SR.WaitHandleCannotBeOpenedException_InvalidHandle, name));
-
- InternalResources.WinIOError();
- }
- return new Semaphore(myHandle);
- }
-
-
-
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
- [PrePrepareMethod()]
- public int Release()
- {
- return Release(1);
- }
-
-
- [ResourceExposure(ResourceScope.None)]
- [ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
- public int Release(int releaseCount)
- {
- if (releaseCount < 1) {
- throw new ArgumentOutOfRangeException("releaseCount", SR.GetString(SR.ArgumentOutOfRange_NeedNonNegNumRequired));
- }
- int previousCount;
-
-
-
-
-
- if (!SafeNativeMethods.ReleaseSemaphore(SafeWaitHandle, releaseCount, out previousCount)) {
- throw new SemaphoreFullException();
- }
-
- return previousCount;
- }
-
- }
- }