2 // System.Threading.Thread.cs
5 // Dick Porter (dick@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
10 using System.Runtime.Remoting.Contexts;
11 using System.Security.Principal;
12 using System.Globalization;
13 using System.Runtime.CompilerServices;
15 namespace System.Threading
17 public sealed class Thread
19 public static Context CurrentContext {
22 // System.Runtime.Remoting.Context not
28 public static IPrincipal CurrentPrincipal {
31 // System.Security.Principal.IPrincipal
32 // not yet implemented
40 // Looks up the object associated with the current thread
41 [MethodImplAttribute(MethodImplOptions.InternalCall)]
42 private extern static Thread CurrentThread_internal();
44 public static Thread CurrentThread {
46 return(CurrentThread_internal());
50 public static LocalDataStoreSlot AllocateDataSlot() {
55 public static LocalDataStoreSlot AllocateNamedDataSlot(string name) {
60 public static void FreeNamedDataSlot(string name) {
64 public static object GetData(LocalDataStoreSlot slot) {
69 public static AppDomain GetDomain() {
74 public static int GetDomainID() {
79 public static LocalDataStoreSlot GetNamedDataSlot(string name) {
84 public static void ResetAbort() {
88 public static void SetData(LocalDataStoreSlot slot, object data) {
92 // Returns milliseconds remaining (due to interrupted sleep)
93 [MethodImplAttribute(MethodImplOptions.InternalCall)]
94 private extern static int Sleep_internal(int ms);
96 // Causes thread to give up its timeslice
97 [MethodImplAttribute(MethodImplOptions.InternalCall)]
98 private extern static void Schedule_internal();
100 public static void Sleep(int millisecondsTimeout) {
101 if(millisecondsTimeout<0) {
102 throw new ArgumentException("Negative timeout");
104 if(millisecondsTimeout==0) {
105 // Schedule another thread
108 int ms_remaining=Sleep_internal(millisecondsTimeout);
110 throw new ThreadInterruptedException("Thread interrupted while sleeping");
114 public static void Sleep(TimeSpan timeout) {
115 // LAMESPEC: says to throw ArgumentException too
116 if(timeout.Milliseconds < 0 || timeout.Milliseconds > Int32.MaxValue) {
117 throw new ArgumentOutOfRangeException("Timeout out of range");
119 if(timeout.Milliseconds==0) {
120 // Schedule another thread
123 int ms_remaining=Sleep_internal(timeout.Milliseconds);
125 throw new ThreadInterruptedException("Thread interrupted while sleeping");
129 private ThreadStart start_delegate=null;
131 public Thread(ThreadStart start) {
133 throw new ArgumentNullException("Null ThreadStart");
136 // Nothing actually happens here, the fun
137 // begins when Thread.Start() is called. For
138 // now, just record what the ThreadStart
140 start_delegate=start;
143 public ApartmentState ApartmentState {
146 return(ApartmentState.Unknown);
153 public CultureInfo CurrentCulture {
163 public CultureInfo CurrentUICulture {
173 public bool IsAlive {
180 public bool IsBackground {
190 private string thread_name=null;
202 public ThreadPriority Priority {
205 return(ThreadPriority.Lowest);
212 public ThreadState ThreadState {
215 return(ThreadState.Unstarted);
219 public void Abort() {
223 public void Abort(object stateInfo) {
227 public void Interrupt() {
235 public bool Join(int millisecondsTimeout) {
236 if(millisecondsTimeout<0) {
237 throw new ArgumentException("Timeout less than zero");
243 public bool Join(TimeSpan timeout) {
244 // LAMESPEC: says to throw ArgumentException too
245 if(timeout.Milliseconds < 0 || timeout.Milliseconds > Int32.MaxValue) {
246 throw new ArgumentOutOfRangeException("timeout out of range");
252 public void Resume() {
256 // stores a pthread_t, which is defined as unsigned long
257 // on my system. I _think_ windows uses "unsigned int" for
258 // its thread handles, so that _should_ work too.
259 private UInt32 system_thread_handle;
261 // Returns the system thread handle
262 [MethodImplAttribute(MethodImplOptions.InternalCall)]
263 private extern UInt32 Start_internal(ThreadStart start);
265 public void Start() {
266 system_thread_handle=Start_internal(start_delegate);
270 public void Suspend() {