5e3c89e17a94fd82055a04ad9eabeceeb9e44f4a
[mono.git] / mcs / class / corlib / System.Threading / Thread.cs
1 //
2 // System.Threading.Thread.cs
3 //
4 // Author:
5 //   Dick Porter (dick@ximian.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 // Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Runtime.Remoting.Contexts;
31 using System.Runtime.Serialization;
32 using System.Runtime.Serialization.Formatters.Binary;
33 using System.Security.Permissions;
34 using System.Security.Principal;
35 using System.Globalization;
36 using System.Runtime.CompilerServices;
37 using System.Runtime.InteropServices;
38 using System.IO;
39 using System.Collections.Generic;
40 using System.Reflection;
41 using System.Security;
42 using System.Runtime.ConstrainedExecution;
43
44 namespace System.Threading {
45         [StructLayout (LayoutKind.Sequential)]
46         internal class InternalThread : CriticalFinalizerObject {
47 #pragma warning disable 169, 414, 649
48                 #region Sync with metadata/object-internals.h
49                 int lock_thread_id;
50                 // stores a thread handle
51                 internal IntPtr system_thread_handle;
52
53                 /* Note this is an opaque object (an array), not a CultureInfo */
54                 private object cached_culture_info;
55                 /* accessed only from unmanaged code */
56                 private IntPtr name;
57                 private int name_len; 
58                 private ThreadState state;
59                 private object abort_exc;
60                 private int abort_state_handle;
61                 /* thread_id is only accessed from unmanaged code */
62                 internal Int64 thread_id;
63                 
64                 /* start_notify is used by the runtime to signal that Start()
65                  * is ok to return
66                  */
67                 private IntPtr start_notify;
68                 private IntPtr stack_ptr;
69                 private UIntPtr static_data; /* GC-tracked */
70                 private IntPtr jit_data;
71                 private IntPtr runtime_thread_info;
72                 /* current System.Runtime.Remoting.Contexts.Context instance
73                    keep as an object to avoid triggering its class constructor when not needed */
74                 private object current_appcontext;
75                 private object pending_exception;
76                 private object root_domain_thread;
77                 internal byte[] _serialized_principal;
78                 internal int _serialized_principal_version;
79                 private IntPtr appdomain_refs;
80                 private int interruption_requested;
81                 private IntPtr suspend_event;
82                 private IntPtr suspended_event;
83                 private IntPtr resume_event;
84                 private IntPtr synch_cs;
85                 internal bool threadpool_thread;
86                 private bool thread_dump_requested;
87                 private bool thread_interrupt_requested;
88                 private IntPtr end_stack;
89                 /* These are used from managed code */
90                 internal int stack_size;
91                 internal byte apartment_state;
92                 internal volatile int critical_region_level;
93                 internal int managed_id;
94                 private int small_id;
95                 private IntPtr manage_callback;
96                 private IntPtr interrupt_on_stop;
97                 private IntPtr flags;
98                 private IntPtr android_tid;
99                 private IntPtr thread_pinning_ref;
100                 private int ignore_next_signal;
101                 /* 
102                  * These fields are used to avoid having to increment corlib versions
103                  * when a new field is added to the unmanaged MonoThread structure.
104                  */
105                 private IntPtr unused0;
106                 private IntPtr unused1;
107                 private IntPtr unused2;
108                 #endregion
109 #pragma warning restore 169, 414, 649
110
111                 // Closes the system thread handle
112                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
113                 private extern void Thread_free_internal(IntPtr handle);
114
115                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
116                 ~InternalThread() {
117                         Thread_free_internal(system_thread_handle);
118                 }
119         }
120
121         [ClassInterface (ClassInterfaceType.None)]
122         [ComVisible (true)]
123         [ComDefaultInterface (typeof (_Thread))]
124         [StructLayout (LayoutKind.Sequential)]
125 #if MOBILE
126         public sealed class Thread : CriticalFinalizerObject {
127 #else
128         public sealed class Thread : CriticalFinalizerObject, _Thread {
129 #endif
130 #pragma warning disable 414             
131                 #region Sync with metadata/object-internals.h
132                 private InternalThread internal_thread;
133                 object start_obj;
134                 private ExecutionContext ec_to_set;
135                 #endregion
136 #pragma warning restore 414
137
138                 IPrincipal principal;
139                 int principal_version;
140                 CultureInfo current_culture;
141                 CultureInfo current_ui_culture;
142
143                 // the name of local_slots, current_thread and _ec is
144                 // important because they are used by the runtime.
145                 [ThreadStatic]
146                 static object[] local_slots;
147
148                 [ThreadStatic]
149                 static Thread current_thread;
150
151                 /* The actual ExecutionContext of the thread.  It's
152                    ThreadStatic so that it's not shared between
153                    AppDomains. */
154                 [ThreadStatic]
155                 static ExecutionContext _ec;
156
157                 static NamedDataSlot namedDataSlot;             
158
159                 // can be both a ThreadStart and a ParameterizedThreadStart
160                 private MulticastDelegate threadstart;
161                 //private string thread_name=null;
162
163                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
164                 private extern void ConstructInternalThread ();
165
166                 private InternalThread Internal {
167                         get {
168                                 if (internal_thread == null)
169                                         ConstructInternalThread ();
170                                 return internal_thread;
171                         }
172                 }
173
174                 public static Context CurrentContext {
175                         [SecurityPermission (SecurityAction.LinkDemand, Infrastructure=true)]
176                         get {
177                                 return(AppDomain.InternalGetContext ());
178                         }
179                 }
180
181                 /*
182                  * These two methods return an array in the target
183                  * domain with the same content as the argument.  If
184                  * the argument is already in the target domain, then
185                  * the argument is returned, otherwise a copy.
186                  */
187                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
188                 private extern static byte[] ByteArrayToRootDomain (byte[] arr);
189
190                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
191                 private extern static byte[] ByteArrayToCurrentDomain (byte[] arr);
192
193                 static void DeserializePrincipal (Thread th)
194                 {
195                         MemoryStream ms = new MemoryStream (ByteArrayToCurrentDomain (th.Internal._serialized_principal));
196                         int type = ms.ReadByte ();
197                         if (type == 0) {
198                                 BinaryFormatter bf = new BinaryFormatter ();
199                                 th.principal = (IPrincipal) bf.Deserialize (ms);
200                                 th.principal_version = th.Internal._serialized_principal_version;
201                         } else if (type == 1) {
202                                 BinaryReader reader = new BinaryReader (ms);
203                                 string name = reader.ReadString ();
204                                 string auth_type = reader.ReadString ();
205                                 int n_roles = reader.ReadInt32 ();
206                                 string [] roles = null;
207                                 if (n_roles >= 0) {
208                                         roles = new string [n_roles];
209                                         for (int i = 0; i < n_roles; i++)
210                                                 roles [i] = reader.ReadString ();
211                                 }
212                                 th.principal = new GenericPrincipal (new GenericIdentity (name, auth_type), roles);
213                         } else if (type == 2 || type == 3) {
214                                 string [] roles = type == 2 ? null : new string [0];
215                                 th.principal = new GenericPrincipal (new GenericIdentity ("", ""), roles);
216                         }
217                 }
218
219                 static void SerializePrincipal (Thread th, IPrincipal value)
220                 {
221                         MemoryStream ms = new MemoryStream ();
222                         bool done = false;
223                         if (value.GetType () == typeof (GenericPrincipal)) {
224                                 GenericPrincipal gp = (GenericPrincipal) value;
225                                 if (gp.Identity != null && gp.Identity.GetType () == typeof (GenericIdentity)) {
226                                         GenericIdentity id = (GenericIdentity) gp.Identity;
227                                         if (id.Name == "" && id.AuthenticationType == "") {
228                                                 if (gp.Roles == null) {
229                                                         ms.WriteByte (2);
230                                                         done = true;
231                                                 } else if (gp.Roles.Length == 0) {
232                                                         ms.WriteByte (3);
233                                                         done = true;
234                                                 }
235                                         } else {
236                                                 ms.WriteByte (1);
237                                                 BinaryWriter br = new BinaryWriter (ms);
238                                                 br.Write (gp.Identity.Name);
239                                                 br.Write (gp.Identity.AuthenticationType);
240                                                 string [] roles = gp.Roles;
241                                                 if  (roles == null) {
242                                                         br.Write ((int) (-1));
243                                                 } else {
244                                                         br.Write (roles.Length);
245                                                         foreach (string s in roles) {
246                                                                 br.Write (s);
247                                                         }
248                                                 }
249                                                 br.Flush ();
250                                                 done = true;
251                                         }
252                                 }
253                         }
254                         if (!done) {
255                                 ms.WriteByte (0);
256                                 BinaryFormatter bf = new BinaryFormatter ();
257                                 try {
258                                         bf.Serialize (ms, value);
259                                 } catch {}
260                         }
261                         th.Internal._serialized_principal = ByteArrayToRootDomain (ms.ToArray ());
262                 }
263
264                 public static IPrincipal CurrentPrincipal {
265                         get {
266                                 Thread th = CurrentThread;
267
268                                 if (th.principal_version != th.Internal._serialized_principal_version)
269                                         th.principal = null;
270
271                                 if (th.principal != null)
272                                         return th.principal;
273
274                                 if (th.Internal._serialized_principal != null) {
275                                         try {
276                                                 DeserializePrincipal (th);
277                                                 return th.principal;
278                                         } catch {}
279                                 }
280
281                                 th.principal = GetDomain ().DefaultPrincipal;
282                                 th.principal_version = th.Internal._serialized_principal_version;
283                                 return th.principal;
284                         }
285                         [SecurityPermission (SecurityAction.Demand, ControlPrincipal = true)]
286                         set {
287                                 Thread th = CurrentThread;
288
289                                 if (value != GetDomain ().DefaultPrincipal) {
290                                         ++th.Internal._serialized_principal_version;
291                                         try {
292                                                 SerializePrincipal (th, value);
293                                         } catch (Exception) {
294                                                 th.Internal._serialized_principal = null;
295                                         }
296                                         th.principal_version = th.Internal._serialized_principal_version;
297                                 } else {
298                                         th.Internal._serialized_principal = null;
299                                 }
300
301                                 th.principal = value;
302                         }
303                 }
304
305                 // Looks up the object associated with the current thread
306                 // this is called by the JIT directly, too
307                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
308                 private extern static InternalThread CurrentInternalThread_internal();
309
310                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
311                 internal extern static uint AllocTlsData (Type type);
312
313                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
314                 internal extern static void DestroyTlsData (uint offset);
315
316                 public static Thread CurrentThread {
317                         [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
318                         get {
319                                 if (current_thread == null)
320                                         current_thread = new Thread (CurrentInternalThread_internal ());
321                                 return current_thread;
322                         }
323                 }
324
325                 internal static int CurrentThreadId {
326                         get {
327                                 return (int)(CurrentThread.internal_thread.thread_id);
328                         }
329                 }
330                 
331                 static NamedDataSlot NamedDataSlot {
332                         get {
333                                 if (namedDataSlot == null)
334                                         Interlocked.CompareExchange (ref namedDataSlot, new NamedDataSlot (), null);
335
336                                 return namedDataSlot;
337                         }
338                 }
339                 
340                 public static LocalDataStoreSlot AllocateNamedDataSlot (string name)
341                 {
342                         return NamedDataSlot.Allocate (name);
343                 }
344
345                 public static void FreeNamedDataSlot (string name)
346                 {
347                         NamedDataSlot.Free (name);
348                 }
349
350                 public static LocalDataStoreSlot AllocateDataSlot ()
351                 {
352                         return new LocalDataStoreSlot (true);
353                 }
354
355                 public static object GetData (LocalDataStoreSlot slot) {
356                         object[] slots = local_slots;
357                         if (slot == null)
358                                 throw new ArgumentNullException ("slot");
359                         if (slots != null && slot.slot < slots.Length)
360                                 return slots [slot.slot];
361                         return null;
362                 }
363
364                 public static void SetData (LocalDataStoreSlot slot, object data) {
365                         object[] slots = local_slots;
366                         if (slot == null)
367                                 throw new ArgumentNullException ("slot");
368                         if (slots == null) {
369                                 slots = new object [slot.slot + 2];
370                                 local_slots = slots;
371                         } else if (slot.slot >= slots.Length) {
372                                 object[] nslots = new object [slot.slot + 2];
373                                 slots.CopyTo (nslots, 0);
374                                 slots = nslots;
375                                 local_slots = slots;
376                         }
377                         slots [slot.slot] = data;
378                 }
379
380                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
381                 internal extern static void FreeLocalSlotValues (int slot, bool thread_local);
382
383                 public static LocalDataStoreSlot GetNamedDataSlot(string name)
384                 {
385                         return NamedDataSlot.Get (name);
386                 }
387                 
388                 public static AppDomain GetDomain() {
389                         return AppDomain.CurrentDomain;
390                 }
391
392                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
393                 public extern static int GetDomainID();
394
395                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
396                 private extern static void ResetAbort_internal();
397
398                 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
399                 public static void ResetAbort ()
400                 {
401                         ResetAbort_internal ();
402                 }
403
404 #if NET_4_0
405                 [HostProtectionAttribute (SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)]
406                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
407                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
408                 public extern static bool Yield ();
409 #endif
410
411
412                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
413                 private extern static void Sleep_internal(int ms);
414
415                 public static void Sleep (int millisecondsTimeout)
416                 {
417                         if (millisecondsTimeout < Timeout.Infinite)
418                                 throw new ArgumentOutOfRangeException ("millisecondsTimeout", "Negative timeout");
419
420                         Sleep_internal (millisecondsTimeout);
421                 }
422
423                 public static void Sleep (TimeSpan timeout)
424                 {
425                         long ms = (long) timeout.TotalMilliseconds;
426                         if (ms < Timeout.Infinite || ms > Int32.MaxValue)
427                                 throw new ArgumentOutOfRangeException ("timeout", "timeout out of range");
428
429                         Sleep_internal ((int) ms);
430                 }
431
432                 // Returns the system thread handle
433                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
434                 private extern IntPtr Thread_internal (MulticastDelegate start);
435
436                 public Thread(ThreadStart start) {
437                         if(start==null) {
438                                 throw new ArgumentNullException("Null ThreadStart");
439                         }
440                         threadstart=start;
441                 }
442
443                 private Thread (InternalThread it) {
444                         internal_thread = it;
445                 }
446                 
447                 // part of ".NETPortable,Version=v4.0,Profile=Profile3" i.e. FX4 and SL4
448                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
449                 ~Thread ()
450                 {
451                 }
452
453                 [Obsolete ("Deprecated in favor of GetApartmentState, SetApartmentState and TrySetApartmentState.")]
454                 public ApartmentState ApartmentState {
455                         get {
456                                 if ((ThreadState & ThreadState.Stopped) != 0)
457                                         throw new ThreadStateException ("Thread is dead; state can not be accessed.");
458
459                                 return (ApartmentState)Internal.apartment_state;
460                         }
461
462                         set {
463                                 TrySetApartmentState (value);
464                         }
465                 }
466
467                 //[MethodImplAttribute (MethodImplOptions.InternalCall)]
468                 //private static extern int current_lcid ();
469
470                 public CultureInfo CurrentCulture {
471                         get {
472                                 CultureInfo culture = current_culture;
473                                 if (culture != null)
474                                         return culture;
475
476                                 current_culture = culture = CultureInfo.ConstructCurrentCulture ();
477                                 NumberFormatter.SetThreadCurrentCulture (culture);
478                                 return culture;
479                         }
480                         
481                         [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
482                         set {
483                                 if (value == null)
484                                         throw new ArgumentNullException ("value");
485
486                                 value.CheckNeutral ();
487                                 current_culture = value;
488                                 NumberFormatter.SetThreadCurrentCulture (value);
489                         }
490                 }
491
492                 public CultureInfo CurrentUICulture {
493                         get {
494                                 CultureInfo culture = current_ui_culture;
495                                 if (culture != null)
496                                         return culture;
497
498                                 current_ui_culture = culture = CultureInfo.ConstructCurrentUICulture ();
499                                 return culture;
500                         }
501                         
502                         set {
503                                 if (value == null)
504                                         throw new ArgumentNullException ("value");
505                                 current_ui_culture = value;
506                         }
507                 }
508
509                 public bool IsThreadPoolThread {
510                         get {
511                                 return IsThreadPoolThreadInternal;
512                         }
513                 }
514
515                 internal bool IsThreadPoolThreadInternal {
516                         get {
517                                 return Internal.threadpool_thread;
518                         }
519                         set {
520                                 Internal.threadpool_thread = value;
521                         }
522                 }
523
524                 public bool IsAlive {
525                         get {
526                                 ThreadState curstate = GetState (Internal);
527                                 
528                                 if((curstate & ThreadState.Aborted) != 0 ||
529                                    (curstate & ThreadState.Stopped) != 0 ||
530                                    (curstate & ThreadState.Unstarted) != 0) {
531                                         return(false);
532                                 } else {
533                                         return(true);
534                                 }
535                         }
536                 }
537
538                 public bool IsBackground {
539                         get {
540                                 ThreadState thread_state = GetState (Internal);
541                                 if ((thread_state & ThreadState.Stopped) != 0)
542                                         throw new ThreadStateException ("Thread is dead; state can not be accessed.");
543
544                                 return (thread_state & ThreadState.Background) != 0;
545                         }
546                         
547                         set {
548                                 if (value) {
549                                         SetState (Internal, ThreadState.Background);
550                                 } else {
551                                         ClrState (Internal, ThreadState.Background);
552                                 }
553                         }
554                 }
555
556                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
557                 private extern static string GetName_internal (InternalThread thread);
558
559                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
560                 private extern static void SetName_internal (InternalThread thread, String name);
561
562                 /* 
563                  * The thread name must be shared by appdomains, so it is stored in
564                  * unmanaged code.
565                  */
566
567                 public string Name {
568                         get {
569                                 return GetName_internal (Internal);
570                         }
571                         
572                         set {
573                                 SetName_internal (Internal, value);
574                         }
575                 }
576
577                 public ThreadPriority Priority {
578                         get {
579                                 return(ThreadPriority.Lowest);
580                         }
581                         
582                         set {
583                                 // FIXME: Implement setter.
584                         }
585                 }
586
587                 public ThreadState ThreadState {
588                         get {
589                                 return GetState (Internal);
590                         }
591                 }
592
593                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
594                 private extern static void Abort_internal (InternalThread thread, object stateInfo);
595
596                 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
597                 public void Abort () 
598                 {
599                         Abort_internal (Internal, null);
600                 }
601
602                 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
603                 public void Abort (object stateInfo) 
604                 {
605                         Abort_internal (Internal, stateInfo);
606                 }
607
608                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
609                 internal extern object GetAbortExceptionState ();
610
611                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
612                 private extern static void Interrupt_internal (InternalThread thread);
613                 
614                 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
615                 public void Interrupt ()
616                 {
617                         Interrupt_internal (Internal);
618                 }
619
620                 // The current thread joins with 'this'. Set ms to 0 to block
621                 // until this actually exits.
622                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
623                 private extern static bool Join_internal(InternalThread thread, int ms, IntPtr handle);
624                 
625                 public void Join()
626                 {
627                         Join_internal(Internal, Timeout.Infinite, Internal.system_thread_handle);
628                 }
629
630                 public bool Join(int millisecondsTimeout)
631                 {
632                         if (millisecondsTimeout < Timeout.Infinite)
633                                 throw new ArgumentOutOfRangeException ("millisecondsTimeout", "Timeout less than zero");
634
635                         return Join_internal (Internal, millisecondsTimeout, Internal.system_thread_handle);
636                 }
637
638                 public bool Join(TimeSpan timeout)
639                 {
640                         long ms = (long) timeout.TotalMilliseconds;
641                         if (ms < Timeout.Infinite || ms > Int32.MaxValue)
642                                 throw new ArgumentOutOfRangeException ("timeout", "timeout out of range");
643
644                         return Join_internal (Internal, (int) ms, Internal.system_thread_handle);
645                 }
646
647                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
648                 public extern static void MemoryBarrier ();
649
650                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
651                 private extern void Resume_internal();
652
653                 [Obsolete ("")]
654                 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
655                 public void Resume () 
656                 {
657                         Resume_internal ();
658                 }
659
660                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
661                 private extern static void SpinWait_nop ();
662
663
664                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
665                 public static void SpinWait (int iterations) 
666                 {
667                         if (iterations < 0)
668                                 return;
669                         while (iterations-- > 0)
670                         {
671                                 SpinWait_nop ();
672                         }
673                 }
674
675 #if MONOTOUCH
676                 static ConstructorInfo nsautoreleasepool_ctor;
677                 
678                 IDisposable GetNSAutoreleasePool ()
679                 {
680                         if (nsautoreleasepool_ctor == null) {
681                                 Type t = Type.GetType ("MonoTouch.Foundation.NSAutoreleasePool, monotouch, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null");
682                                 nsautoreleasepool_ctor = t.GetConstructor (Type.EmptyTypes);
683                         }
684                         return (IDisposable) nsautoreleasepool_ctor.Invoke (null);
685                 }
686                 
687                 private void StartInternal ()
688                 {
689                         using (var pool = GetNSAutoreleasePool ()) {
690                                 current_thread = this;
691                         
692                                 if (threadstart is ThreadStart) {
693                                         ((ThreadStart) threadstart) ();
694                                 } else {
695                                         ((ParameterizedThreadStart) threadstart) (start_obj);
696                                 }
697                         }
698                 }
699 #else
700                 private void StartInternal ()
701                 {
702                         current_thread = this;
703
704                         if (threadstart is ThreadStart) {
705                                 ((ThreadStart) threadstart) ();
706                         } else {
707                                 ((ParameterizedThreadStart) threadstart) (start_obj);
708                         }
709                 }
710 #endif
711                 public void Start() {
712                         // propagate informations from the original thread to the new thread
713                         if (!ExecutionContext.IsFlowSuppressed ())
714                                 ec_to_set = ExecutionContext.Capture ();
715                         Internal._serialized_principal = CurrentThread.Internal._serialized_principal;
716
717                         // Thread_internal creates and starts the new thread, 
718                         if (Thread_internal((ThreadStart) StartInternal) == (IntPtr) 0)
719                                 throw new SystemException ("Thread creation failed.");
720                 }
721
722                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
723                 private extern static void Suspend_internal(InternalThread thread);
724
725                 [Obsolete ("")]
726                 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
727                 public void Suspend ()
728                 {
729                         Suspend_internal (Internal);
730                 }
731
732                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
733                 extern private static void SetState (InternalThread thread, ThreadState set);
734
735                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
736                 extern private static void ClrState (InternalThread thread, ThreadState clr);
737
738                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
739                 extern private static ThreadState GetState (InternalThread thread);
740
741                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
742                 extern public static byte VolatileRead (ref byte address);
743                 
744                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
745                 extern public static double VolatileRead (ref double address);
746                 
747                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
748                 extern public static short VolatileRead (ref short address);
749                 
750                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
751                 extern public static int VolatileRead (ref int address);
752                 
753                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
754                 extern public static long VolatileRead (ref long address);
755                 
756                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
757                 extern public static IntPtr VolatileRead (ref IntPtr address);
758                 
759                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
760                 extern public static object VolatileRead (ref object address);
761
762                 [CLSCompliant(false)]
763                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
764                 extern public static sbyte VolatileRead (ref sbyte address);
765                 
766                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
767                 extern public static float VolatileRead (ref float address);
768
769                 [CLSCompliant (false)]
770                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
771                 extern public static ushort VolatileRead (ref ushort address);
772
773                 [CLSCompliant (false)]
774                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
775                 extern public static uint VolatileRead (ref uint address);
776
777                 [CLSCompliant (false)]
778                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
779                 extern public static ulong VolatileRead (ref ulong address);
780
781                 [CLSCompliant (false)]
782                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
783                 extern public static UIntPtr VolatileRead (ref UIntPtr address);
784
785                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
786                 extern public static void VolatileWrite (ref byte address, byte value);
787                 
788                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
789                 extern public static void VolatileWrite (ref double address, double value);
790                 
791                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
792                 extern public static void VolatileWrite (ref short address, short value);
793                 
794                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
795                 extern public static void VolatileWrite (ref int address, int value);
796                 
797                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
798                 extern public static void VolatileWrite (ref long address, long value);
799                 
800                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
801                 extern public static void VolatileWrite (ref IntPtr address, IntPtr value);
802                 
803                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
804                 extern public static void VolatileWrite (ref object address, object value);
805
806                 [CLSCompliant(false)]
807                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
808                 extern public static void VolatileWrite (ref sbyte address, sbyte value);
809                 
810                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
811                 extern public static void VolatileWrite (ref float address, float value);
812
813                 [CLSCompliant (false)]
814                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
815                 extern public static void VolatileWrite (ref ushort address, ushort value);
816
817                 [CLSCompliant (false)]
818                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
819                 extern public static void VolatileWrite (ref uint address, uint value);
820
821                 [CLSCompliant (false)]
822                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
823                 extern public static void VolatileWrite (ref ulong address, ulong value);
824
825                 [CLSCompliant (false)]
826                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
827                 extern public static void VolatileWrite (ref UIntPtr address, UIntPtr value);
828                 
829
830                 static int CheckStackSize (int maxStackSize)
831                 {
832                         if (maxStackSize < 0)
833                                 throw new ArgumentOutOfRangeException ("less than zero", "maxStackSize");
834
835                         if (maxStackSize < 131072) // make sure stack is at least 128k big
836                                 return 131072;
837
838                         int page_size = Environment.GetPageSize ();
839
840                         if ((maxStackSize % page_size) != 0) // round up to a divisible of page size
841                                 maxStackSize = (maxStackSize / (page_size - 1)) * page_size;
842
843                         int default_stack_size = (IntPtr.Size / 4) * 1024 * 1024; // from wthreads.c
844
845                         if (maxStackSize > default_stack_size)
846                                 return default_stack_size;
847
848                         return maxStackSize; 
849                 }
850
851                 public Thread (ThreadStart start, int maxStackSize)
852                 {
853                         if (start == null)
854                                 throw new ArgumentNullException ("start");
855
856                         threadstart = start;
857                         Internal.stack_size = CheckStackSize (maxStackSize);;
858                 }
859
860                 public Thread (ParameterizedThreadStart start)
861                 {
862                         if (start == null)
863                                 throw new ArgumentNullException ("start");
864
865                         threadstart = start;
866                 }
867
868                 public Thread (ParameterizedThreadStart start, int maxStackSize)
869                 {
870                         if (start == null)
871                                 throw new ArgumentNullException ("start");
872
873                         threadstart = start;
874                         Internal.stack_size = CheckStackSize (maxStackSize);
875                 }
876
877                 [MonoTODO ("limited to CompressedStack support")]
878                 public ExecutionContext ExecutionContext {
879                         [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
880                         get {
881                                 if (_ec == null)
882                                         _ec = new ExecutionContext ();
883                                 return _ec;
884                         }
885                 }
886
887                 public int ManagedThreadId {
888                         [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
889                         get {
890                                 return Internal.managed_id;
891                         }
892                 }
893
894                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
895                 public static void BeginCriticalRegion ()
896                 {
897                         CurrentThread.Internal.critical_region_level++;
898                 }
899
900                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
901                 public static void EndCriticalRegion ()
902                 {
903                         CurrentThread.Internal.critical_region_level--;
904                 }
905
906                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
907                 public static void BeginThreadAffinity ()
908                 {
909                         // Managed and native threads are currently bound together.
910                 }
911
912                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
913                 public static void EndThreadAffinity ()
914                 {
915                         // Managed and native threads are currently bound together.
916                 }
917
918                 public ApartmentState GetApartmentState ()
919                 {
920                         return (ApartmentState)Internal.apartment_state;
921                 }
922
923                 public void SetApartmentState (ApartmentState state)
924                 {
925                         if (!TrySetApartmentState (state))
926                                 throw new InvalidOperationException ("Failed to set the specified COM apartment state.");
927                 }
928
929                 public bool TrySetApartmentState (ApartmentState state) 
930                 {
931                         /* Only throw this exception when changing the
932                          * state of another thread.  See bug 324338
933                          */
934                         if ((this != CurrentThread) &&
935                             (ThreadState & ThreadState.Unstarted) == 0)
936                                 throw new ThreadStateException ("Thread was in an invalid state for the operation being executed.");
937
938                         if ((ApartmentState)Internal.apartment_state != ApartmentState.Unknown)
939                                 return false;
940
941                         Internal.apartment_state = (byte)state;
942
943                         return true;
944                 }
945                 
946                 [ComVisible (false)]
947                 public override int GetHashCode ()
948                 {
949                         return ManagedThreadId;
950                 }
951
952                 public void Start (object parameter)
953                 {
954                         start_obj = parameter;
955                         Start ();
956                 }
957
958                 // NOTE: This method doesn't show in the class library status page because
959                 // it cannot be "found" with the StrongNameIdentityPermission for ECMA key.
960                 // But it's there!
961                 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
962                 [StrongNameIdentityPermission (SecurityAction.LinkDemand, PublicKey="00000000000000000400000000000000")]
963                 [Obsolete ("see CompressedStack class")]
964                 public CompressedStack GetCompressedStack ()
965                 {
966 #if MOBILE
967                         throw new NotSupportedException ();
968 #else                   
969                         // Note: returns null if no CompressedStack has been set.
970                         // However CompressedStack.GetCompressedStack returns an 
971                         // (empty?) CompressedStack instance.
972                         CompressedStack cs = ExecutionContext.SecurityContext.CompressedStack;
973                         return ((cs == null) || cs.IsEmpty ()) ? null : cs.CreateCopy ();
974 #endif
975                 }
976
977                 // NOTE: This method doesn't show in the class library status page because
978                 // it cannot be "found" with the StrongNameIdentityPermission for ECMA key.
979                 // But it's there!
980                 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
981                 [StrongNameIdentityPermission (SecurityAction.LinkDemand, PublicKey="00000000000000000400000000000000")]
982                 [Obsolete ("see CompressedStack class")]
983                 public void SetCompressedStack (CompressedStack stack)
984                 {
985 #if MOBILE
986                         throw new NotSupportedException ();
987 #else
988                         ExecutionContext.SecurityContext.CompressedStack = stack;
989 #endif
990                 }
991
992 #if !MOBILE
993                 void _Thread.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
994                 {
995                         throw new NotImplementedException ();
996                 }
997
998                 void _Thread.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
999                 {
1000                         throw new NotImplementedException ();
1001                 }
1002
1003                 void _Thread.GetTypeInfoCount (out uint pcTInfo)
1004                 {
1005                         throw new NotImplementedException ();
1006                 }
1007
1008                 void _Thread.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
1009                         IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1010                 {
1011                         throw new NotImplementedException ();
1012                 }
1013 #endif
1014         }
1015 }