diff options
Diffstat (limited to 'OpenSim/Framework/ThreadTracker.cs')
-rw-r--r-- | OpenSim/Framework/ThreadTracker.cs | 127 |
1 files changed, 5 insertions, 122 deletions
diff --git a/OpenSim/Framework/ThreadTracker.cs b/OpenSim/Framework/ThreadTracker.cs index d3a239d..b68d9b3 100644 --- a/OpenSim/Framework/ThreadTracker.cs +++ b/OpenSim/Framework/ThreadTracker.cs | |||
@@ -26,138 +26,21 @@ | |||
26 | */ | 26 | */ |
27 | 27 | ||
28 | using System; | 28 | using System; |
29 | using System.Collections; | ||
30 | using System.Collections.Generic; | 29 | using System.Collections.Generic; |
31 | using System.Reflection; | 30 | using System.Reflection; |
32 | using System.Threading; | 31 | using System.Diagnostics; |
33 | using log4net; | 32 | using log4net; |
34 | 33 | ||
35 | namespace OpenSim.Framework | 34 | namespace OpenSim.Framework |
36 | { | 35 | { |
37 | public static class ThreadTracker | 36 | public static class ThreadTracker |
38 | { | 37 | { |
39 | private static readonly ILog m_log | 38 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
40 | = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
41 | |||
42 | private static readonly long ThreadTimeout = 30 * 10000000; | ||
43 | public static List<ThreadTrackerItem> m_Threads; | ||
44 | public static Thread ThreadTrackerThread; | ||
45 | 39 | ||
46 | static ThreadTracker() | 40 | public static ProcessThreadCollection GetThreads() |
47 | { | 41 | { |
48 | #if DEBUG | 42 | Process thisProc = Process.GetCurrentProcess(); |
49 | m_Threads = new List<ThreadTrackerItem>(); | 43 | return thisProc.Threads; |
50 | ThreadTrackerThread = new Thread(ThreadTrackerThreadLoop); | ||
51 | ThreadTrackerThread.Name = "ThreadTrackerThread"; | ||
52 | ThreadTrackerThread.IsBackground = true; | ||
53 | ThreadTrackerThread.Priority = ThreadPriority.BelowNormal; | ||
54 | ThreadTrackerThread.Start(); | ||
55 | Add(ThreadTrackerThread); | ||
56 | #endif | ||
57 | } | 44 | } |
58 | |||
59 | private static void ThreadTrackerThreadLoop() | ||
60 | { | ||
61 | try | ||
62 | { | ||
63 | while (true) | ||
64 | { | ||
65 | Thread.Sleep(5000); | ||
66 | CleanUp(); | ||
67 | } | ||
68 | } | ||
69 | catch (Exception e) | ||
70 | { | ||
71 | m_log.ErrorFormat( | ||
72 | "[THREAD TRACKER]: Thread tracker cleanup thread terminating with exception. Please report this error. Exception is {0}", | ||
73 | e); | ||
74 | } | ||
75 | } | ||
76 | |||
77 | public static void Add(Thread thread) | ||
78 | { | ||
79 | #if DEBUG | ||
80 | if (thread != null) | ||
81 | { | ||
82 | lock (m_Threads) | ||
83 | { | ||
84 | ThreadTrackerItem tti = new ThreadTrackerItem(); | ||
85 | tti.Thread = thread; | ||
86 | tti.LastSeenActive = DateTime.Now.Ticks; | ||
87 | m_Threads.Add(tti); | ||
88 | } | ||
89 | } | ||
90 | #endif | ||
91 | } | ||
92 | |||
93 | public static void Remove(Thread thread) | ||
94 | { | ||
95 | #if DEBUG | ||
96 | lock (m_Threads) | ||
97 | { | ||
98 | foreach (ThreadTrackerItem tti in new ArrayList(m_Threads)) | ||
99 | { | ||
100 | if (tti.Thread == thread) | ||
101 | m_Threads.Remove(tti); | ||
102 | } | ||
103 | } | ||
104 | #endif | ||
105 | } | ||
106 | |||
107 | public static void CleanUp() | ||
108 | { | ||
109 | lock (m_Threads) | ||
110 | { | ||
111 | foreach (ThreadTrackerItem tti in new ArrayList(m_Threads)) | ||
112 | { | ||
113 | try | ||
114 | { | ||
115 | |||
116 | |||
117 | if (tti.Thread.IsAlive) | ||
118 | { | ||
119 | // Its active | ||
120 | tti.LastSeenActive = DateTime.Now.Ticks; | ||
121 | } | ||
122 | else | ||
123 | { | ||
124 | // Its not active -- if its expired then remove it | ||
125 | if (tti.LastSeenActive + ThreadTimeout < DateTime.Now.Ticks) | ||
126 | m_Threads.Remove(tti); | ||
127 | } | ||
128 | } | ||
129 | catch (NullReferenceException) | ||
130 | { | ||
131 | m_Threads.Remove(tti); | ||
132 | } | ||
133 | } | ||
134 | } | ||
135 | } | ||
136 | |||
137 | public static List<Thread> GetThreads() | ||
138 | { | ||
139 | if (m_Threads == null) | ||
140 | return null; | ||
141 | |||
142 | List<Thread> threads = new List<Thread>(); | ||
143 | lock (m_Threads) | ||
144 | { | ||
145 | foreach (ThreadTrackerItem tti in new ArrayList(m_Threads)) | ||
146 | { | ||
147 | threads.Add(tti.Thread); | ||
148 | } | ||
149 | } | ||
150 | return threads; | ||
151 | } | ||
152 | |||
153 | #region Nested type: ThreadTrackerItem | ||
154 | |||
155 | public class ThreadTrackerItem | ||
156 | { | ||
157 | public long LastSeenActive; | ||
158 | public Thread Thread; | ||
159 | } | ||
160 | |||
161 | #endregion | ||
162 | } | 45 | } |
163 | } | 46 | } |