using System; using System.Collections.Generic; using System.Text; using ProcessHacker.Native.Api; using ProcessHacker.Native.Objects; using ProcessHacker.Native.Security; namespace ProcessHacker.Native.Threading { /// /// Represents a semaphore which can be used to control access to a shared resource. /// public class Semaphore : NativeObject { /// /// Creates a binary semaphore. /// public Semaphore() : this(null) { } /// /// Creates a binary semaphore. /// /// The initial count of the semaphore. /// The maximum count of the semaphore. public Semaphore(int initialCount, int maximumCount) : this(null, initialCount, maximumCount) { } /// /// Creates or opens a semaphore. /// /// /// The name of the new semaphore, or the name of an existing semaphore. /// public Semaphore(string name) : this(name, 1, 1) { } /// /// Creates a semaphore. /// /// The name of the new semaphore. /// The initial count of the semaphore. /// The maximum count of the semaphore. public Semaphore(string name, int initialCount, int maximumCount) { this.Handle = SemaphoreHandle.Create( SemaphoreAccess.All, name, ObjectFlags.OpenIf, null, initialCount, maximumCount ); } /// /// Gets the current count of the semaphore. /// public int Count { get { return this.Handle.GetBasicInformation().CurrentCount; } } /// /// Gets the maximum count of the semaphore. /// public int MaximumCount { get { return this.Handle.GetBasicInformation().MaximumCount; } } /// /// Releases the semaphore, incrementing the count. /// public void Release() { this.Handle.Release(); } /// /// Releases the semaphore, incrementing the count by the /// specified amount. /// /// The amount to increment the count by. public void Release(int count) { this.Handle.Release(count); } } }