public QueueBucket(int size)
 {
     this.Array          = new T[size];
     this.PreviousBucket = null;
     this.NextBucket     = null;
     this.Count          = 0;
 }
Beispiel #2
0
 private WorkContext(int initialWorkCapacity, int dedicatedThreads, string name, bool disposable)
 {
     if (dedicatedThreads < 0)
     {
         throw new ArgumentOutOfRangeException("dedicatedThreads", "dedicatedThreads < 0");
     }
     else
     {
         _dedidatedThreadMax = dedicatedThreads;
         _works = new QueueBucket<Work>(initialWorkCapacity);
         Converter<int, Thread> valueFactory = null;
         if (StringHelper.IsNullOrWhiteSpace(name))
         {
             if (_dedidatedThreadMax == 1)
             {
                 valueFactory = input => new Thread(DoWorks)
                 {
                     Name = string.Format("Dedicated Thread on Work.Context {0}", _id),
                     IsBackground = true
                 };
             }
             else if (_dedidatedThreadMax > 1)
             {
                 valueFactory = input => new Thread(DoWorks)
                 {
                     Name = string.Format("Dedicated Thread {0} on Work.Context {1}", input, _id),
                     IsBackground = true
                 };
             }
         }
         else
         {
             if (_dedidatedThreadMax == 1)
             {
                 valueFactory = input => new Thread(DoWorks)
                 {
                     Name = string.Format("Dedicated Thread on {0}", name),
                     IsBackground = true
                 };
             }
             else if (_dedidatedThreadMax > 1)
             {
                 valueFactory = input => new Thread(DoWorks)
                 {
                     Name = string.Format("Dedicated Thread {0} on {1}", input, name),
                     IsBackground = true
                 };
             }
         }
         _threads = new LazyBucket<Thread>
             (
                 valueFactory,
                 dedicatedThreads
             );
         _id = Interlocked.Increment(ref _lastId) - 1;
         _event = new AutoResetEvent(false);
         _work = true;
         _disposable = disposable;
     }
 }
 internal QueueBucket(int size)
 {
     Array          = new T[size];
     PreviousBucket = null;
     NextBucket     = null;
     Count          = 0;
 }
 private void RemoveFirstBucket()
 {
     this.m_firstBucket = this.m_firstBucket.NextBucket;
     this.m_removeIndex = 0;
     if (this.m_firstBucket == null)
     {
         this.m_lastBucket = null;
     }
     else
     {
         this.m_firstBucket.PreviousBucket = null;
     }
 }
 private void RemoveFirstBucket()
 {
     m_firstBucket = m_firstBucket.NextBucket;
     m_removeIndex = 0;
     if (m_firstBucket == null)
     {
         m_lastBucket = null;
     }
     else
     {
         m_firstBucket.PreviousBucket = null;
     }
 }
 public bool MoveNext()
 {
     if (this.m_currentBucket == null)
     {
         this.m_currentBucket = this.m_queue.m_firstBucket;
         this.m_currentIndex  = -1;
     }
     do
     {
         this.m_currentIndex++;
         if (this.m_currentBucket != null && this.m_currentIndex == this.m_queue.m_bucketSize)
         {
             this.m_currentBucket = this.m_currentBucket.NextBucket;
             this.m_currentIndex  = 0;
         }
     }while (this.m_currentBucket != null && this.m_currentBucket.Array[this.m_currentIndex] == null);
     return(this.m_currentBucket != null);
 }
 public bool MoveNext()
 {
     if (m_currentBucket == null)
     {
         m_currentBucket = m_queue.m_firstBucket;
         m_currentIndex  = -1;
     }
     do
     {
         m_currentIndex++;
         if (m_currentBucket != null && m_currentIndex == m_queue.m_bucketSize)
         {
             m_currentBucket = m_currentBucket.NextBucket;
             m_currentIndex  = 0;
         }
     }while (m_currentBucket != null && m_currentBucket.Array[m_currentIndex] == null);
     return(m_currentBucket != null);
 }
 public void RemoveCurrent()
 {
     m_currentBucket.Array[m_currentIndex] = null;
     m_currentBucket.Count--;
     m_queue.m_count--;
     if (m_currentBucket.Count != 0)
     {
         return;
     }
     if (m_currentBucket == m_queue.m_firstBucket)
     {
         m_queue.RemoveFirstBucket();
         m_currentBucket = m_queue.m_firstBucket;
         m_currentIndex  = -1;
     }
     else if (m_currentBucket == m_queue.m_lastBucket)
     {
         if (m_currentBucket.PreviousBucket == null)
         {
             m_queue.m_firstBucket = null;
             m_queue.m_lastBucket  = null;
             m_queue.m_count       = 0;
         }
         else
         {
             m_queue.m_lastBucket            = m_currentBucket.PreviousBucket;
             m_queue.m_lastBucket.NextBucket = null;
             m_queue.m_insertIndex           = m_queue.m_bucketSize;
             m_currentBucket.PreviousBucket  = null;
         }
     }
     else
     {
         m_currentBucket.NextBucket.PreviousBucket = m_currentBucket.PreviousBucket;
         m_currentBucket.PreviousBucket.NextBucket = m_currentBucket.NextBucket;
         QueueBucket currentBucket = m_currentBucket;
         m_currentBucket              = m_currentBucket.NextBucket;
         m_currentIndex               = -1;
         currentBucket.NextBucket     = null;
         currentBucket.PreviousBucket = null;
     }
 }
 public void RemoveCurrent()
 {
     this.m_currentBucket.Array[this.m_currentIndex] = null;
     this.m_currentBucket.Count--;
     this.m_queue.m_count--;
     if (this.m_currentBucket.Count == 0)
     {
         if (this.m_currentBucket == this.m_queue.m_firstBucket)
         {
             this.m_queue.RemoveFirstBucket();
             this.m_currentBucket = this.m_queue.m_firstBucket;
             this.m_currentIndex  = -1;
         }
         else if (this.m_currentBucket == this.m_queue.m_lastBucket)
         {
             if (this.m_currentBucket.PreviousBucket == null)
             {
                 this.m_queue.m_firstBucket = null;
                 this.m_queue.m_lastBucket  = null;
                 this.m_queue.m_count       = 0;
             }
             else
             {
                 this.m_queue.m_lastBucket            = this.m_currentBucket.PreviousBucket;
                 this.m_queue.m_lastBucket.NextBucket = null;
                 this.m_queue.m_insertIndex           = this.m_queue.m_bucketSize;
                 this.m_currentBucket.PreviousBucket  = null;
             }
         }
         else
         {
             this.m_currentBucket.NextBucket.PreviousBucket = this.m_currentBucket.PreviousBucket;
             this.m_currentBucket.PreviousBucket.NextBucket = this.m_currentBucket.NextBucket;
             QueueBucket currentBucket = this.m_currentBucket;
             this.m_currentBucket         = this.m_currentBucket.NextBucket;
             this.m_currentIndex          = -1;
             currentBucket.NextBucket     = null;
             currentBucket.PreviousBucket = null;
         }
     }
 }
 public void Enqueue(T item)
 {
     if (this.m_firstBucket == null)
     {
         this.m_firstBucket                = new QueueBucket(this.m_bucketSize);
         this.m_lastBucket                 = this.m_firstBucket;
         this.m_firstBucket.NextBucket     = null;
         this.m_firstBucket.PreviousBucket = null;
         this.m_insertIndex                = 0;
     }
     if (this.m_insertIndex == this.m_bucketSize)
     {
         QueueBucket lastBucket = this.m_lastBucket;
         this.m_lastBucket                = new QueueBucket(this.m_bucketSize);
         this.m_lastBucket.NextBucket     = null;
         this.m_lastBucket.PreviousBucket = lastBucket;
         lastBucket.NextBucket            = this.m_lastBucket;
         this.m_insertIndex               = 0;
     }
     this.m_lastBucket.Array[this.m_insertIndex] = item;
     this.m_lastBucket.Count++;
     this.m_insertIndex++;
     this.m_count++;
 }
 internal void Enqueue(T item)
 {
     if (m_firstBucket == null)
     {
         m_firstBucket                = new QueueBucket(m_bucketSize);
         m_lastBucket                 = m_firstBucket;
         m_firstBucket.NextBucket     = null;
         m_firstBucket.PreviousBucket = null;
         m_insertIndex                = 0;
     }
     if (m_insertIndex == m_bucketSize)
     {
         QueueBucket lastBucket = m_lastBucket;
         m_lastBucket                = new QueueBucket(m_bucketSize);
         m_lastBucket.NextBucket     = null;
         m_lastBucket.PreviousBucket = lastBucket;
         lastBucket.NextBucket       = m_lastBucket;
         m_insertIndex               = 0;
     }
     m_lastBucket.Array[m_insertIndex] = item;
     m_lastBucket.Count++;
     m_insertIndex++;
     m_count++;
 }
 public void Clear()
 {
     this.m_count       = 0;
     this.m_firstBucket = null;
     this.m_lastBucket  = null;
 }
 public void Reset()
 {
     this.m_currentBucket = null;
     this.m_currentIndex  = -1;
 }
 internal void Clear()
 {
     m_count       = 0;
     m_firstBucket = null;
     m_lastBucket  = null;
 }
 public void Reset()
 {
     m_currentBucket = null;
     m_currentIndex  = -1;
 }