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; /*FIXME remove this on the next corlib version bump*/
55 private IntPtr unused0;
56 internal bool threadpool_thread;
57 /* accessed only from unmanaged code */
60 private ThreadState state;
61 private object abort_exc;
62 private int abort_state_handle;
63 /* thread_id is only accessed from unmanaged code */
64 internal Int64 thread_id;
66 /* start_notify is used by the runtime to signal that Start()
69 private IntPtr start_notify;
70 private IntPtr stack_ptr;
71 private UIntPtr static_data; /* GC-tracked */
72 private IntPtr jit_data;
73 private IntPtr runtime_thread_info;
74 /* current System.Runtime.Remoting.Contexts.Context instance
75 keep as an object to avoid triggering its class constructor when not needed */
76 private object current_appcontext;
77 internal int stack_size;
78 private IntPtr appdomain_refs;
79 private int interruption_requested;
80 private IntPtr suspend_event;
81 private IntPtr suspended_event;
82 private IntPtr resume_event;
83 private IntPtr synch_cs;
84 private bool thread_dump_requested;
85 private IntPtr end_stack;
86 private bool thread_interrupt_requested;
87 internal byte apartment_state;
88 internal volatile int critical_region_level;
90 private IntPtr manage_callback;
91 private object pending_exception;
92 /* This is the ExecutionContext that will be set by
93 start_wrapper() in the runtime. */
94 private ExecutionContext ec_to_set;
96 private IntPtr interrupt_on_stop;
99 * These fields are used to avoid having to increment corlib versions
100 * when a new field is added to the unmanaged MonoThread structure.
102 private IntPtr unused3;
103 private IntPtr unused4;
104 private IntPtr unused5;
105 internal int managed_id;
106 int ignore_next_signal;
108 #pragma warning restore 169, 414, 649
110 internal byte[] _serialized_principal;
111 internal int _serialized_principal_version;
113 // Closes the system thread handle
114 [MethodImplAttribute(MethodImplOptions.InternalCall)]
115 private extern void Thread_free_internal(IntPtr handle);
117 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
119 Thread_free_internal(system_thread_handle);
123 [ClassInterface (ClassInterfaceType.None)]
125 [ComDefaultInterface (typeof (_Thread))]
126 [StructLayout (LayoutKind.Sequential)]
127 public sealed class Thread : CriticalFinalizerObject, _Thread {
128 #pragma warning disable 414
129 #region Sync with metadata/object-internals.h
130 private InternalThread internal_thread;
132 private ExecutionContext ec_to_set;
134 #pragma warning restore 414
136 IPrincipal principal;
137 int principal_version;
138 CultureInfo current_culture;
139 CultureInfo current_ui_culture;
141 // the name of local_slots, current_thread and _ec is
142 // important because they are used by the runtime.
144 static object[] local_slots;
147 static Thread current_thread;
149 /* The actual ExecutionContext of the thread. It's
150 ThreadStatic so that it's not shared between
153 static ExecutionContext _ec;
155 // can be both a ThreadStart and a ParameterizedThreadStart
156 private MulticastDelegate threadstart;
157 //private string thread_name=null;
159 [MethodImplAttribute(MethodImplOptions.InternalCall)]
160 private extern void ConstructInternalThread ();
162 private InternalThread Internal {
164 if (internal_thread == null)
165 ConstructInternalThread ();
166 return internal_thread;
170 public static Context CurrentContext {
171 [SecurityPermission (SecurityAction.LinkDemand, Infrastructure=true)]
173 return(AppDomain.InternalGetContext ());
178 * These two methods return an array in the target
179 * domain with the same content as the argument. If
180 * the argument is already in the target domain, then
181 * the argument is returned, otherwise a copy.
183 [MethodImplAttribute(MethodImplOptions.InternalCall)]
184 private extern static byte[] ByteArrayToRootDomain (byte[] arr);
186 [MethodImplAttribute(MethodImplOptions.InternalCall)]
187 private extern static byte[] ByteArrayToCurrentDomain (byte[] arr);
190 static void DeserializePrincipal (Thread th)
192 MemoryStream ms = new MemoryStream (ByteArrayToCurrentDomain (th.Internal._serialized_principal));
193 int type = ms.ReadByte ();
195 BinaryFormatter bf = new BinaryFormatter ();
196 th.principal = (IPrincipal) bf.Deserialize (ms);
197 th.principal_version = th.Internal._serialized_principal_version;
198 } else if (type == 1) {
199 BinaryReader reader = new BinaryReader (ms);
200 string name = reader.ReadString ();
201 string auth_type = reader.ReadString ();
202 int n_roles = reader.ReadInt32 ();
203 string [] roles = null;
205 roles = new string [n_roles];
206 for (int i = 0; i < n_roles; i++)
207 roles [i] = reader.ReadString ();
209 th.principal = new GenericPrincipal (new GenericIdentity (name, auth_type), roles);
210 } else if (type == 2 || type == 3) {
211 string [] roles = type == 2 ? null : new string [0];
212 th.principal = new GenericPrincipal (new GenericIdentity ("", ""), roles);
216 static void SerializePrincipal (Thread th, IPrincipal value)
218 MemoryStream ms = new MemoryStream ();
220 if (value.GetType () == typeof (GenericPrincipal)) {
221 GenericPrincipal gp = (GenericPrincipal) value;
222 if (gp.Identity != null && gp.Identity.GetType () == typeof (GenericIdentity)) {
223 GenericIdentity id = (GenericIdentity) gp.Identity;
224 if (id.Name == "" && id.AuthenticationType == "") {
225 if (gp.Roles == null) {
228 } else if (gp.Roles.Length == 0) {
234 BinaryWriter br = new BinaryWriter (ms);
235 br.Write (gp.Identity.Name);
236 br.Write (gp.Identity.AuthenticationType);
237 string [] roles = gp.Roles;
239 br.Write ((int) (-1));
241 br.Write (roles.Length);
242 foreach (string s in roles) {
253 BinaryFormatter bf = new BinaryFormatter ();
255 bf.Serialize (ms, value);
258 th.Internal._serialized_principal = ByteArrayToRootDomain (ms.ToArray ());
261 public static IPrincipal CurrentPrincipal {
263 Thread th = CurrentThread;
265 if (th.principal_version != th.Internal._serialized_principal_version)
268 if (th.principal != null)
271 if (th.Internal._serialized_principal != null) {
273 DeserializePrincipal (th);
278 th.principal = GetDomain ().DefaultPrincipal;
279 th.principal_version = th.Internal._serialized_principal_version;
282 [SecurityPermission (SecurityAction.Demand, ControlPrincipal = true)]
284 Thread th = CurrentThread;
286 if (value != GetDomain ().DefaultPrincipal) {
287 ++th.Internal._serialized_principal_version;
289 SerializePrincipal (th, value);
290 } catch (Exception) {
291 th.Internal._serialized_principal = null;
293 th.principal_version = th.Internal._serialized_principal_version;
295 th.Internal._serialized_principal = null;
298 th.principal = value;
303 // Looks up the object associated with the current thread
304 // this is called by the JIT directly, too
305 [MethodImplAttribute(MethodImplOptions.InternalCall)]
306 private extern static InternalThread CurrentInternalThread_internal();
308 [MethodImplAttribute(MethodImplOptions.InternalCall)]
309 internal extern static uint AllocTlsData (Type type);
311 [MethodImplAttribute(MethodImplOptions.InternalCall)]
312 internal extern static void DestroyTlsData (uint offset);
314 public static Thread CurrentThread {
315 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
317 if (current_thread == null)
318 current_thread = new Thread (CurrentInternalThread_internal ());
319 return current_thread;
323 internal static int CurrentThreadId {
325 return (int)(CurrentThread.internal_thread.thread_id);
330 // Stores a hash keyed by strings of LocalDataStoreSlot objects
331 static Hashtable datastorehash;
332 private static object datastore_lock = new object ();
334 private static void InitDataStoreHash () {
335 lock (datastore_lock) {
336 if (datastorehash == null) {
337 datastorehash = Hashtable.Synchronized(new Hashtable());
342 public static LocalDataStoreSlot AllocateNamedDataSlot (string name) {
343 lock (datastore_lock) {
344 if (datastorehash == null)
345 InitDataStoreHash ();
346 LocalDataStoreSlot slot = (LocalDataStoreSlot)datastorehash [name];
348 // This exception isnt documented (of
349 // course) but .net throws it
350 throw new ArgumentException("Named data slot already added");
353 slot = AllocateDataSlot ();
355 datastorehash.Add (name, slot);
361 public static void FreeNamedDataSlot (string name) {
362 lock (datastore_lock) {
363 if (datastorehash == null)
364 InitDataStoreHash ();
365 LocalDataStoreSlot slot = (LocalDataStoreSlot)datastorehash [name];
368 datastorehash.Remove (slot);
373 public static LocalDataStoreSlot AllocateDataSlot () {
374 return new LocalDataStoreSlot (true);
377 public static object GetData (LocalDataStoreSlot slot) {
378 object[] slots = local_slots;
380 throw new ArgumentNullException ("slot");
381 if (slots != null && slot.slot < slots.Length)
382 return slots [slot.slot];
386 public static void SetData (LocalDataStoreSlot slot, object data) {
387 object[] slots = local_slots;
389 throw new ArgumentNullException ("slot");
391 slots = new object [slot.slot + 2];
393 } else if (slot.slot >= slots.Length) {
394 object[] nslots = new object [slot.slot + 2];
395 slots.CopyTo (nslots, 0);
399 slots [slot.slot] = data;
402 [MethodImplAttribute (MethodImplOptions.InternalCall)]
403 internal extern static void FreeLocalSlotValues (int slot, bool thread_local);
405 public static LocalDataStoreSlot GetNamedDataSlot(string name) {
406 lock (datastore_lock) {
407 if (datastorehash == null)
408 InitDataStoreHash ();
409 LocalDataStoreSlot slot=(LocalDataStoreSlot)datastorehash[name];
412 slot=AllocateNamedDataSlot(name);
420 public static AppDomain GetDomain() {
421 return AppDomain.CurrentDomain;
424 [MethodImplAttribute(MethodImplOptions.InternalCall)]
425 public extern static int GetDomainID();
427 [MethodImplAttribute(MethodImplOptions.InternalCall)]
428 private extern static void ResetAbort_internal();
430 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
431 public static void ResetAbort ()
433 ResetAbort_internal ();
436 #if NET_4_0 || MOBILE
437 [HostProtectionAttribute (SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)]
438 [MethodImplAttribute(MethodImplOptions.InternalCall)]
439 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
440 public extern static bool Yield ();
444 [MethodImplAttribute(MethodImplOptions.InternalCall)]
445 private extern static void Sleep_internal(int ms);
447 public static void Sleep (int millisecondsTimeout)
449 if (millisecondsTimeout < Timeout.Infinite)
450 throw new ArgumentOutOfRangeException ("millisecondsTimeout", "Negative timeout");
452 Sleep_internal (millisecondsTimeout);
455 public static void Sleep (TimeSpan timeout)
457 long ms = (long) timeout.TotalMilliseconds;
458 if (ms < Timeout.Infinite || ms > Int32.MaxValue)
459 throw new ArgumentOutOfRangeException ("timeout", "timeout out of range");
461 Sleep_internal ((int) ms);
464 // Returns the system thread handle
465 [MethodImplAttribute(MethodImplOptions.InternalCall)]
466 private extern IntPtr Thread_internal (MulticastDelegate start);
468 public Thread(ThreadStart start) {
470 throw new ArgumentNullException("Null ThreadStart");
475 private Thread (InternalThread it) {
476 internal_thread = it;
479 // part of ".NETPortable,Version=v4.0,Profile=Profile3" i.e. FX4 and SL4
480 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
486 [Obsolete ("Deprecated in favor of GetApartmentState, SetApartmentState and TrySetApartmentState.")]
487 public ApartmentState ApartmentState {
489 if ((ThreadState & ThreadState.Stopped) != 0)
490 throw new ThreadStateException ("Thread is dead; state can not be accessed.");
492 return (ApartmentState)Internal.apartment_state;
496 TrySetApartmentState (value);
501 //[MethodImplAttribute (MethodImplOptions.InternalCall)]
502 //private static extern int current_lcid ();
504 public CultureInfo CurrentCulture {
506 CultureInfo culture = current_culture;
510 current_culture = culture = CultureInfo.ConstructCurrentCulture ();
511 NumberFormatter.SetThreadCurrentCulture (culture);
515 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
518 throw new ArgumentNullException ("value");
520 value.CheckNeutral ();
521 current_culture = value;
522 NumberFormatter.SetThreadCurrentCulture (value);
526 public CultureInfo CurrentUICulture {
528 CultureInfo culture = current_ui_culture;
532 current_ui_culture = culture = CultureInfo.ConstructCurrentUICulture ();
538 throw new ArgumentNullException ("value");
539 current_ui_culture = value;
543 public bool IsThreadPoolThread {
545 return IsThreadPoolThreadInternal;
549 internal bool IsThreadPoolThreadInternal {
551 return Internal.threadpool_thread;
554 Internal.threadpool_thread = value;
558 public bool IsAlive {
560 ThreadState curstate = GetState (Internal);
562 if((curstate & ThreadState.Aborted) != 0 ||
563 (curstate & ThreadState.Stopped) != 0 ||
564 (curstate & ThreadState.Unstarted) != 0) {
572 public bool IsBackground {
574 ThreadState thread_state = GetState (Internal);
575 if ((thread_state & ThreadState.Stopped) != 0)
576 throw new ThreadStateException ("Thread is dead; state can not be accessed.");
578 return (thread_state & ThreadState.Background) != 0;
583 SetState (Internal, ThreadState.Background);
585 ClrState (Internal, ThreadState.Background);
590 [MethodImplAttribute(MethodImplOptions.InternalCall)]
591 private extern static string GetName_internal (InternalThread thread);
593 [MethodImplAttribute(MethodImplOptions.InternalCall)]
594 private extern static void SetName_internal (InternalThread thread, String name);
597 * The thread name must be shared by appdomains, so it is stored in
603 return GetName_internal (Internal);
607 SetName_internal (Internal, value);
612 public ThreadPriority Priority {
614 return(ThreadPriority.Lowest);
618 // FIXME: Implement setter.
623 public ThreadState ThreadState {
625 return GetState (Internal);
629 [MethodImplAttribute(MethodImplOptions.InternalCall)]
630 private extern static void Abort_internal (InternalThread thread, object stateInfo);
632 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
635 Abort_internal (Internal, null);
639 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
640 public void Abort (object stateInfo)
642 Abort_internal (Internal, stateInfo);
645 [MethodImplAttribute(MethodImplOptions.InternalCall)]
646 internal extern object GetAbortExceptionState ();
648 [MethodImplAttribute (MethodImplOptions.InternalCall)]
649 private extern static void Interrupt_internal (InternalThread thread);
651 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
652 public void Interrupt ()
654 Interrupt_internal (Internal);
658 // The current thread joins with 'this'. Set ms to 0 to block
659 // until this actually exits.
660 [MethodImplAttribute(MethodImplOptions.InternalCall)]
661 private extern static bool Join_internal(InternalThread thread, int ms, IntPtr handle);
665 Join_internal(Internal, Timeout.Infinite, Internal.system_thread_handle);
668 public bool Join(int millisecondsTimeout)
670 if (millisecondsTimeout < Timeout.Infinite)
671 throw new ArgumentOutOfRangeException ("millisecondsTimeout", "Timeout less than zero");
673 return Join_internal (Internal, millisecondsTimeout, Internal.system_thread_handle);
677 public bool Join(TimeSpan timeout)
679 long ms = (long) timeout.TotalMilliseconds;
680 if (ms < Timeout.Infinite || ms > Int32.MaxValue)
681 throw new ArgumentOutOfRangeException ("timeout", "timeout out of range");
683 return Join_internal (Internal, (int) ms, Internal.system_thread_handle);
687 [MethodImplAttribute(MethodImplOptions.InternalCall)]
688 public extern static void MemoryBarrier ();
691 [MethodImplAttribute(MethodImplOptions.InternalCall)]
692 private extern void Resume_internal();
695 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
696 public void Resume ()
702 [MethodImplAttribute (MethodImplOptions.InternalCall)]
703 private extern static void SpinWait_nop ();
706 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
707 public static void SpinWait (int iterations)
711 while (iterations-- > 0)
718 private void StartInternal ()
720 current_thread = this;
723 if (threadstart is ThreadStart) {
724 ((ThreadStart) threadstart) ();
726 ((ParameterizedThreadStart) threadstart) (start_obj);
728 } catch (ThreadAbortException) {
730 } catch (Exception ex) {
731 MoonlightUnhandledException (ex);
735 static MethodInfo moonlight_unhandled_exception = null;
737 static internal void MoonlightUnhandledException (Exception e)
740 if (moonlight_unhandled_exception == null) {
741 var assembly = System.Reflection.Assembly.Load ("System.Windows, Version=2.0.5.0, Culture=Neutral, PublicKeyToken=7cec85d7bea7798e");
742 var application = assembly.GetType ("System.Windows.Application");
743 moonlight_unhandled_exception = application.GetMethod ("OnUnhandledException",
744 System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
746 moonlight_unhandled_exception.Invoke (null, new object [] { null, e });
750 Console.WriteLine ("Unexpected exception while trying to report unhandled application exception: {0}", e);
756 static ConstructorInfo nsautoreleasepool_ctor;
758 IDisposable GetNSAutoreleasePool ()
760 if (nsautoreleasepool_ctor == null) {
761 Type t = Type.GetType ("MonoTouch.Foundation.NSAutoreleasePool, monotouch, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null");
762 nsautoreleasepool_ctor = t.GetConstructor (Type.EmptyTypes);
764 return (IDisposable) nsautoreleasepool_ctor.Invoke (null);
767 private void StartInternal ()
769 using (var pool = GetNSAutoreleasePool ()) {
770 current_thread = this;
772 if (threadstart is ThreadStart) {
773 ((ThreadStart) threadstart) ();
775 ((ParameterizedThreadStart) threadstart) (start_obj);
780 private void StartInternal ()
782 current_thread = this;
784 if (threadstart is ThreadStart) {
785 ((ThreadStart) threadstart) ();
787 ((ParameterizedThreadStart) threadstart) (start_obj);
791 public void Start() {
792 // propagate informations from the original thread to the new thread
793 if (!ExecutionContext.IsFlowSuppressed ())
794 ec_to_set = ExecutionContext.Capture ();
795 Internal._serialized_principal = CurrentThread.Internal._serialized_principal;
797 // Thread_internal creates and starts the new thread,
798 if (Thread_internal((ThreadStart) StartInternal) == (IntPtr) 0)
799 throw new SystemException ("Thread creation failed.");
803 [MethodImplAttribute(MethodImplOptions.InternalCall)]
804 private extern static void Suspend_internal(InternalThread thread);
807 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
808 public void Suspend ()
810 Suspend_internal (Internal);
814 [MethodImplAttribute (MethodImplOptions.InternalCall)]
815 extern private static void SetState (InternalThread thread, ThreadState set);
817 [MethodImplAttribute (MethodImplOptions.InternalCall)]
818 extern private static void ClrState (InternalThread thread, ThreadState clr);
820 [MethodImplAttribute (MethodImplOptions.InternalCall)]
821 extern private static ThreadState GetState (InternalThread thread);
823 [MethodImplAttribute (MethodImplOptions.InternalCall)]
824 extern public static byte VolatileRead (ref byte address);
826 [MethodImplAttribute (MethodImplOptions.InternalCall)]
827 extern public static double VolatileRead (ref double address);
829 [MethodImplAttribute (MethodImplOptions.InternalCall)]
830 extern public static short VolatileRead (ref short address);
832 [MethodImplAttribute (MethodImplOptions.InternalCall)]
833 extern public static int VolatileRead (ref int address);
835 [MethodImplAttribute (MethodImplOptions.InternalCall)]
836 extern public static long VolatileRead (ref long address);
838 [MethodImplAttribute (MethodImplOptions.InternalCall)]
839 extern public static IntPtr VolatileRead (ref IntPtr address);
841 [MethodImplAttribute (MethodImplOptions.InternalCall)]
842 extern public static object VolatileRead (ref object address);
844 [CLSCompliant(false)]
845 [MethodImplAttribute (MethodImplOptions.InternalCall)]
846 extern public static sbyte VolatileRead (ref sbyte address);
848 [MethodImplAttribute (MethodImplOptions.InternalCall)]
849 extern public static float VolatileRead (ref float address);
851 [CLSCompliant (false)]
852 [MethodImplAttribute (MethodImplOptions.InternalCall)]
853 extern public static ushort VolatileRead (ref ushort address);
855 [CLSCompliant (false)]
856 [MethodImplAttribute (MethodImplOptions.InternalCall)]
857 extern public static uint VolatileRead (ref uint address);
859 [CLSCompliant (false)]
860 [MethodImplAttribute (MethodImplOptions.InternalCall)]
861 extern public static ulong VolatileRead (ref ulong address);
863 [CLSCompliant (false)]
864 [MethodImplAttribute (MethodImplOptions.InternalCall)]
865 extern public static UIntPtr VolatileRead (ref UIntPtr address);
867 [MethodImplAttribute (MethodImplOptions.InternalCall)]
868 extern public static void VolatileWrite (ref byte address, byte value);
870 [MethodImplAttribute (MethodImplOptions.InternalCall)]
871 extern public static void VolatileWrite (ref double address, double value);
873 [MethodImplAttribute (MethodImplOptions.InternalCall)]
874 extern public static void VolatileWrite (ref short address, short value);
876 [MethodImplAttribute (MethodImplOptions.InternalCall)]
877 extern public static void VolatileWrite (ref int address, int value);
879 [MethodImplAttribute (MethodImplOptions.InternalCall)]
880 extern public static void VolatileWrite (ref long address, long value);
882 [MethodImplAttribute (MethodImplOptions.InternalCall)]
883 extern public static void VolatileWrite (ref IntPtr address, IntPtr value);
885 [MethodImplAttribute (MethodImplOptions.InternalCall)]
886 extern public static void VolatileWrite (ref object address, object value);
888 [CLSCompliant(false)]
889 [MethodImplAttribute (MethodImplOptions.InternalCall)]
890 extern public static void VolatileWrite (ref sbyte address, sbyte value);
892 [MethodImplAttribute (MethodImplOptions.InternalCall)]
893 extern public static void VolatileWrite (ref float address, float value);
895 [CLSCompliant (false)]
896 [MethodImplAttribute (MethodImplOptions.InternalCall)]
897 extern public static void VolatileWrite (ref ushort address, ushort value);
899 [CLSCompliant (false)]
900 [MethodImplAttribute (MethodImplOptions.InternalCall)]
901 extern public static void VolatileWrite (ref uint address, uint value);
903 [CLSCompliant (false)]
904 [MethodImplAttribute (MethodImplOptions.InternalCall)]
905 extern public static void VolatileWrite (ref ulong address, ulong value);
907 [CLSCompliant (false)]
908 [MethodImplAttribute (MethodImplOptions.InternalCall)]
909 extern public static void VolatileWrite (ref UIntPtr address, UIntPtr value);
912 static int CheckStackSize (int maxStackSize)
914 if (maxStackSize < 0)
915 throw new ArgumentOutOfRangeException ("less than zero", "maxStackSize");
917 if (maxStackSize < 131072) // make sure stack is at least 128k big
920 int page_size = Environment.GetPageSize ();
922 if ((maxStackSize % page_size) != 0) // round up to a divisible of page size
923 maxStackSize = (maxStackSize / (page_size - 1)) * page_size;
925 int default_stack_size = (IntPtr.Size / 4) * 1024 * 1024; // from wthreads.c
927 if (maxStackSize > default_stack_size)
928 return default_stack_size;
933 public Thread (ThreadStart start, int maxStackSize)
936 throw new ArgumentNullException ("start");
939 Internal.stack_size = CheckStackSize (maxStackSize);;
942 public Thread (ParameterizedThreadStart start)
945 throw new ArgumentNullException ("start");
950 public Thread (ParameterizedThreadStart start, int maxStackSize)
953 throw new ArgumentNullException ("start");
956 Internal.stack_size = CheckStackSize (maxStackSize);
959 [MonoTODO ("limited to CompressedStack support")]
960 public ExecutionContext ExecutionContext {
961 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
964 _ec = new ExecutionContext ();
969 public int ManagedThreadId {
970 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
972 return Internal.managed_id;
976 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
977 public static void BeginCriticalRegion ()
979 CurrentThread.Internal.critical_region_level++;
982 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
983 public static void EndCriticalRegion ()
985 CurrentThread.Internal.critical_region_level--;
988 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
989 public static void BeginThreadAffinity ()
991 // Managed and native threads are currently bound together.
994 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
995 public static void EndThreadAffinity ()
997 // Managed and native threads are currently bound together.
1001 public ApartmentState GetApartmentState ()
1003 return (ApartmentState)Internal.apartment_state;
1006 public void SetApartmentState (ApartmentState state)
1008 if (!TrySetApartmentState (state))
1009 throw new InvalidOperationException ("Failed to set the specified COM apartment state.");
1012 public bool TrySetApartmentState (ApartmentState state)
1014 /* Only throw this exception when changing the
1015 * state of another thread. See bug 324338
1017 if ((this != CurrentThread) &&
1018 (ThreadState & ThreadState.Unstarted) == 0)
1019 throw new ThreadStateException ("Thread was in an invalid state for the operation being executed.");
1021 if ((ApartmentState)Internal.apartment_state != ApartmentState.Unknown)
1024 Internal.apartment_state = (byte)state;
1030 [ComVisible (false)]
1031 public override int GetHashCode ()
1033 return ManagedThreadId;
1036 public void Start (object parameter)
1038 start_obj = parameter;
1043 // NOTE: This method doesn't show in the class library status page because
1044 // it cannot be "found" with the StrongNameIdentityPermission for ECMA key.
1046 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
1047 [StrongNameIdentityPermission (SecurityAction.LinkDemand, PublicKey="00000000000000000400000000000000")]
1048 [Obsolete ("see CompressedStack class")]
1050 CompressedStack GetCompressedStack ()
1052 // Note: returns null if no CompressedStack has been set.
1053 // However CompressedStack.GetCompressedStack returns an
1054 // (empty?) CompressedStack instance.
1055 CompressedStack cs = ExecutionContext.SecurityContext.CompressedStack;
1056 return ((cs == null) || cs.IsEmpty ()) ? null : cs.CreateCopy ();
1059 // NOTE: This method doesn't show in the class library status page because
1060 // it cannot be "found" with the StrongNameIdentityPermission for ECMA key.
1062 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
1063 [StrongNameIdentityPermission (SecurityAction.LinkDemand, PublicKey="00000000000000000400000000000000")]
1064 [Obsolete ("see CompressedStack class")]
1066 void SetCompressedStack (CompressedStack stack)
1068 ExecutionContext.SecurityContext.CompressedStack = stack;
1073 void _Thread.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1075 throw new NotImplementedException ();
1078 void _Thread.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1080 throw new NotImplementedException ();
1083 void _Thread.GetTypeInfoCount (out uint pcTInfo)
1085 throw new NotImplementedException ();
1088 void _Thread.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
1089 IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1091 throw new NotImplementedException ();