using System.Collections.Generic; using Chernobyl.DesignPatterns.Pool; namespace Chernobyl.Threading.Pools { /// /// Used to make creating threading pools /// easier. /// public abstract class ThreadPool : IPool { /// /// 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 object 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 abstract bool Take(out Job objectToTake); /// /// Returns a previously taken job to the pool. /// /// The job to return /// to the pool. public virtual void Return(Job objectToReturn) { lock (CompletedJobs) CompletedJobs.AddLast(objectToReturn); } /// /// Adds a job to the list of uncompleted jobs. /// /// The job to add to the list. public virtual void Add(Job item) { lock (UncompletedJobs) UncompletedJobs.AddLast(item); } /// /// Clears out all of the jobs in the uncompleted /// and completed jobs lists. /// public void Clear() { lock (UncompletedJobs) UncompletedJobs.Clear(); lock (CompletedJobs) CompletedJobs.Clear(); } /// /// Returns the number of jobs in the completed and uncompleted /// job lists. /// public int Count { get { lock (UncompletedJobs) lock (CompletedJobs) return UncompletedJobs.Count + CompletedJobs.Count; } } /// /// Removes one of the jobs from either the completed /// or uncompleted job lists. /// /// The job to remove. /// True if the job was removed, false if /// otherwise. public virtual bool Remove(Job item) { bool result = false; lock (CompletedJobs) result = CompletedJobs.Remove(item); if (result == false) { lock (UncompletedJobs) return UncompletedJobs.Remove(item); } return true; } /// /// The jobs that have yet to be completed. /// protected LinkedList UncompletedJobs { get; set; } /// /// A list of all the jobs that have been completed or the /// jobs that were returned to this pool through Return(Job). /// protected LinkedList CompletedJobs { get; set; } } }