New test.
[mono.git] / mcs / class / corlib / System.Threading / Thread.cs
1 //
2 // System.Threading.Thread.cs
3 //
4 // Author:
5 //   Dick Porter (dick@ximian.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Runtime.Remoting.Contexts;
31 using System.Runtime.Serialization;
32 using System.Runtime.Serialization.Formatters.Binary;
33 using System.Security.Permissions;
34 using System.Security.Principal;
35 using System.Globalization;
36 using System.Runtime.CompilerServices;
37 using System.Runtime.InteropServices;
38 using System.IO;
39 using System.Collections;
40 using System.Security;
41
42 #if NET_2_0
43 using System.Runtime.ConstrainedExecution;
44 #endif
45
46 namespace System.Threading {
47
48         [ClassInterface (ClassInterfaceType.None)]
49 #if NET_2_0
50         [ComVisible (true)]
51         [ComDefaultInterface (typeof (_Thread))]
52         public sealed class Thread : CriticalFinalizerObject, _Thread {
53 #else
54         public sealed class Thread : _Thread {
55 #endif
56
57                 #region Sync with metadata/object-internals.h
58                 int lock_thread_id;
59                 // stores a thread handle
60                 private IntPtr system_thread_handle;
61
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 */
67                 private IntPtr name;
68                 private int name_len; 
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;
74                 
75                 /* start_notify is used by the runtime to signal that Start()
76                  * is ok to return
77                  */
78                 private IntPtr start_notify;
79                 private IntPtr stack_ptr;
80                 private IntPtr static_data;
81                 private IntPtr jit_data;
82                 private IntPtr lock_data;
83                 Context current_appcontext;
84                 int stack_size;
85                 object start_obj;
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                 /* Don't lock on synch_lock in managed code, since it can result in deadlocks */
92                 private object synch_lock = new Object();
93                 private IntPtr serialized_culture_info;
94                 private int serialized_culture_info_len;
95                 private IntPtr serialized_ui_culture_info;
96                 private int serialized_ui_culture_info_len;
97                 private ExecutionContext _ec;
98                 /* 
99                  * These fields are used to avoid having to increment corlib versions
100                  * when a new field is added to the unmanaged MonoThread structure.
101                  */
102                 private IntPtr unused1;
103                 private IntPtr unused2;
104                 private IntPtr unused3;
105                 private IntPtr unused4;
106                 private IntPtr unused5;
107                 private IntPtr unused6;
108                 private IntPtr unused7;
109                 #endregion
110
111                 // the name of local_slots is important as it's used by the runtime.
112                 [ThreadStatic] 
113                 static object[] local_slots;
114
115                 // can be both a ThreadSart and a ParameterizedThreadStart
116                 private MulticastDelegate threadstart;
117                 private string thread_name=null;
118                 
119                 private IPrincipal _principal;
120
121                 public static Context CurrentContext {
122                         [SecurityPermission (SecurityAction.LinkDemand, Infrastructure=true)]
123                         get {
124                                 return(AppDomain.InternalGetContext ());
125                         }
126                 }
127
128                 public static IPrincipal CurrentPrincipal {
129                         get {
130                                 IPrincipal p = null;
131                                 Thread th = CurrentThread;
132                                 lock (th) {
133                                         p = th._principal;
134                                         if (p == null) {
135                                                 p = GetDomain ().DefaultPrincipal;
136                                                 th._principal = p;
137                                         }
138                                 }
139                                 return p;
140                         }
141                         [SecurityPermission (SecurityAction.Demand, ControlPrincipal = true)]
142                         set {
143                                 CurrentThread._principal = value;
144                         }
145                 }
146
147                 // Looks up the object associated with the current thread
148                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
149                 private extern static Thread CurrentThread_internal();
150                 
151                 public static Thread CurrentThread {
152 #if NET_2_0
153                         [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
154 #endif
155                         get {
156                                 return(CurrentThread_internal());
157                         }
158                 }
159
160                 internal static int CurrentThreadId {
161                         get {
162                                 return (int)(CurrentThread.thread_id);
163                         }
164                 }
165
166                 // Stores a hash keyed by strings of LocalDataStoreSlot objects
167                 static Hashtable datastorehash;
168                 private static object datastore_lock = new object ();
169                 
170                 private static void InitDataStoreHash () {
171                         lock (datastore_lock) {
172                                 if (datastorehash == null) {
173                                         datastorehash = Hashtable.Synchronized(new Hashtable());
174                                 }
175                         }
176                 }
177                 
178                 public static LocalDataStoreSlot AllocateNamedDataSlot (string name) {
179                         lock (datastore_lock) {
180                                 if (datastorehash == null)
181                                         InitDataStoreHash ();
182                                 LocalDataStoreSlot slot = (LocalDataStoreSlot)datastorehash [name];
183                                 if (slot != null) {
184                                         // This exception isnt documented (of
185                                         // course) but .net throws it
186                                         throw new ArgumentException("Named data slot already added");
187                                 }
188                         
189                                 slot = AllocateDataSlot ();
190
191                                 datastorehash.Add (name, slot);
192
193                                 return slot;
194                         }
195                 }
196
197                 public static void FreeNamedDataSlot (string name) {
198                         lock (datastore_lock) {
199                                 if (datastorehash == null)
200                                         InitDataStoreHash ();
201                                 LocalDataStoreSlot slot = (LocalDataStoreSlot)datastorehash [name];
202
203                                 if (slot != null) {
204                                         datastorehash.Remove (slot);
205                                 }
206                         }
207                 }
208
209                 public static LocalDataStoreSlot AllocateDataSlot () {
210                         return new LocalDataStoreSlot (true);
211                 }
212
213                 public static object GetData (LocalDataStoreSlot slot) {
214                         object[] slots = local_slots;
215                         if (slot == null)
216                                 throw new ArgumentNullException ("slot");
217                         if (slots != null && slot.slot < slots.Length)
218                                 return slots [slot.slot];
219                         return null;
220                 }
221
222                 public static void SetData (LocalDataStoreSlot slot, object data) {
223                         object[] slots = local_slots;
224                         if (slot == null)
225                                 throw new ArgumentNullException ("slot");
226                         if (slots == null) {
227                                 slots = new object [slot.slot + 2];
228                                 local_slots = slots;
229                         } else if (slot.slot >= slots.Length) {
230                                 object[] nslots = new object [slot.slot + 2];
231                                 slots.CopyTo (nslots, 0);
232                                 slots = nslots;
233                                 local_slots = slots;
234                         }
235                         slots [slot.slot] = data;
236                 }
237
238                 public static AppDomain GetDomain() {
239                         return AppDomain.CurrentDomain;
240                 }
241
242                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
243                 public extern static int GetDomainID();
244
245                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
246                 internal extern static void FreeLocalSlotValues (int slot, bool thread_local);
247
248                 public static LocalDataStoreSlot GetNamedDataSlot(string name) {
249                         lock (datastore_lock) {
250                                 if (datastorehash == null)
251                                         InitDataStoreHash ();
252                                 LocalDataStoreSlot slot=(LocalDataStoreSlot)datastorehash[name];
253
254                                 if(slot==null) {
255                                         slot=AllocateNamedDataSlot(name);
256                                 }
257                         
258                                 return(slot);
259                         }
260                 }
261                 
262                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
263                 private extern static void ResetAbort_internal();
264
265                 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
266                 public static void ResetAbort ()
267                 {
268                         ResetAbort_internal ();
269                 }
270
271                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
272                 private extern static void Sleep_internal(int ms);
273
274                 public static void Sleep(int millisecondsTimeout) {
275                         if((millisecondsTimeout<0) && (millisecondsTimeout != Timeout.Infinite)) {
276                                 throw new ArgumentException("Negative timeout");
277                         }
278                         Sleep_internal(millisecondsTimeout);
279                 }
280
281                 public static void Sleep(TimeSpan timeout) {
282                         // LAMESPEC: says to throw ArgumentException too
283                         int ms=Convert.ToInt32(timeout.TotalMilliseconds);
284                         
285                         if(ms < 0 || ms > Int32.MaxValue) {
286                                 throw new ArgumentOutOfRangeException("Timeout out of range");
287                         }
288
289                         Sleep_internal(ms);
290                 }
291
292                 // Returns the system thread handle
293                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
294                 private extern IntPtr Thread_internal (MulticastDelegate start);
295
296                 public Thread(ThreadStart start) {
297                         if(start==null) {
298                                 throw new ArgumentNullException("Null ThreadStart");
299                         }
300                         threadstart=start;
301                 }
302
303                 [MonoTODO]
304 #if NET_2_0
305                 [Obsolete ("Deprecated in favor of GetApartmentState, SetApartmentState and TrySetApartmentState.")]
306 #endif
307                 public ApartmentState ApartmentState {
308                         get {
309                                 return(ApartmentState.Unknown);
310                         }
311                         
312                         set {
313                         }
314                 }
315
316                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
317                 private static extern int current_lcid ();
318
319                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
320                 private extern CultureInfo GetCachedCurrentCulture ();
321
322                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
323                 private extern byte[] GetSerializedCurrentCulture ();
324
325                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
326                 private extern void SetCachedCurrentCulture (CultureInfo culture);
327
328                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
329                 private extern void SetSerializedCurrentCulture (byte[] culture);
330
331                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
332                 private extern CultureInfo GetCachedCurrentUICulture ();
333
334                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
335                 private extern byte[] GetSerializedCurrentUICulture ();
336
337                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
338                 private extern void SetCachedCurrentUICulture (CultureInfo culture);
339
340                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
341                 private extern void SetSerializedCurrentUICulture (byte[] culture);
342
343                 /* If the current_lcid() isn't known by CultureInfo,
344                  * it will throw an exception which may cause
345                  * String.Concat to try and recursively look up the
346                  * CurrentCulture, which will throw an exception, etc.
347                  * Use a boolean to short-circuit this scenario.
348                  */
349                 private static bool in_currentculture=false;
350
351                 static object culture_lock = new object ();
352                 
353                 /*
354                  * Thread objects are shared between appdomains, and CurrentCulture
355                  * should always return an object in the calling appdomain. See bug
356                  * http://bugzilla.ximian.com/show_bug.cgi?id=50049 for more info.
357                  * This is hard to implement correctly and efficiently, so the current
358                  * implementation is not perfect: changes made in one appdomain to the 
359                  * state of the current cultureinfo object are not visible to other 
360                  * appdomains.
361                  */             
362                 public CultureInfo CurrentCulture {
363                         get {
364                                 if (in_currentculture)
365                                         /* Bail out */
366                                         return CultureInfo.InvariantCulture;
367
368                                 CultureInfo culture = GetCachedCurrentCulture ();
369                                 if (culture != null)
370                                         return culture;
371
372                                 byte[] arr = GetSerializedCurrentCulture ();
373                                 if (arr == null) {
374                                         lock (culture_lock) {
375                                                 in_currentculture=true;
376                                                 culture = CultureInfo.ConstructCurrentCulture ();
377                                                 //
378                                                 // Don't serialize the culture in this case to avoid
379                                                 // initializing the serialization infrastructure in the
380                                                 // common case when the culture is not set explicitly.
381                                                 //
382                                                 SetCachedCurrentCulture (culture);
383                                                 in_currentculture = false;
384                                                 return culture;
385                                         }
386                                 }
387
388                                 /*
389                                  * No cultureinfo object exists for this domain, so create one
390                                  * by deserializing the serialized form.
391                                  */
392                                 in_currentculture = true;
393                                 try {
394                                         BinaryFormatter bf = new BinaryFormatter ();
395                                         MemoryStream ms = new MemoryStream (arr);
396                                         culture = (CultureInfo)bf.Deserialize (ms);
397                                         SetCachedCurrentCulture (culture);
398                                 } finally {
399                                         in_currentculture = false;
400                                 }
401
402                                 return culture;
403                         }
404                         
405                         [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
406                         set {
407                                 if (value == null)
408                                         throw new ArgumentNullException ("value");
409
410                                 value.CheckNeutral ();
411                                 in_currentculture = true;
412                                 try {
413                                         BinaryFormatter bf = new BinaryFormatter();
414                                         MemoryStream ms = new MemoryStream ();
415                                         bf.Serialize (ms, value);
416
417                                         SetCachedCurrentCulture (value);
418                                         SetSerializedCurrentCulture (ms.GetBuffer ());
419                                 } finally {
420                                         in_currentculture = false;
421                                 }
422                         }
423                 }
424
425                 public CultureInfo CurrentUICulture {
426                         get {
427                                 if (in_currentculture)
428                                         /* Bail out */
429                                         return CultureInfo.InvariantCulture;
430
431                                 CultureInfo culture = GetCachedCurrentUICulture ();
432                                 if (culture != null)
433                                         return culture;
434
435                                 byte[] arr = GetSerializedCurrentUICulture ();
436                                 if (arr == null) {
437                                         lock (culture_lock) {
438                                                 in_currentculture=true;
439                                                 /* We don't
440                                                  * distinguish
441                                                  * between
442                                                  * System and
443                                                  * UI cultures
444                                                  */
445                                                 culture = CultureInfo.ConstructCurrentUICulture ();
446                                                 //
447                                                 // Don't serialize the culture in this case to avoid
448                                                 // initializing the serialization infrastructure in the
449                                                 // common case when the culture is not set explicitly.
450                                                 //
451                                                 SetCachedCurrentUICulture (culture);
452                                                 in_currentculture = false;
453                                                 return culture;
454                                         }
455                                 }
456
457                                 /*
458                                  * No cultureinfo object exists for this domain, so create one
459                                  * by deserializing the serialized form.
460                                  */
461                                 in_currentculture = true;
462                                 try {
463                                         BinaryFormatter bf = new BinaryFormatter ();
464                                         MemoryStream ms = new MemoryStream (arr);
465                                         culture = (CultureInfo)bf.Deserialize (ms);
466                                         SetCachedCurrentUICulture (culture);
467                                 }
468                                 finally {
469                                         in_currentculture = false;
470                                 }
471
472                                 return culture;
473                         }
474                         
475                         set {
476                                 in_currentculture = true;
477                                 
478                                 if (value == null)
479                                         throw new ArgumentNullException ("value");
480
481                                 try {
482                                         BinaryFormatter bf = new BinaryFormatter();
483                                         MemoryStream ms = new MemoryStream ();
484                                         bf.Serialize (ms, value);
485
486                                         SetCachedCurrentUICulture (value);
487                                         SetSerializedCurrentUICulture (ms.GetBuffer ());
488                                 } finally {
489                                         in_currentculture = false;
490                                 }
491                         }
492                 }
493
494                 public bool IsThreadPoolThread {
495                         get {
496                                 return IsThreadPoolThreadInternal;
497                         }
498                 }
499
500                 internal bool IsThreadPoolThreadInternal {
501                         get {
502                                 return threadpool_thread;
503                         }
504                         set {
505                                 threadpool_thread = value;
506                         }
507                 }
508
509                 public bool IsAlive {
510                         get {
511                                 ThreadState curstate = GetState ();
512                                 
513                                 if((curstate & ThreadState.Aborted) != 0 ||
514                                    (curstate & ThreadState.Stopped) != 0 ||
515                                    (curstate & ThreadState.Unstarted) != 0) {
516                                         return(false);
517                                 } else {
518                                         return(true);
519                                 }
520                         }
521                 }
522
523                 public bool IsBackground {
524                         get {
525                                 return (GetState () & ThreadState.Background) != 0;
526                         }
527                         
528                         set {
529                                 if (value) {
530                                         SetState (ThreadState.Background);
531                                 } else {
532                                         ClrState (ThreadState.Background);
533                                 }
534                         }
535                 }
536
537                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
538                 private extern string GetName_internal ();
539
540                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
541                 private extern void SetName_internal (String name);
542
543                 /* 
544                  * The thread name must be shared by appdomains, so it is stored in
545                  * unmanaged code.
546                  */
547
548                 public string Name {
549                         get {
550                                 return GetName_internal ();
551                         }
552                         
553                         set {
554                                 SetName_internal (value);
555                         }
556                 }
557
558                 [MonoTODO]
559                 public ThreadPriority Priority {
560                         get {
561                                 return(ThreadPriority.Lowest);
562                         }
563                         
564                         set {
565                         }
566                 }
567
568                 public ThreadState ThreadState {
569                         get {
570                                 return GetState ();
571                         }
572                 }
573
574                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
575                 private extern void Abort_internal (object stateInfo);
576
577                 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
578                 public void Abort () 
579                 {
580                         Abort_internal (null);
581                 }
582
583                 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
584                 public void Abort (object stateInfo) 
585                 {
586                         Abort_internal (stateInfo);
587                 }
588                 
589
590                 [MonoTODO]
591                 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
592                 public void Interrupt ()
593                 {
594                         throw new NotImplementedException ();
595                 }
596
597                 // The current thread joins with 'this'. Set ms to 0 to block
598                 // until this actually exits.
599                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
600                 private extern bool Join_internal(int ms, IntPtr handle);
601                 
602                 public void Join()
603                 {
604                         Join_internal(Timeout.Infinite, system_thread_handle);
605                 }
606
607                 public bool Join(int millisecondsTimeout)
608                 {
609                         if (millisecondsTimeout != Timeout.Infinite && millisecondsTimeout < 0)
610                                 throw new ArgumentException ("Timeout less than zero", "millisecondsTimeout");
611
612                         return Join_internal(millisecondsTimeout, system_thread_handle);
613                 }
614
615                 public bool Join(TimeSpan timeout)
616                 {
617                         // LAMESPEC: says to throw ArgumentException too
618                         int ms=Convert.ToInt32(timeout.TotalMilliseconds);
619                         
620                         if(ms < 0 || ms > Int32.MaxValue) {
621                                 throw new ArgumentOutOfRangeException("timeout out of range");
622                         }
623                         return Join_internal(ms, system_thread_handle);
624                 }
625
626 #if NET_1_1
627                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
628                 public extern static void MemoryBarrier ();
629 #endif
630                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
631                 private extern void Resume_internal();
632
633 #if NET_2_0
634                 [Obsolete ("")]
635 #endif
636                 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
637                 public void Resume () 
638                 {
639                         Resume_internal ();
640                 }
641
642                 [MonoTODO]
643 #if NET_2_0
644                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
645 #endif
646                 public static void SpinWait (int iterations) 
647                 {
648                         throw new NotImplementedException ();
649                 }
650
651                 public void Start() {
652                         // propagate informations from the original thread to the new thread
653 #if NET_2_0
654                         if (!ExecutionContext.IsFlowSuppressed ())
655                                 _ec = ExecutionContext.Capture ();
656 #else
657                         // before 2.0 this was only used for security (mostly CAS) so we
658                         // do this only if the security manager is active
659                         if (SecurityManager.SecurityEnabled)
660                                 _ec = ExecutionContext.Capture ();
661 #endif
662                         if (CurrentThread._principal != null)
663                                 _principal = CurrentThread._principal;
664
665                         // Thread_internal creates and starts the new thread, 
666                         if (Thread_internal(threadstart) == (IntPtr) 0)
667                                 throw new SystemException ("Thread creation failed.");
668                 }
669
670                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
671                 private extern void Suspend_internal();
672
673 #if NET_2_0
674                 [Obsolete ("")]
675 #endif
676                 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
677                 public void Suspend ()
678                 {
679                         Suspend_internal ();
680                 }
681
682                 // Closes the system thread handle
683                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
684                 private extern void Thread_free_internal(IntPtr handle);
685
686 #if NET_2_0
687                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
688 #endif
689                 ~Thread() {
690                         // Free up the handle
691                         if (system_thread_handle != (IntPtr) 0)
692                                 Thread_free_internal(system_thread_handle);
693                 }
694
695                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
696                 extern private void SetState (ThreadState set);
697                 
698                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
699                 extern private void ClrState (ThreadState clr);
700                 
701                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
702                 extern private ThreadState GetState ();
703
704 #if NET_1_1
705                 
706                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
707                 extern public static byte VolatileRead (ref byte address);
708                 
709                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
710                 extern public static double VolatileRead (ref double address);
711                 
712                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
713                 extern public static short VolatileRead (ref short address);
714                 
715                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
716                 extern public static int VolatileRead (ref int address);
717                 
718                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
719                 extern public static long VolatileRead (ref long address);
720                 
721                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
722                 extern public static IntPtr VolatileRead (ref IntPtr address);
723                 
724                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
725                 extern public static object VolatileRead (ref object address);
726
727                 [CLSCompliant(false)]
728                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
729                 extern public static sbyte VolatileRead (ref sbyte address);
730                 
731                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
732                 extern public static float VolatileRead (ref float address);
733
734                 [CLSCompliant (false)]
735                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
736                 extern public static ushort VolatileRead (ref ushort address);
737
738                 [CLSCompliant (false)]
739                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
740                 extern public static uint VolatileRead (ref uint address);
741
742                 [CLSCompliant (false)]
743                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
744                 extern public static ulong VolatileRead (ref ulong address);
745
746                 [CLSCompliant (false)]
747                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
748                 extern public static UIntPtr VolatileRead (ref UIntPtr address);
749
750                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
751                 extern public static void VolatileWrite (ref byte address, byte value);
752                 
753                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
754                 extern public static void VolatileWrite (ref double address, double value);
755                 
756                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
757                 extern public static void VolatileWrite (ref short address, short value);
758                 
759                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
760                 extern public static void VolatileWrite (ref int address, int value);
761                 
762                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
763                 extern public static void VolatileWrite (ref long address, long value);
764                 
765                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
766                 extern public static void VolatileWrite (ref IntPtr address, IntPtr value);
767                 
768                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
769                 extern public static void VolatileWrite (ref object address, object value);
770
771                 [CLSCompliant(false)]
772                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
773                 extern public static void VolatileWrite (ref sbyte address, sbyte value);
774                 
775                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
776                 extern public static void VolatileWrite (ref float address, float value);
777
778                 [CLSCompliant (false)]
779                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
780                 extern public static void VolatileWrite (ref ushort address, ushort value);
781
782                 [CLSCompliant (false)]
783                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
784                 extern public static void VolatileWrite (ref uint address, uint value);
785
786                 [CLSCompliant (false)]
787                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
788                 extern public static void VolatileWrite (ref ulong address, ulong value);
789
790                 [CLSCompliant (false)]
791                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
792                 extern public static void VolatileWrite (ref UIntPtr address, UIntPtr value);
793                 
794 #endif
795
796 #if NET_2_0
797                 public Thread (ThreadStart start, int maxStackSize)
798                 {
799                         if (start == null)
800                                 throw new ArgumentNullException ("start");
801                         if (maxStackSize < 131072)
802                                 throw new ArgumentException ("< 128 kb", "maxStackSize");
803
804                         threadstart = start;
805                         stack_size = maxStackSize;
806                 }
807
808                 public Thread (ParameterizedThreadStart start)
809                 {
810                         if (start == null)
811                                 throw new ArgumentNullException ("start");
812
813                         threadstart = start;
814                 }
815
816                 public Thread (ParameterizedThreadStart start, int maxStackSize)
817                 {
818                         if (start == null)
819                                 throw new ArgumentNullException ("start");
820                         if (maxStackSize < 131072)
821                                 throw new ArgumentException ("< 128 kb", "maxStackSize");
822
823                         threadstart = start;
824                         stack_size = maxStackSize;
825                 }
826
827                 [MonoTODO ("limited to CompressedStack support")]
828                 public ExecutionContext ExecutionContext {
829                         [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
830                         get {
831                                 if (_ec == null)
832                                         _ec = new ExecutionContext ();
833                                 return _ec;
834                         }
835                 }
836
837                 public int ManagedThreadId {
838                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
839                         get { return (int)thread_id; }
840                 }
841
842                 [MonoTODO]
843                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
844                 public static void BeginCriticalRegion ()
845                 {
846                         throw new NotImplementedException ();
847                 }
848
849                 [MonoTODO]
850                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
851                 public static void EndCriticalRegion ()
852                 {
853                         throw new NotImplementedException ();
854                 }
855
856                 [MonoTODO]
857                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
858                 public static void BeginThreadAffinity ()
859                 {
860                         throw new NotImplementedException ();
861                 }
862
863                 [MonoTODO]
864                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
865                 public static void EndThreadAffinity ()
866                 {
867                         throw new NotImplementedException ();
868                 }
869
870                 //
871                 // We disable warning 618, because we are accessing the
872                 // empty property ApartmentState which produces an Obsolete
873                 // message, but since its an empty routine needed for 1.x
874                 // we use it.
875                 //
876                 // Maybe we should later turn these into internal methods for 1.x
877                 // instead and have the property call these.
878                 
879                 
880                 [MonoTODO]
881                 public ApartmentState GetApartmentState ()
882                 {
883                         return this.ApartmentState;
884                 }
885
886                 [MonoTODO]
887                 public void SetApartmentState (ApartmentState state)
888                 {
889                         this.ApartmentState = state;
890                 }
891
892                 [MonoTODO]
893                 public bool TrySetApartmentState (ApartmentState state)
894                 {
895                         try {
896                                 this.ApartmentState = state;
897                                 return true;
898                         }
899                         catch (ArgumentException) {
900                                 throw;
901                         }
902                         catch {
903                                 return false;
904                         }
905                 }
906                 
907                 [ComVisible (false)]
908                 public override int GetHashCode ()
909                 {
910                         // ??? overridden but not guaranteed to be unique ???
911                         return (int)thread_id;
912                 }
913
914                 public void Start (object parameter)
915                 {
916                         start_obj = parameter;
917                         Start ();
918                 }
919 #else
920                 internal ExecutionContext ExecutionContext {
921                         get {
922                                 if (_ec == null)
923                                         _ec = new ExecutionContext ();
924                                 return _ec;
925                         }
926                 }
927 #endif
928
929                 // NOTE: This method doesn't show in the class library status page because
930                 // it cannot be "found" with the StrongNameIdentityPermission for ECMA key.
931                 // But it's there!
932                 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
933                 [StrongNameIdentityPermission (SecurityAction.LinkDemand, PublicKey="00000000000000000400000000000000")]
934 #if NET_2_0
935                 [Obsolete ("see CompressedStack class")]
936 #endif
937 #if NET_1_1
938                 public
939 #else
940                 internal
941 #endif
942                 CompressedStack GetCompressedStack ()
943                 {
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 ();
949                 }
950
951                 // NOTE: This method doesn't show in the class library status page because
952                 // it cannot be "found" with the StrongNameIdentityPermission for ECMA key.
953                 // But it's there!
954                 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
955                 [StrongNameIdentityPermission (SecurityAction.LinkDemand, PublicKey="00000000000000000400000000000000")]
956 #if NET_2_0
957                 [Obsolete ("see CompressedStack class")]
958 #endif
959 #if NET_1_1
960                 public
961 #else
962                 internal
963 #endif
964                 void SetCompressedStack (CompressedStack stack)
965                 {
966                         ExecutionContext.SecurityContext.CompressedStack = stack;
967                 }
968
969 #if NET_1_1
970                 void _Thread.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
971                 {
972                         throw new NotImplementedException ();
973                 }
974
975                 void _Thread.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
976                 {
977                         throw new NotImplementedException ();
978                 }
979
980                 void _Thread.GetTypeInfoCount (out uint pcTInfo)
981                 {
982                         throw new NotImplementedException ();
983                 }
984
985                 void _Thread.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
986                         IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
987                 {
988                         throw new NotImplementedException ();
989                 }
990 #endif
991         }
992 }