PageRenderTime 27ms CodeModel.GetById 22ms app.highlight 4ms RepoModel.GetById 0ms app.codeStats 0ms

/Aurora/Framework/Utilities/Net4Classes/ObjectPool.cs

https://bitbucket.org/VirtualReality/software-testing
C# | 101 lines | 60 code | 15 blank | 26 comment | 18 complexity | d22547e193d3998b1e5f4a7d755ab170 MD5 | raw file
  1// ObjectPool.cs
  2//
  3// Copyright (c) 2011 Novell
  4//
  5// Authors: 
  6//      J�r�mie "garuma" Laval
  7//
  8// Permission is hereby granted, free of charge, to any person obtaining a copy
  9// of this software and associated documentation files (the "Software"), to deal
 10// in the Software without restriction, including without limitation the rights
 11// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12// copies of the Software, and to permit persons to whom the Software is
 13// furnished to do so, subject to the following conditions:
 14//
 15// The above copyright notice and this permission notice shall be included in
 16// all copies or substantial portions of the Software.
 17//
 18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 24// THE SOFTWARE.
 25//
 26//
 27
 28
 29#if NET_3_5
 30
 31using System;
 32using System.Threading;
 33using System.Collections;
 34using System.Collections.Generic;
 35using System.Runtime.Serialization;
 36
 37namespace System.Collections.Concurrent
 38{
 39    internal abstract class ObjectPool<T> where T : class
 40    {
 41        const int capacity = 20;
 42        const int bit = 0x8000000;
 43
 44        readonly T[] buffer;
 45        int addIndex;
 46        int removeIndex;
 47
 48        public ObjectPool()
 49        {
 50            buffer = new T[capacity];
 51            for (int i = 0; i < capacity; i++)
 52                buffer[i] = Creator();
 53            addIndex = capacity - 1;
 54        }
 55
 56        protected abstract T Creator();
 57
 58        public T Take()
 59        {
 60            if ((addIndex & ~bit) - 1 == removeIndex)
 61                return Creator();
 62
 63            int i;
 64            T result;
 65            int tries = 3;
 66
 67            do
 68            {
 69                i = removeIndex;
 70                if ((addIndex & ~bit) - 1 == i || tries == 0)
 71                    return Creator();
 72                result = buffer[i % capacity];
 73            } while (Interlocked.CompareExchange(ref removeIndex, i + 1, i) != i && --tries > -1);
 74
 75            return result;
 76        }
 77
 78        public void Release(T obj)
 79        {
 80            if (obj == null || addIndex - removeIndex >= capacity - 1)
 81                return;
 82
 83            int i;
 84            int tries = 3;
 85            do
 86            {
 87                do
 88                {
 89                    i = addIndex;
 90                } while ((i & bit) > 0);
 91                if (i - removeIndex >= capacity - 1)
 92                    return;
 93            } while (Interlocked.CompareExchange(ref addIndex, i + 1 + bit, i) != i && --tries > 0);
 94
 95            buffer[i % capacity] = obj;
 96            addIndex = addIndex - bit;
 97        }
 98    }
 99}
100
101#endif