/* * Process Hacker - * semaphore * * Copyright (C) 2009 wj32 * * This file is part of Process Hacker. * * Process Hacker is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Process Hacker is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Process Hacker. If not, see . */ 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 sealed 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); } } }