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;
40 using System.Security;
43 using System.Runtime.ConstrainedExecution;
46 namespace System.Threading {
48 [ClassInterface (ClassInterfaceType.None)]
51 [ComDefaultInterface (typeof (_Thread))]
52 public sealed class Thread : CriticalFinalizerObject, _Thread {
54 public sealed class Thread : _Thread {
57 #region Sync with metadata/object-internals.h
59 // stores a thread handle
60 private IntPtr system_thread_handle;
62 /* Note this is an opaque object (an array), not a CultureInfo */
63 private object cached_culture_info;
64 private IntPtr unused0;
65 private bool threadpool_thread;
66 /* accessed only from unmanaged code */
69 private ThreadState state = ThreadState.Unstarted;
70 private object abort_exc;
71 internal object abort_state;
72 /* thread_id is only accessed from unmanaged code */
73 private Int64 thread_id;
75 /* start_notify is used by the runtime to signal that Start()
78 private IntPtr start_notify;
79 private IntPtr stack_ptr;
80 private UIntPtr static_data; /* GC-tracked */
81 private IntPtr jit_data;
82 private IntPtr lock_data;
83 Context current_appcontext;
86 private IntPtr appdomain_refs;
87 private bool interruption_requested;
88 private IntPtr suspend_event;
89 private IntPtr suspended_event;
90 private IntPtr resume_event;
91 private IntPtr synch_cs;
92 private IntPtr serialized_culture_info;
93 private int serialized_culture_info_len;
94 private IntPtr serialized_ui_culture_info;
95 private int serialized_ui_culture_info_len;
96 private ExecutionContext _ec;
97 private bool thread_dump_requested;
98 private IntPtr end_stack;
99 private bool thread_interrupt_requested;
100 private byte apartment_state = (byte)ApartmentState.Unknown;
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 unused5;
106 private IntPtr unused6;
107 private IntPtr unused7;
110 // the name of local_slots is important as it's used by the runtime.
112 static object[] local_slots;
114 // can be both a ThreadSart and a ParameterizedThreadStart
115 private MulticastDelegate threadstart;
116 private string thread_name=null;
118 private static int _managed_id_counter;
119 private int managed_id;
121 private IPrincipal _principal;
123 public static Context CurrentContext {
124 [SecurityPermission (SecurityAction.LinkDemand, Infrastructure=true)]
126 return(AppDomain.InternalGetContext ());
130 public static IPrincipal CurrentPrincipal {
133 Thread th = CurrentThread;
137 p = GetDomain ().DefaultPrincipal;
143 [SecurityPermission (SecurityAction.Demand, ControlPrincipal = true)]
145 CurrentThread._principal = value;
149 // Looks up the object associated with the current thread
150 [MethodImplAttribute(MethodImplOptions.InternalCall)]
151 private extern static Thread CurrentThread_internal();
153 public static Thread CurrentThread {
155 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
158 return(CurrentThread_internal());
162 internal static int CurrentThreadId {
164 return (int)(CurrentThread.thread_id);
168 // Stores a hash keyed by strings of LocalDataStoreSlot objects
169 static Hashtable datastorehash;
170 private static object datastore_lock = new object ();
172 private static void InitDataStoreHash () {
173 lock (datastore_lock) {
174 if (datastorehash == null) {
175 datastorehash = Hashtable.Synchronized(new Hashtable());
180 public static LocalDataStoreSlot AllocateNamedDataSlot (string name) {
181 lock (datastore_lock) {
182 if (datastorehash == null)
183 InitDataStoreHash ();
184 LocalDataStoreSlot slot = (LocalDataStoreSlot)datastorehash [name];
186 // This exception isnt documented (of
187 // course) but .net throws it
188 throw new ArgumentException("Named data slot already added");
191 slot = AllocateDataSlot ();
193 datastorehash.Add (name, slot);
199 public static void FreeNamedDataSlot (string name) {
200 lock (datastore_lock) {
201 if (datastorehash == null)
202 InitDataStoreHash ();
203 LocalDataStoreSlot slot = (LocalDataStoreSlot)datastorehash [name];
206 datastorehash.Remove (slot);
211 public static LocalDataStoreSlot AllocateDataSlot () {
212 return new LocalDataStoreSlot (true);
215 public static object GetData (LocalDataStoreSlot slot) {
216 object[] slots = local_slots;
218 throw new ArgumentNullException ("slot");
219 if (slots != null && slot.slot < slots.Length)
220 return slots [slot.slot];
224 public static void SetData (LocalDataStoreSlot slot, object data) {
225 object[] slots = local_slots;
227 throw new ArgumentNullException ("slot");
229 slots = new object [slot.slot + 2];
231 } else if (slot.slot >= slots.Length) {
232 object[] nslots = new object [slot.slot + 2];
233 slots.CopyTo (nslots, 0);
237 slots [slot.slot] = data;
240 public static AppDomain GetDomain() {
241 return AppDomain.CurrentDomain;
244 [MethodImplAttribute(MethodImplOptions.InternalCall)]
245 public extern static int GetDomainID();
247 [MethodImplAttribute (MethodImplOptions.InternalCall)]
248 internal extern static void FreeLocalSlotValues (int slot, bool thread_local);
250 public static LocalDataStoreSlot GetNamedDataSlot(string name) {
251 lock (datastore_lock) {
252 if (datastorehash == null)
253 InitDataStoreHash ();
254 LocalDataStoreSlot slot=(LocalDataStoreSlot)datastorehash[name];
257 slot=AllocateNamedDataSlot(name);
264 [MethodImplAttribute(MethodImplOptions.InternalCall)]
265 private extern static void ResetAbort_internal();
267 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
268 public static void ResetAbort ()
270 ResetAbort_internal ();
273 [MethodImplAttribute(MethodImplOptions.InternalCall)]
274 private extern static void Sleep_internal(int ms);
276 public static void Sleep(int millisecondsTimeout) {
277 if((millisecondsTimeout<0) && (millisecondsTimeout != Timeout.Infinite)) {
278 throw new ArgumentException("Negative timeout");
280 Sleep_internal(millisecondsTimeout);
283 public static void Sleep(TimeSpan timeout) {
284 // LAMESPEC: says to throw ArgumentException too
285 int ms=Convert.ToInt32(timeout.TotalMilliseconds);
287 if(ms < 0 || ms > Int32.MaxValue) {
288 throw new ArgumentOutOfRangeException("Timeout out of range");
294 // Returns the system thread handle
295 [MethodImplAttribute(MethodImplOptions.InternalCall)]
296 private extern IntPtr Thread_internal (MulticastDelegate start);
298 [MethodImplAttribute(MethodImplOptions.InternalCall)]
299 private extern void Thread_init ();
301 private static int GetNewManagedId() {
302 return Interlocked.Increment(ref _managed_id_counter);
305 public Thread(ThreadStart start) {
307 throw new ArgumentNullException("Null ThreadStart");
315 [Obsolete ("Deprecated in favor of GetApartmentState, SetApartmentState and TrySetApartmentState.")]
317 public ApartmentState ApartmentState {
319 if ((ThreadState & ThreadState.Stopped) != 0)
320 throw new ThreadStateException ("Thread is dead; state can not be accessed.");
322 return (ApartmentState)apartment_state;
327 TrySetApartmentState (value);
329 if ((ThreadState & ThreadState.Unstarted) == 0)
330 throw new ThreadStateException ("Thread was in an invalid state for the operation being executed.");
332 if (value != ApartmentState.STA && value != ApartmentState.MTA)
333 throw new ArgumentException ("value is not a valid apartment state.");
335 if ((ApartmentState)apartment_state == ApartmentState.Unknown)
336 apartment_state = (byte)value;
341 [MethodImplAttribute (MethodImplOptions.InternalCall)]
342 private static extern int current_lcid ();
344 [MethodImplAttribute (MethodImplOptions.InternalCall)]
345 private extern CultureInfo GetCachedCurrentCulture ();
347 [MethodImplAttribute (MethodImplOptions.InternalCall)]
348 private extern byte[] GetSerializedCurrentCulture ();
350 [MethodImplAttribute (MethodImplOptions.InternalCall)]
351 private extern void SetCachedCurrentCulture (CultureInfo culture);
353 [MethodImplAttribute (MethodImplOptions.InternalCall)]
354 private extern void SetSerializedCurrentCulture (byte[] culture);
356 [MethodImplAttribute (MethodImplOptions.InternalCall)]
357 private extern CultureInfo GetCachedCurrentUICulture ();
359 [MethodImplAttribute (MethodImplOptions.InternalCall)]
360 private extern byte[] GetSerializedCurrentUICulture ();
362 [MethodImplAttribute (MethodImplOptions.InternalCall)]
363 private extern void SetCachedCurrentUICulture (CultureInfo culture);
365 [MethodImplAttribute (MethodImplOptions.InternalCall)]
366 private extern void SetSerializedCurrentUICulture (byte[] culture);
368 /* If the current_lcid() isn't known by CultureInfo,
369 * it will throw an exception which may cause
370 * String.Concat to try and recursively look up the
371 * CurrentCulture, which will throw an exception, etc.
372 * Use a boolean to short-circuit this scenario.
374 private static bool in_currentculture=false;
376 static object culture_lock = new object ();
379 * Thread objects are shared between appdomains, and CurrentCulture
380 * should always return an object in the calling appdomain. See bug
381 * http://bugzilla.ximian.com/show_bug.cgi?id=50049 for more info.
382 * This is hard to implement correctly and efficiently, so the current
383 * implementation is not perfect: changes made in one appdomain to the
384 * state of the current cultureinfo object are not visible to other
387 public CultureInfo CurrentCulture {
389 if (in_currentculture)
391 return CultureInfo.InvariantCulture;
393 CultureInfo culture = GetCachedCurrentCulture ();
397 byte[] arr = GetSerializedCurrentCulture ();
399 lock (culture_lock) {
400 in_currentculture=true;
401 culture = CultureInfo.ConstructCurrentCulture ();
403 // Don't serialize the culture in this case to avoid
404 // initializing the serialization infrastructure in the
405 // common case when the culture is not set explicitly.
407 SetCachedCurrentCulture (culture);
408 in_currentculture = false;
414 * No cultureinfo object exists for this domain, so create one
415 * by deserializing the serialized form.
417 in_currentculture = true;
419 BinaryFormatter bf = new BinaryFormatter ();
420 MemoryStream ms = new MemoryStream (arr);
421 culture = (CultureInfo)bf.Deserialize (ms);
422 SetCachedCurrentCulture (culture);
424 in_currentculture = false;
430 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
433 throw new ArgumentNullException ("value");
435 value.CheckNeutral ();
436 in_currentculture = true;
438 BinaryFormatter bf = new BinaryFormatter();
439 MemoryStream ms = new MemoryStream ();
440 bf.Serialize (ms, value);
442 SetCachedCurrentCulture (value);
443 SetSerializedCurrentCulture (ms.GetBuffer ());
445 in_currentculture = false;
450 public CultureInfo CurrentUICulture {
452 if (in_currentculture)
454 return CultureInfo.InvariantCulture;
456 CultureInfo culture = GetCachedCurrentUICulture ();
460 byte[] arr = GetSerializedCurrentUICulture ();
462 lock (culture_lock) {
463 in_currentculture=true;
470 culture = CultureInfo.ConstructCurrentUICulture ();
472 // Don't serialize the culture in this case to avoid
473 // initializing the serialization infrastructure in the
474 // common case when the culture is not set explicitly.
476 SetCachedCurrentUICulture (culture);
477 in_currentculture = false;
483 * No cultureinfo object exists for this domain, so create one
484 * by deserializing the serialized form.
486 in_currentculture = true;
488 BinaryFormatter bf = new BinaryFormatter ();
489 MemoryStream ms = new MemoryStream (arr);
490 culture = (CultureInfo)bf.Deserialize (ms);
491 SetCachedCurrentUICulture (culture);
494 in_currentculture = false;
501 in_currentculture = true;
504 throw new ArgumentNullException ("value");
507 BinaryFormatter bf = new BinaryFormatter();
508 MemoryStream ms = new MemoryStream ();
509 bf.Serialize (ms, value);
511 SetCachedCurrentUICulture (value);
512 SetSerializedCurrentUICulture (ms.GetBuffer ());
514 in_currentculture = false;
519 public bool IsThreadPoolThread {
521 return IsThreadPoolThreadInternal;
525 internal bool IsThreadPoolThreadInternal {
527 return threadpool_thread;
530 threadpool_thread = value;
534 public bool IsAlive {
536 ThreadState curstate = GetState ();
538 if((curstate & ThreadState.Aborted) != 0 ||
539 (curstate & ThreadState.Stopped) != 0 ||
540 (curstate & ThreadState.Unstarted) != 0) {
548 public bool IsBackground {
550 ThreadState thread_state = GetState ();
551 if ((thread_state & ThreadState.Stopped) != 0)
552 throw new ThreadStateException ("Thread is dead; state can not be accessed.");
554 return (thread_state & ThreadState.Background) != 0;
559 SetState (ThreadState.Background);
561 ClrState (ThreadState.Background);
566 [MethodImplAttribute(MethodImplOptions.InternalCall)]
567 private extern string GetName_internal ();
569 [MethodImplAttribute(MethodImplOptions.InternalCall)]
570 private extern void SetName_internal (String name);
573 * The thread name must be shared by appdomains, so it is stored in
579 return GetName_internal ();
583 SetName_internal (value);
587 public ThreadPriority Priority {
589 return(ThreadPriority.Lowest);
593 // FIXME: Implement setter.
597 public ThreadState ThreadState {
603 [MethodImplAttribute(MethodImplOptions.InternalCall)]
604 private extern void Abort_internal (object stateInfo);
606 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
609 Abort_internal (null);
612 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
613 public void Abort (object stateInfo)
615 Abort_internal (stateInfo);
618 [MethodImplAttribute (MethodImplOptions.InternalCall)]
619 private extern void Interrupt_internal ();
621 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
622 public void Interrupt ()
624 Interrupt_internal ();
627 // The current thread joins with 'this'. Set ms to 0 to block
628 // until this actually exits.
629 [MethodImplAttribute(MethodImplOptions.InternalCall)]
630 private extern bool Join_internal(int ms, IntPtr handle);
634 Join_internal(Timeout.Infinite, system_thread_handle);
637 public bool Join(int millisecondsTimeout)
639 if (millisecondsTimeout != Timeout.Infinite && millisecondsTimeout < 0)
640 throw new ArgumentException ("Timeout less than zero", "millisecondsTimeout");
642 return Join_internal(millisecondsTimeout, system_thread_handle);
645 public bool Join(TimeSpan timeout)
647 // LAMESPEC: says to throw ArgumentException too
648 int ms=Convert.ToInt32(timeout.TotalMilliseconds);
650 if(ms < 0 || ms > Int32.MaxValue) {
651 throw new ArgumentOutOfRangeException("timeout out of range");
653 return Join_internal(ms, system_thread_handle);
657 [MethodImplAttribute(MethodImplOptions.InternalCall)]
658 public extern static void MemoryBarrier ();
660 [MethodImplAttribute(MethodImplOptions.InternalCall)]
661 private extern void Resume_internal();
666 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
667 public void Resume ()
672 [MethodImplAttribute (MethodImplOptions.InternalCall)]
673 private extern static void SpinWait_internal (int iterations);
677 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
679 public static void SpinWait (int iterations)
681 SpinWait_internal (iterations);
684 public void Start() {
685 // propagate informations from the original thread to the new thread
687 if (!ExecutionContext.IsFlowSuppressed ())
688 _ec = ExecutionContext.Capture ();
690 // before 2.0 this was only used for security (mostly CAS) so we
691 // do this only if the security manager is active
692 if (SecurityManager.SecurityEnabled)
693 _ec = ExecutionContext.Capture ();
695 if (CurrentThread._principal != null)
696 _principal = CurrentThread._principal;
698 // Thread_internal creates and starts the new thread,
699 if (Thread_internal(threadstart) == (IntPtr) 0)
700 throw new SystemException ("Thread creation failed.");
703 [MethodImplAttribute(MethodImplOptions.InternalCall)]
704 private extern void Suspend_internal();
709 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
710 public void Suspend ()
715 // Closes the system thread handle
716 [MethodImplAttribute(MethodImplOptions.InternalCall)]
717 private extern void Thread_free_internal(IntPtr handle);
720 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
723 // Free up the handle
724 if (system_thread_handle != (IntPtr) 0)
725 Thread_free_internal(system_thread_handle);
728 [MethodImplAttribute (MethodImplOptions.InternalCall)]
729 extern private void SetState (ThreadState set);
731 [MethodImplAttribute (MethodImplOptions.InternalCall)]
732 extern private void ClrState (ThreadState clr);
734 [MethodImplAttribute (MethodImplOptions.InternalCall)]
735 extern private ThreadState GetState ();
739 [MethodImplAttribute (MethodImplOptions.InternalCall)]
740 extern public static byte VolatileRead (ref byte address);
742 [MethodImplAttribute (MethodImplOptions.InternalCall)]
743 extern public static double VolatileRead (ref double address);
745 [MethodImplAttribute (MethodImplOptions.InternalCall)]
746 extern public static short VolatileRead (ref short address);
748 [MethodImplAttribute (MethodImplOptions.InternalCall)]
749 extern public static int VolatileRead (ref int address);
751 [MethodImplAttribute (MethodImplOptions.InternalCall)]
752 extern public static long VolatileRead (ref long address);
754 [MethodImplAttribute (MethodImplOptions.InternalCall)]
755 extern public static IntPtr VolatileRead (ref IntPtr address);
757 [MethodImplAttribute (MethodImplOptions.InternalCall)]
758 extern public static object VolatileRead (ref object address);
760 [CLSCompliant(false)]
761 [MethodImplAttribute (MethodImplOptions.InternalCall)]
762 extern public static sbyte VolatileRead (ref sbyte address);
764 [MethodImplAttribute (MethodImplOptions.InternalCall)]
765 extern public static float VolatileRead (ref float address);
767 [CLSCompliant (false)]
768 [MethodImplAttribute (MethodImplOptions.InternalCall)]
769 extern public static ushort VolatileRead (ref ushort address);
771 [CLSCompliant (false)]
772 [MethodImplAttribute (MethodImplOptions.InternalCall)]
773 extern public static uint VolatileRead (ref uint address);
775 [CLSCompliant (false)]
776 [MethodImplAttribute (MethodImplOptions.InternalCall)]
777 extern public static ulong VolatileRead (ref ulong address);
779 [CLSCompliant (false)]
780 [MethodImplAttribute (MethodImplOptions.InternalCall)]
781 extern public static UIntPtr VolatileRead (ref UIntPtr address);
783 [MethodImplAttribute (MethodImplOptions.InternalCall)]
784 extern public static void VolatileWrite (ref byte address, byte value);
786 [MethodImplAttribute (MethodImplOptions.InternalCall)]
787 extern public static void VolatileWrite (ref double address, double value);
789 [MethodImplAttribute (MethodImplOptions.InternalCall)]
790 extern public static void VolatileWrite (ref short address, short value);
792 [MethodImplAttribute (MethodImplOptions.InternalCall)]
793 extern public static void VolatileWrite (ref int address, int value);
795 [MethodImplAttribute (MethodImplOptions.InternalCall)]
796 extern public static void VolatileWrite (ref long address, long value);
798 [MethodImplAttribute (MethodImplOptions.InternalCall)]
799 extern public static void VolatileWrite (ref IntPtr address, IntPtr value);
801 [MethodImplAttribute (MethodImplOptions.InternalCall)]
802 extern public static void VolatileWrite (ref object address, object value);
804 [CLSCompliant(false)]
805 [MethodImplAttribute (MethodImplOptions.InternalCall)]
806 extern public static void VolatileWrite (ref sbyte address, sbyte value);
808 [MethodImplAttribute (MethodImplOptions.InternalCall)]
809 extern public static void VolatileWrite (ref float address, float value);
811 [CLSCompliant (false)]
812 [MethodImplAttribute (MethodImplOptions.InternalCall)]
813 extern public static void VolatileWrite (ref ushort address, ushort value);
815 [CLSCompliant (false)]
816 [MethodImplAttribute (MethodImplOptions.InternalCall)]
817 extern public static void VolatileWrite (ref uint address, uint value);
819 [CLSCompliant (false)]
820 [MethodImplAttribute (MethodImplOptions.InternalCall)]
821 extern public static void VolatileWrite (ref ulong address, ulong value);
823 [CLSCompliant (false)]
824 [MethodImplAttribute (MethodImplOptions.InternalCall)]
825 extern public static void VolatileWrite (ref UIntPtr address, UIntPtr value);
830 public Thread (ThreadStart start, int maxStackSize)
833 throw new ArgumentNullException ("start");
834 if (maxStackSize < 131072)
835 throw new ArgumentException ("< 128 kb", "maxStackSize");
838 stack_size = maxStackSize;
842 public Thread (ParameterizedThreadStart start)
845 throw new ArgumentNullException ("start");
851 public Thread (ParameterizedThreadStart start, int maxStackSize)
854 throw new ArgumentNullException ("start");
855 if (maxStackSize < 131072)
856 throw new ArgumentException ("< 128 kb", "maxStackSize");
859 stack_size = maxStackSize;
863 [MonoTODO ("limited to CompressedStack support")]
864 // FIXME: We share the _ec object between appdomains
865 public ExecutionContext ExecutionContext {
866 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
869 _ec = new ExecutionContext ();
874 public int ManagedThreadId {
875 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
877 if (managed_id == 0) {
878 int new_managed_id = GetNewManagedId ();
880 Interlocked.CompareExchange (ref managed_id, new_managed_id, 0);
887 [MonoTODO("Not implemented")]
888 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
889 public static void BeginCriticalRegion ()
891 throw new NotImplementedException ();
894 [MonoTODO("Not implemented")]
895 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
896 public static void EndCriticalRegion ()
898 throw new NotImplementedException ();
901 [MonoTODO("Not implemented")]
902 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
903 public static void BeginThreadAffinity ()
905 throw new NotImplementedException ();
908 [MonoTODO("Not implemented")]
909 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
910 public static void EndThreadAffinity ()
912 throw new NotImplementedException ();
915 public ApartmentState GetApartmentState ()
917 return (ApartmentState)apartment_state;
920 public void SetApartmentState (ApartmentState state)
922 if (!TrySetApartmentState (state))
923 throw new InvalidOperationException ("Failed to set the specified COM apartment state.");
926 public bool TrySetApartmentState (ApartmentState state)
928 if ((ThreadState & ThreadState.Unstarted) == 0)
929 throw new ThreadStateException ("Thread was in an invalid state for the operation being executed.");
931 if ((ApartmentState)apartment_state != ApartmentState.Unknown)
934 apartment_state = (byte)state;
940 public override int GetHashCode ()
942 return ManagedThreadId;
945 public void Start (object parameter)
947 start_obj = parameter;
951 internal ExecutionContext ExecutionContext {
954 _ec = new ExecutionContext ();
960 // NOTE: This method doesn't show in the class library status page because
961 // it cannot be "found" with the StrongNameIdentityPermission for ECMA key.
963 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
964 [StrongNameIdentityPermission (SecurityAction.LinkDemand, PublicKey="00000000000000000400000000000000")]
966 [Obsolete ("see CompressedStack class")]
973 CompressedStack GetCompressedStack ()
975 // Note: returns null if no CompressedStack has been set.
976 // However CompressedStack.GetCompressedStack returns an
977 // (empty?) CompressedStack instance.
978 CompressedStack cs = ExecutionContext.SecurityContext.CompressedStack;
979 return ((cs == null) || cs.IsEmpty ()) ? null : cs.CreateCopy ();
982 // NOTE: This method doesn't show in the class library status page because
983 // it cannot be "found" with the StrongNameIdentityPermission for ECMA key.
985 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
986 [StrongNameIdentityPermission (SecurityAction.LinkDemand, PublicKey="00000000000000000400000000000000")]
988 [Obsolete ("see CompressedStack class")]
995 void SetCompressedStack (CompressedStack stack)
997 ExecutionContext.SecurityContext.CompressedStack = stack;
1001 void _Thread.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1003 throw new NotImplementedException ();
1006 void _Thread.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1008 throw new NotImplementedException ();
1011 void _Thread.GetTypeInfoCount (out uint pcTInfo)
1013 throw new NotImplementedException ();
1016 void _Thread.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
1017 IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1019 throw new NotImplementedException ();