Updates referencesource to .NET 4.7
[mono-project.git] / mcs / class / referencesource / mscorlib / system / threading / thread.cs
blob0c6462309ff498a7482fbdeb5ee03758aa68a7ab
1 // ==++==
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // ==--==
6 //
7 // <OWNER>Microsoft</OWNER>
8 /*=============================================================================
9 **
10 ** Class: Thread
13 ** Purpose: Class for creating and managing a thread.
16 =============================================================================*/
18 namespace System.Threading {
19 using System.Threading;
20 using System.Runtime;
21 using System.Runtime.InteropServices;
22 #if FEATURE_REMOTING
23 using System.Runtime.Remoting.Contexts;
24 using System.Runtime.Remoting.Messaging;
25 #endif
26 using System;
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() {}
46 Delegate _start;
47 Object _startArg = null;
48 ExecutionContext _executionContext = null;
49 internal ThreadHelper(Delegate start)
51 _start = 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)();
70 else
72 ((ParameterizedThreadStart)t._start)(t._startArg);
76 // call back helper
77 #if FEATURE_CORECLR
78 [System.Security.SecuritySafeCritical] // auto-generated
79 #else
80 [System.Security.SecurityCritical]
81 #endif
82 internal void ThreadStart(object obj)
84 _startArg = obj;
85 if (_executionContext != null)
87 ExecutionContext.Run(_executionContext, _ccb, (Object)this);
89 else
91 ((ParameterizedThreadStart)_start)(obj);
95 // call back helper
96 #if FEATURE_CORECLR
97 [System.Security.SecuritySafeCritical] // auto-generated
98 #else
99 [System.Security.SecurityCritical]
100 #endif
101 internal void ThreadStart()
103 if (_executionContext != null)
105 ExecutionContext.Run(_executionContext, _ccb, (Object)this);
107 else
109 ((ThreadStart)_start)();
113 #if !MONO
114 internal struct ThreadHandle
116 private IntPtr m_ptr;
118 internal ThreadHandle(IntPtr pThread)
120 m_ptr = pThread;
123 #endif
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
129 #if !MOBILE
130 , _Thread
131 #endif
133 #if !MONO
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 =========================================================================*/
139 #if FEATURE_REMOTING
140 private Context m_Context;
141 #endif
142 #if !FEATURE_CORECLR
143 private ExecutionContext m_ExecutionContext; // this call context follows the logical thread
144 #endif
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;
152 #endif
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;
174 #if DEBUG
175 private bool m_ForbidExecutionContextMutation;
176 #endif
177 #endif
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 =========================================================================*/
187 [ThreadStatic]
188 static private LocalDataStoreHolder s_LocalDataStore;
190 // Do not move! Order of above fields needs to be preserved for alignment
191 // with native code
192 // See code:#threadCultureInfo
193 #if !FEATURE_LEAK_CULTURE_INFO
194 [ThreadStatic]
195 internal static CultureInfo m_CurrentCulture;
196 [ThreadStatic]
197 internal static CultureInfo m_CurrentUICulture;
198 #endif
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;
207 #else
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;
216 #else
217 m_CurrentUICulture = args.CurrentValue;
218 #endif // FEATURE_LEAK_CULTURE_INFO
221 #if FEATURE_CORECLR
222 // Adding an empty default ctor for annotation purposes
223 [System.Security.SecuritySafeCritical] // auto-generated
224 internal Thread(){}
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) {
235 if (start == null) {
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) {
244 if (start == null) {
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) {
255 if (start == null) {
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) {
264 if (start == null) {
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);
272 #if !MONO
273 [ComVisible(false)]
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
285 get;
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
295 // exception here.
296 if (thread.IsNull())
297 throw new ArgumentException(null, Environment.GetResourceString("Argument_InvalidHandle"));
299 return new ThreadHandle(thread);
301 #endif
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
312 public void Start()
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(
353 ref stackMark,
354 ExecutionContext.CaptureOptions.IgnoreSyncCtx);
355 t.SetExecutionContextHelper(ec);
357 #if FEATURE_IMPERSONATION
358 IPrincipal principal = (IPrincipal)CallContext.Principal;
359 #else
360 IPrincipal principal = null;
361 #endif
362 StartInternal(principal, ref stackMark);
366 #if !FEATURE_CORECLR
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; }
379 #if !MONO && DEBUG
380 internal bool ForbidExecutionContextMutation
382 set { m_ForbidExecutionContextMutation = value; }
384 #endif
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();
396 else
397 result = m_ExecutionContext;
399 return result;
403 [SecurityCritical]
404 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
405 internal ExecutionContext GetMutableExecutionContext()
407 Contract.Assert(Thread.CurrentThread == this);
408 #if !MONO && DEBUG
409 Contract.Assert(!m_ForbidExecutionContextMutation);
410 #endif
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;
425 [SecurityCritical]
426 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
427 internal void SetExecutionContext(ExecutionContext value, bool belongsToCurrentScope)
429 m_ExecutionContext = value;
430 ExecutionContextBelongsToCurrentScope = belongsToCurrentScope;
433 [SecurityCritical]
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
442 #if !MONO
443 [System.Security.SecurityCritical] // auto-generated
444 [ResourceExposure(ResourceScope.None)]
445 [MethodImplAttribute(MethodImplOptions.InternalCall)]
446 private extern void StartInternal(IPrincipal principal, ref StackCrawlMark stackMark);
447 #endif
448 #if FEATURE_COMPRESSEDSTACK || MONO
449 /// <internalonly/>
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"));
457 #if !MONO
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);
467 #endif
469 /// <internalonly/>
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
477 #if !MONO
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 =========================================================================*/
503 #if !FEATURE_CORECLR
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
520 // permission twice.
521 AbortInternal();
523 #endif
525 #if FEATURE_CORECLR
526 [System.Security.SecurityCritical] // auto-generated
527 #else
528 [System.Security.SecuritySafeCritical]
529 #endif
530 #pragma warning disable 618
531 [SecurityPermissionAttribute(SecurityAction.Demand, ControlThread = true)]
532 #pragma warning restore 618
533 public void Abort()
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"),
546 caller,
547 callee));
550 #endif // FEATURE_LEGACYNETCF
551 AbortInternal();
554 // Internal helper (since we can't place security demands on
555 // ecalls/fcalls).
556 [System.Security.SecurityCritical] // auto-generated
557 [ResourceExposure(ResourceScope.None)]
558 [MethodImplAttribute(MethodImplOptions.InternalCall)]
559 private extern void AbortInternal();
560 #endif
561 #if (!FEATURE_CORECLR && !MONO) || MONO_FEATURE_THREAD_ABORT
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();
581 #endif
582 #if (!FEATURE_CORECLR && !MONO) || MONO_FEATURE_THREAD_SUSPEND_RESUME
583 /*=========================================================================
584 ** Suspends the thread. If the thread is already suspended, this call has
585 ** no effect.
587 ** Exceptions: ThreadStateException if the thread has not been started or
588 ** it is dead.
589 =========================================================================*/
590 [System.Security.SecuritySafeCritical] // auto-generated
591 [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)]
592 [SecurityPermission(SecurityAction.Demand, ControlThread=true)]
593 public void Suspend() { SuspendInternal(); }
595 // Internal helper (since we can't place security demands on
596 // ecalls/fcalls).
597 [System.Security.SecurityCritical] // auto-generated
598 [ResourceExposure(ResourceScope.None)]
599 [MethodImplAttribute(MethodImplOptions.InternalCall)]
600 private extern void SuspendInternal();
602 /*=========================================================================
603 ** Resumes a thread that has been suspended.
605 ** Exceptions: ThreadStateException if the thread has not been started or
606 ** it is dead or it isn't in the suspended state.
607 =========================================================================*/
608 [System.Security.SecuritySafeCritical] // auto-generated
609 [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)]
610 [SecurityPermission(SecurityAction.Demand, ControlThread=true)]
611 public void Resume() { ResumeInternal(); }
613 // Internal helper (since we can't place security demands on
614 // ecalls/fcalls).
615 [System.Security.SecurityCritical] // auto-generated
616 [ResourceExposure(ResourceScope.None)]
617 [MethodImplAttribute(MethodImplOptions.InternalCall)]
618 private extern void ResumeInternal();
619 #endif
621 #if !FEATURE_CORECLR || MONO
622 /*=========================================================================
623 ** Interrupts a thread that is inside a Wait(), Sleep() or Join(). If that
624 ** thread is not currently blocked in that manner, it will be interrupted
625 ** when it next begins to block.
626 =========================================================================*/
627 [System.Security.SecuritySafeCritical] // auto-generated
628 [SecurityPermission(SecurityAction.Demand, ControlThread=true)]
629 public void Interrupt() { InterruptInternal(); }
631 // Internal helper (since we can't place security demands on
632 // ecalls/fcalls).
633 [System.Security.SecurityCritical] // auto-generated
634 [ResourceExposure(ResourceScope.None)]
635 [MethodImplAttribute(MethodImplOptions.InternalCall)]
636 private extern void InterruptInternal();
637 #endif
639 /*=========================================================================
640 ** Returns the priority of the thread.
642 ** Exceptions: ThreadStateException if the thread is dead.
643 =========================================================================*/
645 public ThreadPriority Priority {
646 [System.Security.SecuritySafeCritical] // auto-generated
647 get { return (ThreadPriority)GetPriorityNative(); }
648 [System.Security.SecuritySafeCritical] // auto-generated
649 [HostProtection(SelfAffectingThreading=true)]
650 set { SetPriorityNative((int)value); }
652 [System.Security.SecurityCritical] // auto-generated
653 [ResourceExposure(ResourceScope.None)]
654 [MethodImplAttribute(MethodImplOptions.InternalCall)]
655 private extern int GetPriorityNative();
656 [System.Security.SecurityCritical] // auto-generated
657 [ResourceExposure(ResourceScope.None)]
658 [MethodImplAttribute(MethodImplOptions.InternalCall)]
659 private extern void SetPriorityNative(int priority);
660 #if !MONO
661 /*=========================================================================
662 ** Returns true if the thread has been started and is not dead.
663 =========================================================================*/
664 public extern bool IsAlive {
665 [System.Security.SecuritySafeCritical] // auto-generated
666 [MethodImpl(MethodImplOptions.InternalCall)]
667 get;
670 /*=========================================================================
671 ** Returns true if the thread is a threadpool thread.
672 =========================================================================*/
673 public extern bool IsThreadPoolThread {
674 [System.Security.SecuritySafeCritical] // auto-generated
675 [MethodImpl(MethodImplOptions.InternalCall)]
676 get;
678 #endif
679 /*=========================================================================
680 ** Waits for the thread to die or for timeout milliseconds to elapse.
681 ** Returns true if the thread died, or false if the wait timed out. If
682 ** Timeout.Infinite is given as the parameter, no timeout will occur.
684 ** Exceptions: ArgumentException if timeout < 0.
685 ** ThreadInterruptedException if the thread is interrupted while waiting.
686 ** ThreadStateException if the thread has not been started yet.
687 =========================================================================*/
688 [System.Security.SecurityCritical]
689 [ResourceExposure(ResourceScope.None)]
690 [MethodImplAttribute(MethodImplOptions.InternalCall)]
691 private extern bool JoinInternal(int millisecondsTimeout);
693 [System.Security.SecuritySafeCritical]
694 [HostProtection(Synchronization=true, ExternalThreading=true)]
695 public void Join()
697 JoinInternal(Timeout.Infinite);
700 [System.Security.SecuritySafeCritical]
701 [HostProtection(Synchronization=true, ExternalThreading=true)]
702 public bool Join(int millisecondsTimeout)
704 #if MONO
705 if (millisecondsTimeout < Timeout.Infinite)
706 throw new ArgumentOutOfRangeException("millisecondsTimeout", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1"));
707 #endif
708 return JoinInternal(millisecondsTimeout);
711 [HostProtection(Synchronization=true, ExternalThreading=true)]
712 public bool Join(TimeSpan timeout)
714 long tm = (long)timeout.TotalMilliseconds;
715 if (tm < -1 || tm > (long) Int32.MaxValue)
716 throw new ArgumentOutOfRangeException("timeout", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1"));
718 return Join((int)tm);
721 /*=========================================================================
722 ** Suspends the current thread for timeout milliseconds. If timeout == 0,
723 ** forces the thread to give up the remainer of its timeslice. If timeout
724 ** == Timeout.Infinite, no timeout will occur.
726 ** Exceptions: ArgumentException if timeout < 0.
727 ** ThreadInterruptedException if the thread is interrupted while sleeping.
728 =========================================================================*/
729 [System.Security.SecurityCritical] // auto-generated
730 [ResourceExposure(ResourceScope.None)]
731 [MethodImplAttribute(MethodImplOptions.InternalCall)]
732 private static extern void SleepInternal(int millisecondsTimeout);
734 [System.Security.SecuritySafeCritical] // auto-generated
735 public static void Sleep(int millisecondsTimeout)
737 #if MONO
738 if (millisecondsTimeout < Timeout.Infinite)
739 throw new ArgumentOutOfRangeException("millisecondsTimeout", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1"));
740 #endif
741 SleepInternal(millisecondsTimeout);
742 #if !MONO
743 // Ensure we don't return to app code when the pause is underway
744 if(AppDomainPauseManager.IsPaused)
745 AppDomainPauseManager.ResumeEvent.WaitOneWithoutFAS();
746 #endif
749 public static void Sleep(TimeSpan timeout)
751 long tm = (long)timeout.TotalMilliseconds;
752 if (tm < -1 || tm > (long) Int32.MaxValue)
753 throw new ArgumentOutOfRangeException("timeout", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1"));
754 Sleep((int)tm);
757 #if !MONO
758 /* wait for a length of time proportial to 'iterations'. Each iteration is should
759 only take a few machine instructions. Calling this API is preferable to coding
760 a explict busy loop because the hardware can be informed that it is busy waiting. */
762 [System.Security.SecurityCritical] // auto-generated
763 [MethodImplAttribute(MethodImplOptions.InternalCall),
764 HostProtection(Synchronization=true,ExternalThreading=true),
765 ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success),
766 ResourceExposure(ResourceScope.None)]
767 private static extern void SpinWaitInternal(int iterations);
769 [System.Security.SecuritySafeCritical] // auto-generated
770 [HostProtection(Synchronization=true,ExternalThreading=true),
771 ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
772 public static void SpinWait(int iterations)
774 SpinWaitInternal(iterations);
776 #endif
777 [System.Security.SecurityCritical] // auto-generated
778 [ResourceExposure(ResourceScope.None)]
779 #if MONO
780 [MethodImplAttribute(MethodImplOptions.InternalCall)]
781 #else
782 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
783 #endif
784 [SuppressUnmanagedCodeSecurity]
785 [HostProtection(Synchronization = true, ExternalThreading = true),
786 ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
787 private static extern bool YieldInternal();
789 [System.Security.SecuritySafeCritical] // auto-generated
790 [HostProtection(Synchronization = true, ExternalThreading = true),
791 ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
792 public static bool Yield()
794 return YieldInternal();
797 #if !MONO
798 public static Thread CurrentThread {
799 [System.Security.SecuritySafeCritical] // auto-generated
800 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
801 get {
802 Contract.Ensures(Contract.Result<Thread>() != null);
803 return GetCurrentThreadNative();
806 [System.Security.SecurityCritical] // auto-generated
807 [ResourceExposure(ResourceScope.None)]
808 [MethodImplAttribute(MethodImplOptions.InternalCall), ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
809 private static extern Thread GetCurrentThreadNative();
810 #endif
811 [System.Security.SecurityCritical] // auto-generated
812 private void SetStartHelper(Delegate start, int maxStackSize)
814 #if MONO
815 maxStackSize = GetProcessDefaultStackSize(maxStackSize);
816 #else
817 #if FEATURE_CORECLR
818 // We only support default stacks in CoreCLR
819 Contract.Assert(maxStackSize == 0);
820 #else
821 // Only fully-trusted code is allowed to create "large" stacks. Partial-trust falls back to
822 // the default stack size.
823 ulong defaultStackSize = GetProcessDefaultStackSize();
825 if ((ulong)(uint)maxStackSize > defaultStackSize)
829 SecurityPermission.Demand(PermissionType.FullTrust);
831 catch (SecurityException)
833 maxStackSize = (int)Math.Min(defaultStackSize, (ulong)(uint)int.MaxValue);
836 #endif
837 #endif
839 ThreadHelper threadStartCallBack = new ThreadHelper(start);
840 if(start is ThreadStart)
842 SetStart(new ThreadStart(threadStartCallBack.ThreadStart), maxStackSize);
844 else
846 SetStart(new ParameterizedThreadStart(threadStartCallBack.ThreadStart), maxStackSize);
849 #if !MONO
850 [SecurityCritical]
851 [ResourceExposure(ResourceScope.None)]
852 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
853 [SuppressUnmanagedCodeSecurity]
854 private static extern ulong GetProcessDefaultStackSize();
856 /*=========================================================================
857 ** PRIVATE Sets the IThreadable interface for the thread. Assumes that
858 ** start != null.
859 =========================================================================*/
860 [System.Security.SecurityCritical] // auto-generated
861 [ResourceExposure(ResourceScope.None)]
862 [MethodImplAttribute(MethodImplOptions.InternalCall)]
863 private extern void SetStart(Delegate start, int maxStackSize);
865 /*=========================================================================
866 ** Clean up the thread when it goes away.
867 =========================================================================*/
868 [System.Security.SecuritySafeCritical] // auto-generated
869 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
870 ~Thread()
872 // Delegate to the unmanaged portion.
873 InternalFinalize();
876 [System.Security.SecurityCritical] // auto-generated
877 [ResourceExposure(ResourceScope.None)]
878 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
879 [MethodImplAttribute(MethodImplOptions.InternalCall)]
880 private extern void InternalFinalize();
882 #if FEATURE_COMINTEROP
883 [System.Security.SecurityCritical] // auto-generated
884 [ResourceExposure(ResourceScope.None)]
885 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
886 [MethodImplAttribute(MethodImplOptions.InternalCall)]
887 public extern void DisableComObjectEagerCleanup();
888 #endif //FEATURE_COMINTEROP
890 /*=========================================================================
891 ** Return whether or not this thread is a background thread. Background
892 ** threads do not affect when the Execution Engine shuts down.
894 ** Exceptions: ThreadStateException if the thread is dead.
895 =========================================================================*/
896 public bool IsBackground {
897 [System.Security.SecuritySafeCritical] // auto-generated
898 get { return IsBackgroundNative(); }
899 [System.Security.SecuritySafeCritical] // auto-generated
900 [HostProtection(SelfAffectingThreading=true)]
901 set { SetBackgroundNative(value); }
903 [System.Security.SecurityCritical] // auto-generated
904 [ResourceExposure(ResourceScope.None)]
905 [MethodImplAttribute(MethodImplOptions.InternalCall)]
906 private extern bool IsBackgroundNative();
907 [System.Security.SecurityCritical] // auto-generated
908 [ResourceExposure(ResourceScope.None)]
909 [MethodImplAttribute(MethodImplOptions.InternalCall)]
910 private extern void SetBackgroundNative(bool isBackground);
913 /*=========================================================================
914 ** Return the thread state as a consistent set of bits. This is more
915 ** general then IsAlive or IsBackground.
916 =========================================================================*/
917 public ThreadState ThreadState {
918 [System.Security.SecuritySafeCritical] // auto-generated
919 get { return (ThreadState)GetThreadStateNative(); }
922 [System.Security.SecurityCritical] // auto-generated
923 [ResourceExposure(ResourceScope.None)]
924 [MethodImplAttribute(MethodImplOptions.InternalCall)]
925 private extern int GetThreadStateNative();
927 #if FEATURE_COMINTEROP_APARTMENT_SUPPORT
928 /*=========================================================================
929 ** An unstarted thread can be marked to indicate that it will host a
930 ** single-threaded or multi-threaded apartment.
932 ** Exceptions: ArgumentException if state is not a valid apartment state
933 ** (ApartmentSTA or ApartmentMTA).
934 =========================================================================*/
935 [Obsolete("The ApartmentState property has been deprecated. Use GetApartmentState, SetApartmentState or TrySetApartmentState instead.", false)]
936 public ApartmentState ApartmentState
938 [System.Security.SecuritySafeCritical] // auto-generated
941 return (ApartmentState)GetApartmentStateNative();
944 [System.Security.SecuritySafeCritical] // auto-generated
945 [HostProtection(Synchronization=true, SelfAffectingThreading=true)]
948 SetApartmentStateNative((int)value, true);
952 [System.Security.SecuritySafeCritical] // auto-generated
953 public ApartmentState GetApartmentState()
955 return (ApartmentState)GetApartmentStateNative();
958 [System.Security.SecuritySafeCritical] // auto-generated
959 [HostProtection(Synchronization=true, SelfAffectingThreading=true)]
960 public bool TrySetApartmentState(ApartmentState state)
962 return SetApartmentStateHelper(state, false);
965 [System.Security.SecuritySafeCritical] // auto-generated
966 [HostProtection(Synchronization=true, SelfAffectingThreading=true)]
967 public void SetApartmentState(ApartmentState state)
969 bool result = SetApartmentStateHelper(state, true);
970 if (!result)
971 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ApartmentStateSwitchFailed"));
974 [System.Security.SecurityCritical] // auto-generated
975 private bool SetApartmentStateHelper(ApartmentState state, bool fireMDAOnMismatch)
977 ApartmentState retState = (ApartmentState)SetApartmentStateNative((int)state, fireMDAOnMismatch);
979 // Special case where we pass in Unknown and get back MTA.
980 // Once we CoUninitialize the thread, the OS will still
981 // report the thread as implicitly in the MTA if any
982 // other thread in the process is CoInitialized.
983 if ((state == System.Threading.ApartmentState.Unknown) && (retState == System.Threading.ApartmentState.MTA))
984 return true;
986 if (retState != state)
987 return false;
989 return true;
992 [System.Security.SecurityCritical] // auto-generated
993 [ResourceExposure(ResourceScope.None)]
994 [MethodImplAttribute(MethodImplOptions.InternalCall)]
995 private extern int GetApartmentStateNative();
996 [System.Security.SecurityCritical] // auto-generated
997 [ResourceExposure(ResourceScope.None)]
998 [MethodImplAttribute(MethodImplOptions.InternalCall)]
999 private extern int SetApartmentStateNative(int state, bool fireMDAOnMismatch);
1000 [System.Security.SecurityCritical] // auto-generated
1001 [ResourceExposure(ResourceScope.None)]
1002 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1003 private extern void StartupSetApartmentStateInternal();
1004 #endif // FEATURE_COMINTEROP_APARTMENT_SUPPORT
1005 #endif
1006 /*=========================================================================
1007 ** Allocates an un-named data slot. The slot is allocated on ALL the
1008 ** threads.
1009 =========================================================================*/
1010 [HostProtection(SharedState=true, ExternalThreading=true)]
1011 public static LocalDataStoreSlot AllocateDataSlot()
1013 return LocalDataStoreManager.AllocateDataSlot();
1016 /*=========================================================================
1017 ** Allocates a named data slot. The slot is allocated on ALL the
1018 ** threads. Named data slots are "public" and can be manipulated by
1019 ** anyone.
1020 =========================================================================*/
1021 [HostProtection(SharedState=true, ExternalThreading=true)]
1022 public static LocalDataStoreSlot AllocateNamedDataSlot(String name)
1024 return LocalDataStoreManager.AllocateNamedDataSlot(name);
1027 /*=========================================================================
1028 ** Looks up a named data slot. If the name has not been used, a new slot is
1029 ** allocated. Named data slots are "public" and can be manipulated by
1030 ** anyone.
1031 =========================================================================*/
1032 [HostProtection(SharedState=true, ExternalThreading=true)]
1033 public static LocalDataStoreSlot GetNamedDataSlot(String name)
1035 return LocalDataStoreManager.GetNamedDataSlot(name);
1038 /*=========================================================================
1039 ** Frees a named data slot. The slot is allocated on ALL the
1040 ** threads. Named data slots are "public" and can be manipulated by
1041 ** anyone.
1042 =========================================================================*/
1043 [HostProtection(SharedState=true, ExternalThreading=true)]
1044 public static void FreeNamedDataSlot(String name)
1046 LocalDataStoreManager.FreeNamedDataSlot(name);
1049 /*=========================================================================
1050 ** Retrieves the value from the specified slot on the current thread, for that thread's current domain.
1051 =========================================================================*/
1052 [HostProtection(SharedState=true, ExternalThreading=true)]
1053 [ResourceExposure(ResourceScope.AppDomain)]
1054 public static Object GetData(LocalDataStoreSlot slot)
1056 LocalDataStoreHolder dls = s_LocalDataStore;
1057 if (dls == null)
1059 // Make sure to validate the slot even if we take the quick path
1060 LocalDataStoreManager.ValidateSlot(slot);
1061 return null;
1064 return dls.Store.GetData(slot);
1067 /*=========================================================================
1068 ** Sets the data in the specified slot on the currently running thread, for that thread's current domain.
1069 =========================================================================*/
1070 [HostProtection(SharedState=true, ExternalThreading=true)]
1071 [ResourceExposure(ResourceScope.AppDomain)]
1072 public static void SetData(LocalDataStoreSlot slot, Object data)
1074 LocalDataStoreHolder dls = s_LocalDataStore;
1076 // Create new DLS if one hasn't been created for this domain for this thread
1077 if (dls == null) {
1078 dls = LocalDataStoreManager.CreateLocalDataStore();
1079 s_LocalDataStore = dls;
1082 dls.Store.SetData(slot, data);
1085 // #threadCultureInfo
1087 // Background:
1088 // In the desktop runtime, we allow a thread's cultures to travel with the thread
1089 // across AppDomain boundaries. Furthermore we update the native thread with the
1090 // culture of the managed thread. Because of security concerns and potential SxS
1091 // effects, in Silverlight we are making the changes listed below.
1093 // Silverlight Changes:
1094 // - thread instance member cultures (CurrentCulture and CurrentUICulture)
1095 // confined within AppDomains
1096 // - changes to these properties don't affect the underlying native thread
1098 // Ifdef:
1099 // FEATURE_LEAK_CULTURE_INFO : CultureInfos can leak across AppDomains, not
1100 // enabled in Silverlight
1102 // Implementation notes:
1103 // In Silverlight, culture members thread static (per Thread, per AppDomain).
1105 // Quirks:
1106 // An interesting side-effect of isolating cultures within an AppDomain is that we
1107 // now need to special case resource lookup for mscorlib, which transitions to the
1108 // default domain to lookup resources. See Environment.cs for more details.
1110 #if FEATURE_LEAK_CULTURE_INFO
1111 [System.Security.SecurityCritical] // auto-generated
1112 [ResourceExposure(ResourceScope.None)]
1113 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1114 static extern private bool nativeGetSafeCulture(Thread t, int appDomainId, bool isUI, ref CultureInfo safeCulture);
1115 #endif // FEATURE_LEAK_CULTURE_INFO
1117 // As the culture can be customized object then we cannot hold any
1118 // reference to it before we check if it is safe because the app domain
1119 // owning this customized culture may get unloaded while executing this
1120 // code. To achieve that we have to do the check using nativeGetSafeCulture
1121 // as the thread cannot get interrupted during the FCALL.
1122 // If the culture is safe (not customized or created in current app domain)
1123 // then the FCALL will return a reference to that culture otherwise the
1124 // FCALL will return failure. In case of failure we'll return the default culture.
1125 // If the app domain owning a customized culture that is set to teh thread and this
1126 // app domain get unloaded there is a code to clean up the culture from the thread
1127 // using the code in AppDomain::ReleaseDomainStores.
1129 public CultureInfo CurrentUICulture {
1130 get {
1131 Contract.Ensures(Contract.Result<CultureInfo>() != null);
1132 #if FEATURE_APPX
1133 if(AppDomain.IsAppXModel()) {
1134 return CultureInfo.GetCultureInfoForUserPreferredLanguageInAppX() ?? GetCurrentUICultureNoAppX();
1136 else
1137 #endif
1139 return GetCurrentUICultureNoAppX();
1143 [System.Security.SecuritySafeCritical] // auto-generated
1144 [HostProtection(ExternalThreading=true)]
1145 set {
1146 if (value == null) {
1147 throw new ArgumentNullException("value");
1149 Contract.EndContractBlock();
1151 //If they're trying to use a Culture with a name that we can't use in resource lookup,
1152 //don't even let them set it on the thread.
1153 CultureInfo.VerifyCultureName(value, true);
1155 // If you add more pre-conditions to this method, check to see if you also need to
1156 // add them to CultureInfo.DefaultThreadCurrentUICulture.set.
1158 #if FEATURE_LEAK_CULTURE_INFO
1159 if (nativeSetThreadUILocale(value.SortName) == false)
1161 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidResourceCultureName", value.Name));
1163 value.StartCrossDomainTracking();
1164 #else
1165 if (m_CurrentUICulture == null && m_CurrentCulture == null)
1166 nativeInitCultureAccessors();
1167 #endif
1169 #if FEATURE_LEGACYNETCF
1170 if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
1173 // NetCF had a bug where Thread.Current{UI}Culture would set the culture for every thread in the process.
1174 // This was because they stored the value in a regular static field (NetCF has no support for ThreadStatic fields).
1175 // Some apps depend on the broken behavior. We will emulate this behavior by redirecting setters to
1176 // DefaultThreadCurrentUICulture. (Note that this property did not existed in NetCF and so it is fine to piggy back
1177 // on it for the quirk.)
1179 CultureInfo.SetCurrentUICultureQuirk(value);
1180 return;
1182 #endif
1183 if (!AppContextSwitches.NoAsyncCurrentCulture)
1185 if (s_asyncLocalCurrentUICulture == null)
1187 Interlocked.CompareExchange(ref s_asyncLocalCurrentUICulture, new AsyncLocal<CultureInfo>(AsyncLocalSetCurrentUICulture), null);
1190 // this one will set m_CurrentUICulture too
1191 s_asyncLocalCurrentUICulture.Value = value;
1193 else
1195 m_CurrentUICulture = value;
1200 #if FEATURE_LEAK_CULTURE_INFO
1201 [System.Security.SecuritySafeCritical] // auto-generated
1202 #endif
1203 internal CultureInfo GetCurrentUICultureNoAppX() {
1205 Contract.Ensures(Contract.Result<CultureInfo>() != null);
1207 // Fetch a local copy of m_CurrentUICulture to
1208 // avoid ----s that malicious user can introduce
1209 if (m_CurrentUICulture == null) {
1210 CultureInfo appDomainDefaultUICulture = CultureInfo.DefaultThreadCurrentUICulture;
1211 return (appDomainDefaultUICulture != null ? appDomainDefaultUICulture : CultureInfo.UserDefaultUICulture);
1214 #if FEATURE_LEAK_CULTURE_INFO
1215 CultureInfo culture = null;
1217 if (!nativeGetSafeCulture(this, GetDomainID(), true, ref culture) || culture == null) {
1218 return CultureInfo.UserDefaultUICulture;
1221 return culture;
1222 #else
1223 return m_CurrentUICulture;
1224 #endif
1227 // This returns the exposed context for a given context ID.
1228 #if FEATURE_LEAK_CULTURE_INFO
1229 [System.Security.SecurityCritical] // auto-generated
1230 [ResourceExposure(ResourceScope.None)]
1231 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1232 static extern private bool nativeSetThreadUILocale(String locale);
1233 #endif
1235 // As the culture can be customized object then we cannot hold any
1236 // reference to it before we check if it is safe because the app domain
1237 // owning this customized culture may get unloaded while executing this
1238 // code. To achieve that we have to do the check using nativeGetSafeCulture
1239 // as the thread cannot get interrupted during the FCALL.
1240 // If the culture is safe (not customized or created in current app domain)
1241 // then the FCALL will return a reference to that culture otherwise the
1242 // FCALL will return failure. In case of failure we'll return the default culture.
1243 // If the app domain owning a customized culture that is set to teh thread and this
1244 // app domain get unloaded there is a code to clean up the culture from the thread
1245 // using the code in AppDomain::ReleaseDomainStores.
1247 public CultureInfo CurrentCulture {
1248 get {
1249 Contract.Ensures(Contract.Result<CultureInfo>() != null);
1251 #if FEATURE_APPX
1252 if(AppDomain.IsAppXModel()) {
1253 return CultureInfo.GetCultureInfoForUserPreferredLanguageInAppX() ?? GetCurrentCultureNoAppX();
1255 else
1256 #endif
1258 return GetCurrentCultureNoAppX();
1262 [System.Security.SecuritySafeCritical] // auto-generated
1263 #if FEATURE_LEAK_CULTURE_INFO
1264 [SecurityPermission(SecurityAction.Demand, ControlThread = true)]
1265 #endif
1266 set {
1267 if (null==value) {
1268 throw new ArgumentNullException("value");
1270 Contract.EndContractBlock();
1272 // If you add more pre-conditions to this method, check to see if you also need to
1273 // add them to CultureInfo.DefaultThreadCurrentCulture.set.
1275 #if FEATURE_LEAK_CULTURE_INFO
1276 //If we can't set the nativeThreadLocale, we'll just let it stay
1277 //at whatever value it had before. This allows people who use
1278 //just managed code not to be limited by the underlying OS.
1279 CultureInfo.nativeSetThreadLocale(value.SortName);
1280 value.StartCrossDomainTracking();
1281 #else
1282 if (m_CurrentCulture == null && m_CurrentUICulture == null)
1283 nativeInitCultureAccessors();
1284 #endif
1286 #if FEATURE_LEGACYNETCF
1287 if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
1289 // See comment in CurrentUICulture setter
1290 CultureInfo.SetCurrentCultureQuirk(value);
1291 return;
1293 #endif
1294 if (!AppContextSwitches.NoAsyncCurrentCulture)
1296 if (s_asyncLocalCurrentCulture == null)
1298 Interlocked.CompareExchange(ref s_asyncLocalCurrentCulture, new AsyncLocal<CultureInfo>(AsyncLocalSetCurrentCulture), null);
1300 // this one will set m_CurrentCulture too
1301 s_asyncLocalCurrentCulture.Value = value;
1303 else
1305 m_CurrentCulture = value;
1310 #if FEATURE_LEAK_CULTURE_INFO
1311 [System.Security.SecuritySafeCritical] // auto-generated
1312 #endif
1313 private CultureInfo GetCurrentCultureNoAppX() {
1315 Contract.Ensures(Contract.Result<CultureInfo>() != null);
1317 // Fetch a local copy of m_CurrentCulture to
1318 // avoid ----s that malicious user can introduce
1319 if (m_CurrentCulture == null) {
1320 CultureInfo appDomainDefaultCulture = CultureInfo.DefaultThreadCurrentCulture;
1321 return (appDomainDefaultCulture != null ? appDomainDefaultCulture : CultureInfo.UserDefaultCulture);
1324 #if FEATURE_LEAK_CULTURE_INFO
1325 CultureInfo culture = null;
1327 if (!nativeGetSafeCulture(this, GetDomainID(), false, ref culture) || culture == null) {
1328 return CultureInfo.UserDefaultCulture;
1331 return culture;
1332 #else
1333 return m_CurrentCulture;
1334 #endif
1338 #if! FEATURE_LEAK_CULTURE_INFO
1340 #if MONO
1341 static void nativeInitCultureAccessors()
1343 m_CurrentCulture = CultureInfo.ConstructCurrentCulture ();
1344 m_CurrentUICulture = CultureInfo.ConstructCurrentUICulture ();
1346 #else
1347 [System.Security.SecurityCritical] // auto-generated
1348 [ResourceExposure(ResourceScope.None)]
1349 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
1350 [SuppressUnmanagedCodeSecurity]
1351 private static extern void nativeInitCultureAccessors();
1352 #endif
1353 #endif
1355 #if !MONO
1357 /*=============================================================*/
1359 /*======================================================================
1360 ** Current thread context is stored in a slot in the thread local store
1361 ** CurrentContext gets the Context from the slot.
1362 ======================================================================*/
1363 #if FEATURE_REMOTING
1364 public static Context CurrentContext
1366 [System.Security.SecurityCritical] // auto-generated_required
1369 return CurrentThread.GetCurrentContextInternal();
1373 [System.Security.SecurityCritical] // auto-generated
1374 internal Context GetCurrentContextInternal()
1376 if (m_Context == null)
1378 m_Context = Context.DefaultContext;
1380 return m_Context;
1382 #endif
1385 #if FEATURE_IMPERSONATION
1386 // Get and set thread's current principal (for role based security).
1387 public static IPrincipal CurrentPrincipal
1389 [System.Security.SecuritySafeCritical] // auto-generated
1392 lock (CurrentThread)
1394 IPrincipal principal = (IPrincipal)
1395 CallContext.Principal;
1396 if (principal == null)
1398 principal = GetDomain().GetThreadPrincipal();
1399 CallContext.Principal = principal;
1401 return principal;
1405 [System.Security.SecuritySafeCritical] // auto-generated
1406 [SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPrincipal)]
1409 CallContext.Principal = value;
1413 // Private routine called from unmanaged code to set an initial
1414 // principal for a newly created thread.
1415 [System.Security.SecurityCritical] // auto-generated
1416 private void SetPrincipalInternal(IPrincipal principal)
1418 GetMutableExecutionContext().LogicalCallContext.SecurityData.Principal = principal;
1420 #endif // FEATURE_IMPERSONATION
1422 #if FEATURE_REMOTING
1424 // This returns the exposed context for a given context ID.
1425 [System.Security.SecurityCritical] // auto-generated
1426 [ResourceExposure(ResourceScope.None)]
1427 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1428 internal static extern Context GetContextInternal(IntPtr id);
1430 [System.Security.SecurityCritical] // auto-generated
1431 [ResourceExposure(ResourceScope.None)]
1432 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1433 internal extern Object InternalCrossContextCallback(Context ctx, IntPtr ctxID, Int32 appDomainID, InternalCrossContextDelegate ftnToCall, Object[] args);
1435 [System.Security.SecurityCritical] // auto-generated
1436 internal Object InternalCrossContextCallback(Context ctx, InternalCrossContextDelegate ftnToCall, Object[] args)
1438 return InternalCrossContextCallback(ctx, ctx.InternalContextID, 0, ftnToCall, args);
1441 // CompleteCrossContextCallback is called by the EE after transitioning to the requested context
1442 private static Object CompleteCrossContextCallback(InternalCrossContextDelegate ftnToCall, Object[] args)
1444 return ftnToCall(args);
1446 #endif // FEATURE_REMOTING
1448 /*======================================================================
1449 ** Returns the current domain in which current thread is running.
1450 ======================================================================*/
1452 [System.Security.SecurityCritical] // auto-generated
1453 [ResourceExposure(ResourceScope.None)]
1454 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1455 private static extern AppDomain GetDomainInternal();
1456 [System.Security.SecurityCritical] // auto-generated
1457 [ResourceExposure(ResourceScope.None)]
1458 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1459 private static extern AppDomain GetFastDomainInternal();
1461 [System.Security.SecuritySafeCritical] // auto-generated
1462 public static AppDomain GetDomain()
1464 Contract.Ensures(Contract.Result<AppDomain>() != null);
1467 AppDomain ad;
1468 ad = GetFastDomainInternal();
1469 if (ad == null)
1470 ad = GetDomainInternal();
1472 #if FEATURE_REMOTING
1473 Contract.Assert(CurrentThread.m_Context == null || CurrentThread.m_Context.AppDomain == ad, "AppDomains on the managed & unmanaged threads should match");
1474 #endif
1475 return ad;
1480 * This returns a unique id to identify an appdomain.
1482 public static int GetDomainID()
1484 return GetDomain().GetId();
1488 // Retrieves the name of the thread.
1490 public String Name {
1491 get {
1492 return m_Name;
1495 [System.Security.SecuritySafeCritical] // auto-generated
1496 [HostProtection(ExternalThreading=true)]
1497 set {
1498 lock(this) {
1499 if (m_Name != null)
1500 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_WriteOnce"));
1501 m_Name = value;
1503 InformThreadNameChange(GetNativeHandle(), value, (value != null) ? value.Length : 0);
1508 [System.Security.SecurityCritical] // auto-generated
1509 [ResourceExposure(ResourceScope.None)]
1510 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
1511 [SuppressUnmanagedCodeSecurity]
1512 private static extern void InformThreadNameChange(ThreadHandle t, String name, int len);
1514 internal Object AbortReason {
1515 [System.Security.SecurityCritical] // auto-generated
1516 get {
1517 object result = null;
1520 result = GetAbortReason();
1522 catch (Exception e)
1524 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ExceptionStateCrossAppDomain"), e);
1526 return result;
1528 [System.Security.SecurityCritical] // auto-generated
1529 set { SetAbortReason(value); }
1533 * This marks the beginning of a critical code region.
1535 [System.Security.SecuritySafeCritical] // auto-generated
1536 [HostProtection(Synchronization=true, ExternalThreading=true)]
1537 [ResourceExposure(ResourceScope.None)]
1538 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1539 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
1540 public static extern void BeginCriticalRegion();
1543 * This marks the end of a critical code region.
1545 [System.Security.SecuritySafeCritical] // auto-generated
1546 [HostProtection(Synchronization=true, ExternalThreading=true)]
1547 [ResourceExposure(ResourceScope.None)]
1548 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1549 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
1550 public static extern void EndCriticalRegion();
1553 * This marks the beginning of a code region that requires thread affinity.
1555 [System.Security.SecurityCritical] // auto-generated_required
1556 [ResourceExposure(ResourceScope.None)]
1557 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1558 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
1559 public static extern void BeginThreadAffinity();
1562 * This marks the end of a code region that requires thread affinity.
1564 [System.Security.SecurityCritical] // auto-generated_required
1565 [ResourceExposure(ResourceScope.None)]
1566 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1567 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
1568 public static extern void EndThreadAffinity();
1570 /*=========================================================================
1571 ** Volatile Read & Write and MemoryBarrier methods.
1572 ** Provides the ability to read and write values ensuring that the values
1573 ** are read/written each time they are accessed.
1574 =========================================================================*/
1576 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1577 public static byte VolatileRead(ref byte address)
1579 byte ret = address;
1580 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1581 return ret;
1584 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1585 public static short VolatileRead(ref short address)
1587 short ret = address;
1588 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1589 return ret;
1592 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1593 public static int VolatileRead(ref int address)
1595 int ret = address;
1596 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1597 return ret;
1600 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1601 public static long VolatileRead(ref long address)
1603 long ret = address;
1604 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1605 return ret;
1608 [CLSCompliant(false)]
1609 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1610 public static sbyte VolatileRead(ref sbyte address)
1612 sbyte ret = address;
1613 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1614 return ret;
1617 [CLSCompliant(false)]
1618 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1619 public static ushort VolatileRead(ref ushort address)
1621 ushort ret = address;
1622 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1623 return ret;
1626 [CLSCompliant(false)]
1627 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1628 public static uint VolatileRead(ref uint address)
1630 uint ret = address;
1631 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1632 return ret;
1635 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1636 public static IntPtr VolatileRead(ref IntPtr address)
1638 IntPtr ret = address;
1639 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1640 return ret;
1643 [CLSCompliant(false)]
1644 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1645 public static UIntPtr VolatileRead(ref UIntPtr address)
1647 UIntPtr ret = address;
1648 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1649 return ret;
1652 [CLSCompliant(false)]
1653 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1654 public static ulong VolatileRead(ref ulong address)
1656 ulong ret = address;
1657 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1658 return ret;
1661 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1662 public static float VolatileRead(ref float address)
1664 float ret = address;
1665 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1666 return ret;
1669 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1670 public static double VolatileRead(ref double address)
1672 double ret = address;
1673 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1674 return ret;
1677 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1678 public static Object VolatileRead(ref Object address)
1680 Object ret = address;
1681 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1682 return ret;
1685 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1686 public static void VolatileWrite(ref byte address, byte value)
1688 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1689 address = value;
1692 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1693 public static void VolatileWrite(ref short address, short value)
1695 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1696 address = value;
1699 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1700 public static void VolatileWrite(ref int address, int value)
1702 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1703 address = value;
1706 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1707 public static void VolatileWrite(ref long address, long value)
1709 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1710 address = value;
1713 [CLSCompliant(false)]
1714 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1715 public static void VolatileWrite(ref sbyte address, sbyte value)
1717 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1718 address = value;
1721 [CLSCompliant(false)]
1722 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1723 public static void VolatileWrite(ref ushort address, ushort value)
1725 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1726 address = value;
1729 [CLSCompliant(false)]
1730 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1731 public static void VolatileWrite(ref uint address, uint value)
1733 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1734 address = value;
1737 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1738 public static void VolatileWrite(ref IntPtr address, IntPtr value)
1740 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1741 address = value;
1744 [CLSCompliant(false)]
1745 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1746 public static void VolatileWrite(ref UIntPtr address, UIntPtr value)
1748 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1749 address = value;
1752 [CLSCompliant(false)]
1753 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1754 public static void VolatileWrite(ref ulong address, ulong value)
1756 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1757 address = value;
1760 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1761 public static void VolatileWrite(ref float address, float value)
1763 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1764 address = value;
1767 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1768 public static void VolatileWrite(ref double address, double value)
1770 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1771 address = value;
1774 [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations
1775 public static void VolatileWrite(ref Object address, Object value)
1777 MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way.
1778 address = value;
1780 #endif
1781 [System.Security.SecuritySafeCritical] // auto-generated
1782 [ResourceExposure(ResourceScope.None)]
1783 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1784 public static extern void MemoryBarrier();
1786 private static LocalDataStoreMgr LocalDataStoreManager
1788 get
1790 if (s_LocalDataStoreMgr == null)
1792 Interlocked.CompareExchange(ref s_LocalDataStoreMgr, new LocalDataStoreMgr(), null);
1795 return s_LocalDataStoreMgr;
1798 #if !MOBILE
1799 void _Thread.GetTypeInfoCount(out uint pcTInfo)
1801 throw new NotImplementedException();
1804 void _Thread.GetTypeInfo(uint iTInfo, uint lcid, IntPtr ppTInfo)
1806 throw new NotImplementedException();
1809 void _Thread.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1811 throw new NotImplementedException();
1814 void _Thread.Invoke(uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1816 throw new NotImplementedException();
1818 #endif
1819 #if !MONO
1821 // Helper function to set the AbortReason for a thread abort.
1822 // Checks that they're not alredy set, and then atomically updates
1823 // the reason info (object + ADID).
1824 [System.Security.SecurityCritical] // auto-generated
1825 [ResourceExposure(ResourceScope.None)]
1826 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1827 internal extern void SetAbortReason(Object o);
1829 // Helper function to retrieve the AbortReason from a thread
1830 // abort. Will perform cross-AppDomain marshalling if the object
1831 // lives in a different AppDomain from the requester.
1832 [System.Security.SecurityCritical] // auto-generated
1833 [ResourceExposure(ResourceScope.None)]
1834 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1835 internal extern Object GetAbortReason();
1837 // Helper function to clear the AbortReason. Takes care of
1838 // AppDomain related cleanup if required.
1839 [System.Security.SecurityCritical] // auto-generated
1840 [ResourceExposure(ResourceScope.None)]
1841 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1842 internal extern void ClearAbortReason();
1843 #endif
1845 } // End of class Thread
1847 // declaring a local var of this enum type and passing it by ref into a function that needs to do a
1848 // stack crawl will both prevent inlining of the calle and pass an ESP point to stack crawl to
1849 // Declaring these in EH clauses is illegal; they must declared in the main method body
1850 [Serializable]
1851 internal enum StackCrawlMark
1853 LookForMe = 0,
1854 LookForMyCaller = 1,
1855 LookForMyCallersCaller = 2,
1856 LookForThread = 3