~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/Sharpen/Sharpen/Thread.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
namespace Sharpen
 
2
{
 
3
        using System;
 
4
        using System.Threading;
 
5
        using System.Collections.Generic;
 
6
 
 
7
        internal class Thread : Runnable
 
8
        {
 
9
                private static ThreadGroup defaultGroup = new ThreadGroup ();
 
10
                private bool interrupted;
 
11
                private Runnable runnable;
 
12
                private ThreadGroup tgroup;
 
13
                private System.Threading.Thread thread;
 
14
                
 
15
                [ThreadStatic]
 
16
                private static Sharpen.Thread wrapperThread;
 
17
 
 
18
                public Thread () : this(null, null, null)
 
19
                {
 
20
                }
 
21
 
 
22
                public Thread (string name) : this (null, null, name)
 
23
                {
 
24
                }
 
25
 
 
26
                public Thread (ThreadGroup grp, string name) : this (null, grp, name)
 
27
                {
 
28
                }
 
29
 
 
30
                public Thread (Runnable runnable): this (runnable, null, null)
 
31
                {
 
32
                }
 
33
                
 
34
                Thread (Runnable runnable, ThreadGroup grp, string name)
 
35
                {
 
36
                        thread = new System.Threading.Thread (new ThreadStart (InternalRun));
 
37
                        
 
38
                        this.runnable = runnable ?? this;
 
39
                        tgroup = grp ?? defaultGroup;
 
40
                        tgroup.Add (this);
 
41
                        if (name != null)
 
42
                                thread.Name = name;
 
43
                }
 
44
 
 
45
                private Thread (System.Threading.Thread t)
 
46
                {
 
47
                        thread = t;
 
48
                        tgroup = defaultGroup;
 
49
                        tgroup.Add (this);
 
50
                }
 
51
 
 
52
                public static Sharpen.Thread CurrentThread ()
 
53
                {
 
54
                        if (wrapperThread == null) {
 
55
                                wrapperThread = new Sharpen.Thread (System.Threading.Thread.CurrentThread);
 
56
                        }
 
57
                        return wrapperThread;
 
58
                }
 
59
 
 
60
                public string GetName ()
 
61
                {
 
62
                        return thread.Name;
 
63
                }
 
64
 
 
65
                public ThreadGroup GetThreadGroup ()
 
66
                {
 
67
                        return tgroup;
 
68
                }
 
69
 
 
70
                private void InternalRun ()
 
71
                {
 
72
                        wrapperThread = this;
 
73
                        try {
 
74
                                runnable.Run ();
 
75
                        } catch (Exception exception) {
 
76
                                Console.WriteLine (exception);
 
77
                        } finally {
 
78
                                tgroup.Remove (this);
 
79
                        }
 
80
                }
 
81
                
 
82
                public static void Yield ()
 
83
                {
 
84
                }
 
85
 
 
86
                public void Interrupt ()
 
87
                {
 
88
                        lock (thread) {
 
89
                                interrupted = true;
 
90
                                thread.Interrupt ();
 
91
                        }
 
92
                }
 
93
 
 
94
                public static bool Interrupted ()
 
95
                {
 
96
                        if (Sharpen.Thread.wrapperThread == null) {
 
97
                                return false;
 
98
                        }
 
99
                        Sharpen.Thread wrapperThread = Sharpen.Thread.wrapperThread;
 
100
                        lock (wrapperThread) {
 
101
                                bool interrupted = Sharpen.Thread.wrapperThread.interrupted;
 
102
                                Sharpen.Thread.wrapperThread.interrupted = false;
 
103
                                return interrupted;
 
104
                        }
 
105
                }
 
106
 
 
107
                public bool IsAlive ()
 
108
                {
 
109
                        return thread.IsAlive;
 
110
                }
 
111
 
 
112
                public void Join ()
 
113
                {
 
114
                        thread.Join ();
 
115
                }
 
116
 
 
117
                public void Join (long timeout)
 
118
                {
 
119
                        thread.Join ((int)timeout);
 
120
                }
 
121
 
 
122
                public virtual void Run ()
 
123
                {
 
124
                }
 
125
 
 
126
                public void SetDaemon (bool daemon)
 
127
                {
 
128
                        thread.IsBackground = daemon;
 
129
                }
 
130
 
 
131
                public void SetName (string name)
 
132
                {
 
133
                        thread.Name = name;
 
134
                }
 
135
 
 
136
                public static void Sleep (long milis)
 
137
                {
 
138
                        System.Threading.Thread.Sleep ((int)milis);
 
139
                }
 
140
 
 
141
                public void Start ()
 
142
                {
 
143
                        thread.Start ();
 
144
                }
 
145
                
 
146
                public void Abort ()
 
147
                {
 
148
                        thread.Abort ();
 
149
                }
 
150
                
 
151
        }
 
152
 
 
153
        internal class ThreadGroup
 
154
        {
 
155
                private List<Thread> threads = new List<Thread> ();
 
156
                
 
157
                public ThreadGroup()
 
158
                {
 
159
                }
 
160
                
 
161
                public ThreadGroup (string name)
 
162
                {
 
163
                }
 
164
 
 
165
                internal void Add (Thread t)
 
166
                {
 
167
                        lock (threads) {
 
168
                                threads.Add (t);
 
169
                        }
 
170
                }
 
171
                
 
172
                internal void Remove (Thread t)
 
173
                {
 
174
                        lock (threads) {
 
175
                                threads.Remove (t);
 
176
                        }
 
177
                }
 
178
 
 
179
                public int Enumerate (Thread[] array)
 
180
                {
 
181
                        lock (threads) {
 
182
                                int count = Math.Min (array.Length, threads.Count);
 
183
                                threads.CopyTo (0, array, 0, count);
 
184
                                return count;
 
185
                        }
 
186
                }
 
187
        }
 
188
}