using Chernobyl.DesignPatterns.Visitor;
namespace Chernobyl.Threading.Pools.Visitors
{
///
/// A visitor that adds a new synchronized pool to the
/// circular reference chain of synchronized pools.
///
public class AddSynchronizedPoolVisitor : IVisitor
{
///
/// Constructor.
///
/// The pool to add to the circular reference chain.
public AddSynchronizedPoolVisitor(SynchronizedPool poolToAdd)
{
PoolToAdd = poolToAdd;
}
///
/// Visits a synchronized pool and adds the requested pool
/// to this pools' circular reference chain.
///
/// The host to visit
public void Visit(SynchronizedPool host)
{
if (StartHost == null)
{
StartHost = host;
// set the new next pool and set the
host.Next = PoolToAdd;
PoolToAdd.SynchronizingObject = host.SynchronizingObject;
}
else if(host.Next == null)
{
// this is the end of the reference chain
// so we need to hook it back up to the start
host.Next = StartHost;
}
else if (StartHost == host) // don't loop forever
return;
host.SynchronizedPoolCount++;
host.Accept(this);
}
///
/// The first pool that we visited.
///
public SynchronizedPool StartHost { get; protected set; }
///
/// The pool to add to the circular reference chain.
///
public SynchronizedPool PoolToAdd { get; protected set; }
}
}