Imported Upstream version 6.0.0.172

Former-commit-id: f3cc9b82f3e5bd8f0fd3ebc098f789556b44e9cd
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2019-04-12 14:10:50 +00:00
parent 8016999e4d
commit 64ac736ec5
32155 changed files with 3981439 additions and 75368 deletions

View File

@ -12,7 +12,9 @@ using System;
using System.Security;
using System.Collections.Generic;
using System.Runtime.InteropServices;
#if !MONO
using System.Security.Permissions;
#endif
using System.Diagnostics.Contracts;
using System.Runtime;
@ -37,8 +39,9 @@ namespace System.Threading
/// </para>
/// </remarks>
[ComVisible(false)]
#if !MONO
[HostProtection(Synchronization = true, ExternalThreading = true)]
#endif
public class CancellationTokenSource : IDisposable
{
//static sources that can be used as the backing source for 'fixed' CancellationTokens that never change state.

View File

@ -43,6 +43,8 @@ namespace System.Threading
[System.Runtime.InteropServices.ComVisible(true)]
public delegate void ContextCallback(Object state);
internal delegate void ContextCallback<TState>(ref TState state);
#if FEATURE_CORECLR
[SecurityCritical]
@ -908,6 +910,11 @@ namespace System.Threading
RunInternal(executionContext, callback, state, preserveSyncCtx);
}
internal static void RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
{
RunInternal(executionContext, callback, state, false);
}
// Actual implementation of Run is here, in a non-DynamicSecurityMethod, because the JIT seems to refuse to inline callees into
// a DynamicSecurityMethod.
[SecurityCritical]
@ -966,6 +973,72 @@ namespace System.Threading
}
}
// Direct copy of the above RunInternal overload, except that it passes the state into the callback strongly-typed and by ref.
internal static void RunInternal<TState>(ExecutionContext executionContext, ContextCallback<TState> callback, ref TState state)
{
RunInternal(executionContext, callback, ref state, false);
}
// Actual implementation of Run is here, in a non-DynamicSecurityMethod, because the JIT seems to refuse to inline callees into
// a DynamicSecurityMethod.
[SecurityCritical]
[SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "Reviewed for thread safety")]
[HandleProcessCorruptedStateExceptions]
// Direct copy of the above RunInternal overload, except that it passes the state into the callback strongly-typed and by ref.
internal static void RunInternal<TState>(ExecutionContext executionContext, ContextCallback<TState> callback, ref TState state, bool preserveSyncCtx)
// internal static void RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, bool preserveSyncCtx)
{
Contract.Assert(executionContext != null);
if (executionContext.IsPreAllocatedDefault)
{
Contract.Assert(executionContext.IsDefaultFTContext(preserveSyncCtx));
}
else
{
Contract.Assert(executionContext.isNewCapture);
executionContext.isNewCapture = false;
}
Thread currentThread = Thread.CurrentThread;
ExecutionContextSwitcher ecsw = default(ExecutionContextSwitcher);
RuntimeHelpers.PrepareConstrainedRegions();
try
{
ExecutionContext.Reader ec = currentThread.GetExecutionContextReader();
if ( (ec.IsNull || ec.IsDefaultFTContext(preserveSyncCtx)) &&
#if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK
SecurityContext.CurrentlyInDefaultFTSecurityContext(ec) &&
#endif // #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK
executionContext.IsDefaultFTContext(preserveSyncCtx) &&
ec.HasSameLocalValues(executionContext)
)
{
// Neither context is interesting, so we don't need to set the context.
// We do need to reset any changes made by the user's callback,
// so here we establish a "copy-on-write scope". Any changes will
// result in a copy of the context being made, preserving the original
// context.
EstablishCopyOnWriteScope(currentThread, true, ref ecsw);
}
else
{
if (executionContext.IsPreAllocatedDefault)
executionContext = new ExecutionContext();
ecsw = SetExecutionContext(executionContext, preserveSyncCtx);
}
//
// Call the user's callback
//
callback(ref state);
}
finally
{
ecsw.Undo();
}
}
[SecurityCritical]
static internal void EstablishCopyOnWriteScope(ref ExecutionContextSwitcher ecsw)
{

View File

@ -20,7 +20,9 @@
namespace System.Threading {
using System;
#if !MONO
using System.Security.Permissions;
#endif
using System.Runtime;
using System.Runtime.Remoting;
using System.Threading;
@ -29,8 +31,10 @@ namespace System.Threading {
using System.Runtime.Versioning;
using System.Diagnostics.Contracts;
#if !MONO
[HostProtection(Synchronization=true, ExternalThreading=true)]
[System.Runtime.InteropServices.ComVisible(true)]
#endif
public static partial class Monitor
{
/*=========================================================================
@ -41,8 +45,10 @@ namespace System.Threading {
**
** Exceptions: ArgumentNullException if object is null.
=========================================================================*/
#if !MONO
[System.Security.SecuritySafeCritical]
[ResourceExposure(ResourceScope.None)]
#endif
[MethodImplAttribute(MethodImplOptions.InternalCall)]
public static extern void Enter(Object obj);
@ -84,7 +90,9 @@ namespace System.Threading {
** own the lock.
=========================================================================*/
[System.Security.SecuritySafeCritical]
#if !MONO
[ResourceExposure(ResourceScope.None)]
#endif
[MethodImplAttribute(MethodImplOptions.InternalCall)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
public static extern void Exit(Object obj);

View File

@ -25,8 +25,12 @@ namespace System.Threading {
#endif
using System;
using System.Diagnostics;
#if !MONO
using System.Security.Permissions;
#endif
#if FEATURE_IMPERSONATION
using System.Security.Principal;
#endif
using System.Globalization;
using System.Collections.Generic;
using System.Runtime.Serialization;
@ -123,12 +127,18 @@ namespace System.Threading {
}
}
#endif
#if !MONO
// deliberately not [serializable]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(_Thread))]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed partial class Thread : CriticalFinalizerObject
#if !MOBILE
#endif
#if !NETCORE
public
#endif
sealed partial class Thread : CriticalFinalizerObject
#if !MOBILE && !NETCORE
, _Thread
#endif
{
@ -177,6 +187,8 @@ namespace System.Threading {
private bool m_ForbidExecutionContextMutation;
#endif
#endif
#if !NETCORE
/*=========================================================================
** This manager is responsible for storing the global data that is
** shared amongst all the thread local stores.
@ -188,6 +200,7 @@ namespace System.Threading {
=========================================================================*/
[ThreadStatic]
static private LocalDataStoreHolder s_LocalDataStore;
#endif
// Do not move! Order of above fields needs to be preserved for alignment
// with native code
@ -309,7 +322,9 @@ namespace System.Threading {
**
** Exceptions: ThreadStateException if the thread has already been started.
=========================================================================*/
#if !MONO
[HostProtection(Synchronization=true,ExternalThreading=true)]
#endif
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
public void Start()
{
@ -317,7 +332,9 @@ namespace System.Threading {
Start(ref stackMark);
}
#if !MONO
[HostProtection(Synchronization=true,ExternalThreading=true)]
#endif
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
public void Start(object parameter)
{
@ -351,21 +368,23 @@ namespace System.Threading {
// If we reach here with a null delegate, something is broken. But we'll let the StartInternal method take care of
// reporting an error. Just make sure we dont try to dereference a null delegate.
ThreadHelper t = (ThreadHelper)(m_Delegate.Target);
#if !NETCORE
ExecutionContext ec = ExecutionContext.Capture(
ref stackMark,
ExecutionContext.CaptureOptions.IgnoreSyncCtx);
t.SetExecutionContextHelper(ec);
#endif
}
#if FEATURE_IMPERSONATION
IPrincipal principal = (IPrincipal)CallContext.Principal;
#else
IPrincipal principal = null;
object principal = null;
#endif
StartInternal(principal, ref stackMark);
}
#if !FEATURE_CORECLR
#if !FEATURE_CORECLR && !NETCORE
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
internal ExecutionContext.Reader GetExecutionContextReader()
{
@ -447,10 +466,13 @@ namespace System.Threading {
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private extern void StartInternal(IPrincipal principal, ref StackCrawlMark stackMark);
#endif
#if FEATURE_COMPRESSEDSTACK || MONO
#if (FEATURE_COMPRESSEDSTACK || MONO) && !NETCORE
/// <internalonly/>
#if !MONO
[System.Security.SecurityCritical] // auto-generated_required
[DynamicSecurityMethodAttribute()]
#endif
[Obsolete("Thread.SetCompressedStack is no longer supported. Please use the System.Threading.CompressedStack class")]
public void SetCompressedStack( CompressedStack stack )
{
@ -565,8 +587,10 @@ namespace System.Threading {
** Resets a thread abort.
** Should be called by trusted code only
=========================================================================*/
#if !MONO
[System.Security.SecuritySafeCritical] // auto-generated
[SecurityPermissionAttribute(SecurityAction.Demand, ControlThread=true)]
#endif
public static void ResetAbort()
{
Thread thread = Thread.CurrentThread;
@ -576,8 +600,10 @@ namespace System.Threading {
thread.ClearAbortReason();
}
#if !MONO
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
#endif
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private extern void ResetAbortNative();
#endif
@ -590,14 +616,18 @@ namespace System.Threading {
** 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)]
[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)]
#if !MONO
[SecurityPermission(SecurityAction.Demand, ControlThread=true)]
#endif
public void Suspend() { SuspendInternal(); }
// Internal helper (since we can't place security demands on
// ecalls/fcalls).
[System.Security.SecurityCritical] // auto-generated
#if !MONO
[ResourceExposure(ResourceScope.None)]
#endif
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private extern void SuspendInternal();
@ -609,7 +639,9 @@ namespace System.Threading {
=========================================================================*/
[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)]
#if !MONO
[SecurityPermission(SecurityAction.Demand, ControlThread=true)]
#endif
public void Resume() { ResumeInternal(); }
// Internal helper (since we can't place security demands on
@ -626,14 +658,18 @@ namespace System.Threading {
** thread is not currently blocked in that manner, it will be interrupted
** when it next begins to block.
=========================================================================*/
#if !MONO
[System.Security.SecuritySafeCritical] // auto-generated
[SecurityPermission(SecurityAction.Demand, ControlThread=true)]
#endif
public void Interrupt() { InterruptInternal(); }
// Internal helper (since we can't place security demands on
// ecalls/fcalls).
#if !MONO
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
#endif
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private extern void InterruptInternal();
#endif
@ -647,16 +683,25 @@ namespace System.Threading {
public ThreadPriority Priority {
[System.Security.SecuritySafeCritical] // auto-generated
get { return (ThreadPriority)GetPriorityNative(); }
#if !MONO
[System.Security.SecuritySafeCritical] // auto-generated
[HostProtection(SelfAffectingThreading=true)]
#endif
set { SetPriorityNative((int)value); }
}
#if !MONO
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
#endif
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private extern int GetPriorityNative();
#if !MONO
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
#endif
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private extern void SetPriorityNative(int priority);
#if !MONO
@ -687,20 +732,26 @@ namespace System.Threading {
** ThreadInterruptedException if the thread is interrupted while waiting.
** ThreadStateException if the thread has not been started yet.
=========================================================================*/
#if !MONO
[System.Security.SecurityCritical]
[ResourceExposure(ResourceScope.None)]
#endif
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private extern bool JoinInternal(int millisecondsTimeout);
#if !MONO
[System.Security.SecuritySafeCritical]
[HostProtection(Synchronization=true, ExternalThreading=true)]
#endif
public void Join()
{
JoinInternal(Timeout.Infinite);
}
#if !MONO
[System.Security.SecuritySafeCritical]
[HostProtection(Synchronization=true, ExternalThreading=true)]
#endif
public bool Join(int millisecondsTimeout)
{
#if MONO
@ -710,7 +761,9 @@ namespace System.Threading {
return JoinInternal(millisecondsTimeout);
}
#if !MONO
[HostProtection(Synchronization=true, ExternalThreading=true)]
#endif
public bool Join(TimeSpan timeout)
{
long tm = (long)timeout.TotalMilliseconds;
@ -728,8 +781,10 @@ namespace System.Threading {
** Exceptions: ArgumentException if timeout < 0.
** ThreadInterruptedException if the thread is interrupted while sleeping.
=========================================================================*/
#if !MONO
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
#endif
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern void SleepInternal(int millisecondsTimeout);
@ -776,21 +831,23 @@ namespace System.Threading {
SpinWaitInternal(iterations);
}
#endif
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
#if MONO
[MethodImplAttribute(MethodImplOptions.InternalCall)]
#else
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
#endif
[SuppressUnmanagedCodeSecurity]
[HostProtection(Synchronization = true, ExternalThreading = true),
ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
#endif
private static extern bool YieldInternal();
#if !MONO
[System.Security.SecuritySafeCritical] // auto-generated
[HostProtection(Synchronization = true, ExternalThreading = true),
ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
#endif
public static bool Yield()
{
return YieldInternal();
@ -902,12 +959,18 @@ namespace System.Threading {
[HostProtection(SelfAffectingThreading=true)]
set { SetBackgroundNative(value); }
}
#if !MONO
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
#endif
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private extern bool IsBackgroundNative();
#if !MONO
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
#endif
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private extern void SetBackgroundNative(bool isBackground);
@ -1005,11 +1068,15 @@ namespace System.Threading {
private extern void StartupSetApartmentStateInternal();
#endif // FEATURE_COMINTEROP_APARTMENT_SUPPORT
#endif
#if !NETCORE
/*=========================================================================
** Allocates an un-named data slot. The slot is allocated on ALL the
** threads.
=========================================================================*/
#if !MONO
[HostProtection(SharedState=true, ExternalThreading=true)]
#endif
public static LocalDataStoreSlot AllocateDataSlot()
{
return LocalDataStoreManager.AllocateDataSlot();
@ -1020,7 +1087,9 @@ namespace System.Threading {
** threads. Named data slots are "public" and can be manipulated by
** anyone.
=========================================================================*/
#if !MONO
[HostProtection(SharedState=true, ExternalThreading=true)]
#endif
public static LocalDataStoreSlot AllocateNamedDataSlot(String name)
{
return LocalDataStoreManager.AllocateNamedDataSlot(name);
@ -1031,7 +1100,9 @@ namespace System.Threading {
** allocated. Named data slots are "public" and can be manipulated by
** anyone.
=========================================================================*/
#if !MONO
[HostProtection(SharedState=true, ExternalThreading=true)]
#endif
public static LocalDataStoreSlot GetNamedDataSlot(String name)
{
return LocalDataStoreManager.GetNamedDataSlot(name);
@ -1042,7 +1113,9 @@ namespace System.Threading {
** threads. Named data slots are "public" and can be manipulated by
** anyone.
=========================================================================*/
#if !MONO
[HostProtection(SharedState=true, ExternalThreading=true)]
#endif
public static void FreeNamedDataSlot(String name)
{
LocalDataStoreManager.FreeNamedDataSlot(name);
@ -1051,8 +1124,10 @@ namespace System.Threading {
/*=========================================================================
** Retrieves the value from the specified slot on the current thread, for that thread's current domain.
=========================================================================*/
#if !MONO
[HostProtection(SharedState=true, ExternalThreading=true)]
[ResourceExposure(ResourceScope.AppDomain)]
#endif
public static Object GetData(LocalDataStoreSlot slot)
{
LocalDataStoreHolder dls = s_LocalDataStore;
@ -1069,8 +1144,10 @@ namespace System.Threading {
/*=========================================================================
** Sets the data in the specified slot on the currently running thread, for that thread's current domain.
=========================================================================*/
#if !MONO
[HostProtection(SharedState=true, ExternalThreading=true)]
[ResourceExposure(ResourceScope.AppDomain)]
#endif
public static void SetData(LocalDataStoreSlot slot, Object data)
{
LocalDataStoreHolder dls = s_LocalDataStore;
@ -1083,6 +1160,7 @@ namespace System.Threading {
dls.Store.SetData(slot, data);
}
#endif
// #threadCultureInfo
//
@ -1142,8 +1220,10 @@ namespace System.Threading {
}
}
#if !MONO
[System.Security.SecuritySafeCritical] // auto-generated
[HostProtection(ExternalThreading=true)]
#endif
set {
if (value == null) {
throw new ArgumentNullException("value");
@ -1182,7 +1262,10 @@ namespace System.Threading {
return;
}
#endif
#if !NETCORE
if (!AppContextSwitches.NoAsyncCurrentCulture)
#endif
{
if (s_asyncLocalCurrentUICulture == null)
{
@ -1192,10 +1275,12 @@ namespace System.Threading {
// this one will set m_CurrentUICulture too
s_asyncLocalCurrentUICulture.Value = value;
}
#if !NETCORE
else
{
m_CurrentUICulture = value;
}
#endif
}
}
@ -1293,7 +1378,9 @@ namespace System.Threading {
return;
}
#endif
#if !NETCORE
if (!AppContextSwitches.NoAsyncCurrentCulture)
#endif
{
if (s_asyncLocalCurrentCulture == null)
{
@ -1302,10 +1389,12 @@ namespace System.Threading {
// this one will set m_CurrentCulture too
s_asyncLocalCurrentCulture.Value = value;
}
#if !NETCORE
else
{
m_CurrentCulture = value;
}
#endif
}
}
@ -1780,11 +1869,15 @@ namespace System.Threading {
address = value;
}
#endif
#if !MONO
[System.Security.SecuritySafeCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
#endif
[MethodImplAttribute(MethodImplOptions.InternalCall)]
public static extern void MemoryBarrier();
#if !NETCORE
private static LocalDataStoreMgr LocalDataStoreManager
{
get
@ -1797,7 +1890,9 @@ namespace System.Threading {
return s_LocalDataStoreMgr;
}
}
#if !MOBILE
#endif
#if !MOBILE && !NETCORE
void _Thread.GetTypeInfoCount(out uint pcTInfo)
{
throw new NotImplementedException();

View File

@ -34,7 +34,9 @@ namespace System.Threading
{
using System.Security;
using System.Runtime.Remoting;
#if !MONO
using System.Security.Permissions;
#endif
using System;
using System.Runtime.CompilerServices;
using System.Runtime.ConstrainedExecution;
@ -48,6 +50,7 @@ namespace System.Threading
using Microsoft.Win32;
#endif
#if !NETCORE
//
// Interface to something that can be queued to the TP. This is implemented by
// QueueUserWorkItemCallback, Task, and potentially other internal types.
@ -64,6 +67,7 @@ namespace System.Threading
[SecurityCritical]
void MarkAborted(ThreadAbortException tae);
}
#endif
[System.Runtime.InteropServices.ComVisible(true)]
public delegate void WaitCallback(Object state);
@ -90,6 +94,22 @@ namespace System.Threading
[SecurityCritical]
public static readonly ThreadPoolWorkQueue workQueue = new ThreadPoolWorkQueue();
#if NETCORE
/// <summary>Shim used to invoke <see cref="IAsyncStateMachineBox.MoveNext"/> of the supplied <see cref="IAsyncStateMachineBox"/>.</summary>
internal static readonly Action<object> s_invokeAsyncStateMachineBox = state =>
{
if (!(state is IAsyncStateMachineBox box))
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.state);
return;
}
box.MoveNext();
};
#endif
#else
//Per-appDomain quantum (in ms) for which the thread keeps processing
//requests in the current domain.
@ -858,7 +878,11 @@ namespace System.Threading
ThreadPool.ReportThreadStatus(true);
reportedStatus = true;
}
#if NETCORE
workItem.Execute();
#else
workItem.ExecuteWorkItem();
#endif
workItem = null;
}
finally
@ -869,7 +893,11 @@ namespace System.Threading
}
else
{
#if NETCORE
workItem.Execute();
#else
workItem.ExecuteWorkItem();
#endif
workItem = null;
}
@ -892,8 +920,10 @@ namespace System.Threading
// it was executed or not (in debug builds only). Task uses this to communicate the ThreadAbortException to anyone
// who waits for the task to complete.
//
#if !NETCORE
if (workItem != null)
workItem.MarkAborted(tae);
#endif
//
// In this case, the VM is going to request another thread on our behalf. No need to do it twice.
@ -975,7 +1005,11 @@ namespace System.Threading
// if we're in the process of shutting down or unloading the AD. In those cases, the work won't
// execute anyway. And there are subtle ----s involved there that would lead us to do the wrong
// thing anyway. So we'll only clean up if this is a "normal" finalization.
if (!(Environment.HasShutdownStarted || AppDomain.CurrentDomain.IsFinalizingForUnload()))
if (!(Environment.HasShutdownStarted
#if !NETCORE
|| AppDomain.CurrentDomain.IsFinalizingForUnload()
#endif
))
CleanUp();
}
}
@ -1015,8 +1049,8 @@ namespace System.Threading
}
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
// [ResourceExposure(ResourceScope.None)]
// [ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
internal void SetWaitObject(WaitHandle waitObject)
{
@ -1121,8 +1155,8 @@ namespace System.Threading
// This will result in a "leak" of sorts (since the handle will not be cleaned up)
// but the process is exiting anyway.
//
// During AD-unload, we don<6F>t finalize live objects until all threads have been
// aborted out of the AD. Since these locked regions are CERs, we won<6F>t abort them
// During AD-unload, we don<6F>t finalize live objects until all threads have been
// aborted out of the AD. Since these locked regions are CERs, we won<6F>t abort them
// while the lock is held. So there should be no leak on AD-unload.
//
if (Interlocked.CompareExchange(ref m_lock, 1, 0) == 0)
@ -1265,10 +1299,12 @@ namespace System.Threading
state = stateObj;
if (compressStack && !ExecutionContext.IsFlowSuppressed())
{
#if !NETCORE
// clone the exection context
context = ExecutionContext.Capture(
ref stackMark,
ExecutionContext.CaptureOptions.IgnoreSyncCtx | ExecutionContext.CaptureOptions.OptimizeDefaultCase);
#endif
}
}
@ -1283,7 +1319,11 @@ namespace System.Threading
}
[SecurityCritical]
#if NETCORE
void IThreadPoolWorkItem.Execute()
#else
void IThreadPoolWorkItem.ExecuteWorkItem()
#endif
{
#if DEBUG
MarkExecuted(false);
@ -1298,10 +1338,15 @@ namespace System.Threading
}
else
{
ExecutionContext.Run(context, ccb, this, true);
ExecutionContext.Run(context, ccb, this
#if !NETCORE
, true
#endif
);
}
}
#if !NETCORE
[SecurityCritical]
void IThreadPoolWorkItem.MarkAborted(ThreadAbortException tae)
{
@ -1311,6 +1356,7 @@ namespace System.Threading
MarkExecuted(true);
#endif
}
#endif
[System.Security.SecurityCritical]
static internal ContextCallback ccb = new ContextCallback(WaitCallback_Context);
@ -1346,10 +1392,12 @@ namespace System.Threading
if (compressStack && !ExecutionContext.IsFlowSuppressed())
{
#if !NETCORE
// capture the exection context
_executionContext = ExecutionContext.Capture(
ref stackMark,
ExecutionContext.CaptureOptions.IgnoreSyncCtx | ExecutionContext.CaptureOptions.OptimizeDefaultCase);
#endif
}
}
@ -1388,16 +1436,24 @@ namespace System.Threading
using (ExecutionContext executionContext = helper._executionContext.CreateCopy())
{
if (timedOut)
ExecutionContext.Run(executionContext, _ccbt, helper, true);
ExecutionContext.Run(executionContext, _ccbt, helper
#if !NETCORE
, true
#endif
);
else
ExecutionContext.Run(executionContext, _ccbf, helper, true);
ExecutionContext.Run(executionContext, _ccbf, helper
#if !NETCORE
, true
#endif
);
}
}
}
}
[HostProtection(Synchronization=true, ExternalThreading=true)]
// [HostProtection(Synchronization=true, ExternalThreading=true)]
public static partial class ThreadPool
{
#if FEATURE_CORECLR
@ -1405,9 +1461,9 @@ namespace System.Threading
#else
[System.Security.SecuritySafeCritical]
#endif
#pragma warning disable 618
[SecurityPermissionAttribute(SecurityAction.Demand, ControlThread = true)]
#pragma warning restore 618
//#pragma warning disable 618
// [SecurityPermissionAttribute(SecurityAction.Demand, ControlThread = true)]
//#pragma warning restore 618
public static bool SetMaxThreads(int workerThreads, int completionPortThreads)
{
return SetMaxThreadsNative(workerThreads, completionPortThreads);
@ -1424,9 +1480,9 @@ namespace System.Threading
#else
[System.Security.SecuritySafeCritical]
#endif
#pragma warning disable 618
[SecurityPermissionAttribute(SecurityAction.Demand, ControlThread = true)]
#pragma warning restore 618
//#pragma warning disable 618
// [SecurityPermissionAttribute(SecurityAction.Demand, ControlThread = true)]
//#pragma warning restore 618
public static bool SetMinThreads(int workerThreads, int completionPortThreads)
{
return SetMinThreadsNative(workerThreads, completionPortThreads);
@ -1673,11 +1729,36 @@ namespace System.Threading
return QueueUserWorkItemHelper(callBack,state,ref stackMark,false);
}
public static bool QueueUserWorkItem<TState>(Action<TState> callBack, TState state, bool preferLocal)
{
if (callBack == null)
{
throw new ArgumentNullException(nameof(callBack));
}
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
WaitCallback waitCallback = x => callBack((TState)x);
return QueueUserWorkItemHelper(waitCallback,state,ref stackMark,true,!preferLocal);
}
public static bool UnsafeQueueUserWorkItem<TState>(Action<TState> callBack, TState state, bool preferLocal)
{
if (callBack == null)
{
throw new ArgumentNullException(nameof(callBack));
}
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
WaitCallback waitCallback = x => callBack((TState)x);
return QueueUserWorkItemHelper(waitCallback,state,ref stackMark,false,!preferLocal);
}
//ThreadPool has per-appdomain managed queue of work-items. The VM is
//responsible for just scheduling threads into appdomains. After that
//work-items are dispatched from the managed queue.
[System.Security.SecurityCritical] // auto-generated
private static bool QueueUserWorkItemHelper(WaitCallback callBack, Object state, ref StackCrawlMark stackMark, bool compressStack )
private static bool QueueUserWorkItemHelper(WaitCallback callBack, Object state, ref StackCrawlMark stackMark, bool compressStack, bool forceGlobal = true)
{
bool success = true;
@ -1698,7 +1779,7 @@ namespace System.Threading
finally
{
QueueUserWorkItemCallback tpcallBack = new QueueUserWorkItemCallback(callBack, state, compressStack, ref stackMark);
ThreadPoolGlobals.workQueue.Enqueue(tpcallBack, true);
ThreadPoolGlobals.workQueue.Enqueue(tpcallBack, forceGlobal);
success = true;
}
}
@ -1833,12 +1914,12 @@ namespace System.Threading
}
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
// [ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern bool RequestWorkerThread();
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
// [ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
unsafe private static extern bool PostQueuedCompletionStatus(NativeOverlapped* overlapped);
@ -1868,37 +1949,37 @@ namespace System.Threading
// Native methods:
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
// [ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern bool SetMinThreadsNative(int workerThreads, int completionPortThreads);
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
// [ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern bool SetMaxThreadsNative(int workerThreads, int completionPortThreads);
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
// [ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern void GetMinThreadsNative(out int workerThreads, out int completionPortThreads);
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
// [ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern void GetMaxThreadsNative(out int workerThreads, out int completionPortThreads);
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
// [ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern void GetAvailableThreadsNative(out int workerThreads, out int completionPortThreads);
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
// [ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern bool NotifyWorkItemComplete();
[System.Security.SecurityCritical]
[ResourceExposure(ResourceScope.None)]
// [ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern void ReportThreadStatus(bool isWorking);
@ -1911,24 +1992,24 @@ namespace System.Threading
}
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
// [ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern void NotifyWorkItemProgressNative();
#if MONO
[System.Security.SecurityCritical]
[ResourceExposure(ResourceScope.None)]
// [ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern void NotifyWorkItemQueued();
#endif
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
// [ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern bool IsThreadPoolHosted();
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
// [ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern void InitializeVMTp(ref bool enableWorkerTracking);
@ -1950,7 +2031,7 @@ namespace System.Threading
#if !FEATURE_CORECLR
[System.Security.SecuritySafeCritical] // auto-generated
[Obsolete("ThreadPool.BindHandle(IntPtr) has been deprecated. Please use ThreadPool.BindHandle(SafeHandle) instead.", false)]
[SecurityPermissionAttribute( SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
// [SecurityPermissionAttribute( SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public static bool BindHandle(
IntPtr osHandle
)
@ -1964,9 +2045,9 @@ namespace System.Threading
#else
[System.Security.SecuritySafeCritical]
#endif
#pragma warning disable 618
[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
#pragma warning restore 618
//#pragma warning disable 618
// [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
//#pragma warning restore 618
public static bool BindHandle(SafeHandle osHandle)
{
#if FEATURE_CORECLR && !FEATURE_LEGACYNETCF
@ -1993,7 +2074,7 @@ namespace System.Threading
}
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
// [ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
private static extern bool BindIOCompletionCallbackNative(IntPtr fileHandle);

View File

@ -27,7 +27,9 @@ namespace System.Threading
using System.Threading;
using System.Runtime.Remoting;
using System;
#if !MONO
using System.Security.Permissions;
#endif
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
@ -36,7 +38,7 @@ namespace System.Threading
using System.Diagnostics.CodeAnalysis;
[System.Runtime.InteropServices.ComVisible(true)]
#if FEATURE_REMOTING
#if FEATURE_REMOTING || NETCORE
public abstract partial class WaitHandle : MarshalByRefObject, IDisposable {
#else // FEATURE_REMOTING
public abstract partial class WaitHandle : IDisposable {
@ -93,16 +95,20 @@ namespace System.Threading
public virtual IntPtr Handle
{
[System.Security.SecuritySafeCritical] // auto-generated
#if !MONO
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
#endif
get { return safeWaitHandle == null ? InvalidHandle : safeWaitHandle.DangerousGetHandle();}
[System.Security.SecurityCritical] // auto-generated_required
#if !MONO
#if !FEATURE_CORECLR
[SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
#endif
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
#endif
set
{
if (value == InvalidHandle)
@ -132,11 +138,13 @@ namespace System.Threading
public SafeWaitHandle SafeWaitHandle
{
[System.Security.SecurityCritical] // auto-generated_required
#if !MONO
#if !FEATURE_CORECLR
[SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
#endif
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
#endif
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
get
{
@ -148,11 +156,13 @@ namespace System.Threading
}
[System.Security.SecurityCritical] // auto-generated_required
#if !MONO
#if !FEATURE_CORECLR
[SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
#endif
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
#endif
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
set
{
@ -188,8 +198,10 @@ namespace System.Threading
// security check.). While security has fixed that problem, we still
// don't need to do a linktime check here.
[System.Security.SecurityCritical] // auto-generated
#if !MONO
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
#endif
internal void SetHandleInternal(SafeWaitHandle handle)
{
safeWaitHandle = handle;