StatePool.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Runtime.CompilerServices;
  4. namespace Cysharp.Threading.Tasks.Internal
  5. {
  6. internal static class StateTuple
  7. {
  8. public static StateTuple<T1> Create<T1>(T1 item1)
  9. {
  10. return StatePool<T1>.Create(item1);
  11. }
  12. public static StateTuple<T1, T2> Create<T1, T2>(T1 item1, T2 item2)
  13. {
  14. return StatePool<T1, T2>.Create(item1, item2);
  15. }
  16. public static StateTuple<T1, T2, T3> Create<T1, T2, T3>(T1 item1, T2 item2, T3 item3)
  17. {
  18. return StatePool<T1, T2, T3>.Create(item1, item2, item3);
  19. }
  20. }
  21. internal class StateTuple<T1> : IDisposable
  22. {
  23. public T1 Item1;
  24. public void Deconstruct(out T1 item1)
  25. {
  26. item1 = this.Item1;
  27. }
  28. public void Dispose()
  29. {
  30. StatePool<T1>.Return(this);
  31. }
  32. }
  33. internal static class StatePool<T1>
  34. {
  35. static readonly ConcurrentQueue<StateTuple<T1>> queue = new ConcurrentQueue<StateTuple<T1>>();
  36. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  37. public static StateTuple<T1> Create(T1 item1)
  38. {
  39. if (queue.TryDequeue(out var value))
  40. {
  41. value.Item1 = item1;
  42. return value;
  43. }
  44. return new StateTuple<T1> { Item1 = item1 };
  45. }
  46. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  47. public static void Return(StateTuple<T1> tuple)
  48. {
  49. tuple.Item1 = default;
  50. queue.Enqueue(tuple);
  51. }
  52. }
  53. internal class StateTuple<T1, T2> : IDisposable
  54. {
  55. public T1 Item1;
  56. public T2 Item2;
  57. public void Deconstruct(out T1 item1, out T2 item2)
  58. {
  59. item1 = this.Item1;
  60. item2 = this.Item2;
  61. }
  62. public void Dispose()
  63. {
  64. StatePool<T1, T2>.Return(this);
  65. }
  66. }
  67. internal static class StatePool<T1, T2>
  68. {
  69. static readonly ConcurrentQueue<StateTuple<T1, T2>> queue = new ConcurrentQueue<StateTuple<T1, T2>>();
  70. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  71. public static StateTuple<T1, T2> Create(T1 item1, T2 item2)
  72. {
  73. if (queue.TryDequeue(out var value))
  74. {
  75. value.Item1 = item1;
  76. value.Item2 = item2;
  77. return value;
  78. }
  79. return new StateTuple<T1, T2> { Item1 = item1, Item2 = item2 };
  80. }
  81. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  82. public static void Return(StateTuple<T1, T2> tuple)
  83. {
  84. tuple.Item1 = default;
  85. tuple.Item2 = default;
  86. queue.Enqueue(tuple);
  87. }
  88. }
  89. internal class StateTuple<T1, T2, T3> : IDisposable
  90. {
  91. public T1 Item1;
  92. public T2 Item2;
  93. public T3 Item3;
  94. public void Deconstruct(out T1 item1, out T2 item2, out T3 item3)
  95. {
  96. item1 = this.Item1;
  97. item2 = this.Item2;
  98. item3 = this.Item3;
  99. }
  100. public void Dispose()
  101. {
  102. StatePool<T1, T2, T3>.Return(this);
  103. }
  104. }
  105. internal static class StatePool<T1, T2, T3>
  106. {
  107. static readonly ConcurrentQueue<StateTuple<T1, T2, T3>> queue = new ConcurrentQueue<StateTuple<T1, T2, T3>>();
  108. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  109. public static StateTuple<T1, T2, T3> Create(T1 item1, T2 item2, T3 item3)
  110. {
  111. if (queue.TryDequeue(out var value))
  112. {
  113. value.Item1 = item1;
  114. value.Item2 = item2;
  115. value.Item3 = item3;
  116. return value;
  117. }
  118. return new StateTuple<T1, T2, T3> { Item1 = item1, Item2 = item2, Item3 = item3 };
  119. }
  120. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  121. public static void Return(StateTuple<T1, T2, T3> tuple)
  122. {
  123. tuple.Item1 = default;
  124. tuple.Item2 = default;
  125. tuple.Item3 = default;
  126. queue.Enqueue(tuple);
  127. }
  128. }
  129. }