/*
* Process Hacker -
* mutant
*
* 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 mutant which can be used to synchronize access to a shared resource.
///
public sealed class Mutant : NativeObject
{
///
/// Creates a mutant.
///
public Mutant()
: this(null)
{ }
///
/// Creates a mutant.
///
///
/// Whether the mutant should become owned by the current
/// thread when it is created.
///
public Mutant(bool owned)
: this(null, owned)
{ }
///
/// Creates or opens a mutant.
///
///
/// The name of the new mutant, or the name of an existing mutant to open.
///
public Mutant(string name)
: this(name, false)
{ }
///
/// Creates a mutant.
///
///
/// The name of the new mutant.
///
///
/// Whether the mutant should become owned by the current
/// thread when it is created.
///
public Mutant(string name, bool owned)
{
this.Handle = MutantHandle.Create(
MutantAccess.All,
name,
ObjectFlags.OpenIf,
null,
owned
);
}
///
/// Gets whether the mutant is currently owned.
///
public bool Owned
{
get { return this.Handle.GetBasicInformation().CurrentCount <= 0; }
}
///
/// Releases the mutant, allowing other waiting threads to own it.
///
public void Release()
{
this.Handle.Release();
}
}
}