ThreadStaticPool.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. namespace ARMeilleure.Common
  6. {
  7. internal 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. Stack<ThreadStaticPool<T>> 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. Stack<ThreadStaticPool<T>> pools = GetPools(groupId);
  44. lock (pools)
  45. {
  46. _instance.Clear();
  47. pools.Push(_instance);
  48. _instance = null;
  49. }
  50. }
  51. private T[] _pool;
  52. private int _poolUsed = -1;
  53. private int _poolSize;
  54. public ThreadStaticPool(int initialSize)
  55. {
  56. _pool = new T[initialSize];
  57. for (int i = 0; i < initialSize; i++)
  58. {
  59. _pool[i] = new T();
  60. }
  61. _poolSize = initialSize;
  62. }
  63. public T Allocate()
  64. {
  65. int index = Interlocked.Increment(ref _poolUsed);
  66. if (index >= _poolSize)
  67. {
  68. IncreaseSize();
  69. }
  70. return _pool[index];
  71. }
  72. private void IncreaseSize()
  73. {
  74. _poolSize += PoolSizeIncrement;
  75. T[] newArray = new T[_poolSize];
  76. Array.Copy(_pool, 0, newArray, 0, _pool.Length);
  77. for (int i = _pool.Length; i < _poolSize; i++)
  78. {
  79. newArray[i] = new T();
  80. }
  81. Interlocked.Exchange(ref _pool, newArray);
  82. }
  83. public void Clear()
  84. {
  85. _poolUsed = -1;
  86. }
  87. }
  88. }