diff options
Diffstat (limited to 'src/mscorlib/src/System/Threading/Thread.cs')
-rw-r--r-- | src/mscorlib/src/System/Threading/Thread.cs | 633 |
1 files changed, 27 insertions, 606 deletions
diff --git a/src/mscorlib/src/System/Threading/Thread.cs b/src/mscorlib/src/System/Threading/Thread.cs index e62cfae9fe..8294c20c4d 100644 --- a/src/mscorlib/src/System/Threading/Thread.cs +++ b/src/mscorlib/src/System/Threading/Thread.cs @@ -12,16 +12,13 @@ ** =============================================================================*/ +using Internal.Runtime.Augments; + namespace System.Threading { using System.Threading; using System.Runtime; using System.Runtime.InteropServices; -#if FEATURE_REMOTING - using System.Runtime.Remoting.Contexts; - using System.Runtime.Remoting.Messaging; -#endif using System; - using System.Diagnostics; using System.Security.Permissions; using System.Security.Principal; using System.Globalization; @@ -31,13 +28,13 @@ namespace System.Threading { using System.Runtime.ConstrainedExecution; using System.Security; using System.Runtime.Versioning; + using System.Diagnostics; using System.Diagnostics.Contracts; internal delegate Object InternalCrossContextDelegate(Object[] args); internal class ThreadHelper { - [System.Security.SecuritySafeCritical] static ThreadHelper() {} Delegate _start; @@ -53,10 +50,8 @@ namespace System.Threading { _executionContext = ec; } - [System.Security.SecurityCritical] static internal ContextCallback _ccb = new ContextCallback(ThreadStart_Context); - [System.Security.SecurityCritical] static private void ThreadStart_Context(Object state) { ThreadHelper t = (ThreadHelper)state; @@ -71,11 +66,6 @@ namespace System.Threading { } // call back helper - #if FEATURE_CORECLR - [System.Security.SecuritySafeCritical] // auto-generated - #else - [System.Security.SecurityCritical] - #endif internal void ThreadStart(object obj) { _startArg = obj; @@ -90,11 +80,6 @@ namespace System.Threading { } // call back helper - #if FEATURE_CORECLR - [System.Security.SecuritySafeCritical] // auto-generated - #else - [System.Security.SecurityCritical] - #endif internal void ThreadStart() { if (_executionContext != null) @@ -121,25 +106,20 @@ namespace System.Threading { // deliberately not [serializable] [ClassInterface(ClassInterfaceType.None)] [ComDefaultInterface(typeof(_Thread))] -[System.Runtime.InteropServices.ComVisible(true)] - public sealed class Thread : CriticalFinalizerObject, _Thread + [System.Runtime.InteropServices.ComVisible(true)] + public sealed class Thread : RuntimeThread, _Thread { /*========================================================================= ** Data accessed from managed code that needs to be defined in ** ThreadBaseObject to maintain alignment between the two classes. ** DON'T CHANGE THESE UNLESS YOU MODIFY ThreadBaseObject in vm\object.h =========================================================================*/ -#if FEATURE_REMOTING - private Context m_Context; -#endif private ExecutionContext m_ExecutionContext; // this call context follows the logical thread -#if FEATURE_CORECLR private SynchronizationContext m_SynchronizationContext; // On CoreCLR, this is maintained separately from ExecutionContext -#endif private String m_Name; private Delegate m_Delegate; // Delegate - + #if FEATURE_LEAK_CULTURE_INFO private CultureInfo m_CurrentCulture; private CultureInfo m_CurrentUICulture; @@ -212,11 +192,8 @@ namespace System.Threading { #endif // FEATURE_LEAK_CULTURE_INFO } -#if FEATURE_CORECLR // Adding an empty default ctor for annotation purposes - [System.Security.SecuritySafeCritical] // auto-generated internal Thread(){} -#endif // FEATURE_CORECLR /*========================================================================= ** Creates a new Thread object which will begin execution at @@ -224,41 +201,37 @@ namespace System.Threading { ** ** Exceptions: ArgumentNullException if start == null. =========================================================================*/ - [System.Security.SecuritySafeCritical] // auto-generated public Thread(ThreadStart start) { if (start == null) { - throw new ArgumentNullException("start"); + throw new ArgumentNullException(nameof(start)); } Contract.EndContractBlock(); SetStartHelper((Delegate)start,0); //0 will setup Thread with default stackSize } - [System.Security.SecuritySafeCritical] // auto-generated public Thread(ThreadStart start, int maxStackSize) { if (start == null) { - throw new ArgumentNullException("start"); + throw new ArgumentNullException(nameof(start)); } if (0 > maxStackSize) - throw new ArgumentOutOfRangeException("maxStackSize",Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException(nameof(maxStackSize),Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); Contract.EndContractBlock(); SetStartHelper((Delegate)start, maxStackSize); } - [System.Security.SecuritySafeCritical] // auto-generated public Thread(ParameterizedThreadStart start) { if (start == null) { - throw new ArgumentNullException("start"); + throw new ArgumentNullException(nameof(start)); } Contract.EndContractBlock(); SetStartHelper((Delegate)start, 0); } - [System.Security.SecuritySafeCritical] // auto-generated public Thread(ParameterizedThreadStart start, int maxStackSize) { if (start == null) { - throw new ArgumentNullException("start"); + throw new ArgumentNullException(nameof(start)); } if (0 > maxStackSize) - throw new ArgumentOutOfRangeException("maxStackSize",Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException(nameof(maxStackSize),Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); Contract.EndContractBlock(); SetStartHelper((Delegate)start, maxStackSize); } @@ -269,11 +242,10 @@ namespace System.Threading { return m_ManagedThreadId; } - extern public int ManagedThreadId + extern public new int ManagedThreadId { [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] [MethodImplAttribute(MethodImplOptions.InternalCall)] - [System.Security.SecuritySafeCritical] // auto-generated get; } @@ -299,17 +271,15 @@ namespace System.Threading { ** ** Exceptions: ThreadStateException if the thread has already been started. =========================================================================*/ - [HostProtection(Synchronization=true,ExternalThreading=true)] [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable - public void Start() + public new void Start() { StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; Start(ref stackMark); } - [HostProtection(Synchronization=true,ExternalThreading=true)] [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable - public void Start(object parameter) + public new void Start(object parameter) { //In the case of a null delegate (second call to start on same thread) // StartInternal method will take care of the error reporting @@ -325,7 +295,6 @@ namespace System.Threading { Start(ref stackMark); } - [System.Security.SecuritySafeCritical] private void Start(ref StackCrawlMark stackMark) { #if FEATURE_COMINTEROP_APARTMENT_SUPPORT @@ -346,16 +315,11 @@ namespace System.Threading { ExecutionContext.CaptureOptions.IgnoreSyncCtx); t.SetExecutionContextHelper(ec); } -#if FEATURE_IMPERSONATION - IPrincipal principal = (IPrincipal)CallContext.Principal; -#else + IPrincipal principal = null; -#endif StartInternal(principal, ref stackMark); } - -#if FEATURE_CORECLR internal ExecutionContext ExecutionContext { get { return m_ExecutionContext; } @@ -366,89 +330,12 @@ namespace System.Threading { { get { return m_SynchronizationContext; } set { m_SynchronizationContext = value; } - } -#else // !FEATURE_CORECLR - [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] - internal ExecutionContext.Reader GetExecutionContextReader() - { - return new ExecutionContext.Reader(m_ExecutionContext); - } - - internal bool ExecutionContextBelongsToCurrentScope - { - get { return !m_ExecutionContextBelongsToOuterScope; } - set { m_ExecutionContextBelongsToOuterScope = !value; } - } - -#if DEBUG - internal bool ForbidExecutionContextMutation - { - set { m_ForbidExecutionContextMutation = value; } - } -#endif - - // note: please don't access this directly from mscorlib. Use GetMutableExecutionContext or GetExecutionContextReader instead. - public ExecutionContext ExecutionContext - { - [SecuritySafeCritical] - [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] - get - { - ExecutionContext result; - if (this == Thread.CurrentThread) - result = GetMutableExecutionContext(); - else - result = m_ExecutionContext; - - return result; - } } - [SecurityCritical] - [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] - internal ExecutionContext GetMutableExecutionContext() - { - Contract.Assert(Thread.CurrentThread == this); -#if DEBUG - Contract.Assert(!m_ForbidExecutionContextMutation); -#endif - if (m_ExecutionContext == null) - { - m_ExecutionContext = new ExecutionContext(); - } - else if (!ExecutionContextBelongsToCurrentScope) - { - ExecutionContext copy = m_ExecutionContext.CreateMutableCopy(); - m_ExecutionContext = copy; - } - - ExecutionContextBelongsToCurrentScope = true; - return m_ExecutionContext; - } - - [SecurityCritical] - [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] - internal void SetExecutionContext(ExecutionContext value, bool belongsToCurrentScope) - { - m_ExecutionContext = value; - ExecutionContextBelongsToCurrentScope = belongsToCurrentScope; - } - - [SecurityCritical] - [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] - internal void SetExecutionContext(ExecutionContext.Reader value, bool belongsToCurrentScope) - { - m_ExecutionContext = value.DangerousGetRawExecutionContext(); - ExecutionContextBelongsToCurrentScope = belongsToCurrentScope; - } -#endif //!FEATURE_CORECLR - - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] private extern void StartInternal(IPrincipal principal, ref StackCrawlMark stackMark); #if FEATURE_COMPRESSEDSTACK /// <internalonly/> - [System.Security.SecurityCritical] // auto-generated_required [DynamicSecurityMethodAttribute()] [Obsolete("Thread.SetCompressedStack is no longer supported. Please use the System.Threading.CompressedStack class")] public void SetCompressedStack( CompressedStack stack ) @@ -456,17 +343,14 @@ namespace System.Threading { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ThreadAPIsNotSupported")); } - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall), ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] internal extern IntPtr SetAppDomainStack( SafeCompressedStackHandle csHandle); - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall), ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] internal extern void RestoreAppDomainStack( IntPtr appDomainStack); /// <internalonly/> - [System.Security.SecurityCritical] // auto-generated_required [Obsolete("Thread.GetCompressedStack is no longer supported. Please use the System.Threading.CompressedStack class")] public CompressedStack GetCompressedStack() { @@ -478,7 +362,6 @@ namespace System.Threading { // Helper method to get a logical thread ID for StringBuilder (for // correctness) and for FileStream's async code path (for perf, to // avoid creating a Thread instance). - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] internal extern static IntPtr InternalGetCurrentThread(); @@ -497,34 +380,6 @@ namespace System.Threading { ** If Abort is called twice on the same thread, a DuplicateThreadAbort ** exception is thrown. =========================================================================*/ - -#if !FEATURE_CORECLR - [System.Security.SecuritySafeCritical] // auto-generated - [SecurityPermissionAttribute(SecurityAction.Demand, ControlThread=true)] - public void Abort(Object stateInfo) - { - // If two aborts come at the same time, it is possible that the state info - // gets set by one, and the actual abort gets delivered by another. But this - // is not distinguishable by an application. - // The accessor helper will only set the value if it isn't already set, - // and that particular bit of native code can test much faster than this - // code could, because testing might cause a cross-appdomain marshalling. - AbortReason = stateInfo; - - // Note: we demand ControlThread permission, then call AbortInternal directly - // rather than delegating to the Abort() function below. We do this to ensure - // that only callers with ControlThread are allowed to change the AbortReason - // of the thread. We call AbortInternal directly to avoid demanding the same - // permission twice. - AbortInternal(); - } -#endif - - #if FEATURE_CORECLR - [System.Security.SecurityCritical] // auto-generated - #else - [System.Security.SecuritySafeCritical] - #endif #pragma warning disable 618 [SecurityPermissionAttribute(SecurityAction.Demand, ControlThread = true)] #pragma warning restore 618 @@ -535,152 +390,14 @@ namespace System.Threading { // Internal helper (since we can't place security demands on // ecalls/fcalls). - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] private extern void AbortInternal(); -#if !FEATURE_CORECLR - /*========================================================================= - ** Resets a thread abort. - ** Should be called by trusted code only - =========================================================================*/ - [System.Security.SecuritySafeCritical] // auto-generated - [SecurityPermissionAttribute(SecurityAction.Demand, ControlThread=true)] - public static void ResetAbort() - { - Thread thread = Thread.CurrentThread; - if ((thread.ThreadState & ThreadState.AbortRequested) == 0) - throw new ThreadStateException(Environment.GetResourceString("ThreadState_NoAbortRequested")); - thread.ResetAbortNative(); - thread.ClearAbortReason(); - } - - [System.Security.SecurityCritical] // auto-generated - [MethodImplAttribute(MethodImplOptions.InternalCall)] - private extern void ResetAbortNative(); - - /*========================================================================= - ** Suspends the thread. If the thread is already suspended, this call has - ** no effect. - ** - ** Exceptions: ThreadStateException if the thread has not been started or - ** it is dead. - =========================================================================*/ - [System.Security.SecuritySafeCritical] // auto-generated - [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)] - [SecurityPermission(SecurityAction.Demand, ControlThread=true)] - public void Suspend() { SuspendInternal(); } - - // Internal helper (since we can't place security demands on - // ecalls/fcalls). - [System.Security.SecurityCritical] // auto-generated - [MethodImplAttribute(MethodImplOptions.InternalCall)] - private extern void SuspendInternal(); - - /*========================================================================= - ** Resumes a thread that has been suspended. - ** - ** Exceptions: ThreadStateException if the thread has not been started or - ** it is dead or it isn't in the suspended state. - =========================================================================*/ - [System.Security.SecuritySafeCritical] // auto-generated - [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)] - [SecurityPermission(SecurityAction.Demand, ControlThread=true)] - public void Resume() { ResumeInternal(); } - - // Internal helper (since we can't place security demands on - // ecalls/fcalls). - [System.Security.SecurityCritical] // auto-generated - [MethodImplAttribute(MethodImplOptions.InternalCall)] - private extern void ResumeInternal(); - - /*========================================================================= - ** Interrupts a thread that is inside a Wait(), Sleep() or Join(). If that - ** thread is not currently blocked in that manner, it will be interrupted - ** when it next begins to block. - =========================================================================*/ - [System.Security.SecuritySafeCritical] // auto-generated - [SecurityPermission(SecurityAction.Demand, ControlThread=true)] - public void Interrupt() { InterruptInternal(); } - - // Internal helper (since we can't place security demands on - // ecalls/fcalls). - [System.Security.SecurityCritical] // auto-generated - [MethodImplAttribute(MethodImplOptions.InternalCall)] - private extern void InterruptInternal(); -#endif - - /*========================================================================= - ** Returns the priority of the thread. - ** - ** Exceptions: ThreadStateException if the thread is dead. - =========================================================================*/ - - public ThreadPriority Priority { - [System.Security.SecuritySafeCritical] // auto-generated - get { return (ThreadPriority)GetPriorityNative(); } - [System.Security.SecuritySafeCritical] // auto-generated - [HostProtection(SelfAffectingThreading=true)] - set { SetPriorityNative((int)value); } - } - [System.Security.SecurityCritical] // auto-generated - [MethodImplAttribute(MethodImplOptions.InternalCall)] - private extern int GetPriorityNative(); - [System.Security.SecurityCritical] // auto-generated - [MethodImplAttribute(MethodImplOptions.InternalCall)] - private extern void SetPriorityNative(int priority); - - /*========================================================================= - ** Returns true if the thread has been started and is not dead. - =========================================================================*/ - public extern bool IsAlive { - [System.Security.SecuritySafeCritical] // auto-generated - [MethodImpl(MethodImplOptions.InternalCall)] - get; - } - - /*========================================================================= - ** Returns true if the thread is a threadpool thread. - =========================================================================*/ - public extern bool IsThreadPoolThread { - [System.Security.SecuritySafeCritical] // auto-generated - [MethodImpl(MethodImplOptions.InternalCall)] - get; - } - - /*========================================================================= - ** Waits for the thread to die or for timeout milliseconds to elapse. - ** Returns true if the thread died, or false if the wait timed out. If - ** Timeout.Infinite is given as the parameter, no timeout will occur. - ** - ** Exceptions: ArgumentException if timeout < 0. - ** ThreadInterruptedException if the thread is interrupted while waiting. - ** ThreadStateException if the thread has not been started yet. - =========================================================================*/ - [System.Security.SecurityCritical] - [MethodImplAttribute(MethodImplOptions.InternalCall)] - private extern bool JoinInternal(int millisecondsTimeout); - - [System.Security.SecuritySafeCritical] - [HostProtection(Synchronization=true, ExternalThreading=true)] - public void Join() - { - JoinInternal(Timeout.Infinite); - } - - [System.Security.SecuritySafeCritical] - [HostProtection(Synchronization=true, ExternalThreading=true)] - public bool Join(int millisecondsTimeout) - { - return JoinInternal(millisecondsTimeout); - } - - [HostProtection(Synchronization=true, ExternalThreading=true)] public bool Join(TimeSpan timeout) { long tm = (long)timeout.TotalMilliseconds; if (tm < -1 || tm > (long) Int32.MaxValue) - throw new ArgumentOutOfRangeException("timeout", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1")); + throw new ArgumentOutOfRangeException(nameof(timeout), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1")); return Join((int)tm); } @@ -693,12 +410,10 @@ namespace System.Threading { ** Exceptions: ArgumentException if timeout < 0. ** ThreadInterruptedException if the thread is interrupted while sleeping. =========================================================================*/ - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern void SleepInternal(int millisecondsTimeout); - [System.Security.SecuritySafeCritical] // auto-generated - public static void Sleep(int millisecondsTimeout) + public static new void Sleep(int millisecondsTimeout) { SleepInternal(millisecondsTimeout); // Ensure we don't return to app code when the pause is underway @@ -710,7 +425,7 @@ namespace System.Threading { { long tm = (long)timeout.TotalMilliseconds; if (tm < -1 || tm > (long) Int32.MaxValue) - throw new ArgumentOutOfRangeException("timeout", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1")); + throw new ArgumentOutOfRangeException(nameof(timeout), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1")); Sleep((int)tm); } @@ -719,69 +434,41 @@ namespace System.Threading { only take a few machine instructions. Calling this API is preferable to coding a explict busy loop because the hardware can be informed that it is busy waiting. */ - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] - [HostProtection(Synchronization=true,ExternalThreading=true)] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] private static extern void SpinWaitInternal(int iterations); - [System.Security.SecuritySafeCritical] // auto-generated - [HostProtection(Synchronization=true,ExternalThreading=true)] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] - public static void SpinWait(int iterations) + public static new void SpinWait(int iterations) { SpinWaitInternal(iterations); } - [System.Security.SecurityCritical] // auto-generated [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] [SuppressUnmanagedCodeSecurity] - [HostProtection(Synchronization = true, ExternalThreading = true)] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] private static extern bool YieldInternal(); - [System.Security.SecuritySafeCritical] // auto-generated - [HostProtection(Synchronization = true, ExternalThreading = true)] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] - public static bool Yield() + public static new bool Yield() { return YieldInternal(); } - public static Thread CurrentThread { - [System.Security.SecuritySafeCritical] // auto-generated + public static new Thread CurrentThread { [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] get { Contract.Ensures(Contract.Result<Thread>() != null); return GetCurrentThreadNative(); } } - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall), ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] private static extern Thread GetCurrentThreadNative(); - [System.Security.SecurityCritical] // auto-generated private void SetStartHelper(Delegate start, int maxStackSize) { -#if FEATURE_CORECLR // We only support default stacks in CoreCLR - Contract.Assert(maxStackSize == 0); -#else - // Only fully-trusted code is allowed to create "large" stacks. Partial-trust falls back to - // the default stack size. - ulong defaultStackSize = GetProcessDefaultStackSize(); - if ((ulong)(uint)maxStackSize > defaultStackSize) - { - try - { - SecurityPermission.Demand(PermissionType.FullTrust); - } - catch (SecurityException) - { - maxStackSize = (int)Math.Min(defaultStackSize, (ulong)(uint)int.MaxValue); - } - } -#endif + Debug.Assert(maxStackSize == 0); ThreadHelper threadStartCallBack = new ThreadHelper(start); if(start is ThreadStart) @@ -794,7 +481,6 @@ namespace System.Threading { } } - [SecurityCritical] [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] [SuppressUnmanagedCodeSecurity] private static extern ulong GetProcessDefaultStackSize(); @@ -803,14 +489,12 @@ namespace System.Threading { ** PRIVATE Sets the IThreadable interface for the thread. Assumes that ** start != null. =========================================================================*/ - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] private extern void SetStart(Delegate start, int maxStackSize); /*========================================================================= ** Clean up the thread when it goes away. =========================================================================*/ - [System.Security.SecuritySafeCritical] // auto-generated [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] ~Thread() { @@ -818,52 +502,10 @@ namespace System.Threading { InternalFinalize(); } - [System.Security.SecurityCritical] // auto-generated [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] [MethodImplAttribute(MethodImplOptions.InternalCall)] private extern void InternalFinalize(); -#if FEATURE_COMINTEROP - [System.Security.SecurityCritical] // auto-generated - [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] - [MethodImplAttribute(MethodImplOptions.InternalCall)] - public extern void DisableComObjectEagerCleanup(); -#endif //FEATURE_COMINTEROP - - /*========================================================================= - ** Return whether or not this thread is a background thread. Background - ** threads do not affect when the Execution Engine shuts down. - ** - ** Exceptions: ThreadStateException if the thread is dead. - =========================================================================*/ - public bool IsBackground { - [System.Security.SecuritySafeCritical] // auto-generated - get { return IsBackgroundNative(); } - [System.Security.SecuritySafeCritical] // auto-generated - [HostProtection(SelfAffectingThreading=true)] - set { SetBackgroundNative(value); } - } - [System.Security.SecurityCritical] // auto-generated - [MethodImplAttribute(MethodImplOptions.InternalCall)] - private extern bool IsBackgroundNative(); - [System.Security.SecurityCritical] // auto-generated - [MethodImplAttribute(MethodImplOptions.InternalCall)] - private extern void SetBackgroundNative(bool isBackground); - - - /*========================================================================= - ** Return the thread state as a consistent set of bits. This is more - ** general then IsAlive or IsBackground. - =========================================================================*/ - public ThreadState ThreadState { - [System.Security.SecuritySafeCritical] // auto-generated - get { return (ThreadState)GetThreadStateNative(); } - } - - [System.Security.SecurityCritical] // auto-generated - [MethodImplAttribute(MethodImplOptions.InternalCall)] - private extern int GetThreadStateNative(); - #if FEATURE_COMINTEROP_APARTMENT_SUPPORT /*========================================================================= ** An unstarted thread can be marked to indicate that it will host a @@ -875,35 +517,17 @@ namespace System.Threading { [Obsolete("The ApartmentState property has been deprecated. Use GetApartmentState, SetApartmentState or TrySetApartmentState instead.", false)] public ApartmentState ApartmentState { - [System.Security.SecuritySafeCritical] // auto-generated get { return (ApartmentState)GetApartmentStateNative(); } - [System.Security.SecuritySafeCritical] // auto-generated - [HostProtection(Synchronization=true, SelfAffectingThreading=true)] set { SetApartmentStateNative((int)value, true); } } - [System.Security.SecuritySafeCritical] // auto-generated - public ApartmentState GetApartmentState() - { - return (ApartmentState)GetApartmentStateNative(); - } - - [System.Security.SecuritySafeCritical] // auto-generated - [HostProtection(Synchronization=true, SelfAffectingThreading=true)] - public bool TrySetApartmentState(ApartmentState state) - { - return SetApartmentStateHelper(state, false); - } - - [System.Security.SecuritySafeCritical] // auto-generated - [HostProtection(Synchronization=true, SelfAffectingThreading=true)] public void SetApartmentState(ApartmentState state) { bool result = SetApartmentStateHelper(state, true); @@ -911,31 +535,6 @@ namespace System.Threading { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ApartmentStateSwitchFailed")); } - [System.Security.SecurityCritical] // auto-generated - private bool SetApartmentStateHelper(ApartmentState state, bool fireMDAOnMismatch) - { - ApartmentState retState = (ApartmentState)SetApartmentStateNative((int)state, fireMDAOnMismatch); - - // Special case where we pass in Unknown and get back MTA. - // Once we CoUninitialize the thread, the OS will still - // report the thread as implicitly in the MTA if any - // other thread in the process is CoInitialized. - if ((state == System.Threading.ApartmentState.Unknown) && (retState == System.Threading.ApartmentState.MTA)) - return true; - - if (retState != state) - return false; - - return true; - } - - [System.Security.SecurityCritical] // auto-generated - [MethodImplAttribute(MethodImplOptions.InternalCall)] - private extern int GetApartmentStateNative(); - [System.Security.SecurityCritical] // auto-generated - [MethodImplAttribute(MethodImplOptions.InternalCall)] - private extern int SetApartmentStateNative(int state, bool fireMDAOnMismatch); - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] private extern void StartupSetApartmentStateInternal(); #endif // FEATURE_COMINTEROP_APARTMENT_SUPPORT @@ -944,7 +543,6 @@ namespace System.Threading { ** Allocates an un-named data slot. The slot is allocated on ALL the ** threads. =========================================================================*/ - [HostProtection(SharedState=true, ExternalThreading=true)] public static LocalDataStoreSlot AllocateDataSlot() { return LocalDataStoreManager.AllocateDataSlot(); @@ -955,7 +553,6 @@ namespace System.Threading { ** threads. Named data slots are "public" and can be manipulated by ** anyone. =========================================================================*/ - [HostProtection(SharedState=true, ExternalThreading=true)] public static LocalDataStoreSlot AllocateNamedDataSlot(String name) { return LocalDataStoreManager.AllocateNamedDataSlot(name); @@ -966,7 +563,6 @@ namespace System.Threading { ** allocated. Named data slots are "public" and can be manipulated by ** anyone. =========================================================================*/ - [HostProtection(SharedState=true, ExternalThreading=true)] public static LocalDataStoreSlot GetNamedDataSlot(String name) { return LocalDataStoreManager.GetNamedDataSlot(name); @@ -977,7 +573,6 @@ namespace System.Threading { ** threads. Named data slots are "public" and can be manipulated by ** anyone. =========================================================================*/ - [HostProtection(SharedState=true, ExternalThreading=true)] public static void FreeNamedDataSlot(String name) { LocalDataStoreManager.FreeNamedDataSlot(name); @@ -986,7 +581,6 @@ namespace System.Threading { /*========================================================================= ** Retrieves the value from the specified slot on the current thread, for that thread's current domain. =========================================================================*/ - [HostProtection(SharedState=true, ExternalThreading=true)] public static Object GetData(LocalDataStoreSlot slot) { LocalDataStoreHolder dls = s_LocalDataStore; @@ -1003,7 +597,6 @@ namespace System.Threading { /*========================================================================= ** Sets the data in the specified slot on the currently running thread, for that thread's current domain. =========================================================================*/ - [HostProtection(SharedState=true, ExternalThreading=true)] public static void SetData(LocalDataStoreSlot slot, Object data) { LocalDataStoreHolder dls = s_LocalDataStore; @@ -1044,7 +637,6 @@ namespace System.Threading { // default domain to lookup resources. See Environment.cs for more details. // #if FEATURE_LEAK_CULTURE_INFO - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] static extern private bool nativeGetSafeCulture(Thread t, int appDomainId, bool isUI, ref CultureInfo safeCulture); #endif // FEATURE_LEAK_CULTURE_INFO @@ -1075,11 +667,9 @@ namespace System.Threading { } } - [System.Security.SecuritySafeCritical] // auto-generated - [HostProtection(ExternalThreading=true)] set { if (value == null) { - throw new ArgumentNullException("value"); + throw new ArgumentNullException(nameof(value)); } Contract.EndContractBlock(); @@ -1119,7 +709,6 @@ namespace System.Threading { } #if FEATURE_LEAK_CULTURE_INFO - [System.Security.SecuritySafeCritical] // auto-generated #endif internal CultureInfo GetCurrentUICultureNoAppX() { @@ -1152,7 +741,6 @@ namespace System.Threading { // This returns the exposed context for a given context ID. #if FEATURE_LEAK_CULTURE_INFO - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] static extern private bool nativeSetThreadUILocale(String locale); #endif @@ -1184,13 +772,11 @@ namespace System.Threading { } } - [System.Security.SecuritySafeCritical] // auto-generated #if FEATURE_LEAK_CULTURE_INFO - [SecurityPermission(SecurityAction.Demand, ControlThread = true)] #endif set { if (null==value) { - throw new ArgumentNullException("value"); + throw new ArgumentNullException(nameof(value)); } Contract.EndContractBlock(); @@ -1225,7 +811,6 @@ namespace System.Threading { } #if FEATURE_LEAK_CULTURE_INFO - [System.Security.SecuritySafeCritical] // auto-generated #endif private CultureInfo GetCurrentCultureNoAppX() { @@ -1255,114 +840,21 @@ namespace System.Threading { #endif } -#if! FEATURE_LEAK_CULTURE_INFO - [System.Security.SecurityCritical] // auto-generated +#if !FEATURE_LEAK_CULTURE_INFO [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] [SuppressUnmanagedCodeSecurity] private static extern void nativeInitCultureAccessors(); #endif - /*=============================================================*/ - - /*====================================================================== - ** Current thread context is stored in a slot in the thread local store - ** CurrentContext gets the Context from the slot. - ======================================================================*/ -#if FEATURE_REMOTING - public static Context CurrentContext - { - [System.Security.SecurityCritical] // auto-generated_required - get - { - return CurrentThread.GetCurrentContextInternal(); - } - } - - [System.Security.SecurityCritical] // auto-generated - internal Context GetCurrentContextInternal() - { - if (m_Context == null) - { - m_Context = Context.DefaultContext; - } - return m_Context; - } -#endif - - -#if FEATURE_IMPERSONATION - // Get and set thread's current principal (for role based security). - public static IPrincipal CurrentPrincipal - { - [System.Security.SecuritySafeCritical] // auto-generated - get - { - lock (CurrentThread) - { - IPrincipal principal = (IPrincipal) - CallContext.Principal; - if (principal == null) - { - principal = GetDomain().GetThreadPrincipal(); - CallContext.Principal = principal; - } - return principal; - } - } - - [System.Security.SecuritySafeCritical] // auto-generated - [SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPrincipal)] - set - { - CallContext.Principal = value; - } - } - - // Private routine called from unmanaged code to set an initial - // principal for a newly created thread. - [System.Security.SecurityCritical] // auto-generated - private void SetPrincipalInternal(IPrincipal principal) - { - GetMutableExecutionContext().LogicalCallContext.SecurityData.Principal = principal; - } -#endif // FEATURE_IMPERSONATION - -#if FEATURE_REMOTING - - // This returns the exposed context for a given context ID. - [System.Security.SecurityCritical] // auto-generated - [MethodImplAttribute(MethodImplOptions.InternalCall)] - internal static extern Context GetContextInternal(IntPtr id); - - [System.Security.SecurityCritical] // auto-generated - [MethodImplAttribute(MethodImplOptions.InternalCall)] - internal extern Object InternalCrossContextCallback(Context ctx, IntPtr ctxID, Int32 appDomainID, InternalCrossContextDelegate ftnToCall, Object[] args); - - [System.Security.SecurityCritical] // auto-generated - internal Object InternalCrossContextCallback(Context ctx, InternalCrossContextDelegate ftnToCall, Object[] args) - { - return InternalCrossContextCallback(ctx, ctx.InternalContextID, 0, ftnToCall, args); - } - - // CompleteCrossContextCallback is called by the EE after transitioning to the requested context - private static Object CompleteCrossContextCallback(InternalCrossContextDelegate ftnToCall, Object[] args) - { - return ftnToCall(args); - } -#endif // FEATURE_REMOTING - /*====================================================================== ** Returns the current domain in which current thread is running. ======================================================================*/ - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern AppDomain GetDomainInternal(); - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern AppDomain GetFastDomainInternal(); - [System.Security.SecuritySafeCritical] // auto-generated public static AppDomain GetDomain() { Contract.Ensures(Contract.Result<AppDomain>() != null); @@ -1373,9 +865,6 @@ namespace System.Threading { if (ad == null) ad = GetDomainInternal(); -#if FEATURE_REMOTING - Contract.Assert(CurrentThread.m_Context == null || CurrentThread.m_Context.AppDomain == ad, "AppDomains on the managed & unmanaged threads should match"); -#endif return ad; } @@ -1391,13 +880,10 @@ namespace System.Threading { // Retrieves the name of the thread. // - public String Name { + public new String Name { get { return m_Name; - } - [System.Security.SecuritySafeCritical] // auto-generated - [HostProtection(ExternalThreading=true)] set { lock(this) { if (m_Name != null) @@ -1409,13 +895,11 @@ namespace System.Threading { } } - [System.Security.SecurityCritical] // auto-generated [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] [SuppressUnmanagedCodeSecurity] private static extern void InformThreadNameChange(ThreadHandle t, String name, int len); internal Object AbortReason { - [System.Security.SecurityCritical] // auto-generated get { object result = null; try @@ -1428,46 +912,9 @@ namespace System.Threading { } return result; } - [System.Security.SecurityCritical] // auto-generated set { SetAbortReason(value); } } -#if !FEATURE_CORECLR - /* - * This marks the beginning of a critical code region. - */ - [System.Security.SecuritySafeCritical] // auto-generated - [HostProtection(Synchronization=true, ExternalThreading=true)] - [MethodImplAttribute(MethodImplOptions.InternalCall)] - [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] - public static extern void BeginCriticalRegion(); - - /* - * This marks the end of a critical code region. - */ - [System.Security.SecuritySafeCritical] // auto-generated - [HostProtection(Synchronization=true, ExternalThreading=true)] - [MethodImplAttribute(MethodImplOptions.InternalCall)] - [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] - public static extern void EndCriticalRegion(); - - /* - * This marks the beginning of a code region that requires thread affinity. - */ - [System.Security.SecurityCritical] // auto-generated_required - [MethodImplAttribute(MethodImplOptions.InternalCall)] - [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] - public static extern void BeginThreadAffinity(); - - /* - * This marks the end of a code region that requires thread affinity. - */ - [System.Security.SecurityCritical] // auto-generated_required - [MethodImplAttribute(MethodImplOptions.InternalCall)] - [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] - public static extern void EndThreadAffinity(); -#endif // !FEATURE_CORECLR - /*========================================================================= ** Volatile Read & Write and MemoryBarrier methods. ** Provides the ability to read and write values ensuring that the values @@ -1679,7 +1126,6 @@ namespace System.Threading { address = value; } - [System.Security.SecuritySafeCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] public static extern void MemoryBarrier(); @@ -1696,45 +1142,20 @@ namespace System.Threading { } } -#if !FEATURE_CORECLR - void _Thread.GetTypeInfoCount(out uint pcTInfo) - { - throw new NotImplementedException(); - } - - void _Thread.GetTypeInfo(uint iTInfo, uint lcid, IntPtr ppTInfo) - { - throw new NotImplementedException(); - } - - void _Thread.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId) - { - throw new NotImplementedException(); - } - - void _Thread.Invoke(uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr) - { - throw new NotImplementedException(); - } -#endif - // Helper function to set the AbortReason for a thread abort. // Checks that they're not alredy set, and then atomically updates // the reason info (object + ADID). - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] internal extern void SetAbortReason(Object o); // Helper function to retrieve the AbortReason from a thread // abort. Will perform cross-AppDomain marshalling if the object // lives in a different AppDomain from the requester. - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] internal extern Object GetAbortReason(); // Helper function to clear the AbortReason. Takes care of // AppDomain related cleanup if required. - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] internal extern void ClearAbortReason(); |