CyclicDeque.cs :  » 2.6.4-mono-.net-core » System.Threading » System » Threading » Tasks » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » 2.6.4 mono .net core » System.Threading 
System.Threading » System » Threading » Tasks » CyclicDeque.cs
#if NET_4_0 || BOOTSTRAP_NET_4_0
// 
// CyclicDeque.cs
//  
// Author:
//       Jrmie "Garuma" Laval <jeremie.laval@gmail.com>
// 
// Copyright (c) 2009 Jrmie "Garuma" Laval
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Threading;

namespace System.Threading.Tasks{
  internal enum PopResult  {
    Succeed,
    Empty,
    Abort
  }

  internal interface IDequeOperations<T>
  {
    void PushBottom (T obj);
    PopResult PopBottom (out T obj);
    PopResult PopTop (out T obj);
  }
  
  internal interface IValueReader
  {
    long Read (ref long value);
  }
  
  internal class CyclicDeque<T> : IDequeOperations<T>
  {
    readonly IValueReader reader;
    const int BaseSize = 11;
    
    class Reader32 : IValueReader
    {
      public long Read (ref long value)
      {
        return Interlocked.Read (ref value);
      }
    }
    
    class Reader64 : IValueReader
    {
      public long Read (ref long value)
      {
        return value;
      }
    }
    
    public CyclicDeque ()
    {
      // We do the distinction between 32bits and 64bits
      if (IntPtr.Size == 4)
        reader = new Reader32 ();
      else
        reader = new Reader64 ();
    }
    
    long bottom;
    long top;
    long upperBound;
    CircularArray<T> array = new CircularArray<T> (BaseSize);
    
    public void PushBottom (T obj)
    {
      long b = reader.Read (ref bottom);
      var a = array;
      
      // Take care of growing
      if (b - upperBound >= a.Size - 1) {
        upperBound = reader.Read (ref top);
        a = a.Grow (b, upperBound);
        Interlocked.Exchange (ref array, a);
      }
      
      // Register the new value
      a[b] = obj;
      Interlocked.Increment (ref bottom);
    }
    
    public PopResult PopBottom (out T obj)
    {
      obj = default (T);
      
      long b = Interlocked.Decrement (ref bottom);
      var a = array;
      long t = reader.Read (ref top);
      long size = b - t;
      
      if (size < 0) {
        // Set bottom to t
        Interlocked.Add (ref bottom, t - b);
        return PopResult.Empty;
      }
      
      obj = a[b];
      if (size > 0)
        return PopResult.Succeed;
      Interlocked.Add (ref bottom, t + 1 - b);
      
      if (Interlocked.CompareExchange (ref top, t + 1, t) != t)
        return PopResult.Empty;
      
      return PopResult.Succeed;
    }
    
    public PopResult PopTop (out T obj)
    {
      obj = default (T);
      
      long t = reader.Read (ref top);
      long b = reader.Read (ref bottom);
      
      if (b - t <= 0)
        return PopResult.Empty;
      
      if (Interlocked.CompareExchange (ref top, t + 1, t) != t)
        return PopResult.Abort;
      
      var a = array;
      obj = a[t];
      
      return PopResult.Succeed;
    }
    
    public IEnumerable<T> GetEnumerable ()
    {
      var a = array;
      return a.GetEnumerable ();
    }
  }
  
  internal class CircularArray<T>
  {
    readonly int baseSize;
    readonly int size;
    readonly T[] segment;
    
    public CircularArray (int baseSize)
    {
      this.baseSize = baseSize;
      this.size = 1 << baseSize;
      this.segment = new T[size];
    }
    
    public long Size {
      get {
        return size;
      }
    }
    
    public T this[long index] {
      get {
        return segment[index % Size];
      }
      set {
        segment[index % Size] = value;
      }
    }
    
    public CircularArray<T> Grow (long bottom, long top)
    {
      var grow = new CircularArray<T> (baseSize + 1);
      
      for (long i = top; i < bottom; i++) {
        grow[i] = this[i];
      }
      
      return grow;
    }
    
    public IEnumerable<T> GetEnumerable ()
    {
      return ((IEnumerable<T>)segment);
    }
  }
}
#endif
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.