aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/ThreadTracker.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Framework/ThreadTracker.cs')
-rw-r--r--OpenSim/Framework/ThreadTracker.cs106
1 files changed, 106 insertions, 0 deletions
diff --git a/OpenSim/Framework/ThreadTracker.cs b/OpenSim/Framework/ThreadTracker.cs
new file mode 100644
index 0000000..42e3879
--- /dev/null
+++ b/OpenSim/Framework/ThreadTracker.cs
@@ -0,0 +1,106 @@
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Text;
5using System.Threading;
6
7namespace OpenSim.Framework
8{
9 public static class ThreadTracker
10 {
11 public static List<ThreadTrackerItem> m_Threads;
12 public static System.Threading.Thread ThreadTrackerThread;
13 private static readonly long ThreadTimeout = 30 * 10000000;
14
15 static ThreadTracker()
16 {
17#if DEBUG
18 m_Threads = new List<ThreadTrackerItem>();
19 ThreadTrackerThread = new Thread(ThreadTrackerThreadLoop);
20 ThreadTrackerThread.Name = "ThreadTrackerThread";
21 ThreadTrackerThread.IsBackground = true;
22 ThreadTrackerThread.Priority = System.Threading.ThreadPriority.BelowNormal;
23 ThreadTrackerThread.Start();
24#endif
25 }
26
27 private static void ThreadTrackerThreadLoop()
28 {
29 while (true)
30 {
31 Thread.Sleep(5000);
32 CleanUp();
33 }
34 }
35
36 public static void Add(System.Threading.Thread thread)
37 {
38#if DEBUG
39 lock (m_Threads)
40 {
41 ThreadTrackerItem tti = new ThreadTrackerItem();
42 tti.Thread = thread;
43 tti.LastSeenActive = DateTime.Now.Ticks;
44 m_Threads.Add(tti);
45 }
46#endif
47 }
48
49 public static void Remove(System.Threading.Thread thread)
50 {
51#if DEBUG
52 lock (m_Threads)
53 {
54 foreach (ThreadTrackerItem tti in new ArrayList(m_Threads))
55 {
56 if (tti.Thread == thread)
57 m_Threads.Remove(tti);
58 }
59 }
60#endif
61 }
62
63 public static void CleanUp()
64 {
65 lock (m_Threads)
66 {
67 foreach (ThreadTrackerItem tti in new ArrayList(m_Threads))
68 {
69 if (tti.Thread.IsAlive)
70 {
71 // Its active
72 tti.LastSeenActive = DateTime.Now.Ticks;
73 }
74 else
75 {
76 // Its not active -- if its expired then remove it
77 if (tti.LastSeenActive + ThreadTimeout < DateTime.Now.Ticks)
78 m_Threads.Remove(tti);
79 }
80 }
81 }
82 }
83
84 public static List<Thread> GetThreads()
85 {
86 if (m_Threads == null)
87 return null;
88
89 List<Thread> threads = new List<Thread>();
90 lock (m_Threads)
91 {
92 foreach (ThreadTrackerItem tti in new ArrayList(m_Threads))
93 {
94 threads.Add(tti.Thread);
95 }
96 }
97 return threads;
98 }
99
100 public class ThreadTrackerItem
101 {
102 public System.Threading.Thread Thread;
103 public long LastSeenActive;
104 }
105 }
106}