Remove debug statements from KCal backends.
[beagle.git] / Util / ExceptionHandlingThread.cs
blobbd5f6b269c2bbdc8545cb093634597da3b1fa255
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 (e, "Exception caught while executing {0}:{1}",
58 this.method.Target, this.method.Method);
61 lock (live_threads)
62 live_threads.Remove (this.thread);
65 public static Thread Start (ThreadStart method)
67 ExceptionHandlingThread eht = new ExceptionHandlingThread (method);
69 eht.thread = new Thread (new ThreadStart (eht.ThreadStarted));
71 lock (live_threads)
72 live_threads.Add (eht.thread);
74 eht.thread.Start ();
76 return eht.thread;
79 public static void SpewLiveThreads ()
81 bool have_live_thread = false;
83 lock (live_threads) {
84 foreach (Thread t in live_threads) {
85 Logger.Log.Debug ("Live ExceptionHandlingThread: {0}", t.Name);
86 have_live_thread = true;
90 if (! have_live_thread)
91 Logger.Log.Debug ("No live ExceptionHandlingThreads!");
94 public static void AbortThreads ()
96 ArrayList cancel_threads = null;
98 // Copy the list to avoid recursively locking
99 lock (live_threads)
100 cancel_threads = (ArrayList) live_threads.Clone ();
102 foreach (Thread t in cancel_threads) {
103 Logger.Log.Debug ("Aborting thread: {0}", t.Name);
104 t.Abort ();
108 [DllImport ("libbeagleglue")]
109 static extern uint wrap_gettid ();