示例#1
0
        /// <summary>
        /// Schedules the current action for execution.
        /// </summary>
        /// <param name="action">the action to be executed.</param>
        public void Execute(IComputation action)
        {
            scheduler.Enqueue(action);
            queueCount++;

            isSignalled = wait.Set();
        }
示例#2
0
        public void Execute(IComputation computation)
        {
            lock (locker)
            {
                scheduler.Add(computation);
            }

            isSignalled = wait.Set();
        }
示例#3
0
        /// <summary>
        /// Invokes the Fiber Action.
        /// </summary>
        /// <param name="fiber">the fiber computation.</param>
        private void InvokeAction(IComputation fiber)
        {
            object      result      = fiber.Execute();
            FiberStatus fiberStatus = (FiberStatus)result;

            if (fiberStatus == FiberStatus.Done)
            {
                scheduler.Remove(fiber);
            }
        }
        /// <summary>
        /// The Compute.
        /// </summary>
        /// <param name="figures">The figures<see cref="IFigures"/>.</param>
        /// <param name="rubric">The rubric<see cref="MemberRubric"/>.</param>
        /// <returns>The <see cref="IFigures"/>.</returns>
        public static IFigures Compute(this IFigures figures, MemberRubric rubric)
        {
            IComputation ic = figures.Computations.Where(c => ((Computation)c).ContainsFirst(rubric)).FirstOrDefault();

            if (ic != null)
            {
                ic.Compute();
            }
            return(figures);
        }
示例#5
0
        /// <summary>
        /// The thread Processing loop, that consumes up the queue.
        /// </summary>
        private void Process()
        {
            IComputation localComputation = null;

            Thread.BeginThreadAffinity();
            if (cpuId != -1)
            {
                Unsafe.SetThreadAffinityMask(Unsafe.GetCurrentThread(), new IntPtr(1 << (int)cpuId));
            }

            while (true)
            {
                //check if our thread is in the signalled state,
                //if that's true then reset it and continue work.
                if (isSignalled)
                {
                    isSignalled = wait.Reset();
                }

                if (scheduler.IsEmpty == false)
                {
                    localComputation = scheduler.Dequeue();

                    if (localComputation != null)
                    {
                        InvokeAction(localComputation);
                        SmartThreadPool.Reschedule(isInitializedInPool, this, SchedulerAction.Dequeue);
                    }
                }
                else
                {
                    //lets try to steal some of this work.
                    bool workStolen = TryStealWork();

                    //if we stolen something then don't sleap and first check you work queue
                    //and then try to steal again.
                    while (workStolen)
                    {
                        workStolen = TryStealWork();
                    }

                    if (isPendingJoin)
                    {
                        break;
                    }

                    SpinWait();
                }
            }
            //end processing.
            IsStarted = false;
            wait.Close();
            Thread.EndThreadAffinity();
        }
示例#6
0
        public static void QueueWorkItem(IComputation computation)
        {
            ISchedulableThread lowestWorkloadThread = null;

            lowestWorkloadThread = threadScheduler.GetScheduledThread();

            //If a thread is not started then do Start it.
            if (lowestWorkloadThread.IsStarted == false)
            {
                lowestWorkloadThread.Start();
            }

            //schedule a task.
            lowestWorkloadThread.Execute(computation);
            threadScheduler.Reschedule(lowestWorkloadThread, SchedulerAction.Enqueue);
        }
示例#7
0
        public void Queue(IComputation computation)
        {
            if (computation.ComputationType == Resources.Computation_Fiber)
                FiberPool.QueueWorkItem(computation);
            else
            {
                if (computation.ExecutionType == ComputationExecutionType.LongRunning)
                {
                    SmartThread thread = new SmartThread(false, -1);
                    thread.Execute(computation);
                    thread.Start();
                }

                SmartThreadPool.QueueWorkItem(computation);
            }
        }
示例#8
0
        public static void QueueWorkItem(IComputation computation)
        {
            ISchedulableThread fiber = fiberScheduler.GetScheduledThread();

            if (!fiber.IsStarted)
                fiber.Start();

            //This code was commented out as upon fiber framework start we are creating
            //up front many fiber threads therefor it's pointless to exmpand even more
            //as it's hard to define a criteria how to expand.
            //
            //if (((FiberThread)fiber).ComputationCount >= nextFiberThreashold)
            //    fiberScheduler.Add(new FiberThread());

            fiber.Execute(computation);
        }
示例#9
0
        public void Queue(IComputation computation)
        {
            if (computation.ComputationType == Resources.Computation_Fiber)
            {
                FiberPool.QueueWorkItem(computation);
            }
            else
            {
                if (computation.ExecutionType == ComputationExecutionType.LongRunning)
                {
                    SmartThread thread = new SmartThread(false, -1);
                    thread.Execute(computation);
                    thread.Start();
                }

                SmartThreadPool.QueueWorkItem(computation);
            }
        }
示例#10
0
        public static void QueueWorkItem(IComputation computation)
        {
            ISchedulableThread fiber = fiberScheduler.GetScheduledThread();

            if (!fiber.IsStarted)
            {
                fiber.Start();
            }

            //This code was commented out as upon fiber framework start we are creating
            //up front many fiber threads therefor it's pointless to exmpand even more
            //as it's hard to define a criteria how to expand.
            //
            //if (((FiberThread)fiber).ComputationCount >= nextFiberThreashold)
            //    fiberScheduler.Add(new FiberThread());

            fiber.Execute(computation);
        }
示例#11
0
        private void Process()
        {
            int index = 0;

            while (true)
            {
                if (isSignalled)
                {
                    isSignalled = wait.Reset();
                }

                if (index >= Int16.MaxValue)
                {
                    index = 0;
                }

                Monitor.Enter(locker);

                if (scheduler.Count > 0)
                {
                    // Get next fiber thread from scheduller and execute.
                    IComputation fiber = scheduler[index++ % scheduler.Count];
                    InvokeAction(fiber);

                    Monitor.Exit(locker);
                }
                else
                {
                    Monitor.Exit(locker);

                    if (isPendingJoin)
                    {
                        break;
                    }

                    SpinWait();
                }
            }

            //end processing.
            IsStarted = false;
            wait.Close();
        }
示例#12
0
        /// <summary>
        /// Tries to perform a steal from the provided queue.
        /// </summary>
        /// <param name="queue">the queue type.</param>
        /// <param name="condition">the condition for the queue steal.</param>
        /// <returns>a value indicationg the sucess or failure of the operation.</returns>
        private bool TryStealFromQueue(IStealingQueue <IComputation> queue, int conditionThreshold)
        {
            // Try steal as much as possible from the selected thread as the selection
            // procedure to steal is expensive.
            while (!queue.IsEmpty && queue.UnsafeCount >= conditionThreshold)
            {
                IComputation localComputation = queue.DequeueLast();

                if (localComputation != null)
                {
                    stealCount++;
                    InvokeAction(localComputation);

                    return(true);
                }
                else
                {
                    return(false);
                }
            }

            return(false);
        }
示例#13
0
        /// <summary>
        /// Invokes the current action.
        /// </summary>
        /// <param name="localAction">localAction taken from queue.</param>
        private void InvokeAction(IComputation localAction)
        {
            // start to measure time.
            int ticksStart = System.Environment.TickCount;

            // do execute the action.
            localAction.Execute();

            //we do need to reset the stats so that they will not overflow.
            if (totalTime > int.MaxValue)
            {
                executionCount = 0;
                queueCount     = 0;
                totalTime      = 0;
            }

            //increment the counter.
            executionCount++;

            totalTime += System.Environment.TickCount - ticksStart;

            AvgTaskTime = totalTime / executionCount;
        }
示例#14
0
        /// <summary>
        /// Invokes the Fiber Action.
        /// </summary>
        /// <param name="fiber">the fiber computation.</param>
        private void InvokeAction(IComputation fiber)
        {
            object result = fiber.Execute();
            FiberStatus fiberStatus = (FiberStatus)result;

            if (fiberStatus == FiberStatus.Done)
            {
                scheduler.Remove(fiber);
            }
        }
示例#15
0
        public void Execute(IComputation computation)
        {
            lock (locker)
            {
                scheduler.Add(computation);
            }

            isSignalled = wait.Set();
        }
示例#16
0
 public void Init()
 {
     _computation = new Computation();
 }