/*
* Process Hacker -
* run once structure
*
* 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.Threading;
namespace ProcessHacker.Common.Threading
{
///
/// Provides methods for synchronizing the execution of a
/// single action.
///
public struct ActionSync
{
private int _value;
private readonly int _target;
private readonly Action _action;
///
/// Initializes an action-sync structure.
///
/// The action to be executed.
///
/// The target value required in order to execute the action.
///
public ActionSync(Action action, int target)
{
_action = action;
_value = 0;
_target = target;
}
///
/// Gets the current value of the action-sync structure.
///
public int Value
{
get { return _value; }
}
///
/// Increments the current value and executes the action
/// if the target value has been reached due to this call.
///
///
/// The action is guaranteed to execute exactly once,
/// even if two threads increment the value at the same
/// time.
///
public void Increment()
{
if (Interlocked.Increment(ref _value) == _target)
_action();
}
///
/// Increments the current value and executes the action
/// if the target value has been reached.
///
///
/// The action may be executed multiple times, as long
/// as the target value has been reached.
///
public void IncrementMultiple()
{
if (Interlocked.Increment(ref _value) >= _target)
_action();
}
}
}