public PlanItem Next(bool wait) { PlanItem Dequeued = null; lock (CurrentPlanQueue) { bool planNotEmpty = CurrentPlanQueue.Length > CPQueueCursor; if (planNotEmpty) { Dequeued = CurrentPlanQueue[CPQueueCursor]; CPQueueCursor++; } else { PlanItem[] jnewcmpnts = null; PlanItem[] newcmpnts = null; lock (OnceJobs) { if (OnceJobs.Count > 0) { jnewcmpnts = OnceJobs.ToArray(); OnceJobs.Clear(); } } if (jnewcmpnts != null)// deferred jobs found, refill with plan { PlanItem[] Pnewcmpnts = OrderComponents(); newcmpnts = new PlanItem[jnewcmpnts.Length + Pnewcmpnts.Length]; Array.Copy(jnewcmpnts, newcmpnts, jnewcmpnts.Length); Array.Copy(Pnewcmpnts, 0, newcmpnts, jnewcmpnts.Length, Pnewcmpnts.Length); } else// deferred jobs not found, try to refill plan { newcmpnts = OrderComponents(); } planNotEmpty = newcmpnts.Length > 0; if (planNotEmpty) { if (newcmpnts.Length > 1) { // populate queue PopulateQueue(newcmpnts);// let wait handled thread do that with race for job } else { Dequeued = newcmpnts[0];// this thread has exclusive access without race for job } } else { Dequeued = null;// plan is empty } } } if (Dequeued != null) { lock (PlanComponents) { Dequeued.SetStartExecution(); } } else if (wait) { // check if we have a job to wait int waitms = this.BeforeNextMs(); if (waitms > 0) { refilled.WaitOne(waitms); } Dequeued = this.Next(false); } return(Dequeued); }
public PlanItem Next(bool wait) { PlanItem Dequeued = null; lock (InstantPlanQueue) { if (InstantPlanQueue.Length > InstantPQueueCursor) { Dequeued = InstantPlanQueue[InstantPQueueCursor]; if (!Dequeued.ExucutingNow) { InstantPQueueCursor++; //Dequeued.SetStartExecution(); Dequeued.ExucutingNow = true; return(Dequeued); } } //else if (InstantPlanQueue.Length > 0) { bool next = false; //lock (planSync) //{ next = CurrentPlanQueue.Length > CPQueueCursor; //} if (!next) { while (true) { if (InstantPQueueCursor >= InstantPlanQueue.Length) { InstantPQueueCursor = 0; //Thread.Sleep(0); Thread.Yield(); } Dequeued = InstantPlanQueue[InstantPQueueCursor]; InstantPQueueCursor++; if (Dequeued.ExucutingNow) { continue; } //Dequeued.SetStartExecution(); Dequeued.ExucutingNow = true; break; } return(Dequeued); } InstantPQueueCursor = 0; } } lock (planSync) { bool planNotEmpty = CurrentPlanQueue.Length > CPQueueCursor; //int qq = CPQueueCursor; if (planNotEmpty) { //if (CPQueueCursor != qq) throw new Exception(); Dequeued = CurrentPlanQueue[CPQueueCursor]; CPQueueCursor++; Dequeued.SetStartExecution(); } else { PlanItem[] jnewcmpnts = null; PlanItem[] newcmpnts = null; lock (OnceJobs) { if (OnceJobs.Count > 0) { jnewcmpnts = OnceJobs.ToArray(); OnceJobs.Clear(); } } if (jnewcmpnts != null)// deferred jobs found, refill with plan { PlanItem[] Pnewcmpnts = OrderComponents(); newcmpnts = new PlanItem[jnewcmpnts.Length + Pnewcmpnts.Length]; Array.Copy(jnewcmpnts, newcmpnts, jnewcmpnts.Length); Array.Copy(Pnewcmpnts, 0, newcmpnts, jnewcmpnts.Length, Pnewcmpnts.Length); } else// deferred jobs not found, try to refill plan { newcmpnts = OrderComponents(); } planNotEmpty = newcmpnts.Length > 0; if (planNotEmpty) { if (newcmpnts.Length > 1) { // populate queue PopulateQueue(newcmpnts);// let wait handled thread do that with race for job ** // exit from losck //** Dequeued = CurrentPlanQueue[CPQueueCursor]; CPQueueCursor++;// this thread has exclusive access without race for job Dequeued.SetStartExecution(); } else { Dequeued = newcmpnts[0];// this thread has exclusive access without race for job Dequeued.SetStartExecution(); } } else { Dequeued = null;// plan is empty } } } //if (Dequeued != null) //{ // lock (PlanComponents) // { // Dequeued.SetStartExecution(); // } //} //else if (wait) if (wait && Dequeued == null) { // check if we have a job to wait int waitsec = this.BeforeNextSec(); //Console.WriteLine("waitms: {0}", waitms); if (waitsec > 0) { Console.WriteLine("wait {0}", Thread.CurrentThread.Name); refilled.WaitOne(waitsec * 1000);// - System.DateTime.UtcNow.Millisecond); } Dequeued = this.Next(false); } //if (Dequeued == null && InstantPlanQueue.Length > 0) //{ // Dequeued = InstantPlanQueue[InstantPQueueCursor]; // InstantPQueueCursor++; // //Dequeued.SetStartExecution(); //} //Console.WriteLine("deq {0} {1}", Dequeued == null, InstantPlanQueue.Length); return(Dequeued); }