Exemplo n.º 1
0
 /// <summary>
 /// Enqueue unprioritized backround job, maintenance job maybe
 /// </summary>
 /// <param name="job"></param>
 public void DoNonpiorityJob(PlanItem job)
 {
     lock (OnceJobs)
     {
         OnceJobs.Add(job);
     }
 }
Exemplo n.º 2
0
        static void IsolatedThreadEntry(object o)
        {
            ThreadContext ti = o as ThreadContext;

            ti.ManagedID = ti.hThread.ManagedThreadId;
            PlanItem pi = ti.Job;

            pi.JobEntry(ti, pi);

            ExitThread(ti);
        }
Exemplo n.º 3
0
 private int TaskEntry(TaskScheduler.ThreadContext ti, TaskScheduler.PlanItem pi)
 {
     // TODO: remove this method , replace that routing to task registration
     QueueTask task = pi as QueueTask;
     switch (task.Module.Role)
     {
         case ExecutionType.Consumer:
             return ConsumerEntry(task);
             break;
         case ExecutionType.Producer:
             return ProducerEntry(task);
             break;
     }
     return 1;
 }
Exemplo n.º 4
0
        public void CreateIsolatedThreadForPlan(PlanItem pi)
        {
            if (pi.intervalType == IntervalType.isolatedThread)
            {
                Thread thread = new Thread(new ParameterizedThreadStart(IsolatedThreadEntry));
                thread.Name = "TaskScheduler IsolatedThread#" + threads.Count.ToString() + " (" + pi.NameAndDescription + ")";
                ThreadContext ti = new ThreadContext()
                {
                    hThread  = thread,
                    rootPlan = plan,
                    Job      = pi,
                    Isolated = true
                };

                thread.Start(ti);
                threads.Add(ti);
            }
        }
Exemplo n.º 5
0
        private int TaskEntry(TaskScheduler.ThreadContext ti, TaskScheduler.PlanItem pi)
        {
            QueueTask task = pi as QueueTask;

            switch (task.Module.Role)
            {
            case ExecutionType.Consumer:
                return(ConsumerEntry(task));

                break;

            case ExecutionType.Producer:
                return(ProducerEntry(task));

                break;
            }
            return(1);
        }
Exemplo n.º 6
0
 private int IsolatedTaskEntry(TaskScheduler.ThreadContext ti, TaskScheduler.PlanItem pi)
 {
     QueueTask task = pi as QueueTask;
     try
     {
         ((IModIsolatedProducer)task.Module.MI).IsolatedProducer(task.Parameters);
     }
     catch (Exception e)
     {
         logger.Exception(e, "isolated call procedure", "module '{0}' will be turned off", task.ModuleName);
         ti.StopThread = true;
     }
     //task.Module.Producer(task.Parameters);
     while (!ti.StopThread)
     {
         System.Threading.Thread.Sleep(100);
     }
     ((IModIsolatedProducer)task.Module.MI).IsolatedProducerStop();
     return 1;
 }
Exemplo n.º 7
0
        private int BeforeNextMs()
        {
            lock (PlanComponents)
            {
                var n = from i in PlanComponents
                        where !i.Suspended && i.intervalType != IntervalType.isolatedThread &&
                        !i.ExucutingNow && i.MillisecondsBeforeExecute() > 0
                        orderby i.LAMS
                        select i;

                PlanItem Next = n.FirstOrDefault();
                if (Next == null)
                {
                    return(1000);// default wait 1 sec
                }
                else
                {
                    return((int)Next.LAMS);
                }
            }
        }
Exemplo n.º 8
0
        private int BeforeNextSec()
        {
            lock (PlanComponents)
            {
                var n = from i in PlanComponents
                        //where !i.Suspended && i.intervalType != IntervalType.isolatedThread &&
                        //!i.ExucutingNow && i.SecondsBeforeExecute() > 0
                        where !i.Suspended && i.intervalType > IntervalType.withoutInterval &&
                        !i.ExucutingNow && i.SecondsBeforeExecute() > 0
                        orderby i.LAMS
                        select i;

                PlanItem NextJob = n.FirstOrDefault();
                if (NextJob == null)
                {
                    return(1);// default wait 1 sec
                }
                else
                {
                    return((int)NextJob.LAMS);
                }
            }
        }
Exemplo n.º 9
0
        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;
        }
Exemplo n.º 10
0
        private void PopulateQueue(PlanItem[] p)
        {
            CurrentPlanQueue = p;
            CPQueueCursor = 0;

            refilled.Set();
        }
Exemplo n.º 11
0
        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);
        }
Exemplo n.º 12
0
 public void DeferJob(PlanItem job)
 {
     plan.DoNonpiorityJob(job);
 }
Exemplo n.º 13
0
        public void CreateIsolatedThreadForPlan(PlanItem pi)
        {
            if (pi.intervalType == IntervalType.isolatedThread)
            {
                Thread thread = new Thread(new ParameterizedThreadStart(IsolatedThreadEntry));
                thread.Name = "TaskScheduler IsolatedThread#" + pi.NameAndDescription;
                ThreadContext ti = new ThreadContext()
                {
                    hThread = thread,
                    rootPlan = plan,
                    Job = pi,
                    Isolated = true
                };

                thread.Start(ti);
                threads.Add(ti);
            }
        }
Exemplo n.º 14
0
 public void DeferJob(PlanItem job)
 {
     plan.DoNonpiorityJob(job);
 }
Exemplo n.º 15
0
        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);
        }