namespace Chernobyl.Threading.Pools
{
///
/// A pool that just holds a list of completed and
/// uncompleted jobs. Each time take is called, a
/// job is pulled off the uncompleted list and passed
/// back. When the job is given back, that job is placed
/// onto the completed jobs.
///
public class AsynchronizedPool : ThreadPool
{
///
/// Takes a job from the pool. If the pool has
/// no more jobs to give or it does not want to
/// give a job out then null will be returned.
///
/// The job to fill with
/// the taken job.
/// True if there was a job that could be
/// taken from the pool and that job was assigned to
/// , false if otherwise.
public override bool Take(out Job objectToTake)
{
// are their any jobs left?
lock(UncompletedJobs)
{
if (UncompletedJobs.First != null)
{
objectToTake = UncompletedJobs.First.Value;
return true;
}
// refresh our job pool
lock(CompletedJobs)
{
UncompletedJobs = CompletedJobs;
CompletedJobs.Clear();
objectToTake = UncompletedJobs.First.Value;
return true;
}
}
}
}
}