[corlib] Fixed StringBuilder construction bugs in marshalling caused by changes to...
[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         sealed 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                 bool current_culture_set;
141                 bool current_ui_culture_set;
142                 CultureInfo current_culture;
143                 CultureInfo current_ui_culture;
144
145                 // the name of local_slots, current_thread and _ec is
146                 // important because they are used by the runtime.
147                 [ThreadStatic]
148                 static object[] local_slots;
149
150                 [ThreadStatic]
151                 static Thread current_thread;
152
153                 /* The actual ExecutionContext of the thread.  It's
154                    ThreadStatic so that it's not shared between
155                    AppDomains. */
156                 [ThreadStatic]
157                 static ExecutionContext _ec;
158
159                 static NamedDataSlot namedDataSlot;             
160
161                 static internal CultureInfo default_culture;
162                 static internal CultureInfo default_ui_culture;
163
164                 // can be both a ThreadStart and a ParameterizedThreadStart
165                 private MulticastDelegate threadstart;
166                 //private string thread_name=null;
167
168                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
169                 private extern void ConstructInternalThread ();
170
171                 private InternalThread Internal {
172                         get {
173                                 if (internal_thread == null)
174                                         ConstructInternalThread ();
175                                 return internal_thread;
176                         }
177                 }
178
179                 public static Context CurrentContext {
180                         [SecurityPermission (SecurityAction.LinkDemand, Infrastructure=true)]
181                         get {
182                                 return(AppDomain.InternalGetContext ());
183                         }
184                 }
185
186                 /*
187                  * These two methods return an array in the target
188                  * domain with the same content as the argument.  If
189                  * the argument is already in the target domain, then
190                  * the argument is returned, otherwise a copy.
191                  */
192                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
193                 private extern static byte[] ByteArrayToRootDomain (byte[] arr);
194
195                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
196                 private extern static byte[] ByteArrayToCurrentDomain (byte[] arr);
197
198                 static void DeserializePrincipal (Thread th)
199                 {
200                         MemoryStream ms = new MemoryStream (ByteArrayToCurrentDomain (th.Internal._serialized_principal));
201                         int type = ms.ReadByte ();
202                         if (type == 0) {
203                                 BinaryFormatter bf = new BinaryFormatter ();
204                                 th.principal = (IPrincipal) bf.Deserialize (ms);
205                                 th.principal_version = th.Internal._serialized_principal_version;
206                         } else if (type == 1) {
207                                 BinaryReader reader = new BinaryReader (ms);
208                                 string name = reader.ReadString ();
209                                 string auth_type = reader.ReadString ();
210                                 int n_roles = reader.ReadInt32 ();
211                                 string [] roles = null;
212                                 if (n_roles >= 0) {
213                                         roles = new string [n_roles];
214                                         for (int i = 0; i < n_roles; i++)
215                                                 roles [i] = reader.ReadString ();
216                                 }
217                                 th.principal = new GenericPrincipal (new GenericIdentity (name, auth_type), roles);
218                         } else if (type == 2 || type == 3) {
219                                 string [] roles = type == 2 ? null : new string [0];
220                                 th.principal = new GenericPrincipal (new GenericIdentity ("", ""), roles);
221                         }
222                 }
223
224                 static void SerializePrincipal (Thread th, IPrincipal value)
225                 {
226                         MemoryStream ms = new MemoryStream ();
227                         bool done = false;
228                         if (value.GetType () == typeof (GenericPrincipal)) {
229                                 GenericPrincipal gp = (GenericPrincipal) value;
230                                 if (gp.Identity != null && gp.Identity.GetType () == typeof (GenericIdentity)) {
231                                         GenericIdentity id = (GenericIdentity) gp.Identity;
232                                         if (id.Name == "" && id.AuthenticationType == "") {
233                                                 if (gp.Roles == null) {
234                                                         ms.WriteByte (2);
235                                                         done = true;
236                                                 } else if (gp.Roles.Length == 0) {
237                                                         ms.WriteByte (3);
238                                                         done = true;
239                                                 }
240                                         } else {
241                                                 ms.WriteByte (1);
242                                                 BinaryWriter br = new BinaryWriter (ms);
243                                                 br.Write (gp.Identity.Name);
244                                                 br.Write (gp.Identity.AuthenticationType);
245                                                 string [] roles = gp.Roles;
246                                                 if  (roles == null) {
247                                                         br.Write ((int) (-1));
248                                                 } else {
249                                                         br.Write (roles.Length);
250                                                         foreach (string s in roles) {
251                                                                 br.Write (s);
252                                                         }
253                                                 }
254                                                 br.Flush ();
255                                                 done = true;
256                                         }
257                                 }
258                         }
259                         if (!done) {
260                                 ms.WriteByte (0);
261                                 BinaryFormatter bf = new BinaryFormatter ();
262                                 try {
263                                         bf.Serialize (ms, value);
264                                 } catch {}
265                         }
266                         th.Internal._serialized_principal = ByteArrayToRootDomain (ms.ToArray ());
267                 }
268
269                 public static IPrincipal CurrentPrincipal {
270                         get {
271                                 Thread th = CurrentThread;
272
273                                 if (th.principal_version != th.Internal._serialized_principal_version)
274                                         th.principal = null;
275
276                                 if (th.principal != null)
277                                         return th.principal;
278
279                                 if (th.Internal._serialized_principal != null) {
280                                         try {
281                                                 DeserializePrincipal (th);
282                                                 return th.principal;
283                                         } catch {}
284                                 }
285
286                                 th.principal = GetDomain ().DefaultPrincipal;
287                                 th.principal_version = th.Internal._serialized_principal_version;
288                                 return th.principal;
289                         }
290                         [SecurityPermission (SecurityAction.Demand, ControlPrincipal = true)]
291                         set {
292                                 Thread th = CurrentThread;
293
294                                 if (value != GetDomain ().DefaultPrincipal) {
295                                         ++th.Internal._serialized_principal_version;
296                                         try {
297                                                 SerializePrincipal (th, value);
298                                         } catch (Exception) {
299                                                 th.Internal._serialized_principal = null;
300                                         }
301                                         th.principal_version = th.Internal._serialized_principal_version;
302                                 } else {
303                                         th.Internal._serialized_principal = null;
304                                 }
305
306                                 th.principal = value;
307                         }
308                 }
309
310                 // Looks up the object associated with the current thread
311                 // this is called by the JIT directly, too
312                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
313                 private extern static InternalThread CurrentInternalThread_internal();
314
315                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
316                 internal extern static uint AllocTlsData (Type type);
317
318                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
319                 internal extern static void DestroyTlsData (uint offset);
320
321                 public static Thread CurrentThread {
322                         [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
323                         get {
324                                 if (current_thread == null)
325                                         current_thread = new Thread (CurrentInternalThread_internal ());
326                                 return current_thread;
327                         }
328                 }
329
330                 internal static int CurrentThreadId {
331                         get {
332                                 return (int)(CurrentThread.internal_thread.thread_id);
333                         }
334                 }
335                 
336                 static NamedDataSlot NamedDataSlot {
337                         get {
338                                 if (namedDataSlot == null)
339                                         Interlocked.CompareExchange (ref namedDataSlot, new NamedDataSlot (), null);
340
341                                 return namedDataSlot;
342                         }
343                 }
344                 
345                 public static LocalDataStoreSlot AllocateNamedDataSlot (string name)
346                 {
347                         return NamedDataSlot.Allocate (name);
348                 }
349
350                 public static void FreeNamedDataSlot (string name)
351                 {
352                         NamedDataSlot.Free (name);
353                 }
354
355                 public static LocalDataStoreSlot AllocateDataSlot ()
356                 {
357                         return new LocalDataStoreSlot (true);
358                 }
359
360                 public static object GetData (LocalDataStoreSlot slot) {
361                         object[] slots = local_slots;
362                         if (slot == null)
363                                 throw new ArgumentNullException ("slot");
364                         if (slots != null && slot.slot < slots.Length)
365                                 return slots [slot.slot];
366                         return null;
367                 }
368
369                 public static void SetData (LocalDataStoreSlot slot, object data) {
370                         object[] slots = local_slots;
371                         if (slot == null)
372                                 throw new ArgumentNullException ("slot");
373                         if (slots == null) {
374                                 slots = new object [slot.slot + 2];
375                                 local_slots = slots;
376                         } else if (slot.slot >= slots.Length) {
377                                 object[] nslots = new object [slot.slot + 2];
378                                 slots.CopyTo (nslots, 0);
379                                 slots = nslots;
380                                 local_slots = slots;
381                         }
382                         slots [slot.slot] = data;
383                 }
384
385                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
386                 internal extern static void FreeLocalSlotValues (int slot, bool thread_local);
387
388                 public static LocalDataStoreSlot GetNamedDataSlot(string name)
389                 {
390                         return NamedDataSlot.Get (name);
391                 }
392                 
393                 public static AppDomain GetDomain() {
394                         return AppDomain.CurrentDomain;
395                 }
396
397                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
398                 public extern static int GetDomainID();
399
400                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
401                 private extern static void ResetAbort_internal();
402
403                 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
404                 public static void ResetAbort ()
405                 {
406                         ResetAbort_internal ();
407                 }
408
409                 [HostProtectionAttribute (SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)]
410                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
411                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
412                 public extern static bool Yield ();
413
414
415                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
416                 private extern static void Sleep_internal(int ms);
417
418                 public static void Sleep (int millisecondsTimeout)
419                 {
420                         if (millisecondsTimeout < Timeout.Infinite)
421                                 throw new ArgumentOutOfRangeException ("millisecondsTimeout", "Negative timeout");
422
423                         Sleep_internal (millisecondsTimeout);
424                 }
425
426                 public static void Sleep (TimeSpan timeout)
427                 {
428                         long ms = (long) timeout.TotalMilliseconds;
429                         if (ms < Timeout.Infinite || ms > Int32.MaxValue)
430                                 throw new ArgumentOutOfRangeException ("timeout", "timeout out of range");
431
432                         Sleep_internal ((int) ms);
433                 }
434
435                 // Returns the system thread handle
436                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
437                 private extern IntPtr Thread_internal (MulticastDelegate start);
438
439                 public Thread(ThreadStart start) {
440                         if(start==null) {
441                                 throw new ArgumentNullException("Null ThreadStart");
442                         }
443                         threadstart=start;
444                 }
445
446                 private Thread (InternalThread it) {
447                         internal_thread = it;
448                 }
449                 
450                 // part of ".NETPortable,Version=v4.0,Profile=Profile3" i.e. FX4 and SL4
451                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
452                 ~Thread ()
453                 {
454                 }
455
456                 [Obsolete ("Deprecated in favor of GetApartmentState, SetApartmentState and TrySetApartmentState.")]
457                 public ApartmentState ApartmentState {
458                         get {
459                                 if ((ThreadState & ThreadState.Stopped) != 0)
460                                         throw new ThreadStateException ("Thread is dead; state can not be accessed.");
461
462                                 return (ApartmentState)Internal.apartment_state;
463                         }
464
465                         set {
466                                 TrySetApartmentState (value);
467                         }
468                 }
469
470                 //[MethodImplAttribute (MethodImplOptions.InternalCall)]
471                 //private static extern int current_lcid ();
472
473                 public CultureInfo CurrentCulture {
474                         get {
475                                 CultureInfo culture = current_culture;
476                                 if (current_culture_set && culture != null)
477                                         return culture;
478
479                                 if (default_culture != null)
480                                         return default_culture;
481
482                                 current_culture = culture = CultureInfo.ConstructCurrentCulture ();
483                                 return culture;
484                         }
485                         
486                         [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
487                         set {
488                                 if (value == null)
489                                         throw new ArgumentNullException ("value");
490
491                                 value.CheckNeutral ();
492                                 current_culture = value;
493                                 current_culture_set = true;
494                         }
495                 }
496
497                 public CultureInfo CurrentUICulture {
498                         get {
499                                 CultureInfo culture = current_ui_culture;
500                                 if (current_ui_culture_set && culture != null)
501                                         return culture;
502
503                                 if (default_ui_culture != null)
504                                         return default_ui_culture;
505
506                                 current_ui_culture = culture = CultureInfo.ConstructCurrentUICulture ();
507                                 return culture;
508                         }
509                         
510                         set {
511                                 if (value == null)
512                                         throw new ArgumentNullException ("value");
513                                 current_ui_culture = value;
514                                 current_ui_culture_set = true;
515                         }
516                 }
517
518                 public bool IsThreadPoolThread {
519                         get {
520                                 return IsThreadPoolThreadInternal;
521                         }
522                 }
523
524                 internal bool IsThreadPoolThreadInternal {
525                         get {
526                                 return Internal.threadpool_thread;
527                         }
528                         set {
529                                 Internal.threadpool_thread = value;
530                         }
531                 }
532
533                 public bool IsAlive {
534                         get {
535                                 ThreadState curstate = GetState (Internal);
536                                 
537                                 if((curstate & ThreadState.Aborted) != 0 ||
538                                    (curstate & ThreadState.Stopped) != 0 ||
539                                    (curstate & ThreadState.Unstarted) != 0) {
540                                         return(false);
541                                 } else {
542                                         return(true);
543                                 }
544                         }
545                 }
546
547                 public bool IsBackground {
548                         get {
549                                 ThreadState thread_state = GetState (Internal);
550                                 if ((thread_state & ThreadState.Stopped) != 0)
551                                         throw new ThreadStateException ("Thread is dead; state can not be accessed.");
552
553                                 return (thread_state & ThreadState.Background) != 0;
554                         }
555                         
556                         set {
557                                 if (value) {
558                                         SetState (Internal, ThreadState.Background);
559                                 } else {
560                                         ClrState (Internal, ThreadState.Background);
561                                 }
562                         }
563                 }
564
565                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
566                 private extern static string GetName_internal (InternalThread thread);
567
568                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
569                 private extern static void SetName_internal (InternalThread thread, String name);
570
571                 /* 
572                  * The thread name must be shared by appdomains, so it is stored in
573                  * unmanaged code.
574                  */
575
576                 public string Name {
577                         get {
578                                 return GetName_internal (Internal);
579                         }
580                         
581                         set {
582                                 SetName_internal (Internal, value);
583                         }
584                 }
585
586                 public ThreadPriority Priority {
587                         get {
588                                 return (ThreadPriority)GetPriority (Internal);
589                         }
590                         
591                         set {
592                                 // FIXME: This doesn't do anything yet
593                                 SetPriority (Internal, (int)value);
594                         }
595                 }
596
597                 public ThreadState ThreadState {
598                         get {
599                                 return GetState (Internal);
600                         }
601                 }
602
603                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
604                 private extern static void Abort_internal (InternalThread thread, object stateInfo);
605
606                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
607                 private extern static int GetPriority (InternalThread thread);
608
609                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
610                 private extern static void SetPriority (InternalThread thread, int priority);
611
612                 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
613                 public void Abort () 
614                 {
615                         Abort_internal (Internal, null);
616                 }
617
618                 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
619                 public void Abort (object stateInfo) 
620                 {
621                         Abort_internal (Internal, stateInfo);
622                 }
623
624                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
625                 internal extern object GetAbortExceptionState ();
626
627                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
628                 private extern static void Interrupt_internal (InternalThread thread);
629                 
630                 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
631                 public void Interrupt ()
632                 {
633                         Interrupt_internal (Internal);
634                 }
635
636                 // The current thread joins with 'this'. Set ms to 0 to block
637                 // until this actually exits.
638                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
639                 private extern static bool Join_internal(InternalThread thread, int ms, IntPtr handle);
640                 
641                 public void Join()
642                 {
643                         Join_internal(Internal, Timeout.Infinite, Internal.system_thread_handle);
644                 }
645
646                 public bool Join(int millisecondsTimeout)
647                 {
648                         if (millisecondsTimeout < Timeout.Infinite)
649                                 throw new ArgumentOutOfRangeException ("millisecondsTimeout", "Timeout less than zero");
650
651                         return Join_internal (Internal, millisecondsTimeout, Internal.system_thread_handle);
652                 }
653
654                 public bool Join(TimeSpan timeout)
655                 {
656                         long ms = (long) timeout.TotalMilliseconds;
657                         if (ms < Timeout.Infinite || ms > Int32.MaxValue)
658                                 throw new ArgumentOutOfRangeException ("timeout", "timeout out of range");
659
660                         return Join_internal (Internal, (int) ms, Internal.system_thread_handle);
661                 }
662
663                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
664                 public extern static void MemoryBarrier ();
665
666                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
667                 private extern void Resume_internal();
668
669                 [Obsolete ("")]
670                 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
671                 public void Resume () 
672                 {
673                         Resume_internal ();
674                 }
675
676                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
677                 private extern static void SpinWait_nop ();
678
679
680                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
681                 public static void SpinWait (int iterations) 
682                 {
683                         if (iterations < 0)
684                                 return;
685                         while (iterations-- > 0)
686                         {
687                                 SpinWait_nop ();
688                         }
689                 }
690
691                 private void StartInternal ()
692                 {
693                         current_thread = this;
694
695                         if (threadstart is ThreadStart) {
696                                 ((ThreadStart) threadstart) ();
697                         } else {
698                                 ((ParameterizedThreadStart) threadstart) (start_obj);
699                         }
700                 }
701
702                 public void Start() {
703                         // propagate informations from the original thread to the new thread
704                         ec_to_set = ExecutionContext.Capture (false, true);
705                         Internal._serialized_principal = CurrentThread.Internal._serialized_principal;
706
707                         // Thread_internal creates and starts the new thread, 
708                         if (Thread_internal((ThreadStart) StartInternal) == (IntPtr) 0)
709                                 throw new SystemException ("Thread creation failed.");
710                 }
711
712                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
713                 private extern static void Suspend_internal(InternalThread thread);
714
715                 [Obsolete ("")]
716                 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
717                 public void Suspend ()
718                 {
719                         Suspend_internal (Internal);
720                 }
721
722                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
723                 extern private static void SetState (InternalThread thread, ThreadState set);
724
725                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
726                 extern private static void ClrState (InternalThread thread, ThreadState clr);
727
728                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
729                 extern private static ThreadState GetState (InternalThread thread);
730
731                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
732                 extern public static byte VolatileRead (ref byte address);
733                 
734                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
735                 extern public static double VolatileRead (ref double address);
736                 
737                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
738                 extern public static short VolatileRead (ref short address);
739                 
740                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
741                 extern public static int VolatileRead (ref int address);
742                 
743                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
744                 extern public static long VolatileRead (ref long address);
745                 
746                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
747                 extern public static IntPtr VolatileRead (ref IntPtr address);
748                 
749                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
750                 extern public static object VolatileRead (ref object address);
751
752                 [CLSCompliant(false)]
753                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
754                 extern public static sbyte VolatileRead (ref sbyte address);
755                 
756                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
757                 extern public static float VolatileRead (ref float address);
758
759                 [CLSCompliant (false)]
760                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
761                 extern public static ushort VolatileRead (ref ushort address);
762
763                 [CLSCompliant (false)]
764                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
765                 extern public static uint VolatileRead (ref uint address);
766
767                 [CLSCompliant (false)]
768                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
769                 extern public static ulong VolatileRead (ref ulong address);
770
771                 [CLSCompliant (false)]
772                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
773                 extern public static UIntPtr VolatileRead (ref UIntPtr address);
774
775                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
776                 extern public static void VolatileWrite (ref byte address, byte value);
777                 
778                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
779                 extern public static void VolatileWrite (ref double address, double value);
780                 
781                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
782                 extern public static void VolatileWrite (ref short address, short value);
783                 
784                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
785                 extern public static void VolatileWrite (ref int address, int value);
786                 
787                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
788                 extern public static void VolatileWrite (ref long address, long value);
789                 
790                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
791                 extern public static void VolatileWrite (ref IntPtr address, IntPtr value);
792                 
793                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
794                 extern public static void VolatileWrite (ref object address, object value);
795
796                 [CLSCompliant(false)]
797                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
798                 extern public static void VolatileWrite (ref sbyte address, sbyte value);
799                 
800                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
801                 extern public static void VolatileWrite (ref float address, float value);
802
803                 [CLSCompliant (false)]
804                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
805                 extern public static void VolatileWrite (ref ushort address, ushort value);
806
807                 [CLSCompliant (false)]
808                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
809                 extern public static void VolatileWrite (ref uint address, uint value);
810
811                 [CLSCompliant (false)]
812                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
813                 extern public static void VolatileWrite (ref ulong address, ulong value);
814
815                 [CLSCompliant (false)]
816                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
817                 extern public static void VolatileWrite (ref UIntPtr address, UIntPtr value);
818                 
819                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
820                 extern static int SystemMaxStackStize ();
821
822                 static int CheckStackSize (int maxStackSize)
823                 {
824                         if (maxStackSize < 0)
825                                 throw new ArgumentOutOfRangeException ("less than zero", "maxStackSize");
826
827                         if (maxStackSize < 131072) // make sure stack is at least 128k big
828                                 return 131072;
829
830                         int page_size = Environment.GetPageSize ();
831
832                         if ((maxStackSize % page_size) != 0) // round up to a divisible of page size
833                                 maxStackSize = (maxStackSize / (page_size - 1)) * page_size;
834
835                         /* Respect the max stack size imposed by the system*/
836                         return Math.Min (maxStackSize, SystemMaxStackStize ());
837                 }
838
839                 public Thread (ThreadStart start, int maxStackSize)
840                 {
841                         if (start == null)
842                                 throw new ArgumentNullException ("start");
843
844                         threadstart = start;
845                         Internal.stack_size = CheckStackSize (maxStackSize);;
846                 }
847
848                 public Thread (ParameterizedThreadStart start)
849                 {
850                         if (start == null)
851                                 throw new ArgumentNullException ("start");
852
853                         threadstart = start;
854                 }
855
856                 public Thread (ParameterizedThreadStart start, int maxStackSize)
857                 {
858                         if (start == null)
859                                 throw new ArgumentNullException ("start");
860
861                         threadstart = start;
862                         Internal.stack_size = CheckStackSize (maxStackSize);
863                 }
864
865                 public ExecutionContext ExecutionContext {
866                         [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
867                         get {
868                                 if (_ec == null)
869                                         _ec = new ExecutionContext ();
870                                 return _ec;
871                         }
872                         internal set {
873                                 _ec = value;
874                         }
875                 }
876
877                 internal bool HasExecutionContext {
878                         get {
879                                 return _ec != null;
880                         }
881                 }
882
883                 internal void BranchExecutionContext (out ExecutionContext.Switcher switcher)
884                 {
885                         if (_ec == null) {
886                                 switcher =  new ExecutionContext.Switcher ();
887                         } else {
888                                 switcher = new ExecutionContext.Switcher (_ec);
889                                 _ec.CopyOnWrite = true;
890                         }
891                 }
892
893                 internal void RestoreExecutionContext (ref ExecutionContext.Switcher switcher)
894                 {
895                         if (switcher.IsEmpty) {
896                                 _ec = null;
897                                 return;
898                         }
899
900                         switcher.Restore (_ec);
901                 }
902
903                 public int ManagedThreadId {
904                         [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
905                         get {
906                                 return Internal.managed_id;
907                         }
908                 }
909
910                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
911                 public static void BeginCriticalRegion ()
912                 {
913                         CurrentThread.Internal.critical_region_level++;
914                 }
915
916                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
917                 public static void EndCriticalRegion ()
918                 {
919                         CurrentThread.Internal.critical_region_level--;
920                 }
921
922                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
923                 public static void BeginThreadAffinity ()
924                 {
925                         // Managed and native threads are currently bound together.
926                 }
927
928                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
929                 public static void EndThreadAffinity ()
930                 {
931                         // Managed and native threads are currently bound together.
932                 }
933
934                 public ApartmentState GetApartmentState ()
935                 {
936                         return (ApartmentState)Internal.apartment_state;
937                 }
938
939                 public void SetApartmentState (ApartmentState state)
940                 {
941                         if (!TrySetApartmentState (state))
942                                 throw new InvalidOperationException ("Failed to set the specified COM apartment state.");
943                 }
944
945                 public bool TrySetApartmentState (ApartmentState state) 
946                 {
947                         if ((ThreadState & ThreadState.Unstarted) == 0)
948                                 throw new ThreadStateException ("Thread was in an invalid state for the operation being executed.");
949
950                         if ((ApartmentState)Internal.apartment_state != ApartmentState.Unknown && 
951                             (ApartmentState)Internal.apartment_state != state)
952                                 return false;
953
954                         Internal.apartment_state = (byte)state;
955
956                         return true;
957                 }
958                 
959                 [ComVisible (false)]
960                 public override int GetHashCode ()
961                 {
962                         return ManagedThreadId;
963                 }
964
965                 public void Start (object parameter)
966                 {
967                         start_obj = parameter;
968                         Start ();
969                 }
970
971                 // NOTE: This method doesn't show in the class library status page because
972                 // it cannot be "found" with the StrongNameIdentityPermission for ECMA key.
973                 // But it's there!
974                 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
975                 [StrongNameIdentityPermission (SecurityAction.LinkDemand, PublicKey="00000000000000000400000000000000")]
976                 [Obsolete ("see CompressedStack class")]
977                 public CompressedStack GetCompressedStack ()
978                 {
979 #if MOBILE
980                         throw new NotSupportedException ();
981 #else                   
982                         // Note: returns null if no CompressedStack has been set.
983                         // However CompressedStack.GetCompressedStack returns an 
984                         // (empty?) CompressedStack instance.
985                         CompressedStack cs = ExecutionContext.SecurityContext.CompressedStack;
986                         return ((cs == null) || cs.IsEmpty ()) ? null : cs.CreateCopy ();
987 #endif
988                 }
989
990                 // NOTE: This method doesn't show in the class library status page because
991                 // it cannot be "found" with the StrongNameIdentityPermission for ECMA key.
992                 // But it's there!
993                 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
994                 [StrongNameIdentityPermission (SecurityAction.LinkDemand, PublicKey="00000000000000000400000000000000")]
995                 [Obsolete ("see CompressedStack class")]
996                 public void SetCompressedStack (CompressedStack stack)
997                 {
998 #if MOBILE
999                         throw new NotSupportedException ();
1000 #else
1001                         ExecutionContext.SecurityContext.CompressedStack = stack;
1002 #endif
1003                 }
1004
1005 #if !MOBILE
1006                 void _Thread.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1007                 {
1008                         throw new NotImplementedException ();
1009                 }
1010
1011                 void _Thread.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1012                 {
1013                         throw new NotImplementedException ();
1014                 }
1015
1016                 void _Thread.GetTypeInfoCount (out uint pcTInfo)
1017                 {
1018                         throw new NotImplementedException ();
1019                 }
1020
1021                 void _Thread.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
1022                         IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1023                 {
1024                         throw new NotImplementedException ();
1025                 }
1026 #endif
1027         }
1028 }