6ef1658424360144254485b42943efcb76d0fb52
[mono.git] / mcs / class / corlib / System.Collections.Concurrent / ObjectPool.cs
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 #if NET_4_0 || MOBILE || BOOTSTRAP_NET_4_0 || INSIDE_SYSTEM_WEB
29
30 using System;
31 using System.Threading;
32 using System.Collections;
33 using System.Collections.Generic;
34 using System.Runtime.Serialization;
35
36 namespace System.Collections.Concurrent
37 {
38         internal abstract class ObjectPool<T> where T : class
39         {
40                 // This is the number of objects we are going to cache
41                 const int capacity = 20;
42                 /* We use this bit in addIndex to synchronize the array and the index itself
43                  * Namely when we update addIndex we also set that bit for the time the value
44                  * in the array hasn't still been updated to the object we are returning to the cache
45                  */
46                 const int bit = 0x8000000;
47
48                 readonly T[] buffer;
49                 int addIndex;
50                 int removeIndex;
51
52                 public ObjectPool ()
53                 {
54                         buffer = new T[capacity];
55                         for (int i = 0; i < capacity; i++)
56                                 buffer[i] = Creator ();
57                         addIndex = capacity - 1;
58                 }
59
60                 /* Code that want to use a pool subclass it and
61                  * implement that method. In most case 'new T ()'.
62                  */
63                 protected abstract T Creator ();
64
65                 public T Take ()
66                 {
67                         // If no element in the cache, we return a new object
68                         if ((addIndex & ~bit) - 1 == removeIndex)
69                                 return Creator ();
70
71                         int i;
72                         T result;
73                         int tries = 3;
74
75                         do {
76                                 i = removeIndex;
77                                 // We return a new element when looping becomes too costly
78                                 if ((addIndex & ~bit) - 1 == i || tries == 0)
79                                         return Creator ();
80                                 result = buffer[i % capacity];
81                         } while (Interlocked.CompareExchange (ref removeIndex, i + 1, i) != i && --tries > -1);
82
83                         return result;
84                 }
85
86                 public void Release (T obj)
87                 {
88                         if (obj == null || addIndex - removeIndex >= capacity - 1)
89                                 return;
90
91                         int i;
92                         int tries = 3;
93                         do {
94                                 // While an array update is ongoing (i.e. an extra write op) we loop
95                                 do {
96                                         i = addIndex;
97                                 } while ((i & bit) > 0);
98                                 // If no more room just forget about the object altogether
99                                 if (i - removeIndex >= capacity - 1)
100                                         return;
101                                 // We update addIndex and notify that we are going to set buffer correctly
102                         } while (Interlocked.CompareExchange (ref addIndex, i + 1 + bit, i) != i && --tries > 0);
103
104                         buffer[i % capacity] = obj;
105                         Thread.MemoryBarrier ();
106                         // Since bit essentialy acts like a lock, we simply use an atomic read/write combo
107                         addIndex = addIndex - bit;
108                 }
109         }
110 }
111
112 #endif