/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. #if NET_3_5
  28. using System;
  29. using System.Threading;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using System.Runtime.Serialization;
  33. namespace System.Collections.Concurrent
  34. {
  35. internal abstract class ObjectPool<T> where T : class
  36. {
  37. const int capacity = 20;
  38. const int bit = 0x8000000;
  39. readonly T[] buffer;
  40. int addIndex;
  41. int removeIndex;
  42. public ObjectPool()
  43. {
  44. buffer = new T[capacity];
  45. for (int i = 0; i < capacity; i++)
  46. buffer[i] = Creator();
  47. addIndex = capacity - 1;
  48. }
  49. protected abstract T Creator();
  50. public T Take()
  51. {
  52. if ((addIndex & ~bit) - 1 == removeIndex)
  53. return Creator();
  54. int i;
  55. T result;
  56. int tries = 3;
  57. do
  58. {
  59. i = removeIndex;
  60. if ((addIndex & ~bit) - 1 == i || tries == 0)
  61. return Creator();
  62. result = buffer[i % capacity];
  63. } while (Interlocked.CompareExchange(ref removeIndex, i + 1, i) != i && --tries > -1);
  64. return result;
  65. }
  66. public void Release(T obj)
  67. {
  68. if (obj == null || addIndex - removeIndex >= capacity - 1)
  69. return;
  70. int i;
  71. int tries = 3;
  72. do
  73. {
  74. do
  75. {
  76. i = addIndex;
  77. } while ((i & bit) > 0);
  78. if (i - removeIndex >= capacity - 1)
  79. return;
  80. } while (Interlocked.CompareExchange(ref addIndex, i + 1 + bit, i) != i && --tries > 0);
  81. buffer[i % capacity] = obj;
  82. addIndex = addIndex - bit;
  83. }
  84. }
  85. }
  86. #endif