ThreadTracker.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Text;
  31. using System.Threading;
  32. namespace OpenSim.Framework
  33. {
  34. public static class ThreadTracker
  35. {
  36. public static List<ThreadTrackerItem> m_Threads;
  37. public static System.Threading.Thread ThreadTrackerThread;
  38. private static readonly long ThreadTimeout = 30 * 10000000;
  39. static ThreadTracker()
  40. {
  41. #if DEBUG
  42. m_Threads = new List<ThreadTrackerItem>();
  43. ThreadTrackerThread = new Thread(ThreadTrackerThreadLoop);
  44. ThreadTrackerThread.Name = "ThreadTrackerThread";
  45. ThreadTrackerThread.IsBackground = true;
  46. ThreadTrackerThread.Priority = System.Threading.ThreadPriority.BelowNormal;
  47. ThreadTrackerThread.Start();
  48. #endif
  49. }
  50. private static void ThreadTrackerThreadLoop()
  51. {
  52. while (true)
  53. {
  54. Thread.Sleep(5000);
  55. CleanUp();
  56. }
  57. }
  58. public static void Add(System.Threading.Thread thread)
  59. {
  60. #if DEBUG
  61. lock (m_Threads)
  62. {
  63. ThreadTrackerItem tti = new ThreadTrackerItem();
  64. tti.Thread = thread;
  65. tti.LastSeenActive = DateTime.Now.Ticks;
  66. m_Threads.Add(tti);
  67. }
  68. #endif
  69. }
  70. public static void Remove(System.Threading.Thread thread)
  71. {
  72. #if DEBUG
  73. lock (m_Threads)
  74. {
  75. foreach (ThreadTrackerItem tti in new ArrayList(m_Threads))
  76. {
  77. if (tti.Thread == thread)
  78. m_Threads.Remove(tti);
  79. }
  80. }
  81. #endif
  82. }
  83. public static void CleanUp()
  84. {
  85. lock (m_Threads)
  86. {
  87. foreach (ThreadTrackerItem tti in new ArrayList(m_Threads))
  88. {
  89. if (tti.Thread.IsAlive)
  90. {
  91. // Its active
  92. tti.LastSeenActive = DateTime.Now.Ticks;
  93. }
  94. else
  95. {
  96. // Its not active -- if its expired then remove it
  97. if (tti.LastSeenActive + ThreadTimeout < DateTime.Now.Ticks)
  98. m_Threads.Remove(tti);
  99. }
  100. }
  101. }
  102. }
  103. public static List<Thread> GetThreads()
  104. {
  105. if (m_Threads == null)
  106. return null;
  107. List<Thread> threads = new List<Thread>();
  108. lock (m_Threads)
  109. {
  110. foreach (ThreadTrackerItem tti in new ArrayList(m_Threads))
  111. {
  112. threads.Add(tti.Thread);
  113. }
  114. }
  115. return threads;
  116. }
  117. public class ThreadTrackerItem
  118. {
  119. public System.Threading.Thread Thread;
  120. public long LastSeenActive;
  121. }
  122. }
  123. }