CacheItemPriorityQueue.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // Author(s):
  3. // Marek Habersack <[email protected]>
  4. //
  5. // (C) 2009-2010 Novell, Inc (http://novell.com)
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Text;
  32. using System.Threading;
  33. using System.Xml;
  34. namespace System.Web.Caching
  35. {
  36. sealed partial class CacheItemPriorityQueue
  37. {
  38. const int INITIAL_HEAP_SIZE = 32;
  39. const int HEAP_RESIZE_THRESHOLD = 8192;
  40. CacheItem[] heap;
  41. int heapSize = 0;
  42. int heapCount = 0;
  43. // See comment for the cacheLock field at top of System.Web.Caching/Cache.cs
  44. ReaderWriterLockSlim queueLock;
  45. public int Count {
  46. get { return heapCount; }
  47. }
  48. public int Size {
  49. get { return heapSize; }
  50. }
  51. public CacheItemPriorityQueue ()
  52. {
  53. queueLock = new ReaderWriterLockSlim ();
  54. InitDebugMode ();
  55. }
  56. void ResizeHeap (int newSize)
  57. {
  58. //CacheItem[] oldHeap = heap;
  59. Array.Resize <CacheItem> (ref heap, newSize);
  60. // TODO: The code helps the GC in case the array is pinned. In such instance clearing
  61. // the old array will release references to the CacheItems stored in there. If the
  62. // array is not pinned, otoh, this is a waste of time.
  63. // Currently we don't know if the array is pinned or not so it's safer to always clear it.
  64. // However when we have more precise stack scanning the code should be
  65. // revisited.
  66. //
  67. // FIXME: code disabled for now as it causes NREX to be thrown in Bubble{Up,Down}
  68. //
  69. // if (oldHeap != null) {
  70. // ((IList)oldHeap).Clear ();
  71. // oldHeap = null;
  72. // }
  73. }
  74. CacheItem[] GetHeapWithGrow ()
  75. {
  76. if (heap == null) {
  77. heap = new CacheItem [INITIAL_HEAP_SIZE];
  78. heapSize = INITIAL_HEAP_SIZE;
  79. heapCount = 0;
  80. return heap;
  81. }
  82. if (heapCount >= heapSize) {
  83. heapSize <<= 1;
  84. ResizeHeap (heapSize);
  85. }
  86. return heap;
  87. }
  88. CacheItem[] GetHeapWithShrink ()
  89. {
  90. if (heap == null)
  91. return null;
  92. if (heapSize > HEAP_RESIZE_THRESHOLD) {
  93. int halfTheSize = heapSize >> 1;
  94. if (heapCount < halfTheSize)
  95. ResizeHeap (halfTheSize + (heapCount / 3));
  96. }
  97. return heap;
  98. }
  99. public void Enqueue (CacheItem item)
  100. {
  101. if (item == null)
  102. return;
  103. CacheItem[] heap;
  104. try {
  105. queueLock.EnterWriteLock ();
  106. heap = GetHeapWithGrow ();
  107. heap [heapCount++] = item;
  108. BubbleUp (heap);
  109. AddSequenceEntry (item, EDSequenceEntryType.Enqueue);
  110. } finally {
  111. // See comment at the top of the file, above queueLock declaration
  112. queueLock.ExitWriteLock ();
  113. }
  114. }
  115. public CacheItem Dequeue ()
  116. {
  117. CacheItem ret = null;
  118. CacheItem[] heap;
  119. int index;
  120. try {
  121. queueLock.EnterWriteLock ();
  122. heap = GetHeapWithShrink ();
  123. if (heap == null || heapCount == 0)
  124. return null;
  125. ret = heap [0];
  126. index = --heapCount;
  127. heap [0] = heap [index];
  128. heap [index] = null;
  129. if (heapCount > 0)
  130. BubbleDown (heap);
  131. AddSequenceEntry (ret, EDSequenceEntryType.Dequeue);
  132. return ret;
  133. } finally {
  134. // See comment at the top of the file, above queueLock declaration
  135. queueLock.ExitWriteLock ();
  136. }
  137. }
  138. public CacheItem Peek ()
  139. {
  140. CacheItem ret;
  141. try {
  142. queueLock.EnterReadLock ();
  143. if (heap == null || heapCount == 0)
  144. return null;
  145. ret = heap [0];
  146. AddSequenceEntry (ret, EDSequenceEntryType.Peek);
  147. return ret;
  148. } finally {
  149. // See comment at the top of the file, above queueLock declaration
  150. queueLock.ExitReadLock ();
  151. }
  152. }
  153. void BubbleDown (CacheItem[] heap)
  154. {
  155. int index = 0;
  156. int left = 1;
  157. int right = 2;
  158. CacheItem item = heap [0];
  159. int selected = (right < heapCount && heap [right].ExpiresAt < heap [left].ExpiresAt) ? 2 : 1;
  160. while (selected < heapCount && heap [selected].ExpiresAt < item.ExpiresAt) {
  161. heap [index] = heap [selected];
  162. index = selected;
  163. left = (index << 1) + 1;
  164. right = left + 1;
  165. selected = right < heapCount && heap [right].ExpiresAt < heap [left].ExpiresAt ? right : left;
  166. }
  167. heap [index] = item;
  168. }
  169. void BubbleUp (CacheItem[] heap)
  170. {
  171. int index, parentIndex;
  172. CacheItem parent, item;
  173. if (heapCount <= 1)
  174. return;
  175. index = heapCount - 1;
  176. parentIndex = (index - 1) >> 1;
  177. item = heap [index];
  178. while (index > 0) {
  179. parent = heap [parentIndex];
  180. if (heap [index].ExpiresAt >= parent.ExpiresAt)
  181. break;
  182. heap [index] = parent;
  183. index = parentIndex;
  184. parentIndex = (index - 1) >> 1;
  185. }
  186. heap [index] = item;
  187. }
  188. }
  189. }