ThreadInt64PersistentCounter.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Diagnostics;
  5. using System.Runtime.CompilerServices;
  6. namespace System.Threading
  7. {
  8. internal sealed class ThreadInt64PersistentCounter
  9. {
  10. // This type is used by Monitor for lock contention counting, so can't use an object for a lock. Also it's preferable
  11. // (though currently not required) to disallow/ignore thread interrupt for uses of this lock here. Using Lock directly
  12. // is a possibility but maybe less compatible with other runtimes. Lock cases are relatively rare, static instance
  13. // should be ok.
  14. private static readonly LowLevelLock s_lock = new LowLevelLock();
  15. private readonly ThreadLocal<ThreadLocalNode> _threadLocalNode = new ThreadLocal<ThreadLocalNode>(trackAllValues: true);
  16. private long _overflowCount;
  17. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  18. public void Increment()
  19. {
  20. ThreadLocalNode node = _threadLocalNode.Value;
  21. if (node != null)
  22. {
  23. node.Increment();
  24. return;
  25. }
  26. TryCreateNode();
  27. }
  28. [MethodImpl(MethodImplOptions.NoInlining)]
  29. private void TryCreateNode()
  30. {
  31. Debug.Assert(_threadLocalNode.Value == null);
  32. try
  33. {
  34. _threadLocalNode.Value = new ThreadLocalNode(this);
  35. }
  36. catch (OutOfMemoryException)
  37. {
  38. }
  39. }
  40. public long Count
  41. {
  42. get
  43. {
  44. long count = 0;
  45. try
  46. {
  47. s_lock.Acquire();
  48. try
  49. {
  50. count = _overflowCount;
  51. foreach (ThreadLocalNode node in _threadLocalNode.ValuesAsEnumerable)
  52. {
  53. if (node != null)
  54. {
  55. count += node.Count;
  56. }
  57. }
  58. return count;
  59. }
  60. finally
  61. {
  62. s_lock.Release();
  63. }
  64. }
  65. catch (OutOfMemoryException)
  66. {
  67. // Some allocation occurs above and it may be a bit awkward to get an OOM from this property getter
  68. return count;
  69. }
  70. }
  71. }
  72. private sealed class ThreadLocalNode
  73. {
  74. private uint _count;
  75. private readonly ThreadInt64PersistentCounter _counter;
  76. public ThreadLocalNode(ThreadInt64PersistentCounter counter)
  77. {
  78. Debug.Assert(counter != null);
  79. _count = 1;
  80. _counter = counter;
  81. }
  82. ~ThreadLocalNode()
  83. {
  84. ThreadInt64PersistentCounter counter = _counter;
  85. s_lock.Acquire();
  86. try
  87. {
  88. counter._overflowCount += _count;
  89. }
  90. finally
  91. {
  92. s_lock.Release();
  93. }
  94. }
  95. public uint Count => _count;
  96. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  97. public void Increment()
  98. {
  99. uint newCount = _count + 1;
  100. if (newCount != 0)
  101. {
  102. _count = newCount;
  103. return;
  104. }
  105. OnIncrementOverflow();
  106. }
  107. [MethodImpl(MethodImplOptions.NoInlining)]
  108. private void OnIncrementOverflow()
  109. {
  110. // Accumulate the count for this increment into the overflow count and reset the thread-local count
  111. // The lock, in coordination with other places that read these values, ensures that both changes below become
  112. // visible together
  113. ThreadInt64PersistentCounter counter = _counter;
  114. s_lock.Acquire();
  115. try
  116. {
  117. _count = 0;
  118. counter._overflowCount += (long)uint.MaxValue + 1;
  119. }
  120. finally
  121. {
  122. s_lock.Release();
  123. }
  124. }
  125. }
  126. }
  127. }