* Filters/FilterPackage.cs, Filters/FilterRPM.cs,
[beagle.git] / Util / ExceptionHandlingThread.cs
blobcc482d34516e31485b55fafb90115c154ac47bd8
1 //
2 // ExceptionHandlingThread.cs
3 //
4 // Copyright (C) 2005 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
27 using System;
28 using System.Collections;
29 using System.Runtime.InteropServices;
30 using System.Threading;
32 namespace Beagle.Util {
34 public class ExceptionHandlingThread {
36 private static ArrayList live_threads = new ArrayList ();
37 private Thread thread;
38 private ThreadStart method;
40 private ExceptionHandlingThread (ThreadStart method)
42 if (method == null)
43 throw new ArgumentNullException ("method");
45 this.method = method;
48 private void ThreadStarted ()
50 this.thread.Name = String.Format ("EHT {0:00000} {1}:{2}", wrap_gettid (), method.Target == null ? "(static)" : method.Target.ToString (), method.Method);
52 try {
53 this.method ();
54 } catch (ThreadAbortException e) {
55 Logger.Log.Debug ("{0}:\n{1}\n", this.thread.Name, e.StackTrace);
56 } catch (Exception e) {
57 Logger.Log.Warn ("Exception caught while executing {0}:{1}",
58 this.method.Target, this.method.Method);
59 Logger.Log.Warn (e);
62 lock (live_threads)
63 live_threads.Remove (this.thread);
66 public static Thread Start (ThreadStart method)
68 ExceptionHandlingThread eht = new ExceptionHandlingThread (method);
70 eht.thread = new Thread (new ThreadStart (eht.ThreadStarted));
72 lock (live_threads)
73 live_threads.Add (eht.thread);
75 eht.thread.Start ();
77 return eht.thread;
80 public static void SpewLiveThreads ()
82 bool have_live_thread = false;
84 lock (live_threads) {
85 foreach (Thread t in live_threads) {
86 Logger.Log.Debug ("Live ExceptionHandlingThread: {0}", t.Name);
87 have_live_thread = true;
91 if (! have_live_thread)
92 Logger.Log.Debug ("No live ExceptionHandlingThreads!");
95 public static void AbortThreads ()
97 ArrayList cancel_threads = null;
99 // Copy the list to avoid recursively locking
100 lock (live_threads)
101 cancel_threads = (ArrayList) live_threads.Clone ();
103 foreach (Thread t in cancel_threads) {
104 Logger.Log.Debug ("Aborting thread: {0}", t.Name);
105 t.Abort ();
109 [DllImport ("libbeagleglue")]
110 static extern uint wrap_gettid ();