Example #1
0
        private void Balance()
        {
            BalancedThread[] array = this.balancedThreadList;
            if (array == null)
            {
                return;
            }
            Scheduler.Schedule(array[0].JobProcessor, this.threadBalancer, 500);
            ThreadLoad     threadLoad      = null;
            BalancedThread balancedThread  = null;
            BalancedThread balancedThread2 = null;

            for (int i = 0; i < array.Length; i++)
            {
                ThreadLoad mostBurden = array[i].MostBurden;
                if (mostBurden != null && array[i].TotalLoad >= 128 && (threadLoad == null || mostBurden.PreviousLoad < threadLoad.PreviousLoad))
                {
                    balancedThread = array[i];
                    threadLoad     = mostBurden;
                }
                else if (balancedThread2 == null || balancedThread2.TotalLoad > array[i].TotalLoad)
                {
                    balancedThread2 = array[i];
                }
            }
            if (balancedThread != null && balancedThread2 != null && balancedThread.TotalLoad >= balancedThread2.TotalLoad * 3)
            {
                balancedThread.Change(threadLoad, balancedThread2);
            }
        }
Example #2
0
 public void Remove(ThreadLoad threadLoad)
 {
     if (this.balancedThreadList == null)
     {
         throw new InvalidOperationException("Instance not initialized");
     }
     threadLoad.Parent.Remove(threadLoad);
 }
Example #3
0
 public void Change(ThreadLoad threadLoad, BalancedThread newBalancedThread)
 {
     if (!this.jobProcessor.IsInThread())
     {
         this.jobProcessor.Enqueue(Job.Create <ThreadLoad, BalancedThread>(new Action <ThreadLoad, BalancedThread>(this.Change), threadLoad, newBalancedThread));
         return;
     }
     if (threadLoad.Parent != this)
     {
         return;
     }
     this.Remove(threadLoad);
     newBalancedThread.Add(threadLoad);
 }
Example #4
0
 public void Add(ThreadLoad threadLoad)
 {
     if (this.balancedThreadList == null)
     {
         throw new InvalidOperationException("Instance not initialized");
     }
     if (threadLoad.Parent != null)
     {
         throw new InvalidOperationException("ThreadLoad had been added to another LoadBalancer");
     }
     this.balancedThreadList[this.insertIndex].Add(threadLoad);
     if (this.insertIndex == this.balancedThreadList.Length - 1)
     {
         this.insertIndex = 0;
         return;
     }
     this.insertIndex++;
 }
Example #5
0
 public void Add(ThreadLoad threadLoad)
 {
     if (!this.jobProcessor.IsInThread())
     {
         this.jobProcessor.Enqueue(Job.Create <ThreadLoad>(new Action <ThreadLoad>(this.Add), threadLoad));
         return;
     }
     if (threadLoad.Parent == this)
     {
         this.Reserve(threadLoad);
         return;
     }
     if (threadLoad.Parent != null)
     {
         throw new InvalidOperationException("ThreadLoad can be registered to one LoadBalancer at same time");
     }
     threadLoad.Parent       = this;
     threadLoad.PreviousLoad = 0;
     this.Reserve(threadLoad);
 }
Example #6
0
        private void Operate()
        {
            if (this.jobList.Count == 0)
            {
                return;
            }
            ThreadLoad threadLoad = this.jobList.RemoveMin();

            try
            {
                threadLoad.InvokeOperationable();
            }
            finally
            {
                if (threadLoad.Parent == this)
                {
                    this.Reserve(threadLoad);
                }
            }
        }
        private void ThreadLoad_Operationable(ThreadLoad sender, EventArgs e)
        {
            this.threadLoad.Load = this.totalLoad;
            this.AddPendingLoadFragments();
            if (this.jobList.Count == 0)
            {
                return;
            }
            LoadFragment loadFragment = this.jobList.RemoveMin();

            try
            {
                loadFragment.InvokeOperationable();
            }
            finally
            {
                if (loadFragment.Manager == this)
                {
                    this.DoReserve(loadFragment);
                }
            }
        }
Example #8
0
        public void Reserve(ThreadLoad threadLoad)
        {
            if (!this.jobProcessor.IsInThread())
            {
                this.jobProcessor.Enqueue(Job.Create <ThreadLoad>(new Action <ThreadLoad>(this.Reserve), threadLoad));
                return;
            }
            if (threadLoad.Parent != this)
            {
                throw new InvalidOperationException();
            }
            if (threadLoad.PriorityQueueElement.Valid)
            {
                return;
            }
            int previousLoad = threadLoad.PreviousLoad;
            int load         = threadLoad.Load;

            threadLoad.PreviousLoad = load;
            this.totalLoad         += load - previousLoad;
            if (load != 0)
            {
                threadLoad.PriorityQueueElement.Value    = threadLoad;
                threadLoad.PriorityQueueElement.Priority = threadLoad.Priority;
                this.jobList.Add(threadLoad.PriorityQueueElement);
                if (this.jobList.Count > 0)
                {
                    this.jobProcessor.Enqueue(this.job);
                }
                if (this.mostBurden != null && (this.mostBurden.PreviousLoad == 0 || this.mostBurden.PreviousLoad * 3 > this.totalLoad))
                {
                    this.mostBurden = null;
                }
                if (threadLoad.Load * 3 <= this.totalLoad && (this.mostBurden == null || this.mostBurden.PreviousLoad < load))
                {
                    this.mostBurden = threadLoad;
                }
            }
        }
Example #9
0
 public void Remove(ThreadLoad threadLoad)
 {
     if (!this.jobProcessor.IsInThread())
     {
         this.jobProcessor.Enqueue(Job.Create <ThreadLoad>(new Action <ThreadLoad>(this.Remove), threadLoad));
         return;
     }
     if (threadLoad.Parent != this)
     {
         throw new InvalidOperationException("ThreadLoad must be registered to proper LoadBalancer before deregister.");
     }
     if (threadLoad.PreviousLoad != 0)
     {
         this.jobList.Remove(threadLoad.PriorityQueueElement);
         this.totalLoad -= threadLoad.PreviousLoad;
     }
     if (this.mostBurden == threadLoad)
     {
         this.mostBurden = null;
     }
     threadLoad.Parent = null;
 }