From b2ed348aa2746fbf034b713d006e40366c479d5a Mon Sep 17 00:00:00 2001 From: John Hurliman Date: Thu, 22 Oct 2009 12:33:23 -0700 Subject: Implemented a Watchdog class. Do not manually create Thread objects anymore, use Watchdog.StartThread(). While your thread is running call Watchdog.UpdateThread(). When it is shutting down call Watchdog.RemoveThread(). Most of the threads in OpenSim have been updated --- .../Framework/Servers/HttpServer/BaseHttpServer.cs | 8 - .../HttpServer/PollServiceRequestManager.cs | 6 +- OpenSim/Framework/Watchdog.cs | 183 +++++++++++++++++++++ 3 files changed, 185 insertions(+), 12 deletions(-) create mode 100644 OpenSim/Framework/Watchdog.cs (limited to 'OpenSim/Framework') diff --git a/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs b/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs index 85d7be2..bec5ed3 100644 --- a/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs +++ b/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs @@ -1559,15 +1559,7 @@ namespace OpenSim.Framework.Servers.HttpServer public void Start() { m_log.Info("[HTTPD]: Starting up HTTP Server"); - - //m_workerThread = new Thread(new ThreadStart(StartHTTP)); - //m_workerThread.Name = "HttpThread"; - //m_workerThread.IsBackground = true; - //m_workerThread.Start(); - //ThreadTracker.Add(m_workerThread); StartHTTP(); - - } private void StartHTTP() diff --git a/OpenSim/Framework/Servers/HttpServer/PollServiceRequestManager.cs b/OpenSim/Framework/Servers/HttpServer/PollServiceRequestManager.cs index 1c54581..e7a64f7 100644 --- a/OpenSim/Framework/Servers/HttpServer/PollServiceRequestManager.cs +++ b/OpenSim/Framework/Servers/HttpServer/PollServiceRequestManager.cs @@ -50,9 +50,7 @@ namespace OpenSim.Framework.Servers.HttpServer m_WorkerThreadCount = pWorkerThreadCount; m_workerThreads = new Thread[m_WorkerThreadCount]; m_PollServiceWorkerThreads = new PollServiceWorkerThread[m_WorkerThreadCount]; - m_watcherThread = new Thread(ThreadStart); - //startup worker threads for (uint i=0;i + /// Manages launching threads and keeping watch over them for timeouts + /// + public static class Watchdog + { + /// Timer interval in milliseconds for the watchdog timer + const double WATCHDOG_INTERVAL_MS = 2500.0d; + /// Maximum timeout in milliseconds before a thread is considered dead + const int WATCHDOG_TIMEOUT_MS = 5000; + + [System.Diagnostics.DebuggerDisplay("{Thread.Name}")] + private class ThreadWatchdogInfo + { + public Thread Thread; + public int LastTick; + + public ThreadWatchdogInfo(Thread thread) + { + Thread = thread; + LastTick = Environment.TickCount & Int32.MaxValue; + } + } + + /// + /// This event is called whenever a tracked thread is stopped or + /// has not called UpdateThread() in time + /// + /// The thread that has been identified as dead + /// The last time this thread called UpdateThread() + public delegate void WatchdogTimeout(Thread thread, int lastTick); + + /// This event is called whenever a tracked thread is + /// stopped or has not called UpdateThread() in time + public static event WatchdogTimeout OnWatchdogTimeout; + + private static Dictionary m_threads; + private static System.Timers.Timer m_watchdogTimer; + + static Watchdog() + { + m_threads = new Dictionary(); + m_watchdogTimer = new System.Timers.Timer(WATCHDOG_INTERVAL_MS); + m_watchdogTimer.AutoReset = false; + m_watchdogTimer.Elapsed += WatchdogTimerElapsed; + m_watchdogTimer.Start(); + } + + /// + /// Start a new thread that is tracked by the watchdog timer + /// + /// The method that will be executed in a new thread + /// A name to give to the new thread + /// Priority to run the thread at + /// True to run this thread as a background + /// thread, otherwise false + /// The newly created Thread object + public static Thread StartThread(ThreadStart start, string name, ThreadPriority priority, bool isBackground) + { + Thread thread = new Thread(start); + thread.Name = name; + thread.Priority = priority; + thread.IsBackground = isBackground; + thread.Start(); + + lock (m_threads) + m_threads.Add(thread.ManagedThreadId, new ThreadWatchdogInfo(thread)); + + return thread; + } + + /// + /// Marks the current thread as alive + /// + public static void UpdateThread() + { + UpdateThread(Thread.CurrentThread.ManagedThreadId); + } + + /// + /// Marks a thread as alive + /// + /// The ManagedThreadId of the thread to mark as + /// alive + public static void UpdateThread(int threadID) + { + ThreadWatchdogInfo threadInfo; + + lock (m_threads) + { + if (m_threads.TryGetValue(threadID, out threadInfo)) + { + threadInfo.LastTick = Environment.TickCount & Int32.MaxValue; + } + } + } + + /// + /// Stops watchdog tracking on the current thread + /// + /// True if the thread was removed from the list of tracked + /// threads, otherwise false + public static bool RemoveThread() + { + return RemoveThread(Thread.CurrentThread.ManagedThreadId); + } + + /// + /// Stops watchdog tracking on a thread + /// + /// The ManagedThreadId of the thread to stop + /// tracking + /// True if the thread was removed from the list of tracked + /// threads, otherwise false + public static bool RemoveThread(int threadID) + { + lock (m_threads) + return m_threads.Remove(threadID); + } + + private static void WatchdogTimerElapsed(object sender, System.Timers.ElapsedEventArgs e) + { + WatchdogTimeout callback = OnWatchdogTimeout; + + if (callback != null) + { + ThreadWatchdogInfo timedOut = null; + + lock (m_threads) + { + int now = Environment.TickCount; + + foreach (ThreadWatchdogInfo threadInfo in m_threads.Values) + { + if (threadInfo.Thread.ThreadState == ThreadState.Stopped || now - threadInfo.LastTick >= WATCHDOG_TIMEOUT_MS) + { + timedOut = threadInfo; + m_threads.Remove(threadInfo.Thread.ManagedThreadId); + break; + } + } + } + + if (timedOut != null) + callback(timedOut.Thread, timedOut.LastTick); + } + + m_watchdogTimer.Start(); + } + } +} -- cgit v1.1