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.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)]
125 public sealed class Thread : CriticalFinalizerObject, _Thread {
126 #pragma warning disable 414
127 #region Sync with metadata/object-internals.h
128 private InternalThread internal_thread;
130 private ExecutionContext ec_to_set;
132 #pragma warning restore 414
134 IPrincipal principal;
135 int principal_version;
136 CultureInfo current_culture;
137 CultureInfo current_ui_culture;
139 // the name of local_slots, current_thread and _ec is
140 // important because they are used by the runtime.
142 static object[] local_slots;
145 static Thread current_thread;
147 /* The actual ExecutionContext of the thread. It's
148 ThreadStatic so that it's not shared between
151 static ExecutionContext _ec;
153 // can be both a ThreadStart and a ParameterizedThreadStart
154 private MulticastDelegate threadstart;
155 //private string thread_name=null;
157 [MethodImplAttribute(MethodImplOptions.InternalCall)]
158 private extern void ConstructInternalThread ();
160 private InternalThread Internal {
162 if (internal_thread == null)
163 ConstructInternalThread ();
164 return internal_thread;
168 public static Context CurrentContext {
169 [SecurityPermission (SecurityAction.LinkDemand, Infrastructure=true)]
171 return(AppDomain.InternalGetContext ());
176 * These two methods return an array in the target
177 * domain with the same content as the argument. If
178 * the argument is already in the target domain, then
179 * the argument is returned, otherwise a copy.
181 [MethodImplAttribute(MethodImplOptions.InternalCall)]
182 private extern static byte[] ByteArrayToRootDomain (byte[] arr);
184 [MethodImplAttribute(MethodImplOptions.InternalCall)]
185 private extern static byte[] ByteArrayToCurrentDomain (byte[] arr);
188 static void DeserializePrincipal (Thread th)
190 MemoryStream ms = new MemoryStream (ByteArrayToCurrentDomain (th.Internal._serialized_principal));
191 int type = ms.ReadByte ();
193 BinaryFormatter bf = new BinaryFormatter ();
194 th.principal = (IPrincipal) bf.Deserialize (ms);
195 th.principal_version = th.Internal._serialized_principal_version;
196 } else if (type == 1) {
197 BinaryReader reader = new BinaryReader (ms);
198 string name = reader.ReadString ();
199 string auth_type = reader.ReadString ();
200 int n_roles = reader.ReadInt32 ();
201 string [] roles = null;
203 roles = new string [n_roles];
204 for (int i = 0; i < n_roles; i++)
205 roles [i] = reader.ReadString ();
207 th.principal = new GenericPrincipal (new GenericIdentity (name, auth_type), roles);
208 } else if (type == 2 || type == 3) {
209 string [] roles = type == 2 ? null : new string [0];
210 th.principal = new GenericPrincipal (new GenericIdentity ("", ""), roles);
214 static void SerializePrincipal (Thread th, IPrincipal value)
216 MemoryStream ms = new MemoryStream ();
218 if (value.GetType () == typeof (GenericPrincipal)) {
219 GenericPrincipal gp = (GenericPrincipal) value;
220 if (gp.Identity != null && gp.Identity.GetType () == typeof (GenericIdentity)) {
221 GenericIdentity id = (GenericIdentity) gp.Identity;
222 if (id.Name == "" && id.AuthenticationType == "") {
223 if (gp.Roles == null) {
226 } else if (gp.Roles.Length == 0) {
232 BinaryWriter br = new BinaryWriter (ms);
233 br.Write (gp.Identity.Name);
234 br.Write (gp.Identity.AuthenticationType);
235 string [] roles = gp.Roles;
237 br.Write ((int) (-1));
239 br.Write (roles.Length);
240 foreach (string s in roles) {
251 BinaryFormatter bf = new BinaryFormatter ();
253 bf.Serialize (ms, value);
256 th.Internal._serialized_principal = ByteArrayToRootDomain (ms.ToArray ());
259 public static IPrincipal CurrentPrincipal {
261 Thread th = CurrentThread;
263 if (th.principal_version != th.Internal._serialized_principal_version)
266 if (th.principal != null)
269 if (th.Internal._serialized_principal != null) {
271 DeserializePrincipal (th);
276 th.principal = GetDomain ().DefaultPrincipal;
277 th.principal_version = th.Internal._serialized_principal_version;
280 [SecurityPermission (SecurityAction.Demand, ControlPrincipal = true)]
282 Thread th = CurrentThread;
284 if (value != GetDomain ().DefaultPrincipal) {
285 ++th.Internal._serialized_principal_version;
287 SerializePrincipal (th, value);
288 } catch (Exception) {
289 th.Internal._serialized_principal = null;
291 th.principal_version = th.Internal._serialized_principal_version;
293 th.Internal._serialized_principal = null;
296 th.principal = value;
301 // Looks up the object associated with the current thread
302 // this is called by the JIT directly, too
303 [MethodImplAttribute(MethodImplOptions.InternalCall)]
304 private extern static InternalThread CurrentInternalThread_internal();
306 [MethodImplAttribute(MethodImplOptions.InternalCall)]
307 internal extern static uint AllocTlsData (Type type);
309 [MethodImplAttribute(MethodImplOptions.InternalCall)]
310 internal extern static void DestroyTlsData (uint offset);
312 public static Thread CurrentThread {
313 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
315 if (current_thread == null)
316 current_thread = new Thread (CurrentInternalThread_internal ());
317 return current_thread;
321 internal static int CurrentThreadId {
323 return (int)(CurrentThread.internal_thread.thread_id);
328 // Stores a hash keyed by strings of LocalDataStoreSlot objects
329 static Hashtable datastorehash;
330 private static object datastore_lock = new object ();
332 private static void InitDataStoreHash () {
333 lock (datastore_lock) {
334 if (datastorehash == null) {
335 datastorehash = Hashtable.Synchronized(new Hashtable());
340 public static LocalDataStoreSlot AllocateNamedDataSlot (string name) {
341 lock (datastore_lock) {
342 if (datastorehash == null)
343 InitDataStoreHash ();
344 LocalDataStoreSlot slot = (LocalDataStoreSlot)datastorehash [name];
346 // This exception isnt documented (of
347 // course) but .net throws it
348 throw new ArgumentException("Named data slot already added");
351 slot = AllocateDataSlot ();
353 datastorehash.Add (name, slot);
359 public static void FreeNamedDataSlot (string name) {
360 lock (datastore_lock) {
361 if (datastorehash == null)
362 InitDataStoreHash ();
363 LocalDataStoreSlot slot = (LocalDataStoreSlot)datastorehash [name];
366 datastorehash.Remove (slot);
371 public static LocalDataStoreSlot AllocateDataSlot () {
372 return new LocalDataStoreSlot (true);
375 public static object GetData (LocalDataStoreSlot slot) {
376 object[] slots = local_slots;
378 throw new ArgumentNullException ("slot");
379 if (slots != null && slot.slot < slots.Length)
380 return slots [slot.slot];
384 public static void SetData (LocalDataStoreSlot slot, object data) {
385 object[] slots = local_slots;
387 throw new ArgumentNullException ("slot");
389 slots = new object [slot.slot + 2];
391 } else if (slot.slot >= slots.Length) {
392 object[] nslots = new object [slot.slot + 2];
393 slots.CopyTo (nslots, 0);
397 slots [slot.slot] = data;
400 [MethodImplAttribute (MethodImplOptions.InternalCall)]
401 internal extern static void FreeLocalSlotValues (int slot, bool thread_local);
403 public static LocalDataStoreSlot GetNamedDataSlot(string name) {
404 lock (datastore_lock) {
405 if (datastorehash == null)
406 InitDataStoreHash ();
407 LocalDataStoreSlot slot=(LocalDataStoreSlot)datastorehash[name];
410 slot=AllocateNamedDataSlot(name);
418 public static AppDomain GetDomain() {
419 return AppDomain.CurrentDomain;
422 [MethodImplAttribute(MethodImplOptions.InternalCall)]
423 public extern static int GetDomainID();
425 [MethodImplAttribute(MethodImplOptions.InternalCall)]
426 private extern static void ResetAbort_internal();
428 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
429 public static void ResetAbort ()
431 ResetAbort_internal ();
434 #if NET_4_0 || MOBILE
435 [HostProtectionAttribute (SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)]
436 [MethodImplAttribute(MethodImplOptions.InternalCall)]
437 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
438 public extern static bool Yield ();
442 [MethodImplAttribute(MethodImplOptions.InternalCall)]
443 private extern static void Sleep_internal(int ms);
445 public static void Sleep (int millisecondsTimeout)
447 if (millisecondsTimeout < Timeout.Infinite)
448 throw new ArgumentOutOfRangeException ("millisecondsTimeout", "Negative timeout");
450 Sleep_internal (millisecondsTimeout);
453 public static void Sleep (TimeSpan timeout)
455 long ms = (long) timeout.TotalMilliseconds;
456 if (ms < Timeout.Infinite || ms > Int32.MaxValue)
457 throw new ArgumentOutOfRangeException ("timeout", "timeout out of range");
459 Sleep_internal ((int) ms);
462 // Returns the system thread handle
463 [MethodImplAttribute(MethodImplOptions.InternalCall)]
464 private extern IntPtr Thread_internal (MulticastDelegate start);
466 public Thread(ThreadStart start) {
468 throw new ArgumentNullException("Null ThreadStart");
473 private Thread (InternalThread it) {
474 internal_thread = it;
477 // part of ".NETPortable,Version=v4.0,Profile=Profile3" i.e. FX4 and SL4
478 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
484 [Obsolete ("Deprecated in favor of GetApartmentState, SetApartmentState and TrySetApartmentState.")]
485 public ApartmentState ApartmentState {
487 if ((ThreadState & ThreadState.Stopped) != 0)
488 throw new ThreadStateException ("Thread is dead; state can not be accessed.");
490 return (ApartmentState)Internal.apartment_state;
494 TrySetApartmentState (value);
499 //[MethodImplAttribute (MethodImplOptions.InternalCall)]
500 //private static extern int current_lcid ();
502 public CultureInfo CurrentCulture {
504 CultureInfo culture = current_culture;
508 current_culture = culture = CultureInfo.ConstructCurrentCulture ();
509 NumberFormatter.SetThreadCurrentCulture (culture);
513 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
516 throw new ArgumentNullException ("value");
518 value.CheckNeutral ();
519 current_culture = value;
520 NumberFormatter.SetThreadCurrentCulture (value);
524 public CultureInfo CurrentUICulture {
526 CultureInfo culture = current_ui_culture;
530 current_ui_culture = culture = CultureInfo.ConstructCurrentUICulture ();
536 throw new ArgumentNullException ("value");
537 current_ui_culture = value;
541 public bool IsThreadPoolThread {
543 return IsThreadPoolThreadInternal;
547 internal bool IsThreadPoolThreadInternal {
549 return Internal.threadpool_thread;
552 Internal.threadpool_thread = value;
556 public bool IsAlive {
558 ThreadState curstate = GetState (Internal);
560 if((curstate & ThreadState.Aborted) != 0 ||
561 (curstate & ThreadState.Stopped) != 0 ||
562 (curstate & ThreadState.Unstarted) != 0) {
570 public bool IsBackground {
572 ThreadState thread_state = GetState (Internal);
573 if ((thread_state & ThreadState.Stopped) != 0)
574 throw new ThreadStateException ("Thread is dead; state can not be accessed.");
576 return (thread_state & ThreadState.Background) != 0;
581 SetState (Internal, ThreadState.Background);
583 ClrState (Internal, ThreadState.Background);
588 [MethodImplAttribute(MethodImplOptions.InternalCall)]
589 private extern static string GetName_internal (InternalThread thread);
591 [MethodImplAttribute(MethodImplOptions.InternalCall)]
592 private extern static void SetName_internal (InternalThread thread, String name);
595 * The thread name must be shared by appdomains, so it is stored in
601 return GetName_internal (Internal);
605 SetName_internal (Internal, value);
610 public ThreadPriority Priority {
612 return(ThreadPriority.Lowest);
616 // FIXME: Implement setter.
621 public ThreadState ThreadState {
623 return GetState (Internal);
627 [MethodImplAttribute(MethodImplOptions.InternalCall)]
628 private extern static void Abort_internal (InternalThread thread, object stateInfo);
630 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
633 Abort_internal (Internal, null);
637 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
638 public void Abort (object stateInfo)
640 Abort_internal (Internal, stateInfo);
643 [MethodImplAttribute(MethodImplOptions.InternalCall)]
644 internal extern object GetAbortExceptionState ();
646 [MethodImplAttribute (MethodImplOptions.InternalCall)]
647 private extern static void Interrupt_internal (InternalThread thread);
649 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
650 public void Interrupt ()
652 Interrupt_internal (Internal);
656 // The current thread joins with 'this'. Set ms to 0 to block
657 // until this actually exits.
658 [MethodImplAttribute(MethodImplOptions.InternalCall)]
659 private extern static bool Join_internal(InternalThread thread, int ms, IntPtr handle);
663 Join_internal(Internal, Timeout.Infinite, Internal.system_thread_handle);
666 public bool Join(int millisecondsTimeout)
668 if (millisecondsTimeout < Timeout.Infinite)
669 throw new ArgumentOutOfRangeException ("millisecondsTimeout", "Timeout less than zero");
671 return Join_internal (Internal, millisecondsTimeout, Internal.system_thread_handle);
675 public bool Join(TimeSpan timeout)
677 long ms = (long) timeout.TotalMilliseconds;
678 if (ms < Timeout.Infinite || ms > Int32.MaxValue)
679 throw new ArgumentOutOfRangeException ("timeout", "timeout out of range");
681 return Join_internal (Internal, (int) ms, Internal.system_thread_handle);
685 [MethodImplAttribute(MethodImplOptions.InternalCall)]
686 public extern static void MemoryBarrier ();
689 [MethodImplAttribute(MethodImplOptions.InternalCall)]
690 private extern void Resume_internal();
693 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
694 public void Resume ()
700 [MethodImplAttribute (MethodImplOptions.InternalCall)]
701 private extern static void SpinWait_nop ();
704 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
705 public static void SpinWait (int iterations)
709 while (iterations-- > 0)
716 private void StartInternal ()
718 current_thread = this;
721 if (threadstart is ThreadStart) {
722 ((ThreadStart) threadstart) ();
724 ((ParameterizedThreadStart) threadstart) (start_obj);
726 } catch (ThreadAbortException) {
728 } catch (Exception ex) {
729 MoonlightUnhandledException (ex);
733 static MethodInfo moonlight_unhandled_exception = null;
735 static internal void MoonlightUnhandledException (Exception e)
738 if (moonlight_unhandled_exception == null) {
739 var assembly = System.Reflection.Assembly.Load ("System.Windows, Version=2.0.5.0, Culture=Neutral, PublicKeyToken=7cec85d7bea7798e");
740 var application = assembly.GetType ("System.Windows.Application");
741 moonlight_unhandled_exception = application.GetMethod ("OnUnhandledException",
742 System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
744 moonlight_unhandled_exception.Invoke (null, new object [] { null, e });
748 Console.WriteLine ("Unexpected exception while trying to report unhandled application exception: {0}", e);
754 static ConstructorInfo nsautoreleasepool_ctor;
756 IDisposable GetNSAutoreleasePool ()
758 if (nsautoreleasepool_ctor == null) {
759 Type t = Type.GetType ("MonoTouch.Foundation.NSAutoreleasePool, monotouch, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null");
760 nsautoreleasepool_ctor = t.GetConstructor (Type.EmptyTypes);
762 return (IDisposable) nsautoreleasepool_ctor.Invoke (null);
765 private void StartInternal ()
767 using (var pool = GetNSAutoreleasePool ()) {
768 current_thread = this;
770 if (threadstart is ThreadStart) {
771 ((ThreadStart) threadstart) ();
773 ((ParameterizedThreadStart) threadstart) (start_obj);
778 private void StartInternal ()
780 current_thread = this;
782 if (threadstart is ThreadStart) {
783 ((ThreadStart) threadstart) ();
785 ((ParameterizedThreadStart) threadstart) (start_obj);
789 public void Start() {
790 // propagate informations from the original thread to the new thread
791 if (!ExecutionContext.IsFlowSuppressed ())
792 ec_to_set = ExecutionContext.Capture ();
793 Internal._serialized_principal = CurrentThread.Internal._serialized_principal;
795 // Thread_internal creates and starts the new thread,
796 if (Thread_internal((ThreadStart) StartInternal) == (IntPtr) 0)
797 throw new SystemException ("Thread creation failed.");
801 [MethodImplAttribute(MethodImplOptions.InternalCall)]
802 private extern static void Suspend_internal(InternalThread thread);
805 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
806 public void Suspend ()
808 Suspend_internal (Internal);
812 [MethodImplAttribute (MethodImplOptions.InternalCall)]
813 extern private static void SetState (InternalThread thread, ThreadState set);
815 [MethodImplAttribute (MethodImplOptions.InternalCall)]
816 extern private static void ClrState (InternalThread thread, ThreadState clr);
818 [MethodImplAttribute (MethodImplOptions.InternalCall)]
819 extern private static ThreadState GetState (InternalThread thread);
821 [MethodImplAttribute (MethodImplOptions.InternalCall)]
822 extern public static byte VolatileRead (ref byte address);
824 [MethodImplAttribute (MethodImplOptions.InternalCall)]
825 extern public static double VolatileRead (ref double address);
827 [MethodImplAttribute (MethodImplOptions.InternalCall)]
828 extern public static short VolatileRead (ref short address);
830 [MethodImplAttribute (MethodImplOptions.InternalCall)]
831 extern public static int VolatileRead (ref int address);
833 [MethodImplAttribute (MethodImplOptions.InternalCall)]
834 extern public static long VolatileRead (ref long address);
836 [MethodImplAttribute (MethodImplOptions.InternalCall)]
837 extern public static IntPtr VolatileRead (ref IntPtr address);
839 [MethodImplAttribute (MethodImplOptions.InternalCall)]
840 extern public static object VolatileRead (ref object address);
842 [CLSCompliant(false)]
843 [MethodImplAttribute (MethodImplOptions.InternalCall)]
844 extern public static sbyte VolatileRead (ref sbyte address);
846 [MethodImplAttribute (MethodImplOptions.InternalCall)]
847 extern public static float VolatileRead (ref float address);
849 [CLSCompliant (false)]
850 [MethodImplAttribute (MethodImplOptions.InternalCall)]
851 extern public static ushort VolatileRead (ref ushort address);
853 [CLSCompliant (false)]
854 [MethodImplAttribute (MethodImplOptions.InternalCall)]
855 extern public static uint VolatileRead (ref uint address);
857 [CLSCompliant (false)]
858 [MethodImplAttribute (MethodImplOptions.InternalCall)]
859 extern public static ulong VolatileRead (ref ulong address);
861 [CLSCompliant (false)]
862 [MethodImplAttribute (MethodImplOptions.InternalCall)]
863 extern public static UIntPtr VolatileRead (ref UIntPtr address);
865 [MethodImplAttribute (MethodImplOptions.InternalCall)]
866 extern public static void VolatileWrite (ref byte address, byte value);
868 [MethodImplAttribute (MethodImplOptions.InternalCall)]
869 extern public static void VolatileWrite (ref double address, double value);
871 [MethodImplAttribute (MethodImplOptions.InternalCall)]
872 extern public static void VolatileWrite (ref short address, short value);
874 [MethodImplAttribute (MethodImplOptions.InternalCall)]
875 extern public static void VolatileWrite (ref int address, int value);
877 [MethodImplAttribute (MethodImplOptions.InternalCall)]
878 extern public static void VolatileWrite (ref long address, long value);
880 [MethodImplAttribute (MethodImplOptions.InternalCall)]
881 extern public static void VolatileWrite (ref IntPtr address, IntPtr value);
883 [MethodImplAttribute (MethodImplOptions.InternalCall)]
884 extern public static void VolatileWrite (ref object address, object value);
886 [CLSCompliant(false)]
887 [MethodImplAttribute (MethodImplOptions.InternalCall)]
888 extern public static void VolatileWrite (ref sbyte address, sbyte value);
890 [MethodImplAttribute (MethodImplOptions.InternalCall)]
891 extern public static void VolatileWrite (ref float address, float value);
893 [CLSCompliant (false)]
894 [MethodImplAttribute (MethodImplOptions.InternalCall)]
895 extern public static void VolatileWrite (ref ushort address, ushort value);
897 [CLSCompliant (false)]
898 [MethodImplAttribute (MethodImplOptions.InternalCall)]
899 extern public static void VolatileWrite (ref uint address, uint value);
901 [CLSCompliant (false)]
902 [MethodImplAttribute (MethodImplOptions.InternalCall)]
903 extern public static void VolatileWrite (ref ulong address, ulong value);
905 [CLSCompliant (false)]
906 [MethodImplAttribute (MethodImplOptions.InternalCall)]
907 extern public static void VolatileWrite (ref UIntPtr address, UIntPtr value);
910 static int CheckStackSize (int maxStackSize)
912 if (maxStackSize < 0)
913 throw new ArgumentOutOfRangeException ("less than zero", "maxStackSize");
915 if (maxStackSize < 131072) // make sure stack is at least 128k big
918 int page_size = Environment.GetPageSize ();
920 if ((maxStackSize % page_size) != 0) // round up to a divisible of page size
921 maxStackSize = (maxStackSize / (page_size - 1)) * page_size;
923 int default_stack_size = (IntPtr.Size / 4) * 1024 * 1024; // from wthreads.c
925 if (maxStackSize > default_stack_size)
926 return default_stack_size;
931 public Thread (ThreadStart start, int maxStackSize)
934 throw new ArgumentNullException ("start");
937 Internal.stack_size = CheckStackSize (maxStackSize);;
940 public Thread (ParameterizedThreadStart start)
943 throw new ArgumentNullException ("start");
948 public Thread (ParameterizedThreadStart start, int maxStackSize)
951 throw new ArgumentNullException ("start");
954 Internal.stack_size = CheckStackSize (maxStackSize);
957 [MonoTODO ("limited to CompressedStack support")]
958 public ExecutionContext ExecutionContext {
959 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
962 _ec = new ExecutionContext ();
967 public int ManagedThreadId {
968 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
970 return Internal.managed_id;
974 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
975 public static void BeginCriticalRegion ()
977 CurrentThread.Internal.critical_region_level++;
980 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
981 public static void EndCriticalRegion ()
983 CurrentThread.Internal.critical_region_level--;
986 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
987 public static void BeginThreadAffinity ()
989 // Managed and native threads are currently bound together.
992 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
993 public static void EndThreadAffinity ()
995 // Managed and native threads are currently bound together.
999 public ApartmentState GetApartmentState ()
1001 return (ApartmentState)Internal.apartment_state;
1004 public void SetApartmentState (ApartmentState state)
1006 if (!TrySetApartmentState (state))
1007 throw new InvalidOperationException ("Failed to set the specified COM apartment state.");
1010 public bool TrySetApartmentState (ApartmentState state)
1012 /* Only throw this exception when changing the
1013 * state of another thread. See bug 324338
1015 if ((this != CurrentThread) &&
1016 (ThreadState & ThreadState.Unstarted) == 0)
1017 throw new ThreadStateException ("Thread was in an invalid state for the operation being executed.");
1019 if ((ApartmentState)Internal.apartment_state != ApartmentState.Unknown)
1022 Internal.apartment_state = (byte)state;
1028 [ComVisible (false)]
1029 public override int GetHashCode ()
1031 return ManagedThreadId;
1034 public void Start (object parameter)
1036 start_obj = parameter;
1041 // NOTE: This method doesn't show in the class library status page because
1042 // it cannot be "found" with the StrongNameIdentityPermission for ECMA key.
1044 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
1045 [StrongNameIdentityPermission (SecurityAction.LinkDemand, PublicKey="00000000000000000400000000000000")]
1046 [Obsolete ("see CompressedStack class")]
1048 CompressedStack GetCompressedStack ()
1050 // Note: returns null if no CompressedStack has been set.
1051 // However CompressedStack.GetCompressedStack returns an
1052 // (empty?) CompressedStack instance.
1053 CompressedStack cs = ExecutionContext.SecurityContext.CompressedStack;
1054 return ((cs == null) || cs.IsEmpty ()) ? null : cs.CreateCopy ();
1057 // NOTE: This method doesn't show in the class library status page because
1058 // it cannot be "found" with the StrongNameIdentityPermission for ECMA key.
1060 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
1061 [StrongNameIdentityPermission (SecurityAction.LinkDemand, PublicKey="00000000000000000400000000000000")]
1062 [Obsolete ("see CompressedStack class")]
1064 void SetCompressedStack (CompressedStack stack)
1066 ExecutionContext.SecurityContext.CompressedStack = stack;
1071 void _Thread.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1073 throw new NotImplementedException ();
1076 void _Thread.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1078 throw new NotImplementedException ();
1081 void _Thread.GetTypeInfoCount (out uint pcTInfo)
1083 throw new NotImplementedException ();
1086 void _Thread.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
1087 IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1089 throw new NotImplementedException ();