ThreadStaticPool.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. namespace ARMeilleure.Common
  6. {
  7. class ThreadStaticPool<T> where T : class, new()
  8. {
  9. private const int PoolSizeIncrement = 200;
  10. [ThreadStatic]
  11. private static ThreadStaticPool<T> _instance;
  12. public static ThreadStaticPool<T> Instance
  13. {
  14. get
  15. {
  16. if (_instance == null)
  17. {
  18. PreparePool(0); // So that we can still use a pool when blindly initializing one.
  19. }
  20. return _instance;
  21. }
  22. }
  23. private static ConcurrentDictionary<int, Stack<ThreadStaticPool<T>>> _pools = new ConcurrentDictionary<int, Stack<ThreadStaticPool<T>>>();
  24. private static Stack<ThreadStaticPool<T>> GetPools(int groupId)
  25. {
  26. return _pools.GetOrAdd(groupId, x => new Stack<ThreadStaticPool<T>>());
  27. }
  28. public static void PreparePool(int groupId)
  29. {
  30. // Prepare the pool for this thread, ideally using an existing one from the specified group.
  31. if (_instance == null)
  32. {
  33. var pools = GetPools(groupId);
  34. lock (pools)
  35. {
  36. _instance = (pools.Count != 0) ? pools.Pop() : new ThreadStaticPool<T>(PoolSizeIncrement * 2);
  37. }
  38. }
  39. }
  40. public static void ReturnPool(int groupId)
  41. {
  42. // Reset and return the pool for this thread to the specified group.
  43. var pools = GetPools(groupId);
  44. lock (pools)
  45. {
  46. _instance.Clear();
  47. pools.Push(_instance);
  48. _instance = null;
  49. }
  50. }
  51. public static void ResetPools()
  52. {
  53. // Resets any static references to the pools used by threads for each group, allowing them to be garbage collected.
  54. foreach (var pools in _pools.Values)
  55. {
  56. pools.Clear();
  57. }
  58. _pools.Clear();
  59. }
  60. private T[] _pool;
  61. private int _poolUsed = -1;
  62. private int _poolSize;
  63. public ThreadStaticPool(int initialSize)
  64. {
  65. _pool = new T[initialSize];
  66. for (int i = 0; i < initialSize; i++)
  67. {
  68. _pool[i] = new T();
  69. }
  70. _poolSize = initialSize;
  71. }
  72. public T Allocate()
  73. {
  74. int index = Interlocked.Increment(ref _poolUsed);
  75. if (index >= _poolSize)
  76. {
  77. IncreaseSize();
  78. }
  79. return _pool[index];
  80. }
  81. private void IncreaseSize()
  82. {
  83. _poolSize += PoolSizeIncrement;
  84. T[] newArray = new T[_poolSize];
  85. Array.Copy(_pool, 0, newArray, 0, _pool.Length);
  86. for (int i = _pool.Length; i < _poolSize; i++)
  87. {
  88. newArray[i] = new T();
  89. }
  90. Interlocked.Exchange(ref _pool, newArray);
  91. }
  92. public void Clear()
  93. {
  94. _poolUsed = -1;
  95. }
  96. }
  97. }