2 // System.Threading.Thread.cs
5 // Dick Porter (dick@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
8 // Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
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;
39 using System.Collections.Generic;
40 using System.Reflection;
41 using System.Security;
42 using System.Runtime.ConstrainedExecution;
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
50 // stores a thread handle
51 internal IntPtr system_thread_handle;
53 /* Note this is an opaque object (an array), not a CultureInfo */
54 private object cached_culture_info;
55 /* accessed only from unmanaged code */
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;
64 /* start_notify is used by the runtime to signal that Start()
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;
95 private IntPtr manage_callback;
96 private IntPtr interrupt_on_stop;
98 private IntPtr android_tid;
99 private IntPtr thread_pinning_ref;
100 private int ignore_next_signal;
102 * These fields are used to avoid having to increment corlib versions
103 * when a new field is added to the unmanaged MonoThread structure.
105 private IntPtr unused0;
106 private IntPtr unused1;
107 private IntPtr unused2;
109 #pragma warning restore 169, 414, 649
111 // Closes the system thread handle
112 [MethodImplAttribute(MethodImplOptions.InternalCall)]
113 private extern void Thread_free_internal(IntPtr handle);
115 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
117 Thread_free_internal(system_thread_handle);
121 [ClassInterface (ClassInterfaceType.None)]
123 [ComDefaultInterface (typeof (_Thread))]
124 [StructLayout (LayoutKind.Sequential)]
126 public sealed class Thread : CriticalFinalizerObject {
128 public sealed class Thread : CriticalFinalizerObject, _Thread {
130 #pragma warning disable 414
131 #region Sync with metadata/object-internals.h
132 private InternalThread internal_thread;
134 private ExecutionContext ec_to_set;
136 #pragma warning restore 414
138 IPrincipal principal;
139 int principal_version;
140 CultureInfo current_culture;
141 CultureInfo current_ui_culture;
143 // the name of local_slots, current_thread and _ec is
144 // important because they are used by the runtime.
146 static object[] local_slots;
149 static Thread current_thread;
151 /* The actual ExecutionContext of the thread. It's
152 ThreadStatic so that it's not shared between
155 static ExecutionContext _ec;
157 static NamedDataSlot namedDataSlot;
159 // can be both a ThreadStart and a ParameterizedThreadStart
160 private MulticastDelegate threadstart;
161 //private string thread_name=null;
163 [MethodImplAttribute(MethodImplOptions.InternalCall)]
164 private extern void ConstructInternalThread ();
166 private InternalThread Internal {
168 if (internal_thread == null)
169 ConstructInternalThread ();
170 return internal_thread;
174 public static Context CurrentContext {
175 [SecurityPermission (SecurityAction.LinkDemand, Infrastructure=true)]
177 return(AppDomain.InternalGetContext ());
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.
187 [MethodImplAttribute(MethodImplOptions.InternalCall)]
188 private extern static byte[] ByteArrayToRootDomain (byte[] arr);
190 [MethodImplAttribute(MethodImplOptions.InternalCall)]
191 private extern static byte[] ByteArrayToCurrentDomain (byte[] arr);
193 static void DeserializePrincipal (Thread th)
195 MemoryStream ms = new MemoryStream (ByteArrayToCurrentDomain (th.Internal._serialized_principal));
196 int type = ms.ReadByte ();
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;
208 roles = new string [n_roles];
209 for (int i = 0; i < n_roles; i++)
210 roles [i] = reader.ReadString ();
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);
219 static void SerializePrincipal (Thread th, IPrincipal value)
221 MemoryStream ms = new MemoryStream ();
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) {
231 } else if (gp.Roles.Length == 0) {
237 BinaryWriter br = new BinaryWriter (ms);
238 br.Write (gp.Identity.Name);
239 br.Write (gp.Identity.AuthenticationType);
240 string [] roles = gp.Roles;
242 br.Write ((int) (-1));
244 br.Write (roles.Length);
245 foreach (string s in roles) {
256 BinaryFormatter bf = new BinaryFormatter ();
258 bf.Serialize (ms, value);
261 th.Internal._serialized_principal = ByteArrayToRootDomain (ms.ToArray ());
264 public static IPrincipal CurrentPrincipal {
266 Thread th = CurrentThread;
268 if (th.principal_version != th.Internal._serialized_principal_version)
271 if (th.principal != null)
274 if (th.Internal._serialized_principal != null) {
276 DeserializePrincipal (th);
281 th.principal = GetDomain ().DefaultPrincipal;
282 th.principal_version = th.Internal._serialized_principal_version;
285 [SecurityPermission (SecurityAction.Demand, ControlPrincipal = true)]
287 Thread th = CurrentThread;
289 if (value != GetDomain ().DefaultPrincipal) {
290 ++th.Internal._serialized_principal_version;
292 SerializePrincipal (th, value);
293 } catch (Exception) {
294 th.Internal._serialized_principal = null;
296 th.principal_version = th.Internal._serialized_principal_version;
298 th.Internal._serialized_principal = null;
301 th.principal = value;
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();
310 [MethodImplAttribute(MethodImplOptions.InternalCall)]
311 internal extern static uint AllocTlsData (Type type);
313 [MethodImplAttribute(MethodImplOptions.InternalCall)]
314 internal extern static void DestroyTlsData (uint offset);
316 public static Thread CurrentThread {
317 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
319 if (current_thread == null)
320 current_thread = new Thread (CurrentInternalThread_internal ());
321 return current_thread;
325 internal static int CurrentThreadId {
327 return (int)(CurrentThread.internal_thread.thread_id);
331 static NamedDataSlot NamedDataSlot {
333 if (namedDataSlot == null)
334 Interlocked.CompareExchange (ref namedDataSlot, new NamedDataSlot (), null);
336 return namedDataSlot;
340 public static LocalDataStoreSlot AllocateNamedDataSlot (string name)
342 return NamedDataSlot.Allocate (name);
345 public static void FreeNamedDataSlot (string name)
347 NamedDataSlot.Free (name);
350 public static LocalDataStoreSlot AllocateDataSlot ()
352 return new LocalDataStoreSlot (true);
355 public static object GetData (LocalDataStoreSlot slot) {
356 object[] slots = local_slots;
358 throw new ArgumentNullException ("slot");
359 if (slots != null && slot.slot < slots.Length)
360 return slots [slot.slot];
364 public static void SetData (LocalDataStoreSlot slot, object data) {
365 object[] slots = local_slots;
367 throw new ArgumentNullException ("slot");
369 slots = new object [slot.slot + 2];
371 } else if (slot.slot >= slots.Length) {
372 object[] nslots = new object [slot.slot + 2];
373 slots.CopyTo (nslots, 0);
377 slots [slot.slot] = data;
380 [MethodImplAttribute (MethodImplOptions.InternalCall)]
381 internal extern static void FreeLocalSlotValues (int slot, bool thread_local);
383 public static LocalDataStoreSlot GetNamedDataSlot(string name)
385 return NamedDataSlot.Get (name);
388 public static AppDomain GetDomain() {
389 return AppDomain.CurrentDomain;
392 [MethodImplAttribute(MethodImplOptions.InternalCall)]
393 public extern static int GetDomainID();
395 [MethodImplAttribute(MethodImplOptions.InternalCall)]
396 private extern static void ResetAbort_internal();
398 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
399 public static void ResetAbort ()
401 ResetAbort_internal ();
405 [HostProtectionAttribute (SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)]
406 [MethodImplAttribute(MethodImplOptions.InternalCall)]
407 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
408 public extern static bool Yield ();
412 [MethodImplAttribute(MethodImplOptions.InternalCall)]
413 private extern static void Sleep_internal(int ms);
415 public static void Sleep (int millisecondsTimeout)
417 if (millisecondsTimeout < Timeout.Infinite)
418 throw new ArgumentOutOfRangeException ("millisecondsTimeout", "Negative timeout");
420 Sleep_internal (millisecondsTimeout);
423 public static void Sleep (TimeSpan timeout)
425 long ms = (long) timeout.TotalMilliseconds;
426 if (ms < Timeout.Infinite || ms > Int32.MaxValue)
427 throw new ArgumentOutOfRangeException ("timeout", "timeout out of range");
429 Sleep_internal ((int) ms);
432 // Returns the system thread handle
433 [MethodImplAttribute(MethodImplOptions.InternalCall)]
434 private extern IntPtr Thread_internal (MulticastDelegate start);
436 public Thread(ThreadStart start) {
438 throw new ArgumentNullException("Null ThreadStart");
443 private Thread (InternalThread it) {
444 internal_thread = it;
447 // part of ".NETPortable,Version=v4.0,Profile=Profile3" i.e. FX4 and SL4
448 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
453 [Obsolete ("Deprecated in favor of GetApartmentState, SetApartmentState and TrySetApartmentState.")]
454 public ApartmentState ApartmentState {
456 if ((ThreadState & ThreadState.Stopped) != 0)
457 throw new ThreadStateException ("Thread is dead; state can not be accessed.");
459 return (ApartmentState)Internal.apartment_state;
463 TrySetApartmentState (value);
467 //[MethodImplAttribute (MethodImplOptions.InternalCall)]
468 //private static extern int current_lcid ();
470 public CultureInfo CurrentCulture {
472 CultureInfo culture = current_culture;
476 current_culture = culture = CultureInfo.ConstructCurrentCulture ();
477 NumberFormatter.SetThreadCurrentCulture (culture);
481 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
484 throw new ArgumentNullException ("value");
486 value.CheckNeutral ();
487 current_culture = value;
488 NumberFormatter.SetThreadCurrentCulture (value);
492 public CultureInfo CurrentUICulture {
494 CultureInfo culture = current_ui_culture;
498 current_ui_culture = culture = CultureInfo.ConstructCurrentUICulture ();
504 throw new ArgumentNullException ("value");
505 current_ui_culture = value;
509 public bool IsThreadPoolThread {
511 return IsThreadPoolThreadInternal;
515 internal bool IsThreadPoolThreadInternal {
517 return Internal.threadpool_thread;
520 Internal.threadpool_thread = value;
524 public bool IsAlive {
526 ThreadState curstate = GetState (Internal);
528 if((curstate & ThreadState.Aborted) != 0 ||
529 (curstate & ThreadState.Stopped) != 0 ||
530 (curstate & ThreadState.Unstarted) != 0) {
538 public bool IsBackground {
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.");
544 return (thread_state & ThreadState.Background) != 0;
549 SetState (Internal, ThreadState.Background);
551 ClrState (Internal, ThreadState.Background);
556 [MethodImplAttribute(MethodImplOptions.InternalCall)]
557 private extern static string GetName_internal (InternalThread thread);
559 [MethodImplAttribute(MethodImplOptions.InternalCall)]
560 private extern static void SetName_internal (InternalThread thread, String name);
563 * The thread name must be shared by appdomains, so it is stored in
569 return GetName_internal (Internal);
573 SetName_internal (Internal, value);
577 public ThreadPriority Priority {
579 return(ThreadPriority.Lowest);
583 // FIXME: Implement setter.
587 public ThreadState ThreadState {
589 return GetState (Internal);
593 [MethodImplAttribute(MethodImplOptions.InternalCall)]
594 private extern static void Abort_internal (InternalThread thread, object stateInfo);
596 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
599 Abort_internal (Internal, null);
602 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
603 public void Abort (object stateInfo)
605 Abort_internal (Internal, stateInfo);
608 [MethodImplAttribute(MethodImplOptions.InternalCall)]
609 internal extern object GetAbortExceptionState ();
611 [MethodImplAttribute (MethodImplOptions.InternalCall)]
612 private extern static void Interrupt_internal (InternalThread thread);
614 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
615 public void Interrupt ()
617 Interrupt_internal (Internal);
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);
627 Join_internal(Internal, Timeout.Infinite, Internal.system_thread_handle);
630 public bool Join(int millisecondsTimeout)
632 if (millisecondsTimeout < Timeout.Infinite)
633 throw new ArgumentOutOfRangeException ("millisecondsTimeout", "Timeout less than zero");
635 return Join_internal (Internal, millisecondsTimeout, Internal.system_thread_handle);
638 public bool Join(TimeSpan timeout)
640 long ms = (long) timeout.TotalMilliseconds;
641 if (ms < Timeout.Infinite || ms > Int32.MaxValue)
642 throw new ArgumentOutOfRangeException ("timeout", "timeout out of range");
644 return Join_internal (Internal, (int) ms, Internal.system_thread_handle);
647 [MethodImplAttribute(MethodImplOptions.InternalCall)]
648 public extern static void MemoryBarrier ();
650 [MethodImplAttribute(MethodImplOptions.InternalCall)]
651 private extern void Resume_internal();
654 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
655 public void Resume ()
660 [MethodImplAttribute (MethodImplOptions.InternalCall)]
661 private extern static void SpinWait_nop ();
664 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
665 public static void SpinWait (int iterations)
669 while (iterations-- > 0)
675 private void StartInternal ()
677 current_thread = this;
679 if (threadstart is ThreadStart) {
680 ((ThreadStart) threadstart) ();
682 ((ParameterizedThreadStart) threadstart) (start_obj);
686 public void Start() {
687 // propagate informations from the original thread to the new thread
688 if (!ExecutionContext.IsFlowSuppressed ())
689 ec_to_set = ExecutionContext.Capture ();
690 Internal._serialized_principal = CurrentThread.Internal._serialized_principal;
692 // Thread_internal creates and starts the new thread,
693 if (Thread_internal((ThreadStart) StartInternal) == (IntPtr) 0)
694 throw new SystemException ("Thread creation failed.");
697 [MethodImplAttribute(MethodImplOptions.InternalCall)]
698 private extern static void Suspend_internal(InternalThread thread);
701 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
702 public void Suspend ()
704 Suspend_internal (Internal);
707 [MethodImplAttribute (MethodImplOptions.InternalCall)]
708 extern private static void SetState (InternalThread thread, ThreadState set);
710 [MethodImplAttribute (MethodImplOptions.InternalCall)]
711 extern private static void ClrState (InternalThread thread, ThreadState clr);
713 [MethodImplAttribute (MethodImplOptions.InternalCall)]
714 extern private static ThreadState GetState (InternalThread thread);
716 [MethodImplAttribute (MethodImplOptions.InternalCall)]
717 extern public static byte VolatileRead (ref byte address);
719 [MethodImplAttribute (MethodImplOptions.InternalCall)]
720 extern public static double VolatileRead (ref double address);
722 [MethodImplAttribute (MethodImplOptions.InternalCall)]
723 extern public static short VolatileRead (ref short address);
725 [MethodImplAttribute (MethodImplOptions.InternalCall)]
726 extern public static int VolatileRead (ref int address);
728 [MethodImplAttribute (MethodImplOptions.InternalCall)]
729 extern public static long VolatileRead (ref long address);
731 [MethodImplAttribute (MethodImplOptions.InternalCall)]
732 extern public static IntPtr VolatileRead (ref IntPtr address);
734 [MethodImplAttribute (MethodImplOptions.InternalCall)]
735 extern public static object VolatileRead (ref object address);
737 [CLSCompliant(false)]
738 [MethodImplAttribute (MethodImplOptions.InternalCall)]
739 extern public static sbyte VolatileRead (ref sbyte address);
741 [MethodImplAttribute (MethodImplOptions.InternalCall)]
742 extern public static float VolatileRead (ref float address);
744 [CLSCompliant (false)]
745 [MethodImplAttribute (MethodImplOptions.InternalCall)]
746 extern public static ushort VolatileRead (ref ushort address);
748 [CLSCompliant (false)]
749 [MethodImplAttribute (MethodImplOptions.InternalCall)]
750 extern public static uint VolatileRead (ref uint address);
752 [CLSCompliant (false)]
753 [MethodImplAttribute (MethodImplOptions.InternalCall)]
754 extern public static ulong VolatileRead (ref ulong address);
756 [CLSCompliant (false)]
757 [MethodImplAttribute (MethodImplOptions.InternalCall)]
758 extern public static UIntPtr VolatileRead (ref UIntPtr address);
760 [MethodImplAttribute (MethodImplOptions.InternalCall)]
761 extern public static void VolatileWrite (ref byte address, byte value);
763 [MethodImplAttribute (MethodImplOptions.InternalCall)]
764 extern public static void VolatileWrite (ref double address, double value);
766 [MethodImplAttribute (MethodImplOptions.InternalCall)]
767 extern public static void VolatileWrite (ref short address, short value);
769 [MethodImplAttribute (MethodImplOptions.InternalCall)]
770 extern public static void VolatileWrite (ref int address, int value);
772 [MethodImplAttribute (MethodImplOptions.InternalCall)]
773 extern public static void VolatileWrite (ref long address, long value);
775 [MethodImplAttribute (MethodImplOptions.InternalCall)]
776 extern public static void VolatileWrite (ref IntPtr address, IntPtr value);
778 [MethodImplAttribute (MethodImplOptions.InternalCall)]
779 extern public static void VolatileWrite (ref object address, object value);
781 [CLSCompliant(false)]
782 [MethodImplAttribute (MethodImplOptions.InternalCall)]
783 extern public static void VolatileWrite (ref sbyte address, sbyte value);
785 [MethodImplAttribute (MethodImplOptions.InternalCall)]
786 extern public static void VolatileWrite (ref float address, float value);
788 [CLSCompliant (false)]
789 [MethodImplAttribute (MethodImplOptions.InternalCall)]
790 extern public static void VolatileWrite (ref ushort address, ushort value);
792 [CLSCompliant (false)]
793 [MethodImplAttribute (MethodImplOptions.InternalCall)]
794 extern public static void VolatileWrite (ref uint address, uint value);
796 [CLSCompliant (false)]
797 [MethodImplAttribute (MethodImplOptions.InternalCall)]
798 extern public static void VolatileWrite (ref ulong address, ulong value);
800 [CLSCompliant (false)]
801 [MethodImplAttribute (MethodImplOptions.InternalCall)]
802 extern public static void VolatileWrite (ref UIntPtr address, UIntPtr value);
805 static int CheckStackSize (int maxStackSize)
807 if (maxStackSize < 0)
808 throw new ArgumentOutOfRangeException ("less than zero", "maxStackSize");
810 if (maxStackSize < 131072) // make sure stack is at least 128k big
813 int page_size = Environment.GetPageSize ();
815 if ((maxStackSize % page_size) != 0) // round up to a divisible of page size
816 maxStackSize = (maxStackSize / (page_size - 1)) * page_size;
818 int default_stack_size = (IntPtr.Size / 4) * 1024 * 1024; // from wthreads.c
820 if (maxStackSize > default_stack_size)
821 return default_stack_size;
826 public Thread (ThreadStart start, int maxStackSize)
829 throw new ArgumentNullException ("start");
832 Internal.stack_size = CheckStackSize (maxStackSize);;
835 public Thread (ParameterizedThreadStart start)
838 throw new ArgumentNullException ("start");
843 public Thread (ParameterizedThreadStart start, int maxStackSize)
846 throw new ArgumentNullException ("start");
849 Internal.stack_size = CheckStackSize (maxStackSize);
852 [MonoTODO ("limited to CompressedStack support")]
853 public ExecutionContext ExecutionContext {
854 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
857 _ec = new ExecutionContext ();
862 public int ManagedThreadId {
863 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
865 return Internal.managed_id;
869 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
870 public static void BeginCriticalRegion ()
872 CurrentThread.Internal.critical_region_level++;
875 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
876 public static void EndCriticalRegion ()
878 CurrentThread.Internal.critical_region_level--;
881 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
882 public static void BeginThreadAffinity ()
884 // Managed and native threads are currently bound together.
887 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
888 public static void EndThreadAffinity ()
890 // Managed and native threads are currently bound together.
893 public ApartmentState GetApartmentState ()
895 return (ApartmentState)Internal.apartment_state;
898 public void SetApartmentState (ApartmentState state)
900 if (!TrySetApartmentState (state))
901 throw new InvalidOperationException ("Failed to set the specified COM apartment state.");
904 public bool TrySetApartmentState (ApartmentState state)
906 /* Only throw this exception when changing the
907 * state of another thread. See bug 324338
909 if ((this != CurrentThread) &&
910 (ThreadState & ThreadState.Unstarted) == 0)
911 throw new ThreadStateException ("Thread was in an invalid state for the operation being executed.");
913 if ((ApartmentState)Internal.apartment_state != ApartmentState.Unknown)
916 Internal.apartment_state = (byte)state;
922 public override int GetHashCode ()
924 return ManagedThreadId;
927 public void Start (object parameter)
929 start_obj = parameter;
933 // NOTE: This method doesn't show in the class library status page because
934 // it cannot be "found" with the StrongNameIdentityPermission for ECMA key.
936 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
937 [StrongNameIdentityPermission (SecurityAction.LinkDemand, PublicKey="00000000000000000400000000000000")]
938 [Obsolete ("see CompressedStack class")]
939 public CompressedStack GetCompressedStack ()
942 throw new NotSupportedException ();
944 // Note: returns null if no CompressedStack has been set.
945 // However CompressedStack.GetCompressedStack returns an
946 // (empty?) CompressedStack instance.
947 CompressedStack cs = ExecutionContext.SecurityContext.CompressedStack;
948 return ((cs == null) || cs.IsEmpty ()) ? null : cs.CreateCopy ();
952 // NOTE: This method doesn't show in the class library status page because
953 // it cannot be "found" with the StrongNameIdentityPermission for ECMA key.
955 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
956 [StrongNameIdentityPermission (SecurityAction.LinkDemand, PublicKey="00000000000000000400000000000000")]
957 [Obsolete ("see CompressedStack class")]
958 public void SetCompressedStack (CompressedStack stack)
961 throw new NotSupportedException ();
963 ExecutionContext.SecurityContext.CompressedStack = stack;
968 void _Thread.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
970 throw new NotImplementedException ();
973 void _Thread.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
975 throw new NotImplementedException ();
978 void _Thread.GetTypeInfoCount (out uint pcTInfo)
980 throw new NotImplementedException ();
983 void _Thread.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
984 IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
986 throw new NotImplementedException ();