3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // <OWNER>Microsoft</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;
24 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;
143 private ExecutionContext m_ExecutionContext; // this call context follows the logical thread
146 private String m_Name;
147 private Delegate m_Delegate; // Delegate
149 #if FEATURE_LEAK_CULTURE_INFO
150 private CultureInfo m_CurrentCulture;
151 private CultureInfo m_CurrentUICulture;
153 private Object m_ThreadStartArg;
155 /*=========================================================================
156 ** The base implementation of Thread is all native. The following fields
157 ** should never be used in the C# code. They are here to define the proper
158 ** space so the thread object may be allocated. DON'T CHANGE THESE UNLESS
159 ** YOU MODIFY ThreadBaseObject in vm\object.h
160 =========================================================================*/
161 #pragma warning disable 169
162 #pragma warning disable 414 // These fields are not used from managed.
163 // IntPtrs need to be together, and before ints, because IntPtrs are 64-bit
164 // fields on 64-bit platforms, where they will be sorted together.
166 private IntPtr DONT_USE_InternalThread; // Pointer
167 private int m_Priority; // INT32
168 private int m_ManagedThreadId; // INT32
170 #pragma warning restore 414
171 #pragma warning restore 169
173 private bool m_ExecutionContextBelongsToOuterScope;
175 private bool m_ForbidExecutionContextMutation;
178 /*=========================================================================
179 ** This manager is responsible for storing the global data that is
180 ** shared amongst all the thread local stores.
181 =========================================================================*/
182 static private LocalDataStoreMgr s_LocalDataStoreMgr;
184 /*=========================================================================
185 ** Thread-local data store
186 =========================================================================*/
188 static private LocalDataStoreHolder s_LocalDataStore;
190 // Do not move! Order of above fields needs to be preserved for alignment
192 // See code:#threadCultureInfo
193 #if !FEATURE_LEAK_CULTURE_INFO
195 internal static CultureInfo m_CurrentCulture;
197 internal static CultureInfo m_CurrentUICulture;
200 static AsyncLocal<CultureInfo> s_asyncLocalCurrentCulture;
201 static AsyncLocal<CultureInfo> s_asyncLocalCurrentUICulture;
203 static void AsyncLocalSetCurrentCulture(AsyncLocalValueChangedArgs<CultureInfo> args)
205 #if FEATURE_LEAK_CULTURE_INFO
206 Thread.CurrentThread.m_CurrentCulture = args.CurrentValue;
208 m_CurrentCulture = args.CurrentValue;
209 #endif // FEATURE_LEAK_CULTURE_INFO
212 static void AsyncLocalSetCurrentUICulture(AsyncLocalValueChangedArgs<CultureInfo> args)
214 #if FEATURE_LEAK_CULTURE_INFO
215 Thread.CurrentThread.m_CurrentUICulture = args.CurrentValue;
217 m_CurrentUICulture = args.CurrentValue;
218 #endif // FEATURE_LEAK_CULTURE_INFO
222 // Adding an empty default ctor for annotation purposes
223 [System.Security.SecuritySafeCritical] // auto-generated
225 #endif // FEATURE_CORECLR
227 /*=========================================================================
228 ** Creates a new Thread object which will begin execution at
229 ** start.ThreadStart on a new thread when the Start method is called.
231 ** Exceptions: ArgumentNullException if start == null.
232 =========================================================================*/
233 [System.Security.SecuritySafeCritical] // auto-generated
234 public Thread(ThreadStart start) {
236 throw new ArgumentNullException("start");
238 Contract.EndContractBlock();
239 SetStartHelper((Delegate)start,0); //0 will setup Thread with default stackSize
242 [System.Security.SecuritySafeCritical] // auto-generated
243 public Thread(ThreadStart start, int maxStackSize) {
245 throw new ArgumentNullException("start");
247 if (0 > maxStackSize)
248 throw new ArgumentOutOfRangeException("maxStackSize",Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
249 Contract.EndContractBlock();
250 SetStartHelper((Delegate)start, maxStackSize);
253 [System.Security.SecuritySafeCritical] // auto-generated
254 public Thread(ParameterizedThreadStart start) {
256 throw new ArgumentNullException("start");
258 Contract.EndContractBlock();
259 SetStartHelper((Delegate)start, 0);
262 [System.Security.SecuritySafeCritical] // auto-generated
263 public Thread(ParameterizedThreadStart start, int maxStackSize) {
265 throw new ArgumentNullException("start");
267 if (0 > maxStackSize)
268 throw new ArgumentOutOfRangeException("maxStackSize",Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
269 Contract.EndContractBlock();
270 SetStartHelper((Delegate)start, maxStackSize);
274 public override int GetHashCode()
276 return m_ManagedThreadId;
279 extern public int ManagedThreadId
281 [ResourceExposure(ResourceScope.None)]
282 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
283 [MethodImplAttribute(MethodImplOptions.InternalCall)]
284 [System.Security.SecuritySafeCritical] // auto-generated
288 // Returns handle for interop with EE. The handle is guaranteed to be non-null.
289 internal unsafe ThreadHandle GetNativeHandle()
291 IntPtr thread = DONT_USE_InternalThread;
293 // This should never happen under normal circumstances. m_assembly is always assigned before it is handed out to the user.
294 // There are ways how to create an unitialized objects through remoting, etc. Avoid AVing in the EE by throwing a nice
297 throw new ArgumentException(null, Environment.GetResourceString("Argument_InvalidHandle"));
299 return new ThreadHandle(thread);
303 /*=========================================================================
304 ** Spawns off a new thread which will begin executing at the ThreadStart
305 ** method on the IThreadable interface passed in the constructor. Once the
306 ** thread is dead, it cannot be restarted with another call to Start.
308 ** Exceptions: ThreadStateException if the thread has already been started.
309 =========================================================================*/
310 [HostProtection(Synchronization=true,ExternalThreading=true)]
311 [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
314 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
315 Start(ref stackMark);
318 [HostProtection(Synchronization=true,ExternalThreading=true)]
319 [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
320 public void Start(object parameter)
322 //In the case of a null delegate (second call to start on same thread)
323 // StartInternal method will take care of the error reporting
324 if(m_Delegate is ThreadStart)
326 //We expect the thread to be setup with a ParameterizedThreadStart
327 // if this constructor is called.
328 //If we got here then that wasn't the case
329 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ThreadWrongThreadStart"));
331 m_ThreadStartArg = parameter;
332 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
333 Start(ref stackMark);
336 [System.Security.SecuritySafeCritical]
337 private void Start(ref StackCrawlMark stackMark)
339 #if FEATURE_COMINTEROP_APARTMENT_SUPPORT
340 // Eagerly initialize the COM Apartment state of the thread if we're allowed to.
341 StartupSetApartmentStateInternal();
342 #endif // FEATURE_COMINTEROP_APARTMENT_SUPPORT
344 // Attach current thread's security principal object to the new
345 // thread. Be careful not to bind the current thread to a principal
346 // if it's not already bound.
347 if (m_Delegate != null)
349 // If we reach here with a null delegate, something is broken. But we'll let the StartInternal method take care of
350 // reporting an error. Just make sure we dont try to dereference a null delegate.
351 ThreadHelper t = (ThreadHelper)(m_Delegate.Target);
352 ExecutionContext ec = ExecutionContext.Capture(
354 ExecutionContext.CaptureOptions.IgnoreSyncCtx);
355 t.SetExecutionContextHelper(ec);
357 #if FEATURE_IMPERSONATION
358 IPrincipal principal = (IPrincipal)CallContext.Principal;
360 IPrincipal principal = null;
362 StartInternal(principal, ref stackMark);
367 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
368 internal ExecutionContext.Reader GetExecutionContextReader()
370 return new ExecutionContext.Reader(m_ExecutionContext);
373 internal bool ExecutionContextBelongsToCurrentScope
375 get { return !m_ExecutionContextBelongsToOuterScope; }
376 set { m_ExecutionContextBelongsToOuterScope = !value; }
380 internal bool ForbidExecutionContextMutation
382 set { m_ForbidExecutionContextMutation = value; }
386 // note: please don't access this directly from mscorlib. Use GetMutableExecutionContext or GetExecutionContextReader instead.
387 public ExecutionContext ExecutionContext
389 [SecuritySafeCritical]
390 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
393 ExecutionContext result;
394 if (this == Thread.CurrentThread)
395 result = GetMutableExecutionContext();
397 result = m_ExecutionContext;
404 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
405 internal ExecutionContext GetMutableExecutionContext()
407 Contract.Assert(Thread.CurrentThread == this);
409 Contract.Assert(!m_ForbidExecutionContextMutation);
411 if (m_ExecutionContext == null)
413 m_ExecutionContext = new ExecutionContext();
415 else if (!ExecutionContextBelongsToCurrentScope)
417 ExecutionContext copy = m_ExecutionContext.CreateMutableCopy();
418 m_ExecutionContext = copy;
421 ExecutionContextBelongsToCurrentScope = true;
422 return m_ExecutionContext;
426 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
427 internal void SetExecutionContext(ExecutionContext value, bool belongsToCurrentScope)
429 m_ExecutionContext = value;
430 ExecutionContextBelongsToCurrentScope = belongsToCurrentScope;
434 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
435 internal void SetExecutionContext(ExecutionContext.Reader value, bool belongsToCurrentScope)
437 m_ExecutionContext = value.DangerousGetRawExecutionContext();
438 ExecutionContextBelongsToCurrentScope = belongsToCurrentScope;
440 #endif //!FEATURE_CORECLR
443 [System.Security.SecurityCritical] // auto-generated
444 [ResourceExposure(ResourceScope.None)]
445 [MethodImplAttribute(MethodImplOptions.InternalCall)]
446 private extern void StartInternal(IPrincipal principal, ref StackCrawlMark stackMark);
448 #if FEATURE_COMPRESSEDSTACK || MONO
450 [System.Security.SecurityCritical] // auto-generated_required
451 [DynamicSecurityMethodAttribute()]
452 [Obsolete("Thread.SetCompressedStack is no longer supported. Please use the System.Threading.CompressedStack class")]
453 public void SetCompressedStack( CompressedStack stack )
455 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ThreadAPIsNotSupported"));
458 [System.Security.SecurityCritical] // auto-generated
459 [ResourceExposure(ResourceScope.None)]
460 [MethodImplAttribute(MethodImplOptions.InternalCall), ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
461 internal extern IntPtr SetAppDomainStack( SafeCompressedStackHandle csHandle);
463 [System.Security.SecurityCritical] // auto-generated
464 [ResourceExposure(ResourceScope.None)]
465 [MethodImplAttribute(MethodImplOptions.InternalCall), ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
466 internal extern void RestoreAppDomainStack( IntPtr appDomainStack);
470 [System.Security.SecurityCritical] // auto-generated_required
471 [Obsolete("Thread.GetCompressedStack is no longer supported. Please use the System.Threading.CompressedStack class")]
472 public CompressedStack GetCompressedStack()
474 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ThreadAPIsNotSupported"));
476 #endif // #if FEATURE_COMPRESSEDSTACK
479 // Helper method to get a logical thread ID for StringBuilder (for
480 // correctness) and for FileStream's async code path (for perf, to
481 // avoid creating a Thread instance).
482 [System.Security.SecurityCritical] // auto-generated
483 [ResourceExposure(ResourceScope.None)]
484 [MethodImplAttribute(MethodImplOptions.InternalCall)]
485 internal extern static IntPtr InternalGetCurrentThread();
487 /*=========================================================================
488 ** Raises a ThreadAbortException in the thread, which usually
489 ** results in the thread's death. The ThreadAbortException is a special
490 ** exception that is not catchable. The finally clauses of all try
491 ** statements will be executed before the thread dies. This includes the
492 ** finally that a thread might be executing at the moment the Abort is raised.
493 ** The thread is not stopped immediately--you must Join on the
494 ** thread to guarantee it has stopped.
495 ** It is possible for a thread to do an unbounded amount of computation in
496 ** the finally's and thus indefinitely delay the threads death.
497 ** If Abort() is called on a thread that has not been started, the thread
498 ** will abort when Start() is called.
499 ** If Abort is called twice on the same thread, a DuplicateThreadAbort
500 ** exception is thrown.
501 =========================================================================*/
504 [System.Security.SecuritySafeCritical] // auto-generated
505 [SecurityPermissionAttribute(SecurityAction.Demand, ControlThread=true)]
506 public void Abort(Object stateInfo)
508 // If two aborts come at the same time, it is possible that the state info
509 // gets set by one, and the actual abort gets delivered by another. But this
510 // is not distinguishable by an application.
511 // The accessor helper will only set the value if it isn't already set,
512 // and that particular bit of native code can test much faster than this
513 // code could, because testing might cause a cross-appdomain marshalling.
514 AbortReason = stateInfo;
516 // Note: we demand ControlThread permission, then call AbortInternal directly
517 // rather than delegating to the Abort() function below. We do this to ensure
518 // that only callers with ControlThread are allowed to change the AbortReason
519 // of the thread. We call AbortInternal directly to avoid demanding the same
526 [System.Security.SecurityCritical] // auto-generated
528 [System.Security.SecuritySafeCritical]
530 #pragma warning disable 618
531 [SecurityPermissionAttribute(SecurityAction.Demand, ControlThread = true)]
532 #pragma warning restore 618
535 #if FEATURE_LEGACYNETCF
536 if(CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
538 System.Reflection.Assembly callingAssembly = System.Reflection.Assembly.GetCallingAssembly();
539 if(callingAssembly != null && !callingAssembly.IsProfileAssembly)
541 string caller = new StackFrame(1).GetMethod().FullName;
542 string callee = System.Reflection.MethodBase.GetCurrentMethod().FullName;
543 throw new MethodAccessException(String.Format(
544 CultureInfo.CurrentCulture,
545 Environment.GetResourceString("Arg_MethodAccessException_WithCaller"),
550 #endif // FEATURE_LEGACYNETCF
554 // Internal helper (since we can't place security demands on
556 [System.Security.SecurityCritical] // auto-generated
557 [ResourceExposure(ResourceScope.None)]
558 [MethodImplAttribute(MethodImplOptions.InternalCall)]
559 private extern void AbortInternal();
561 #if !FEATURE_CORECLR || MONO
562 /*=========================================================================
563 ** Resets a thread abort.
564 ** Should be called by trusted code only
565 =========================================================================*/
566 [System.Security.SecuritySafeCritical] // auto-generated
567 [SecurityPermissionAttribute(SecurityAction.Demand, ControlThread=true)]
568 public static void ResetAbort()
570 Thread thread = Thread.CurrentThread;
571 if ((thread.ThreadState & ThreadState.AbortRequested) == 0)
572 throw new ThreadStateException(Environment.GetResourceString("ThreadState_NoAbortRequested"));
573 thread.ResetAbortNative();
574 thread.ClearAbortReason();
577 [System.Security.SecurityCritical] // auto-generated
578 [ResourceExposure(ResourceScope.None)]
579 [MethodImplAttribute(MethodImplOptions.InternalCall)]
580 private extern void ResetAbortNative();
582 /*=========================================================================
583 ** Suspends the thread. If the thread is already suspended, this call has
586 ** Exceptions: ThreadStateException if the thread has not been started or
588 =========================================================================*/
589 [System.Security.SecuritySafeCritical] // auto-generated
590 [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)]
591 [SecurityPermission(SecurityAction.Demand, ControlThread=true)]
592 public void Suspend() { SuspendInternal(); }
594 // Internal helper (since we can't place security demands on
596 [System.Security.SecurityCritical] // auto-generated
597 [ResourceExposure(ResourceScope.None)]
598 [MethodImplAttribute(MethodImplOptions.InternalCall)]
599 private extern void SuspendInternal();
601 /*=========================================================================
602 ** Resumes a thread that has been suspended.
604 ** Exceptions: ThreadStateException if the thread has not been started or
605 ** it is dead or it isn't in the suspended state.
606 =========================================================================*/
607 [System.Security.SecuritySafeCritical] // auto-generated
608 [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)]
609 [SecurityPermission(SecurityAction.Demand, ControlThread=true)]
610 public void Resume() { ResumeInternal(); }
612 // Internal helper (since we can't place security demands on
614 [System.Security.SecurityCritical] // auto-generated
615 [ResourceExposure(ResourceScope.None)]
616 [MethodImplAttribute(MethodImplOptions.InternalCall)]
617 private extern void ResumeInternal();
619 /*=========================================================================
620 ** Interrupts a thread that is inside a Wait(), Sleep() or Join(). If that
621 ** thread is not currently blocked in that manner, it will be interrupted
622 ** when it next begins to block.
623 =========================================================================*/
624 [System.Security.SecuritySafeCritical] // auto-generated
625 [SecurityPermission(SecurityAction.Demand, ControlThread=true)]
626 public void Interrupt() { InterruptInternal(); }
628 // Internal helper (since we can't place security demands on
630 [System.Security.SecurityCritical] // auto-generated
631 [ResourceExposure(ResourceScope.None)]
632 [MethodImplAttribute(MethodImplOptions.InternalCall)]
633 private extern void InterruptInternal();
636 /*=========================================================================
637 ** Returns the priority of the thread.
639 ** Exceptions: ThreadStateException if the thread is dead.
640 =========================================================================*/
642 public ThreadPriority Priority {
643 [System.Security.SecuritySafeCritical] // auto-generated
644 get { return (ThreadPriority)GetPriorityNative(); }
645 [System.Security.SecuritySafeCritical] // auto-generated
646 [HostProtection(SelfAffectingThreading=true)]
647 set { SetPriorityNative((int)value); }
649 [System.Security.SecurityCritical] // auto-generated
650 [ResourceExposure(ResourceScope.None)]
651 [MethodImplAttribute(MethodImplOptions.InternalCall)]
652 private extern int GetPriorityNative();
653 [System.Security.SecurityCritical] // auto-generated
654 [ResourceExposure(ResourceScope.None)]
655 [MethodImplAttribute(MethodImplOptions.InternalCall)]
656 private extern void SetPriorityNative(int priority);
658 /*=========================================================================
659 ** Returns true if the thread has been started and is not dead.
660 =========================================================================*/
661 public extern bool IsAlive {
662 [System.Security.SecuritySafeCritical] // auto-generated
663 [MethodImpl(MethodImplOptions.InternalCall)]
667 /*=========================================================================
668 ** Returns true if the thread is a threadpool thread.
669 =========================================================================*/
670 public extern bool IsThreadPoolThread {
671 [System.Security.SecuritySafeCritical] // auto-generated
672 [MethodImpl(MethodImplOptions.InternalCall)]
676 /*=========================================================================
677 ** Waits for the thread to die or for timeout milliseconds to elapse.
678 ** Returns true if the thread died, or false if the wait timed out. If
679 ** Timeout.Infinite is given as the parameter, no timeout will occur.
681 ** Exceptions: ArgumentException if timeout < 0.
682 ** ThreadInterruptedException if the thread is interrupted while waiting.
683 ** ThreadStateException if the thread has not been started yet.
684 =========================================================================*/
685 [System.Security.SecurityCritical]
686 [ResourceExposure(ResourceScope.None)]
687 [MethodImplAttribute(MethodImplOptions.InternalCall)]
688 private extern bool JoinInternal(int millisecondsTimeout);
690 [System.Security.SecuritySafeCritical]
691 [HostProtection(Synchronization=true, ExternalThreading=true)]
694 JoinInternal(Timeout.Infinite);
697 [System.Security.SecuritySafeCritical]
698 [HostProtection(Synchronization=true, ExternalThreading=true)]
699 public bool Join(int millisecondsTimeout)
702 if (millisecondsTimeout < Timeout.Infinite)
703 throw new ArgumentOutOfRangeException("millisecondsTimeout", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1"));
705 return JoinInternal(millisecondsTimeout);
708 [HostProtection(Synchronization=true, ExternalThreading=true)]
709 public bool Join(TimeSpan timeout)
711 long tm = (long)timeout.TotalMilliseconds;
712 if (tm < -1 || tm > (long) Int32.MaxValue)
713 throw new ArgumentOutOfRangeException("timeout", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1"));
715 return Join((int)tm);
718 /*=========================================================================
719 ** Suspends the current thread for timeout milliseconds. If timeout == 0,
720 ** forces the thread to give up the remainer of its timeslice. If timeout
721 ** == Timeout.Infinite, no timeout will occur.
723 ** Exceptions: ArgumentException if timeout < 0.
724 ** ThreadInterruptedException if the thread is interrupted while sleeping.
725 =========================================================================*/
726 [System.Security.SecurityCritical] // auto-generated
727 [ResourceExposure(ResourceScope.None)]
728 [MethodImplAttribute(MethodImplOptions.InternalCall)]
729 private static extern void SleepInternal(int millisecondsTimeout);
731 [System.Security.SecuritySafeCritical] // auto-generated
732 public static void Sleep(int millisecondsTimeout)
735 if (millisecondsTimeout < Timeout.Infinite)
736 throw new ArgumentOutOfRangeException("millisecondsTimeout", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1"));
738 SleepInternal(millisecondsTimeout);
740 // Ensure we don't return to app code when the pause is underway
741 if(AppDomainPauseManager.IsPaused)
742 AppDomainPauseManager.ResumeEvent.WaitOneWithoutFAS();
746 public static void Sleep(TimeSpan timeout)
748 long tm = (long)timeout.TotalMilliseconds;
749 if (tm < -1 || tm > (long) Int32.MaxValue)
750 throw new ArgumentOutOfRangeException("timeout", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1"));
755 /* wait for a length of time proportial to 'iterations'. Each iteration is should
756 only take a few machine instructions. Calling this API is preferable to coding
757 a explict busy loop because the hardware can be informed that it is busy waiting. */
759 [System.Security.SecurityCritical] // auto-generated
760 [MethodImplAttribute(MethodImplOptions.InternalCall),
761 HostProtection(Synchronization=true,ExternalThreading=true),
762 ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success),
763 ResourceExposure(ResourceScope.None)]
764 private static extern void SpinWaitInternal(int iterations);
766 [System.Security.SecuritySafeCritical] // auto-generated
767 [HostProtection(Synchronization=true,ExternalThreading=true),
768 ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
769 public static void SpinWait(int iterations)
771 SpinWaitInternal(iterations);
774 [System.Security.SecurityCritical] // auto-generated
775 [ResourceExposure(ResourceScope.None)]
776 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
777 [SuppressUnmanagedCodeSecurity]
778 [HostProtection(Synchronization = true, ExternalThreading = true),
779 ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
780 private static extern bool YieldInternal();
782 [System.Security.SecuritySafeCritical] // auto-generated
783 [HostProtection(Synchronization = true, ExternalThreading = true),
784 ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
785 public static bool Yield()
787 return YieldInternal();
791 public static Thread CurrentThread {
792 [System.Security.SecuritySafeCritical] // auto-generated
793 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
795 Contract.Ensures(Contract.Result<Thread>() != null);
796 return GetCurrentThreadNative();
799 [System.Security.SecurityCritical] // auto-generated
800 [ResourceExposure(ResourceScope.None)]
801 [MethodImplAttribute(MethodImplOptions.InternalCall), ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
802 private static extern Thread GetCurrentThreadNative();
804 [System.Security.SecurityCritical] // auto-generated
805 private void SetStartHelper(Delegate start, int maxStackSize)
808 maxStackSize = GetProcessDefaultStackSize(maxStackSize);
811 // We only support default stacks in CoreCLR
812 Contract.Assert(maxStackSize == 0);
814 // Only fully-trusted code is allowed to create "large" stacks. Partial-trust falls back to
815 // the default stack size.
816 ulong defaultStackSize = GetProcessDefaultStackSize();
818 if ((ulong)(uint)maxStackSize > defaultStackSize)
822 SecurityPermission.Demand(PermissionType.FullTrust);
824 catch (SecurityException)
826 maxStackSize = (int)Math.Min(defaultStackSize, (ulong)(uint)int.MaxValue);
832 ThreadHelper threadStartCallBack = new ThreadHelper(start);
833 if(start is ThreadStart)
835 SetStart(new ThreadStart(threadStartCallBack.ThreadStart), maxStackSize);
839 SetStart(new ParameterizedThreadStart(threadStartCallBack.ThreadStart), maxStackSize);
844 [ResourceExposure(ResourceScope.None)]
845 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
846 [SuppressUnmanagedCodeSecurity]
847 private static extern ulong GetProcessDefaultStackSize();
849 /*=========================================================================
850 ** PRIVATE Sets the IThreadable interface for the thread. Assumes that
852 =========================================================================*/
853 [System.Security.SecurityCritical] // auto-generated
854 [ResourceExposure(ResourceScope.None)]
855 [MethodImplAttribute(MethodImplOptions.InternalCall)]
856 private extern void SetStart(Delegate start, int maxStackSize);
858 /*=========================================================================
859 ** Clean up the thread when it goes away.
860 =========================================================================*/
861 [System.Security.SecuritySafeCritical] // auto-generated
862 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
865 // Delegate to the unmanaged portion.
869 [System.Security.SecurityCritical] // auto-generated
870 [ResourceExposure(ResourceScope.None)]
871 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
872 [MethodImplAttribute(MethodImplOptions.InternalCall)]
873 private extern void InternalFinalize();
875 #if FEATURE_COMINTEROP
876 [System.Security.SecurityCritical] // auto-generated
877 [ResourceExposure(ResourceScope.None)]
878 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
879 [MethodImplAttribute(MethodImplOptions.InternalCall)]
880 public extern void DisableComObjectEagerCleanup();
881 #endif //FEATURE_COMINTEROP
883 /*=========================================================================
884 ** Return whether or not this thread is a background thread. Background
885 ** threads do not affect when the Execution Engine shuts down.
887 ** Exceptions: ThreadStateException if the thread is dead.
888 =========================================================================*/
889 public bool IsBackground {
890 [System.Security.SecuritySafeCritical] // auto-generated
891 get { return IsBackgroundNative(); }
892 [System.Security.SecuritySafeCritical] // auto-generated
893 [HostProtection(SelfAffectingThreading=true)]
894 set { SetBackgroundNative(value); }
896 [System.Security.SecurityCritical] // auto-generated
897 [ResourceExposure(ResourceScope.None)]
898 [MethodImplAttribute(MethodImplOptions.InternalCall)]
899 private extern bool IsBackgroundNative();
900 [System.Security.SecurityCritical] // auto-generated
901 [ResourceExposure(ResourceScope.None)]
902 [MethodImplAttribute(MethodImplOptions.InternalCall)]
903 private extern void SetBackgroundNative(bool isBackground);
906 /*=========================================================================
907 ** Return the thread state as a consistent set of bits. This is more
908 ** general then IsAlive or IsBackground.
909 =========================================================================*/
910 public ThreadState ThreadState {
911 [System.Security.SecuritySafeCritical] // auto-generated
912 get { return (ThreadState)GetThreadStateNative(); }
915 [System.Security.SecurityCritical] // auto-generated
916 [ResourceExposure(ResourceScope.None)]
917 [MethodImplAttribute(MethodImplOptions.InternalCall)]
918 private extern int GetThreadStateNative();
920 #if FEATURE_COMINTEROP_APARTMENT_SUPPORT
921 /*=========================================================================
922 ** An unstarted thread can be marked to indicate that it will host a
923 ** single-threaded or multi-threaded apartment.
925 ** Exceptions: ArgumentException if state is not a valid apartment state
926 ** (ApartmentSTA or ApartmentMTA).
927 =========================================================================*/
928 [Obsolete("The ApartmentState property has been deprecated. Use GetApartmentState, SetApartmentState or TrySetApartmentState instead.", false)]
929 public ApartmentState ApartmentState
931 [System.Security.SecuritySafeCritical] // auto-generated
934 return (ApartmentState)GetApartmentStateNative();
937 [System.Security.SecuritySafeCritical] // auto-generated
938 [HostProtection(Synchronization=true, SelfAffectingThreading=true)]
941 SetApartmentStateNative((int)value, true);
945 [System.Security.SecuritySafeCritical] // auto-generated
946 public ApartmentState GetApartmentState()
948 return (ApartmentState)GetApartmentStateNative();
951 [System.Security.SecuritySafeCritical] // auto-generated
952 [HostProtection(Synchronization=true, SelfAffectingThreading=true)]
953 public bool TrySetApartmentState(ApartmentState state)
955 return SetApartmentStateHelper(state, false);
958 [System.Security.SecuritySafeCritical] // auto-generated
959 [HostProtection(Synchronization=true, SelfAffectingThreading=true)]
960 public void SetApartmentState(ApartmentState state)
962 bool result = SetApartmentStateHelper(state, true);
964 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ApartmentStateSwitchFailed"));
967 [System.Security.SecurityCritical] // auto-generated
968 private bool SetApartmentStateHelper(ApartmentState state, bool fireMDAOnMismatch)
970 ApartmentState retState = (ApartmentState)SetApartmentStateNative((int)state, fireMDAOnMismatch);
972 // Special case where we pass in Unknown and get back MTA.
973 // Once we CoUninitialize the thread, the OS will still
974 // report the thread as implicitly in the MTA if any
975 // other thread in the process is CoInitialized.
976 if ((state == System.Threading.ApartmentState.Unknown) && (retState == System.Threading.ApartmentState.MTA))
979 if (retState != state)
985 [System.Security.SecurityCritical] // auto-generated
986 [ResourceExposure(ResourceScope.None)]
987 [MethodImplAttribute(MethodImplOptions.InternalCall)]
988 private extern int GetApartmentStateNative();
989 [System.Security.SecurityCritical] // auto-generated
990 [ResourceExposure(ResourceScope.None)]
991 [MethodImplAttribute(MethodImplOptions.InternalCall)]
992 private extern int SetApartmentStateNative(int state, bool fireMDAOnMismatch);
993 [System.Security.SecurityCritical] // auto-generated
994 [ResourceExposure(ResourceScope.None)]
995 [MethodImplAttribute(MethodImplOptions.InternalCall)]
996 private extern void StartupSetApartmentStateInternal();
997 #endif // FEATURE_COMINTEROP_APARTMENT_SUPPORT
999 /*=========================================================================
1000 ** Allocates an un-named data slot. The slot is allocated on ALL the
1002 =========================================================================*/
1003 [HostProtection(SharedState=true, ExternalThreading=true)]
1004 public static LocalDataStoreSlot AllocateDataSlot()
1006 return LocalDataStoreManager.AllocateDataSlot();
1009 /*=========================================================================
1010 ** Allocates a named data slot. The slot is allocated on ALL the
1011 ** threads. Named data slots are "public" and can be manipulated by
1013 =========================================================================*/
1014 [HostProtection(SharedState=true, ExternalThreading=true)]
1015 public static LocalDataStoreSlot AllocateNamedDataSlot(String name)
1017 return LocalDataStoreManager.AllocateNamedDataSlot(name);
1020 /*=========================================================================
1021 ** Looks up a named data slot. If the name has not been used, a new slot is
1022 ** allocated. Named data slots are "public" and can be manipulated by
1024 =========================================================================*/
1025 [HostProtection(SharedState=true, ExternalThreading=true)]
1026 public static LocalDataStoreSlot GetNamedDataSlot(String name)
1028 return LocalDataStoreManager.GetNamedDataSlot(name);
1031 /*=========================================================================
1032 ** Frees a named data slot. The slot is allocated on ALL the
1033 ** threads. Named data slots are "public" and can be manipulated by
1035 =========================================================================*/
1036 [HostProtection(SharedState=true, ExternalThreading=true)]
1037 public static void FreeNamedDataSlot(String name)
1039 LocalDataStoreManager.FreeNamedDataSlot(name);
1042 /*=========================================================================
1043 ** Retrieves the value from the specified slot on the current thread, for that thread's current domain.
1044 =========================================================================*/
1045 [HostProtection(SharedState=true, ExternalThreading=true)]
1046 [ResourceExposure(ResourceScope.AppDomain)]
1047 public static Object GetData(LocalDataStoreSlot slot)
1049 LocalDataStoreHolder dls = s_LocalDataStore;
1052 // Make sure to validate the slot even if we take the quick path
1053 LocalDataStoreManager.ValidateSlot(slot);
1057 return dls.Store.GetData(slot);
1060 /*=========================================================================
1061 ** Sets the data in the specified slot on the currently running thread, for that thread's current domain.
1062 =========================================================================*/
1063 [HostProtection(SharedState=true, ExternalThreading=true)]
1064 [ResourceExposure(ResourceScope.AppDomain)]
1065 public static void SetData(LocalDataStoreSlot slot, Object data)
1067 LocalDataStoreHolder dls = s_LocalDataStore;
1069 // Create new DLS if one hasn't been created for this domain for this thread
1071 dls = LocalDataStoreManager.CreateLocalDataStore();
1072 s_LocalDataStore = dls;
1075 dls.Store.SetData(slot, data);
1079 // #threadCultureInfo
1082 // In the desktop runtime, we allow a thread's cultures to travel with the thread
1083 // across AppDomain boundaries. Furthermore we update the native thread with the
1084 // culture of the managed thread. Because of security concerns and potential SxS
1085 // effects, in Silverlight we are making the changes listed below.
1087 // Silverlight Changes:
1088 // - thread instance member cultures (CurrentCulture and CurrentUICulture)
1089 // confined within AppDomains
1090 // - changes to these properties don't affect the underlying native thread
1093 // FEATURE_LEAK_CULTURE_INFO : CultureInfos can leak across AppDomains, not
1094 // enabled in Silverlight
1096 // Implementation notes:
1097 // In Silverlight, culture members thread static (per Thread, per AppDomain).
1100 // An interesting side-effect of isolating cultures within an AppDomain is that we
1101 // now need to special case resource lookup for mscorlib, which transitions to the
1102 // default domain to lookup resources. See Environment.cs for more details.
1104 #if FEATURE_LEAK_CULTURE_INFO
1105 [System.Security.SecurityCritical] // auto-generated
1106 [ResourceExposure(ResourceScope.None)]
1107 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1108 static extern private bool nativeGetSafeCulture(Thread t, int appDomainId, bool isUI, ref CultureInfo safeCulture);
1109 #endif // FEATURE_LEAK_CULTURE_INFO
1111 // As the culture can be customized object then we cannot hold any
1112 // reference to it before we check if it is safe because the app domain
1113 // owning this customized culture may get unloaded while executing this
1114 // code. To achieve that we have to do the check using nativeGetSafeCulture
1115 // as the thread cannot get interrupted during the FCALL.
1116 // If the culture is safe (not customized or created in current app domain)
1117 // then the FCALL will return a reference to that culture otherwise the
1118 // FCALL will return failure. In case of failure we'll return the default culture.
1119 // If the app domain owning a customized culture that is set to teh thread and this
1120 // app domain get unloaded there is a code to clean up the culture from the thread
1121 // using the code in AppDomain::ReleaseDomainStores.
1123 public CultureInfo CurrentUICulture {
1125 Contract.Ensures(Contract.Result<CultureInfo>() != null);
1127 if(AppDomain.IsAppXModel()) {
1128 return CultureInfo.GetCultureInfoForUserPreferredLanguageInAppX() ?? GetCurrentUICultureNoAppX();
1133 return GetCurrentUICultureNoAppX();
1137 [System.Security.SecuritySafeCritical] // auto-generated
1138 [HostProtection(ExternalThreading=true)]
1140 if (value == null) {
1141 throw new ArgumentNullException("value");
1143 Contract.EndContractBlock();
1145 //If they're trying to use a Culture with a name that we can't use in resource lookup,
1146 //don't even let them set it on the thread.
1147 CultureInfo.VerifyCultureName(value, true);
1149 // If you add more pre-conditions to this method, check to see if you also need to
1150 // add them to CultureInfo.DefaultThreadCurrentUICulture.set.
1152 #if FEATURE_LEAK_CULTURE_INFO
1153 if (nativeSetThreadUILocale(value.SortName) == false)
1155 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidResourceCultureName", value.Name));
1157 value.StartCrossDomainTracking();
1159 if (m_CurrentUICulture == null && m_CurrentCulture == null)
1160 nativeInitCultureAccessors();
1163 #if FEATURE_LEGACYNETCF
1164 if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
1173 CultureInfo.SetCurrentUICultureQuirk(value);
1177 if (!AppContextSwitches.NoAsyncCurrentCulture)
1179 if (s_asyncLocalCurrentUICulture == null)
1181 Interlocked.CompareExchange(ref s_asyncLocalCurrentUICulture, new AsyncLocal<CultureInfo>(AsyncLocalSetCurrentUICulture), null);
1184 // this one will set m_CurrentUICulture too
1185 s_asyncLocalCurrentUICulture.Value = value;
1189 m_CurrentUICulture = value;
1194 #if FEATURE_LEAK_CULTURE_INFO
1195 [System.Security.SecuritySafeCritical] // auto-generated
1197 internal CultureInfo GetCurrentUICultureNoAppX() {
1199 Contract.Ensures(Contract.Result<CultureInfo>() != null);
1201 // Fetch a local copy of m_CurrentUICulture to
1202 // avoid ----s that malicious user can introduce
1203 if (m_CurrentUICulture == null) {
1204 CultureInfo appDomainDefaultUICulture = CultureInfo.DefaultThreadCurrentUICulture;
1205 return (appDomainDefaultUICulture != null ? appDomainDefaultUICulture : CultureInfo.UserDefaultUICulture);
1208 #if FEATURE_LEAK_CULTURE_INFO
1209 CultureInfo culture = null;
1211 if (!nativeGetSafeCulture(this, GetDomainID(), true, ref culture) || culture == null) {
1212 return CultureInfo.UserDefaultUICulture;
1217 return m_CurrentUICulture;
1221 // This returns the exposed context for a given context ID.
1222 #if FEATURE_LEAK_CULTURE_INFO
1223 [System.Security.SecurityCritical] // auto-generated
1224 [ResourceExposure(ResourceScope.None)]
1225 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1226 static extern private bool nativeSetThreadUILocale(String locale);
1229 // As the culture can be customized object then we cannot hold any
1230 // reference to it before we check if it is safe because the app domain
1231 // owning this customized culture may get unloaded while executing this
1232 // code. To achieve that we have to do the check using nativeGetSafeCulture
1233 // as the thread cannot get interrupted during the FCALL.
1234 // If the culture is safe (not customized or created in current app domain)
1235 // then the FCALL will return a reference to that culture otherwise the
1236 // FCALL will return failure. In case of failure we'll return the default culture.
1237 // If the app domain owning a customized culture that is set to teh thread and this
1238 // app domain get unloaded there is a code to clean up the culture from the thread
1239 // using the code in AppDomain::ReleaseDomainStores.
1241 public CultureInfo CurrentCulture {
1243 Contract.Ensures(Contract.Result<CultureInfo>() != null);
1246 if(AppDomain.IsAppXModel()) {
1247 return CultureInfo.GetCultureInfoForUserPreferredLanguageInAppX() ?? GetCurrentCultureNoAppX();
1252 return GetCurrentCultureNoAppX();
1256 [System.Security.SecuritySafeCritical] // auto-generated
1257 #if FEATURE_LEAK_CULTURE_INFO
1258 [SecurityPermission(SecurityAction.Demand, ControlThread = true)]
1262 throw new ArgumentNullException("value");
1264 Contract.EndContractBlock();
1266 // If you add more pre-conditions to this method, check to see if you also need to
1267 // add them to CultureInfo.DefaultThreadCurrentCulture.set.
1269 #if FEATURE_LEAK_CULTURE_INFO
1270 //If we can't set the nativeThreadLocale, we'll just let it stay
1271 //at whatever value it had before. This allows people who use
1272 //just managed code not to be limited by the underlying OS.
1273 CultureInfo.nativeSetThreadLocale(value.SortName);
1274 value.StartCrossDomainTracking();
1276 if (m_CurrentCulture == null && m_CurrentUICulture == null)
1277 nativeInitCultureAccessors();
1280 #if FEATURE_LEGACYNETCF
1281 if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
1283 // See comment in CurrentUICulture setter
1284 CultureInfo.SetCurrentCultureQuirk(value);
1288 if (!AppContextSwitches.NoAsyncCurrentCulture)
1290 if (s_asyncLocalCurrentCulture == null)
1292 Interlocked.CompareExchange(ref s_asyncLocalCurrentCulture, new AsyncLocal<CultureInfo>(AsyncLocalSetCurrentCulture), null);
1294 // this one will set m_CurrentCulture too
1295 s_asyncLocalCurrentCulture.Value = value;
1299 m_CurrentCulture = value;
1304 #if FEATURE_LEAK_CULTURE_INFO
1305 [System.Security.SecuritySafeCritical] // auto-generated
1307 private CultureInfo GetCurrentCultureNoAppX() {
1309 Contract.Ensures(Contract.Result<CultureInfo>() != null);
1311 // Fetch a local copy of m_CurrentCulture to
1312 // avoid ----s that malicious user can introduce
1313 if (m_CurrentCulture == null) {
1314 CultureInfo appDomainDefaultCulture = CultureInfo.DefaultThreadCurrentCulture;
1315 return (appDomainDefaultCulture != null ? appDomainDefaultCulture : CultureInfo.UserDefaultCulture);
1318 #if FEATURE_LEAK_CULTURE_INFO
1319 CultureInfo culture = null;
1321 if (!nativeGetSafeCulture(this, GetDomainID(), false, ref culture) || culture == null) {
1322 return CultureInfo.UserDefaultCulture;
1327 return m_CurrentCulture;
1332 #if! FEATURE_LEAK_CULTURE_INFO
1333 [System.Security.SecurityCritical] // auto-generated
1334 [ResourceExposure(ResourceScope.None)]
1335 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
1336 [SuppressUnmanagedCodeSecurity]
1337 private static extern void nativeInitCultureAccessors();
1340 /*=============================================================*/
1342 /*======================================================================
1343 ** Current thread context is stored in a slot in the thread local store
1344 ** CurrentContext gets the Context from the slot.
1345 ======================================================================*/
1346 #if FEATURE_REMOTING
1347 public static Context CurrentContext
1349 [System.Security.SecurityCritical] // auto-generated_required
1352 return CurrentThread.GetCurrentContextInternal();
1356 [System.Security.SecurityCritical] // auto-generated
1357 internal Context GetCurrentContextInternal()
1359 if (m_Context == null)
1361 m_Context = Context.DefaultContext;
1368 #if FEATURE_IMPERSONATION
1369 // Get and set thread's current principal (for role based security).
1370 public static IPrincipal CurrentPrincipal
1372 [System.Security.SecuritySafeCritical] // auto-generated
1375 lock (CurrentThread)
1377 IPrincipal principal = (IPrincipal)
1378 CallContext.Principal;
1379 if (principal == null)
1381 principal = GetDomain().GetThreadPrincipal();
1382 CallContext.Principal = principal;
1388 [System.Security.SecuritySafeCritical] // auto-generated
1389 [SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPrincipal)]
1392 CallContext.Principal = value;
1396 // Private routine called from unmanaged code to set an initial
1397 // principal for a newly created thread.
1398 [System.Security.SecurityCritical] // auto-generated
1399 private void SetPrincipalInternal(IPrincipal principal)
1401 GetMutableExecutionContext().LogicalCallContext.SecurityData.Principal = principal;
1403 #endif // FEATURE_IMPERSONATION
1405 #if FEATURE_REMOTING
1407 // This returns the exposed context for a given context ID.
1408 [System.Security.SecurityCritical] // auto-generated
1409 [ResourceExposure(ResourceScope.None)]
1410 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1411 internal static extern Context GetContextInternal(IntPtr id);
1413 [System.Security.SecurityCritical] // auto-generated
1414 [ResourceExposure(ResourceScope.None)]
1415 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1416 internal extern Object InternalCrossContextCallback(Context ctx, IntPtr ctxID, Int32 appDomainID, InternalCrossContextDelegate ftnToCall, Object[] args);
1418 [System.Security.SecurityCritical] // auto-generated
1419 internal Object InternalCrossContextCallback(Context ctx, InternalCrossContextDelegate ftnToCall, Object[] args)
1421 return InternalCrossContextCallback(ctx, ctx.InternalContextID, 0, ftnToCall, args);
1424 // CompleteCrossContextCallback is called by the EE after transitioning to the requested context
1425 private static Object CompleteCrossContextCallback(InternalCrossContextDelegate ftnToCall, Object[] args)
1427 return ftnToCall(args);
1429 #endif // FEATURE_REMOTING
1431 /*======================================================================
1432 ** Returns the current domain in which current thread is running.
1433 ======================================================================*/
1435 [System.Security.SecurityCritical] // auto-generated
1436 [ResourceExposure(ResourceScope.None)]
1437 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1438 private static extern AppDomain GetDomainInternal();
1439 [System.Security.SecurityCritical] // auto-generated
1440 [ResourceExposure(ResourceScope.None)]
1441 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1442 private static extern AppDomain GetFastDomainInternal();
1444 [System.Security.SecuritySafeCritical] // auto-generated
1445 public static AppDomain GetDomain()
1447 Contract.Ensures(Contract.Result<AppDomain>() != null);
1451 ad = GetFastDomainInternal();
1453 ad = GetDomainInternal();
1455 #if FEATURE_REMOTING
1456 Contract.Assert(CurrentThread.m_Context == null || CurrentThread.m_Context.AppDomain == ad, "AppDomains on the managed & unmanaged threads should match");
1463 * This returns a unique id to identify an appdomain.
1465 public static int GetDomainID()
1467 return GetDomain().GetId();
1471 // Retrieves the name of the thread.
1473 public String Name {
1478 [System.Security.SecuritySafeCritical] // auto-generated
1479 [HostProtection(ExternalThreading=true)]
1483 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_WriteOnce"));
1486 InformThreadNameChange(GetNativeHandle(), value, (value != null) ? value.Length : 0);
1491 [System.Security.SecurityCritical] // auto-generated
1492 [ResourceExposure(ResourceScope.None)]
1493 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
1494 [SuppressUnmanagedCodeSecurity]
1495 private static extern void InformThreadNameChange(ThreadHandle t, String name, int len);
1497 internal Object AbortReason {
1498 [System.Security.SecurityCritical] // auto-generated
1500 object result = null;
1503 result = GetAbortReason();
1507 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ExceptionStateCrossAppDomain"), e);
1511 [System.Security.SecurityCritical] // auto-generated
1512 set { SetAbortReason(value); }
1516 * This marks the beginning of a critical code region.
1518 [System.Security.SecuritySafeCritical] // auto-generated
1519 [HostProtection(Synchronization=true, ExternalThreading=true)]
1520 [ResourceExposure(ResourceScope.None)]
1521 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1522 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
1523 public static extern void BeginCriticalRegion();
1526 * This marks the end of a critical code region.
1528 [System.Security.SecuritySafeCritical] // auto-generated
1529 [HostProtection(Synchronization=true, ExternalThreading=true)]
1530 [ResourceExposure(ResourceScope.None)]
1531 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1532 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
1533 public static extern void EndCriticalRegion();
1536 * This marks the beginning of a code region that requires thread affinity.
1538 [System.Security.SecurityCritical] // auto-generated_required
1539 [ResourceExposure(ResourceScope.None)]
1540 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1541 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
1542 public static extern void BeginThreadAffinity();
1545 * This marks the end of a code region that requires thread affinity.
1547 [System.Security.SecurityCritical] // auto-generated_required
1548 [ResourceExposure(ResourceScope.None)]
1549 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1550 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
1551 public static extern void EndThreadAffinity();
1553 /*=========================================================================
1554 ** Volatile Read & Write and MemoryBarrier methods.
1555 ** Provides the ability to read and write values ensuring that the values
1556 ** are read/written each time they are accessed.
1557 =========================================================================*/
1559 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1560 public static byte VolatileRead(ref byte address)
1563 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1567 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1568 public static short VolatileRead(ref short address)
1570 short ret = address;
1571 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1575 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1576 public static int VolatileRead(ref int address)
1579 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1583 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1584 public static long VolatileRead(ref long address)
1587 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1591 [CLSCompliant(false)]
1592 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1593 public static sbyte VolatileRead(ref sbyte address)
1595 sbyte ret = address;
1596 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1600 [CLSCompliant(false)]
1601 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1602 public static ushort VolatileRead(ref ushort address)
1604 ushort ret = address;
1605 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1609 [CLSCompliant(false)]
1610 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1611 public static uint VolatileRead(ref uint address)
1614 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1618 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1619 public static IntPtr VolatileRead(ref IntPtr address)
1621 IntPtr ret = address;
1622 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1626 [CLSCompliant(false)]
1627 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1628 public static UIntPtr VolatileRead(ref UIntPtr address)
1630 UIntPtr ret = address;
1631 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1635 [CLSCompliant(false)]
1636 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1637 public static ulong VolatileRead(ref ulong address)
1639 ulong ret = address;
1640 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1644 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1645 public static float VolatileRead(ref float address)
1647 float ret = address;
1648 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1652 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1653 public static double VolatileRead(ref double address)
1655 double ret = address;
1656 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1660 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1661 public static Object VolatileRead(ref Object address)
1663 Object ret = address;
1664 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1668 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1669 public static void VolatileWrite(ref byte address, byte 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 short address, short value)
1678 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1682 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1683 public static void VolatileWrite(ref int address, int value)
1685 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1689 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1690 public static void VolatileWrite(ref long address, long value)
1692 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1696 [CLSCompliant(false)]
1697 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1698 public static void VolatileWrite(ref sbyte address, sbyte value)
1700 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1704 [CLSCompliant(false)]
1705 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1706 public static void VolatileWrite(ref ushort address, ushort value)
1708 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1712 [CLSCompliant(false)]
1713 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1714 public static void VolatileWrite(ref uint address, uint value)
1716 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1720 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1721 public static void VolatileWrite(ref IntPtr address, IntPtr value)
1723 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1727 [CLSCompliant(false)]
1728 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1729 public static void VolatileWrite(ref UIntPtr address, UIntPtr value)
1731 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1735 [CLSCompliant(false)]
1736 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1737 public static void VolatileWrite(ref ulong address, ulong value)
1739 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1743 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1744 public static void VolatileWrite(ref float address, float value)
1746 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1750 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1751 public static void VolatileWrite(ref double address, double value)
1753 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1757 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1758 public static void VolatileWrite(ref Object address, Object value)
1760 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1764 [System.Security.SecuritySafeCritical] // auto-generated
1765 [ResourceExposure(ResourceScope.None)]
1766 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1767 public static extern void MemoryBarrier();
1769 private static LocalDataStoreMgr LocalDataStoreManager
1773 if (s_LocalDataStoreMgr == null)
1775 Interlocked.CompareExchange(ref s_LocalDataStoreMgr, new LocalDataStoreMgr(), null);
1778 return s_LocalDataStoreMgr;
1782 #if !FEATURE_CORECLR && !MOBILE
1783 void _Thread.GetTypeInfoCount(out uint pcTInfo)
1785 throw new NotImplementedException();
1788 void _Thread.GetTypeInfo(uint iTInfo, uint lcid, IntPtr ppTInfo)
1790 throw new NotImplementedException();
1793 void _Thread.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1795 throw new NotImplementedException();
1798 void _Thread.Invoke(uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1800 throw new NotImplementedException();
1805 // Helper function to set the AbortReason for a thread abort.
1806 // Checks that they're not alredy set, and then atomically updates
1807 // the reason info (object + ADID).
1808 [System.Security.SecurityCritical] // auto-generated
1809 [ResourceExposure(ResourceScope.None)]
1810 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1811 internal extern void SetAbortReason(Object o);
1813 // Helper function to retrieve the AbortReason from a thread
1814 // abort. Will perform cross-AppDomain marshalling if the object
1815 // lives in a different AppDomain from the requester.
1816 [System.Security.SecurityCritical] // auto-generated
1817 [ResourceExposure(ResourceScope.None)]
1818 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1819 internal extern Object GetAbortReason();
1821 // Helper function to clear the AbortReason. Takes care of
1822 // AppDomain related cleanup if required.
1823 [System.Security.SecurityCritical] // auto-generated
1824 [ResourceExposure(ResourceScope.None)]
1825 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1826 internal extern void ClearAbortReason();
1829 } // End of class Thread
1831 // declaring a local var of this enum type and passing it by ref into a function that needs to do a
1832 // stack crawl will both prevent inlining of the calle and pass an ESP point to stack crawl to
1833 // Declaring these in EH clauses is illegal; they must declared in the main method body
1835 internal enum StackCrawlMark
1838 LookForMyCaller = 1,
1839 LookForMyCallersCaller = 2,