3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // <OWNER>[....]</OWNER>
8 /*=============================================================================
13 ** Purpose: Class for creating and managing a thread.
16 =============================================================================*/
18 namespace System.Threading {
19 using System.Threading;
21 using System.Runtime.InteropServices;
23 using System.Runtime.Remoting.Contexts;
25 using System.Runtime.Remoting.Messaging;
27 using System.Diagnostics;
28 using System.Security.Permissions;
29 using System.Security.Principal;
30 using System.Globalization;
31 using System.Collections.Generic;
32 using System.Runtime.Serialization;
33 using System.Runtime.CompilerServices;
34 using System.Runtime.ConstrainedExecution;
35 using System.Security;
36 using System.Runtime.Versioning;
37 using System.Diagnostics.Contracts;
39 internal delegate Object InternalCrossContextDelegate(Object[] args);
41 internal class ThreadHelper
43 [System.Security.SecuritySafeCritical]
44 static ThreadHelper() {}
47 Object _startArg = null;
48 ExecutionContext _executionContext = null;
49 internal ThreadHelper(Delegate start)
54 internal void SetExecutionContextHelper(ExecutionContext ec)
56 _executionContext = ec;
59 [System.Security.SecurityCritical]
60 static internal ContextCallback _ccb = new ContextCallback(ThreadStart_Context);
62 [System.Security.SecurityCritical]
63 static private void ThreadStart_Context(Object state)
65 ThreadHelper t = (ThreadHelper)state;
66 if (t._start is ThreadStart)
68 ((ThreadStart)t._start)();
72 ((ParameterizedThreadStart)t._start)(t._startArg);
78 [System.Security.SecuritySafeCritical] // auto-generated
80 [System.Security.SecurityCritical]
82 internal void ThreadStart(object obj)
85 if (_executionContext != null)
87 ExecutionContext.Run(_executionContext, _ccb, (Object)this);
91 ((ParameterizedThreadStart)_start)(obj);
97 [System.Security.SecuritySafeCritical] // auto-generated
99 [System.Security.SecurityCritical]
101 internal void ThreadStart()
103 if (_executionContext != null)
105 ExecutionContext.Run(_executionContext, _ccb, (Object)this);
109 ((ThreadStart)_start)();
114 internal struct ThreadHandle
116 private IntPtr m_ptr;
118 internal ThreadHandle(IntPtr pThread)
124 // deliberately not [serializable]
125 [ClassInterface(ClassInterfaceType.None)]
126 [ComDefaultInterface(typeof(_Thread))]
127 [System.Runtime.InteropServices.ComVisible(true)]
128 public sealed partial class Thread : CriticalFinalizerObject
134 /*=========================================================================
135 ** Data accessed from managed code that needs to be defined in
136 ** ThreadBaseObject to maintain alignment between the two classes.
137 ** DON'T CHANGE THESE UNLESS YOU MODIFY ThreadBaseObject in vm\object.h
138 =========================================================================*/
140 private Context m_Context;
142 private ExecutionContext m_ExecutionContext; // this call context follows the logical thread
144 private String m_Name;
145 private Delegate m_Delegate; // Delegate
147 #if FEATURE_LEAK_CULTURE_INFO
148 private CultureInfo m_CurrentCulture;
149 private CultureInfo m_CurrentUICulture;
151 private Object m_ThreadStartArg;
153 /*=========================================================================
154 ** The base implementation of Thread is all native. The following fields
155 ** should never be used in the C# code. They are here to define the proper
156 ** space so the thread object may be allocated. DON'T CHANGE THESE UNLESS
157 ** YOU MODIFY ThreadBaseObject in vm\object.h
158 =========================================================================*/
159 #pragma warning disable 169
160 #pragma warning disable 414 // These fields are not used from managed.
161 // IntPtrs need to be together, and before ints, because IntPtrs are 64-bit
162 // fields on 64-bit platforms, where they will be sorted together.
164 private IntPtr DONT_USE_InternalThread; // Pointer
165 private int m_Priority; // INT32
166 private int m_ManagedThreadId; // INT32
168 #pragma warning restore 414
169 #pragma warning restore 169
171 private bool m_ExecutionContextBelongsToOuterScope;
173 private bool m_ForbidExecutionContextMutation;
176 /*=========================================================================
177 ** This manager is responsible for storing the global data that is
178 ** shared amongst all the thread local stores.
179 =========================================================================*/
180 static private LocalDataStoreMgr s_LocalDataStoreMgr;
182 /*=========================================================================
183 ** Thread-local data store
184 =========================================================================*/
186 static private LocalDataStoreHolder s_LocalDataStore;
188 // Do not move! Order of above fields needs to be preserved for alignment
190 // See code:#threadCultureInfo
191 #if !FEATURE_LEAK_CULTURE_INFO
193 private static CultureInfo m_CurrentCulture;
195 private static CultureInfo m_CurrentUICulture;
199 // Adding an empty default ctor for annotation purposes
200 [System.Security.SecuritySafeCritical] // auto-generated
202 #endif // FEATURE_CORECLR
204 /*=========================================================================
205 ** Creates a new Thread object which will begin execution at
206 ** start.ThreadStart on a new thread when the Start method is called.
208 ** Exceptions: ArgumentNullException if start == null.
209 =========================================================================*/
210 [System.Security.SecuritySafeCritical] // auto-generated
211 public Thread(ThreadStart start) {
213 throw new ArgumentNullException("start");
215 Contract.EndContractBlock();
216 SetStartHelper((Delegate)start,0); //0 will setup Thread with default stackSize
219 [System.Security.SecuritySafeCritical] // auto-generated
220 public Thread(ThreadStart start, int maxStackSize) {
222 throw new ArgumentNullException("start");
224 if (0 > maxStackSize)
225 throw new ArgumentOutOfRangeException("maxStackSize",Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
226 Contract.EndContractBlock();
227 SetStartHelper((Delegate)start, maxStackSize);
230 [System.Security.SecuritySafeCritical] // auto-generated
231 public Thread(ParameterizedThreadStart start) {
233 throw new ArgumentNullException("start");
235 Contract.EndContractBlock();
236 SetStartHelper((Delegate)start, 0);
239 [System.Security.SecuritySafeCritical] // auto-generated
240 public Thread(ParameterizedThreadStart start, int maxStackSize) {
242 throw new ArgumentNullException("start");
244 if (0 > maxStackSize)
245 throw new ArgumentOutOfRangeException("maxStackSize",Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
246 Contract.EndContractBlock();
247 SetStartHelper((Delegate)start, maxStackSize);
251 public override int GetHashCode()
253 return m_ManagedThreadId;
256 extern public int ManagedThreadId
258 [ResourceExposure(ResourceScope.None)]
259 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
260 [MethodImplAttribute(MethodImplOptions.InternalCall)]
261 [System.Security.SecuritySafeCritical] // auto-generated
265 // Returns handle for interop with EE. The handle is guaranteed to be non-null.
266 internal unsafe ThreadHandle GetNativeHandle()
268 IntPtr thread = DONT_USE_InternalThread;
270 // This should never happen under normal circumstances. m_assembly is always assigned before it is handed out to the user.
271 // There are ways how to create an unitialized objects through remoting, etc. Avoid AVing in the EE by throwing a nice
274 throw new ArgumentException(null, Environment.GetResourceString("Argument_InvalidHandle"));
276 return new ThreadHandle(thread);
280 /*=========================================================================
281 ** Spawns off a new thread which will begin executing at the ThreadStart
282 ** method on the IThreadable interface passed in the constructor. Once the
283 ** thread is dead, it cannot be restarted with another call to Start.
285 ** Exceptions: ThreadStateException if the thread has already been started.
286 =========================================================================*/
287 [HostProtection(Synchronization=true,ExternalThreading=true)]
288 [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
291 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
292 Start(ref stackMark);
295 [HostProtection(Synchronization=true,ExternalThreading=true)]
296 [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
297 public void Start(object parameter)
299 //In the case of a null delegate (second call to start on same thread)
300 // StartInternal method will take care of the error reporting
301 if(m_Delegate is ThreadStart)
303 //We expect the thread to be setup with a ParameterizedThreadStart
304 // if this constructor is called.
305 //If we got here then that wasn't the case
306 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ThreadWrongThreadStart"));
308 m_ThreadStartArg = parameter;
309 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
310 Start(ref stackMark);
313 [System.Security.SecuritySafeCritical]
314 private void Start(ref StackCrawlMark stackMark)
316 #if FEATURE_COMINTEROP_APARTMENT_SUPPORT
317 // Eagerly initialize the COM Apartment state of the thread if we're allowed to.
318 StartupSetApartmentStateInternal();
319 #endif // FEATURE_COMINTEROP_APARTMENT_SUPPORT
321 // Attach current thread's security principal object to the new
322 // thread. Be careful not to bind the current thread to a principal
323 // if it's not already bound.
324 if (m_Delegate != null)
326 // If we reach here with a null delegate, something is broken. But we'll let the StartInternal method take care of
327 // reporting an error. Just make sure we dont try to dereference a null delegate.
328 ThreadHelper t = (ThreadHelper)(m_Delegate.Target);
329 ExecutionContext ec = ExecutionContext.Capture(
331 ExecutionContext.CaptureOptions.IgnoreSyncCtx);
332 t.SetExecutionContextHelper(ec);
334 IPrincipal principal = (IPrincipal)CallContext.Principal;
335 StartInternal(principal, ref stackMark);
339 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
340 internal ExecutionContext.Reader GetExecutionContextReader()
342 return new ExecutionContext.Reader(m_ExecutionContext);
345 internal bool ExecutionContextBelongsToCurrentScope
347 get { return !m_ExecutionContextBelongsToOuterScope; }
348 set { m_ExecutionContextBelongsToOuterScope = !value; }
352 internal bool ForbidExecutionContextMutation
354 set { m_ForbidExecutionContextMutation = value; }
358 // note: please don't access this directly from mscorlib. Use GetMutableExecutionContext or GetExecutionContextReader instead.
359 public ExecutionContext ExecutionContext
361 [SecuritySafeCritical]
362 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
365 ExecutionContext result;
366 if (this == Thread.CurrentThread)
367 result = GetMutableExecutionContext();
369 result = m_ExecutionContext;
376 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
377 internal ExecutionContext GetMutableExecutionContext()
379 Contract.Assert(Thread.CurrentThread == this);
381 Contract.Assert(!m_ForbidExecutionContextMutation);
383 if (m_ExecutionContext == null)
385 m_ExecutionContext = new ExecutionContext();
387 else if (!ExecutionContextBelongsToCurrentScope)
389 ExecutionContext copy = m_ExecutionContext.CreateMutableCopy();
390 m_ExecutionContext = copy;
393 ExecutionContextBelongsToCurrentScope = true;
394 return m_ExecutionContext;
398 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
399 internal void SetExecutionContext(ExecutionContext value, bool belongsToCurrentScope)
401 m_ExecutionContext = value;
402 ExecutionContextBelongsToCurrentScope = belongsToCurrentScope;
406 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
407 internal void SetExecutionContext(ExecutionContext.Reader value, bool belongsToCurrentScope)
409 m_ExecutionContext = value.DangerousGetRawExecutionContext();
410 ExecutionContextBelongsToCurrentScope = belongsToCurrentScope;
413 [System.Security.SecurityCritical] // auto-generated
414 [ResourceExposure(ResourceScope.None)]
415 [MethodImplAttribute(MethodImplOptions.InternalCall)]
416 private extern void StartInternal(IPrincipal principal, ref StackCrawlMark stackMark);
418 #if FEATURE_COMPRESSEDSTACK || MONO
420 [System.Security.SecurityCritical] // auto-generated_required
421 [DynamicSecurityMethodAttribute()]
422 [Obsolete("Thread.SetCompressedStack is no longer supported. Please use the System.Threading.CompressedStack class")]
423 public void SetCompressedStack( CompressedStack stack )
425 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ThreadAPIsNotSupported"));
428 [System.Security.SecurityCritical] // auto-generated
429 [ResourceExposure(ResourceScope.None)]
430 [MethodImplAttribute(MethodImplOptions.InternalCall), ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
431 internal extern IntPtr SetAppDomainStack( SafeCompressedStackHandle csHandle);
433 [System.Security.SecurityCritical] // auto-generated
434 [ResourceExposure(ResourceScope.None)]
435 [MethodImplAttribute(MethodImplOptions.InternalCall), ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
436 internal extern void RestoreAppDomainStack( IntPtr appDomainStack);
440 [System.Security.SecurityCritical] // auto-generated_required
441 [Obsolete("Thread.GetCompressedStack is no longer supported. Please use the System.Threading.CompressedStack class")]
442 public CompressedStack GetCompressedStack()
444 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ThreadAPIsNotSupported"));
446 #endif // #if FEATURE_COMPRESSEDSTACK
449 // Helper method to get a logical thread ID for StringBuilder (for
450 // correctness) and for FileStream's async code path (for perf, to
451 // avoid creating a Thread instance).
452 [System.Security.SecurityCritical] // auto-generated
453 [ResourceExposure(ResourceScope.None)]
454 [MethodImplAttribute(MethodImplOptions.InternalCall)]
456 [System.Runtime.ForceTokenStabilization]
457 #endif //!FEATURE_CORECLR
458 internal extern static IntPtr InternalGetCurrentThread();
460 /*=========================================================================
461 ** Raises a ThreadAbortException in the thread, which usually
462 ** results in the thread's death. The ThreadAbortException is a special
463 ** exception that is not catchable. The finally clauses of all try
464 ** statements will be executed before the thread dies. This includes the
465 ** finally that a thread might be executing at the moment the Abort is raised.
466 ** The thread is not stopped immediately--you must Join on the
467 ** thread to guarantee it has stopped.
468 ** It is possible for a thread to do an unbounded amount of computation in
469 ** the finally's and thus indefinitely delay the threads death.
470 ** If Abort() is called on a thread that has not been started, the thread
471 ** will abort when Start() is called.
472 ** If Abort is called twice on the same thread, a DuplicateThreadAbort
473 ** exception is thrown.
474 =========================================================================*/
477 [System.Security.SecuritySafeCritical] // auto-generated
478 [SecurityPermissionAttribute(SecurityAction.Demand, ControlThread=true)]
479 public void Abort(Object stateInfo)
481 // If two aborts come at the same time, it is possible that the state info
482 // gets set by one, and the actual abort gets delivered by another. But this
483 // is not distinguishable by an application.
484 // The accessor helper will only set the value if it isn't already set,
485 // and that particular bit of native code can test much faster than this
486 // code could, because testing might cause a cross-appdomain marshalling.
487 AbortReason = stateInfo;
489 // Note: we demand ControlThread permission, then call AbortInternal directly
490 // rather than delegating to the Abort() function below. We do this to ensure
491 // that only callers with ControlThread are allowed to change the AbortReason
492 // of the thread. We call AbortInternal directly to avoid demanding the same
499 [System.Security.SecurityCritical] // auto-generated
501 [System.Security.SecuritySafeCritical]
503 #pragma warning disable 618
504 [SecurityPermissionAttribute(SecurityAction.Demand, ControlThread = true)]
505 #pragma warning restore 618
508 #if FEATURE_LEGACYNETCF
509 if(CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
511 System.Reflection.Assembly callingAssembly = System.Reflection.Assembly.GetCallingAssembly();
512 if(callingAssembly != null && !callingAssembly.IsProfileAssembly)
514 string caller = new StackFrame(1).GetMethod().FullName;
515 string callee = System.Reflection.MethodBase.GetCurrentMethod().FullName;
516 throw new MethodAccessException(String.Format(
517 CultureInfo.CurrentCulture,
518 Environment.GetResourceString("Arg_MethodAccessException_WithCaller"),
523 #endif // FEATURE_LEGACYNETCF
527 // Internal helper (since we can't place security demands on
529 [System.Security.SecurityCritical] // auto-generated
530 [ResourceExposure(ResourceScope.None)]
531 [MethodImplAttribute(MethodImplOptions.InternalCall)]
532 private extern void AbortInternal();
534 #if !FEATURE_CORECLR || MONO
535 /*=========================================================================
536 ** Resets a thread abort.
537 ** Should be called by trusted code only
538 =========================================================================*/
539 [System.Security.SecuritySafeCritical] // auto-generated
540 [SecurityPermissionAttribute(SecurityAction.Demand, ControlThread=true)]
541 public static void ResetAbort()
543 Thread thread = Thread.CurrentThread;
544 if ((thread.ThreadState & ThreadState.AbortRequested) == 0)
545 throw new ThreadStateException(Environment.GetResourceString("ThreadState_NoAbortRequested"));
546 thread.ResetAbortNative();
547 thread.ClearAbortReason();
550 [System.Security.SecurityCritical] // auto-generated
551 [ResourceExposure(ResourceScope.None)]
552 [MethodImplAttribute(MethodImplOptions.InternalCall)]
553 private extern void ResetAbortNative();
555 /*=========================================================================
556 ** Suspends the thread. If the thread is already suspended, this call has
559 ** Exceptions: ThreadStateException if the thread has not been started or
561 =========================================================================*/
562 [System.Security.SecuritySafeCritical] // auto-generated
563 [Obsolete("Thread.Suspend has been deprecated. Please use other classes in System.Threading, such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect resources. http://go.microsoft.com/fwlink/?linkid=14202", false)][SecurityPermission(SecurityAction.Demand, ControlThread=true)]
564 [SecurityPermission(SecurityAction.Demand, ControlThread=true)]
565 public void Suspend() { SuspendInternal(); }
567 // Internal helper (since we can't place security demands on
569 [System.Security.SecurityCritical] // auto-generated
570 [ResourceExposure(ResourceScope.None)]
571 [MethodImplAttribute(MethodImplOptions.InternalCall)]
572 private extern void SuspendInternal();
574 /*=========================================================================
575 ** Resumes a thread that has been suspended.
577 ** Exceptions: ThreadStateException if the thread has not been started or
578 ** it is dead or it isn't in the suspended state.
579 =========================================================================*/
580 [System.Security.SecuritySafeCritical] // auto-generated
581 [Obsolete("Thread.Resume has been deprecated. Please use other classes in System.Threading, such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect resources. http://go.microsoft.com/fwlink/?linkid=14202", false)]
582 [SecurityPermission(SecurityAction.Demand, ControlThread=true)]
583 public void Resume() { ResumeInternal(); }
585 // Internal helper (since we can't place security demands on
587 [System.Security.SecurityCritical] // auto-generated
588 [ResourceExposure(ResourceScope.None)]
589 [MethodImplAttribute(MethodImplOptions.InternalCall)]
590 private extern void ResumeInternal();
592 /*=========================================================================
593 ** Interrupts a thread that is inside a Wait(), Sleep() or Join(). If that
594 ** thread is not currently blocked in that manner, it will be interrupted
595 ** when it next begins to block.
596 =========================================================================*/
597 [System.Security.SecuritySafeCritical] // auto-generated
598 [SecurityPermission(SecurityAction.Demand, ControlThread=true)]
599 public void Interrupt() { InterruptInternal(); }
601 // Internal helper (since we can't place security demands on
603 [System.Security.SecurityCritical] // auto-generated
604 [ResourceExposure(ResourceScope.None)]
605 [MethodImplAttribute(MethodImplOptions.InternalCall)]
606 private extern void InterruptInternal();
609 /*=========================================================================
610 ** Returns the priority of the thread.
612 ** Exceptions: ThreadStateException if the thread is dead.
613 =========================================================================*/
615 public ThreadPriority Priority {
616 [System.Security.SecuritySafeCritical] // auto-generated
617 get { return (ThreadPriority)GetPriorityNative(); }
618 [System.Security.SecuritySafeCritical] // auto-generated
619 [HostProtection(SelfAffectingThreading=true)]
620 set { SetPriorityNative((int)value); }
622 [System.Security.SecurityCritical] // auto-generated
623 [ResourceExposure(ResourceScope.None)]
624 [MethodImplAttribute(MethodImplOptions.InternalCall)]
625 private extern int GetPriorityNative();
626 [System.Security.SecurityCritical] // auto-generated
627 [ResourceExposure(ResourceScope.None)]
628 [MethodImplAttribute(MethodImplOptions.InternalCall)]
629 private extern void SetPriorityNative(int priority);
631 /*=========================================================================
632 ** Returns true if the thread has been started and is not dead.
633 =========================================================================*/
634 public extern bool IsAlive {
635 [System.Security.SecuritySafeCritical] // auto-generated
636 [MethodImpl(MethodImplOptions.InternalCall)]
640 /*=========================================================================
641 ** Returns true if the thread is a threadpool thread.
642 =========================================================================*/
643 public extern bool IsThreadPoolThread {
644 [System.Security.SecuritySafeCritical] // auto-generated
645 [MethodImpl(MethodImplOptions.InternalCall)]
649 /*=========================================================================
650 ** Waits for the thread to die or for timeout milliseconds to elapse.
651 ** Returns true if the thread died, or false if the wait timed out. If
652 ** Timeout.Infinite is given as the parameter, no timeout will occur.
654 ** Exceptions: ArgumentException if timeout < 0.
655 ** ThreadInterruptedException if the thread is interrupted while waiting.
656 ** ThreadStateException if the thread has not been started yet.
657 =========================================================================*/
658 [System.Security.SecurityCritical]
659 [ResourceExposure(ResourceScope.None)]
660 [MethodImplAttribute(MethodImplOptions.InternalCall)]
661 private extern bool JoinInternal(int millisecondsTimeout);
663 [System.Security.SecuritySafeCritical]
664 [HostProtection(Synchronization=true, ExternalThreading=true)]
667 JoinInternal(Timeout.Infinite);
670 [System.Security.SecuritySafeCritical]
671 [HostProtection(Synchronization=true, ExternalThreading=true)]
672 public bool Join(int millisecondsTimeout)
675 if (millisecondsTimeout < Timeout.Infinite)
676 throw new ArgumentOutOfRangeException("millisecondsTimeout", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1"));
678 return JoinInternal(millisecondsTimeout);
681 [HostProtection(Synchronization=true, ExternalThreading=true)]
682 public bool Join(TimeSpan timeout)
684 long tm = (long)timeout.TotalMilliseconds;
685 if (tm < -1 || tm > (long) Int32.MaxValue)
686 throw new ArgumentOutOfRangeException("timeout", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1"));
688 return Join((int)tm);
691 /*=========================================================================
692 ** Suspends the current thread for timeout milliseconds. If timeout == 0,
693 ** forces the thread to give up the remainer of its timeslice. If timeout
694 ** == Timeout.Infinite, no timeout will occur.
696 ** Exceptions: ArgumentException if timeout < 0.
697 ** ThreadInterruptedException if the thread is interrupted while sleeping.
698 =========================================================================*/
699 [System.Security.SecurityCritical] // auto-generated
700 [ResourceExposure(ResourceScope.None)]
701 [MethodImplAttribute(MethodImplOptions.InternalCall)]
702 private static extern void SleepInternal(int millisecondsTimeout);
704 [System.Security.SecuritySafeCritical] // auto-generated
705 public static void Sleep(int millisecondsTimeout)
708 if (millisecondsTimeout < Timeout.Infinite)
709 throw new ArgumentOutOfRangeException("millisecondsTimeout", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1"));
711 SleepInternal(millisecondsTimeout);
713 // Ensure we don't return to app code when the pause is underway
714 if(AppDomainPauseManager.IsPaused)
715 AppDomainPauseManager.ResumeEvent.WaitOneWithoutFAS();
719 public static void Sleep(TimeSpan timeout)
721 long tm = (long)timeout.TotalMilliseconds;
722 if (tm < -1 || tm > (long) Int32.MaxValue)
723 throw new ArgumentOutOfRangeException("timeout", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1"));
728 /* wait for a length of time proportial to 'iterations'. Each iteration is should
729 only take a few machine instructions. Calling this API is preferable to coding
730 a explict busy loop because the hardware can be informed that it is busy waiting. */
732 [System.Security.SecurityCritical] // auto-generated
733 [MethodImplAttribute(MethodImplOptions.InternalCall),
734 HostProtection(Synchronization=true,ExternalThreading=true),
735 ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success),
736 ResourceExposure(ResourceScope.None)]
737 private static extern void SpinWaitInternal(int iterations);
739 [System.Security.SecuritySafeCritical] // auto-generated
740 [HostProtection(Synchronization=true,ExternalThreading=true),
741 ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
742 public static void SpinWait(int iterations)
744 SpinWaitInternal(iterations);
747 [System.Security.SecurityCritical] // auto-generated
748 [MethodImplAttribute(MethodImplOptions.InternalCall),
749 HostProtection(Synchronization = true, ExternalThreading = true),
750 ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success),
751 ResourceExposure(ResourceScope.None)]
752 private static extern bool YieldInternal();
754 [System.Security.SecuritySafeCritical] // auto-generated
755 [HostProtection(Synchronization = true, ExternalThreading = true),
756 ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
757 public static bool Yield()
759 return YieldInternal();
763 public static Thread CurrentThread {
764 [System.Security.SecuritySafeCritical] // auto-generated
765 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
767 [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
770 Contract.Ensures(Contract.Result<Thread>() != null);
771 return GetCurrentThreadNative();
774 [System.Security.SecurityCritical] // auto-generated
775 [ResourceExposure(ResourceScope.None)]
776 [MethodImplAttribute(MethodImplOptions.InternalCall), ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
777 private static extern Thread GetCurrentThreadNative();
779 [System.Security.SecurityCritical] // auto-generated
780 private void SetStartHelper(Delegate start, int maxStackSize)
783 maxStackSize = GetProcessDefaultStackSize(maxStackSize);
786 // We only support default stacks in CoreCLR
787 Contract.Assert(maxStackSize == 0);
789 // Only fully-trusted code is allowed to create "large" stacks. Partial-trust falls back to
790 // the default stack size.
791 ulong defaultStackSize = GetProcessDefaultStackSize();
793 if ((ulong)(uint)maxStackSize > defaultStackSize)
797 SecurityPermission.Demand(PermissionType.FullTrust);
799 catch (SecurityException)
801 maxStackSize = (int)Math.Min(defaultStackSize, (ulong)(uint)int.MaxValue);
807 ThreadHelper threadStartCallBack = new ThreadHelper(start);
808 if(start is ThreadStart)
810 SetStart(new ThreadStart(threadStartCallBack.ThreadStart), maxStackSize);
814 SetStart(new ParameterizedThreadStart(threadStartCallBack.ThreadStart), maxStackSize);
819 [ResourceExposure(ResourceScope.None)]
820 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
821 [SuppressUnmanagedCodeSecurity]
822 private static extern ulong GetProcessDefaultStackSize();
824 /*=========================================================================
825 ** PRIVATE Sets the IThreadable interface for the thread. Assumes that
827 =========================================================================*/
828 [System.Security.SecurityCritical] // auto-generated
829 [ResourceExposure(ResourceScope.None)]
830 [MethodImplAttribute(MethodImplOptions.InternalCall)]
831 private extern void SetStart(Delegate start, int maxStackSize);
833 /*=========================================================================
834 ** Clean up the thread when it goes away.
835 =========================================================================*/
836 [System.Security.SecuritySafeCritical] // auto-generated
837 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
840 // Delegate to the unmanaged portion.
844 [System.Security.SecurityCritical] // auto-generated
845 [ResourceExposure(ResourceScope.None)]
846 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
847 [MethodImplAttribute(MethodImplOptions.InternalCall)]
848 private extern void InternalFinalize();
850 #if FEATURE_COMINTEROP
851 [System.Security.SecurityCritical] // auto-generated
852 [ResourceExposure(ResourceScope.None)]
853 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
854 [MethodImplAttribute(MethodImplOptions.InternalCall)]
855 public extern void DisableComObjectEagerCleanup();
856 #endif //FEATURE_COMINTEROP
858 /*=========================================================================
859 ** Return whether or not this thread is a background thread. Background
860 ** threads do not affect when the Execution Engine shuts down.
862 ** Exceptions: ThreadStateException if the thread is dead.
863 =========================================================================*/
864 public bool IsBackground {
865 [System.Security.SecuritySafeCritical] // auto-generated
866 get { return IsBackgroundNative(); }
867 [System.Security.SecuritySafeCritical] // auto-generated
868 [HostProtection(SelfAffectingThreading=true)]
869 set { SetBackgroundNative(value); }
871 [System.Security.SecurityCritical] // auto-generated
872 [ResourceExposure(ResourceScope.None)]
873 [MethodImplAttribute(MethodImplOptions.InternalCall)]
874 private extern bool IsBackgroundNative();
875 [System.Security.SecurityCritical] // auto-generated
876 [ResourceExposure(ResourceScope.None)]
877 [MethodImplAttribute(MethodImplOptions.InternalCall)]
878 private extern void SetBackgroundNative(bool isBackground);
881 /*=========================================================================
882 ** Return the thread state as a consistent set of bits. This is more
883 ** general then IsAlive or IsBackground.
884 =========================================================================*/
885 public ThreadState ThreadState {
886 [System.Security.SecuritySafeCritical] // auto-generated
887 get { return (ThreadState)GetThreadStateNative(); }
890 [System.Security.SecurityCritical] // auto-generated
891 [ResourceExposure(ResourceScope.None)]
892 [MethodImplAttribute(MethodImplOptions.InternalCall)]
893 private extern int GetThreadStateNative();
895 #if FEATURE_COMINTEROP_APARTMENT_SUPPORT
896 /*=========================================================================
897 ** An unstarted thread can be marked to indicate that it will host a
898 ** single-threaded or multi-threaded apartment.
900 ** Exceptions: ArgumentException if state is not a valid apartment state
901 ** (ApartmentSTA or ApartmentMTA).
902 =========================================================================*/
903 [Obsolete("The ApartmentState property has been deprecated. Use GetApartmentState, SetApartmentState or TrySetApartmentState instead.", false)]
904 public ApartmentState ApartmentState
906 [System.Security.SecuritySafeCritical] // auto-generated
909 return (ApartmentState)GetApartmentStateNative();
912 [System.Security.SecuritySafeCritical] // auto-generated
913 [HostProtection(Synchronization=true, SelfAffectingThreading=true)]
916 SetApartmentStateNative((int)value, true);
920 [System.Security.SecuritySafeCritical] // auto-generated
921 public ApartmentState GetApartmentState()
923 return (ApartmentState)GetApartmentStateNative();
926 [System.Security.SecuritySafeCritical] // auto-generated
927 [HostProtection(Synchronization=true, SelfAffectingThreading=true)]
928 public bool TrySetApartmentState(ApartmentState state)
930 return SetApartmentStateHelper(state, false);
933 [System.Security.SecuritySafeCritical] // auto-generated
934 [HostProtection(Synchronization=true, SelfAffectingThreading=true)]
935 public void SetApartmentState(ApartmentState state)
937 bool result = SetApartmentStateHelper(state, true);
939 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ApartmentStateSwitchFailed"));
942 [System.Security.SecurityCritical] // auto-generated
943 private bool SetApartmentStateHelper(ApartmentState state, bool fireMDAOnMismatch)
945 ApartmentState retState = (ApartmentState)SetApartmentStateNative((int)state, fireMDAOnMismatch);
947 // Special case where we pass in Unknown and get back MTA.
948 // Once we CoUninitialize the thread, the OS will still
949 // report the thread as implicitly in the MTA if any
950 // other thread in the process is CoInitialized.
951 if ((state == System.Threading.ApartmentState.Unknown) && (retState == System.Threading.ApartmentState.MTA))
954 if (retState != state)
960 [System.Security.SecurityCritical] // auto-generated
961 [ResourceExposure(ResourceScope.None)]
962 [MethodImplAttribute(MethodImplOptions.InternalCall)]
963 private extern int GetApartmentStateNative();
964 [System.Security.SecurityCritical] // auto-generated
965 [ResourceExposure(ResourceScope.None)]
966 [MethodImplAttribute(MethodImplOptions.InternalCall)]
967 private extern int SetApartmentStateNative(int state, bool fireMDAOnMismatch);
968 [System.Security.SecurityCritical] // auto-generated
969 [ResourceExposure(ResourceScope.None)]
970 [MethodImplAttribute(MethodImplOptions.InternalCall)]
971 private extern void StartupSetApartmentStateInternal();
972 #endif // FEATURE_COMINTEROP_APARTMENT_SUPPORT
974 /*=========================================================================
975 ** Allocates an un-named data slot. The slot is allocated on ALL the
977 =========================================================================*/
978 [HostProtection(SharedState=true, ExternalThreading=true)]
979 public static LocalDataStoreSlot AllocateDataSlot()
981 return LocalDataStoreManager.AllocateDataSlot();
984 /*=========================================================================
985 ** Allocates a named data slot. The slot is allocated on ALL the
986 ** threads. Named data slots are "public" and can be manipulated by
988 =========================================================================*/
989 [HostProtection(SharedState=true, ExternalThreading=true)]
990 public static LocalDataStoreSlot AllocateNamedDataSlot(String name)
992 return LocalDataStoreManager.AllocateNamedDataSlot(name);
995 /*=========================================================================
996 ** Looks up a named data slot. If the name has not been used, a new slot is
997 ** allocated. Named data slots are "public" and can be manipulated by
999 =========================================================================*/
1000 [HostProtection(SharedState=true, ExternalThreading=true)]
1001 public static LocalDataStoreSlot GetNamedDataSlot(String name)
1003 return LocalDataStoreManager.GetNamedDataSlot(name);
1006 /*=========================================================================
1007 ** Frees a named data slot. The slot is allocated on ALL the
1008 ** threads. Named data slots are "public" and can be manipulated by
1010 =========================================================================*/
1011 [HostProtection(SharedState=true, ExternalThreading=true)]
1012 public static void FreeNamedDataSlot(String name)
1014 LocalDataStoreManager.FreeNamedDataSlot(name);
1017 /*=========================================================================
1018 ** Retrieves the value from the specified slot on the current thread, for that thread's current domain.
1019 =========================================================================*/
1020 [HostProtection(SharedState=true, ExternalThreading=true)]
1021 [ResourceExposure(ResourceScope.AppDomain)]
1022 public static Object GetData(LocalDataStoreSlot slot)
1024 LocalDataStoreHolder dls = s_LocalDataStore;
1027 // Make sure to validate the slot even if we take the quick path
1028 LocalDataStoreManager.ValidateSlot(slot);
1032 return dls.Store.GetData(slot);
1035 /*=========================================================================
1036 ** Sets the data in the specified slot on the currently running thread, for that thread's current domain.
1037 =========================================================================*/
1038 [HostProtection(SharedState=true, ExternalThreading=true)]
1039 [ResourceExposure(ResourceScope.AppDomain)]
1040 public static void SetData(LocalDataStoreSlot slot, Object data)
1042 LocalDataStoreHolder dls = s_LocalDataStore;
1044 // Create new DLS if one hasn't been created for this domain for this thread
1046 dls = LocalDataStoreManager.CreateLocalDataStore();
1047 s_LocalDataStore = dls;
1050 dls.Store.SetData(slot, data);
1054 // #threadCultureInfo
1057 // In the desktop runtime, we allow a thread's cultures to travel with the thread
1058 // across AppDomain boundaries. Furthermore we update the native thread with the
1059 // culture of the managed thread. Because of security concerns and potential SxS
1060 // effects, in Silverlight we are making the changes listed below.
1062 // Silverlight Changes:
1063 // - thread instance member cultures (CurrentCulture and CurrentUICulture)
1064 // confined within AppDomains
1065 // - changes to these properties don't affect the underlying native thread
1068 // FEATURE_LEAK_CULTURE_INFO : CultureInfos can leak across AppDomains, not
1069 // enabled in Silverlight
1071 // Implementation notes:
1072 // In Silverlight, culture members thread static (per Thread, per AppDomain).
1075 // An interesting side-effect of isolating cultures within an AppDomain is that we
1076 // now need to special case resource lookup for mscorlib, which transitions to the
1077 // default domain to lookup resources. See Environment.cs for more details.
1079 #if FEATURE_LEAK_CULTURE_INFO
1080 [System.Security.SecurityCritical] // auto-generated
1081 [ResourceExposure(ResourceScope.None)]
1082 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1083 static extern private bool nativeGetSafeCulture(Thread t, int appDomainId, bool isUI, ref CultureInfo safeCulture);
1084 #endif // FEATURE_LEAK_CULTURE_INFO
1086 // As the culture can be customized object then we cannot hold any
1087 // reference to it before we check if it is safe because the app domain
1088 // owning this customized culture may get unloaded while executing this
1089 // code. To achieve that we have to do the check using nativeGetSafeCulture
1090 // as the thread cannot get interrupted during the FCALL.
1091 // If the culture is safe (not customized or created in current app domain)
1092 // then the FCALL will return a reference to that culture otherwise the
1093 // FCALL will return failure. In case of failure we'll return the default culture.
1094 // If the app domain owning a customized culture that is set to teh thread and this
1095 // app domain get unloaded there is a code to clean up the culture from the thread
1096 // using the code in AppDomain::ReleaseDomainStores.
1098 public CultureInfo CurrentUICulture {
1100 Contract.Ensures(Contract.Result<CultureInfo>() != null);
1102 if(AppDomain.IsAppXModel()) {
1103 return CultureInfo.GetCultureInfoForUserPreferredLanguageInAppX() ?? GetCurrentUICultureNoAppX();
1108 return GetCurrentUICultureNoAppX();
1112 [System.Security.SecuritySafeCritical] // auto-generated
1113 [HostProtection(ExternalThreading=true)]
1115 if (value == null) {
1116 throw new ArgumentNullException("value");
1118 Contract.EndContractBlock();
1120 //If they're trying to use a Culture with a name that we can't use in resource lookup,
1121 //don't even let them set it on the thread.
1122 CultureInfo.VerifyCultureName(value, true);
1124 // If you add more pre-conditions to this method, check to see if you also need to
1125 // add them to CultureInfo.DefaultThreadCurrentUICulture.set.
1127 #if FEATURE_LEAK_CULTURE_INFO
1128 if (nativeSetThreadUILocale(value.SortName) == false)
1130 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidResourceCultureName", value.Name));
1132 value.StartCrossDomainTracking();
1134 if (m_CurrentUICulture == null && m_CurrentCulture == null)
1135 nativeInitCultureAccessors();
1138 #if FEATURE_LEGACYNETCF
1139 if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
1142 // NetCF had a bug where Thread.Current{UI}Culture would set the culture for every thread in the process.
1143 // This was because they stored the value in a regular static field (NetCF has no support for ThreadStatic fields).
1144 // Some apps depend on the broken behavior. We will emulate this behavior by redirecting setters to
1145 // DefaultThreadCurrentUICulture. (Note that this property did not existed in NetCF and so it is fine to piggy back
1146 // on it for the quirk.)
1148 CultureInfo.SetCurrentUICultureQuirk(value);
1153 m_CurrentUICulture = value;
1157 #if FEATURE_LEAK_CULTURE_INFO
1158 [System.Security.SecuritySafeCritical] // auto-generated
1160 internal CultureInfo GetCurrentUICultureNoAppX() {
1162 Contract.Ensures(Contract.Result<CultureInfo>() != null);
1164 // Fetch a local copy of m_CurrentUICulture to
1165 // avoid ----s that malicious user can introduce
1166 if (m_CurrentUICulture == null) {
1167 CultureInfo appDomainDefaultUICulture = CultureInfo.DefaultThreadCurrentUICulture;
1168 return (appDomainDefaultUICulture != null ? appDomainDefaultUICulture : CultureInfo.UserDefaultUICulture);
1171 #if FEATURE_LEAK_CULTURE_INFO
1172 CultureInfo culture = null;
1174 if (!nativeGetSafeCulture(this, GetDomainID(), true, ref culture) || culture == null) {
1175 return CultureInfo.UserDefaultUICulture;
1180 return m_CurrentUICulture;
1184 // This returns the exposed context for a given context ID.
1185 #if FEATURE_LEAK_CULTURE_INFO
1186 [System.Security.SecurityCritical] // auto-generated
1187 [ResourceExposure(ResourceScope.None)]
1188 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1189 static extern private bool nativeSetThreadUILocale(String locale);
1192 // As the culture can be customized object then we cannot hold any
1193 // reference to it before we check if it is safe because the app domain
1194 // owning this customized culture may get unloaded while executing this
1195 // code. To achieve that we have to do the check using nativeGetSafeCulture
1196 // as the thread cannot get interrupted during the FCALL.
1197 // If the culture is safe (not customized or created in current app domain)
1198 // then the FCALL will return a reference to that culture otherwise the
1199 // FCALL will return failure. In case of failure we'll return the default culture.
1200 // If the app domain owning a customized culture that is set to teh thread and this
1201 // app domain get unloaded there is a code to clean up the culture from the thread
1202 // using the code in AppDomain::ReleaseDomainStores.
1204 public CultureInfo CurrentCulture {
1206 Contract.Ensures(Contract.Result<CultureInfo>() != null);
1209 if(AppDomain.IsAppXModel()) {
1210 return CultureInfo.GetCultureInfoForUserPreferredLanguageInAppX() ?? GetCurrentCultureNoAppX();
1215 return GetCurrentCultureNoAppX();
1219 [System.Security.SecuritySafeCritical] // auto-generated
1220 #if FEATURE_LEAK_CULTURE_INFO
1221 [SecurityPermission(SecurityAction.Demand, ControlThread = true)]
1225 throw new ArgumentNullException("value");
1227 Contract.EndContractBlock();
1229 // If you add more pre-conditions to this method, check to see if you also need to
1230 // add them to CultureInfo.DefaultThreadCurrentCulture.set.
1232 #if FEATURE_LEAK_CULTURE_INFO
1233 //If we can't set the nativeThreadLocale, we'll just let it stay
1234 //at whatever value it had before. This allows people who use
1235 //just managed code not to be limited by the underlying OS.
1236 CultureInfo.nativeSetThreadLocale(value.SortName);
1237 value.StartCrossDomainTracking();
1239 if (m_CurrentCulture == null && m_CurrentUICulture == null)
1240 nativeInitCultureAccessors();
1243 #if FEATURE_LEGACYNETCF
1244 if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
1246 // See comment in CurrentUICulture setter
1247 CultureInfo.SetCurrentCultureQuirk(value);
1252 m_CurrentCulture = value;
1256 #if FEATURE_LEAK_CULTURE_INFO
1257 [System.Security.SecuritySafeCritical] // auto-generated
1259 private CultureInfo GetCurrentCultureNoAppX() {
1261 Contract.Ensures(Contract.Result<CultureInfo>() != null);
1263 // Fetch a local copy of m_CurrentCulture to
1264 // avoid ----s that malicious user can introduce
1265 if (m_CurrentCulture == null) {
1266 CultureInfo appDomainDefaultCulture = CultureInfo.DefaultThreadCurrentCulture;
1267 return (appDomainDefaultCulture != null ? appDomainDefaultCulture : CultureInfo.UserDefaultCulture);
1270 #if FEATURE_LEAK_CULTURE_INFO
1271 CultureInfo culture = null;
1273 if (!nativeGetSafeCulture(this, GetDomainID(), false, ref culture) || culture == null) {
1274 return CultureInfo.UserDefaultCulture;
1279 return m_CurrentCulture;
1284 #if! FEATURE_LEAK_CULTURE_INFO
1285 [System.Security.SecurityCritical] // auto-generated
1286 [ResourceExposure(ResourceScope.None)]
1287 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
1288 [SuppressUnmanagedCodeSecurity]
1289 private static extern void nativeInitCultureAccessors();
1292 /*=============================================================*/
1294 /*======================================================================
1295 ** Current thread context is stored in a slot in the thread local store
1296 ** CurrentContext gets the Context from the slot.
1297 ======================================================================*/
1298 #if FEATURE_REMOTING
1299 public static Context CurrentContext
1301 [System.Security.SecurityCritical] // auto-generated_required
1304 return CurrentThread.GetCurrentContextInternal();
1308 [System.Security.SecurityCritical] // auto-generated
1309 internal Context GetCurrentContextInternal()
1311 if (m_Context == null)
1313 m_Context = Context.DefaultContext;
1320 #if FEATURE_IMPERSONATION
1321 // Get and set thread's current principal (for role based security).
1322 public static IPrincipal CurrentPrincipal
1324 [System.Security.SecuritySafeCritical] // auto-generated
1327 lock (CurrentThread)
1329 IPrincipal principal = (IPrincipal)
1330 CallContext.Principal;
1331 if (principal == null)
1333 principal = GetDomain().GetThreadPrincipal();
1334 CallContext.Principal = principal;
1340 [System.Security.SecuritySafeCritical] // auto-generated
1341 [SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPrincipal)]
1342 #if !FEATURE_CORECLR
1343 [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
1347 CallContext.Principal = value;
1350 #endif // FEATURE_IMPERSONATION
1352 // Private routine called from unmanaged code to set an initial
1353 // principal for a newly created thread.
1354 [System.Security.SecurityCritical] // auto-generated
1355 private void SetPrincipalInternal(IPrincipal principal)
1357 GetMutableExecutionContext().LogicalCallContext.SecurityData.Principal = principal;
1360 #if FEATURE_REMOTING
1362 // This returns the exposed context for a given context ID.
1363 [System.Security.SecurityCritical] // auto-generated
1364 [ResourceExposure(ResourceScope.None)]
1365 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1366 internal static extern Context GetContextInternal(IntPtr id);
1368 [System.Security.SecurityCritical] // auto-generated
1369 [ResourceExposure(ResourceScope.None)]
1370 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1371 internal extern Object InternalCrossContextCallback(Context ctx, IntPtr ctxID, Int32 appDomainID, InternalCrossContextDelegate ftnToCall, Object[] args);
1373 [System.Security.SecurityCritical] // auto-generated
1374 internal Object InternalCrossContextCallback(Context ctx, InternalCrossContextDelegate ftnToCall, Object[] args)
1376 return InternalCrossContextCallback(ctx, ctx.InternalContextID, 0, ftnToCall, args);
1379 // CompleteCrossContextCallback is called by the EE after transitioning to the requested context
1380 private static Object CompleteCrossContextCallback(InternalCrossContextDelegate ftnToCall, Object[] args)
1382 return ftnToCall(args);
1384 #endif // FEATURE_REMOTING
1386 /*======================================================================
1387 ** Returns the current domain in which current thread is running.
1388 ======================================================================*/
1390 [System.Security.SecurityCritical] // auto-generated
1391 [ResourceExposure(ResourceScope.None)]
1392 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1393 private static extern AppDomain GetDomainInternal();
1394 [System.Security.SecurityCritical] // auto-generated
1395 [ResourceExposure(ResourceScope.None)]
1396 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1397 private static extern AppDomain GetFastDomainInternal();
1399 [System.Security.SecuritySafeCritical] // auto-generated
1400 public static AppDomain GetDomain()
1402 Contract.Ensures(Contract.Result<AppDomain>() != null);
1406 ad = GetFastDomainInternal();
1408 ad = GetDomainInternal();
1410 #if FEATURE_REMOTING
1411 Contract.Assert(CurrentThread.m_Context == null || CurrentThread.m_Context.AppDomain == ad, "AppDomains on the managed & unmanaged threads should match");
1418 * This returns a unique id to identify an appdomain.
1420 public static int GetDomainID()
1422 return GetDomain().GetId();
1426 // Retrieves the name of the thread.
1428 public String Name {
1433 [System.Security.SecuritySafeCritical] // auto-generated
1434 [HostProtection(ExternalThreading=true)]
1438 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_WriteOnce"));
1441 InformThreadNameChange(GetNativeHandle(), value, (value != null) ? value.Length : 0);
1446 [System.Security.SecurityCritical] // auto-generated
1447 [ResourceExposure(ResourceScope.None)]
1448 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
1449 [SuppressUnmanagedCodeSecurity]
1450 private static extern void InformThreadNameChange(ThreadHandle t, String name, int len);
1452 internal Object AbortReason {
1453 [System.Security.SecurityCritical] // auto-generated
1455 object result = null;
1458 result = GetAbortReason();
1462 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ExceptionStateCrossAppDomain"), e);
1466 [System.Security.SecurityCritical] // auto-generated
1467 set { SetAbortReason(value); }
1471 * This marks the beginning of a critical code region.
1473 [System.Security.SecuritySafeCritical] // auto-generated
1474 [HostProtection(Synchronization=true, ExternalThreading=true)]
1475 [ResourceExposure(ResourceScope.None)]
1476 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1477 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
1478 public static extern void BeginCriticalRegion();
1481 * This marks the end of a critical code region.
1483 [System.Security.SecuritySafeCritical] // auto-generated
1484 [HostProtection(Synchronization=true, ExternalThreading=true)]
1485 [ResourceExposure(ResourceScope.None)]
1486 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1487 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
1488 public static extern void EndCriticalRegion();
1491 * This marks the beginning of a code region that requires thread affinity.
1493 [System.Security.SecurityCritical] // auto-generated_required
1494 [ResourceExposure(ResourceScope.None)]
1495 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1496 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
1497 public static extern void BeginThreadAffinity();
1500 * This marks the end of a code region that requires thread affinity.
1502 [System.Security.SecurityCritical] // auto-generated_required
1503 [ResourceExposure(ResourceScope.None)]
1504 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1505 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
1506 public static extern void EndThreadAffinity();
1508 /*=========================================================================
1509 ** Volatile Read & Write and MemoryBarrier methods.
1510 ** Provides the ability to read and write values ensuring that the values
1511 ** are read/written each time they are accessed.
1512 =========================================================================*/
1514 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1515 public static byte VolatileRead(ref byte address)
1518 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1522 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1523 public static short VolatileRead(ref short address)
1525 short ret = address;
1526 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1530 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1531 public static int VolatileRead(ref int address)
1534 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1538 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1539 public static long VolatileRead(ref long address)
1542 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1546 [CLSCompliant(false)]
1547 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1548 public static sbyte VolatileRead(ref sbyte address)
1550 sbyte ret = address;
1551 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1555 [CLSCompliant(false)]
1556 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1557 public static ushort VolatileRead(ref ushort address)
1559 ushort ret = address;
1560 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1564 [CLSCompliant(false)]
1565 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1566 public static uint VolatileRead(ref uint address)
1569 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1573 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1574 public static IntPtr VolatileRead(ref IntPtr address)
1576 IntPtr ret = address;
1577 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1581 [CLSCompliant(false)]
1582 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1583 public static UIntPtr VolatileRead(ref UIntPtr address)
1585 UIntPtr ret = address;
1586 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1590 [CLSCompliant(false)]
1591 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1592 public static ulong VolatileRead(ref ulong address)
1594 ulong ret = address;
1595 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1599 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1600 public static float VolatileRead(ref float address)
1602 float ret = address;
1603 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1607 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1608 public static double VolatileRead(ref double address)
1610 double ret = address;
1611 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1615 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1616 public static Object VolatileRead(ref Object address)
1618 Object ret = address;
1619 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1623 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1624 public static void VolatileWrite(ref byte address, byte value)
1626 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1630 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1631 public static void VolatileWrite(ref short address, short value)
1633 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1637 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1638 public static void VolatileWrite(ref int address, int value)
1640 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1644 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1645 public static void VolatileWrite(ref long address, long value)
1647 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1651 [CLSCompliant(false)]
1652 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1653 public static void VolatileWrite(ref sbyte address, sbyte value)
1655 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1659 [CLSCompliant(false)]
1660 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1661 public static void VolatileWrite(ref ushort address, ushort value)
1663 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1667 [CLSCompliant(false)]
1668 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1669 public static void VolatileWrite(ref uint address, uint value)
1671 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1675 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1676 public static void VolatileWrite(ref IntPtr address, IntPtr value)
1678 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1682 [CLSCompliant(false)]
1683 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1684 public static void VolatileWrite(ref UIntPtr address, UIntPtr value)
1686 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1690 [CLSCompliant(false)]
1691 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1692 public static void VolatileWrite(ref ulong address, ulong value)
1694 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1698 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1699 public static void VolatileWrite(ref float address, float value)
1701 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1705 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1706 public static void VolatileWrite(ref double address, double value)
1708 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1712 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1713 public static void VolatileWrite(ref Object address, Object value)
1715 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1719 [System.Security.SecuritySafeCritical] // auto-generated
1720 [ResourceExposure(ResourceScope.None)]
1721 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1722 public static extern void MemoryBarrier();
1724 private static LocalDataStoreMgr LocalDataStoreManager
1728 if (s_LocalDataStoreMgr == null)
1730 Interlocked.CompareExchange(ref s_LocalDataStoreMgr, new LocalDataStoreMgr(), null);
1733 return s_LocalDataStoreMgr;
1737 void _Thread.GetTypeInfoCount(out uint pcTInfo)
1739 throw new NotImplementedException();
1742 void _Thread.GetTypeInfo(uint iTInfo, uint lcid, IntPtr ppTInfo)
1744 throw new NotImplementedException();
1747 void _Thread.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1749 throw new NotImplementedException();
1752 void _Thread.Invoke(uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1754 throw new NotImplementedException();
1758 // Helper function to set the AbortReason for a thread abort.
1759 // Checks that they're not alredy set, and then atomically updates
1760 // the reason info (object + ADID).
1761 [System.Security.SecurityCritical] // auto-generated
1762 [ResourceExposure(ResourceScope.None)]
1763 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1764 internal extern void SetAbortReason(Object o);
1766 // Helper function to retrieve the AbortReason from a thread
1767 // abort. Will perform cross-AppDomain marshalling if the object
1768 // lives in a different AppDomain from the requester.
1769 [System.Security.SecurityCritical] // auto-generated
1770 [ResourceExposure(ResourceScope.None)]
1771 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1772 internal extern Object GetAbortReason();
1774 // Helper function to clear the AbortReason. Takes care of
1775 // AppDomain related cleanup if required.
1776 [System.Security.SecurityCritical] // auto-generated
1777 [ResourceExposure(ResourceScope.None)]
1778 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1779 internal extern void ClearAbortReason();
1782 } // End of class Thread
1784 // declaring a local var of this enum type and passing it by ref into a function that needs to do a
1785 // stack crawl will both prevent inlining of the calle and pass an ESP point to stack crawl to
1786 // Declaring these in EH clauses is illegal; they must declared in the main method body
1788 internal enum StackCrawlMark
1791 LookForMyCaller = 1,
1792 LookForMyCallersCaller = 2,