using System;
namespace Chernobyl.Threading.Pools
{
///
/// A pool that is used with SynchronizedPools to
/// synchronize data safes.
///
public class DataSafeSynchronizePool : SynchronizedPool
{
///
/// Takes an object from the pool. If the pool has
/// no more objects to give or it does not want to
/// give an object out then null will be returned.
///
/// The object to fill with
/// the taken object.
/// True if there was an object that could be
/// taken from the pool and that object was assigned to
/// , false if otherwise.
public override bool Take(out Job objectToTake)
{
// we don't give out jobs
objectToTake = null;
return false;
}
///
/// Requests a job from this job pool. If this job pool
/// does not have a job then RequestJob() is called on it
/// until the
///
/// The first pool to call this method. This is
/// the pool that wants a job.
/// The pool that is calling this function. You should
/// always pass 'this' as the parameter.
/// The number of times this method was called.
/// A job if one is available from our pool or any of the other pools;
/// null if no pool has a job.
public override Job RequestJob(SynchronizedPool requestingPool, SynchronizedPool callingPool, ref uint poolCount)
{
// check to see if we have looped back around to ourself
// requestingPool and callingPool will be equal if we
// have called this function on ourself.
if (requestingPool != callingPool)
if (requestingPool == this)
return null; // we have looped all the way around
// increment the pool count for this pool and
// the request count
poolCount++;
RequestCount++;
if(RequestCount == SynchronizedPoolCount)
{
RequestCount = 0;
// we need to synchronize all of the
if (OnSynchronize != null)
OnSynchronize(this, EventArgs.Empty);
}
if (Next == null)
throw new Exception("SynchronizedPools must be linked together in a circular referencing fashion.");
return Next.RequestJob(requestingPool, this, ref poolCount);
}
///
/// The event that gets fired when data in the DataSafes should
/// be synchronized.
///
public event EventHandler OnSynchronize;
///
/// The number of times a request for a job has been made
/// through the method .
///
uint RequestCount {get; set;}
}
}