Skip to content

Commit

Permalink
NSAutoreleasePool instance should be added to all threads. (#52023)
Browse files Browse the repository at this point in the history
Add an NSAutoreleasePool to all managed create threads including the Main and Finalizer.

This expands the current support where support was only added to ThreadPool threads. 

New feature switch was created and the ThreadPool one was removed.
 - System.Threading.Thread.EnableAutoreleasePool

Updated AutoReleaseTest for the new scenarios.
  • Loading branch information
AaronRobinsonMSFT authored May 10, 2021
1 parent fe14d1c commit 60735c6
Show file tree
Hide file tree
Showing 32 changed files with 518 additions and 347 deletions.
2 changes: 1 addition & 1 deletion docs/workflow/trimming/feature-switches.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ configurations but their defaults might vary as any SDK can set the defaults dif
| HttpActivityPropagationSupport | System.Net.Http.EnableActivityPropagation | Any dependency related to diagnostics support for System.Net.Http is trimmed when set to false |
| UseNativeHttpHandler | System.Net.Http.UseNativeHttpHandler | HttpClient uses by default platform native implementation of HttpMessageHandler if set to true. |
| StartupHookSupport | System.StartupHookProvider.IsSupported | Startup hooks are disabled when set to false. Startup hook related functionality can be trimmed. |
| TBD | System.Threading.ThreadPool.EnableDispatchAutoreleasePool | When set to true, creates an NSAutoreleasePool around each thread pool work item on applicable platforms. |
| TBD | System.Threading.Thread.EnableAutoreleasePool | When set to true, creates an NSAutoreleasePool for each thread and thread pool work item on applicable platforms. |
| CustomResourceTypesSupport | System.Resources.ResourceManager.AllowCustomResourceTypes | Use of custom resource types is disabled when set to false. ResourceManager code paths that use reflection for custom types can be trimmed. |
| EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization | System.ComponentModel.TypeConverter.EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization | BinaryFormatter serialization support is trimmed when set to false. |
| BuiltInComInteropSupport | System.Runtime.InteropServices.BuiltInComInterop.IsSupported | Built-in COM support is trimmed when set to false. |
Expand Down
5 changes: 5 additions & 0 deletions src/coreclr/clr.featuredefines.props
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<FeatureInstantiatingStubAsIL Condition="'$(Platform)' != 'x86'">true</FeatureInstantiatingStubAsIL>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetsOSX)' == 'true' OR '$(TargetsMacCatalyst)' == 'true' OR '$(TargetsiOS)' == 'true' OR '$(TargetstvOS)' == 'true'">
<FeatureObjCMarshal>true</FeatureObjCMarshal>
</PropertyGroup>

<PropertyGroup>
<DefineConstants Condition="'$(FeatureArrayStubAsIL)' == 'true'">$(DefineConstants);FEATURE_ARRAYSTUB_AS_IL</DefineConstants>
<DefineConstants Condition="'$(FeatureMulticastStubAsIL)' == 'true'">$(DefineConstants);FEATURE_MULTICASTSTUB_AS_IL</DefineConstants>
Expand All @@ -44,6 +48,7 @@
<DefineConstants Condition="'$(FeatureComWrappers)' == 'true'">$(DefineConstants);FEATURE_COMWRAPPERS</DefineConstants>
<DefineConstants Condition="'$(FeatureCominterop)' == 'true'">$(DefineConstants);FEATURE_COMINTEROP</DefineConstants>
<DefineConstants Condition="'$(FeatureCominteropApartmentSupport)' == 'true'">$(DefineConstants);FEATURE_COMINTEROP_APARTMENT_SUPPORT</DefineConstants>
<DefineConstants Condition="'$(FeatureObjCMarshal)' == 'true'">$(DefineConstants);FEATURE_OBJCMARSHAL</DefineConstants>
<DefineConstants Condition="'$(FeatureManagedEtw)' == 'true'">$(DefineConstants);FEATURE_MANAGED_ETW</DefineConstants>
<DefineConstants Condition="'$(FeatureManagedEtwChannels)' == 'true'">$(DefineConstants);FEATURE_MANAGED_ETW_CHANNELS</DefineConstants>
<DefineConstants Condition="'$(FeaturePerfTracing)' == 'true'">$(DefineConstants);FEATURE_PERFTRACING</DefineConstants>
Expand Down
4 changes: 4 additions & 0 deletions src/coreclr/clrdefinitions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ if(CLR_CMAKE_TARGET_WIN32)
add_definitions(-DFEATURE_COMINTEROP_UNMANAGED_ACTIVATION)
endif(CLR_CMAKE_TARGET_WIN32)

if(CLR_CMAKE_TARGET_OSX OR CLR_CMAKE_TARGET_MACCATALYST OR CLR_CMAKE_TARGET_IOS OR CLR_CMAKE_TARGET_TVOS)
add_definitions(-DFEATURE_OBJCMARSHAL)
endif(CLR_CMAKE_TARGET_OSX OR CLR_CMAKE_TARGET_MACCATALYST OR CLR_CMAKE_TARGET_IOS OR CLR_CMAKE_TARGET_TVOS)

add_definitions(-DFEATURE_BASICFREEZE)
add_definitions(-DFEATURE_CORECLR)
add_definitions(-DFEATURE_CORESYSTEM)
Expand Down
8 changes: 8 additions & 0 deletions src/coreclr/vm/assembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1656,9 +1656,17 @@ INT32 Assembly::ExecuteMainMethod(PTRARRAYREF *stringArgs, BOOL waitForOtherThre
AppDomain * pDomain = pThread->GetDomain();
pDomain->SetRootAssembly(pMeth->GetAssembly());

// Perform additional managed thread initialization.
// This would is normally done in the runtime when a managed
// thread is started, but is done here instead since the
// Main thread wasn't started by the runtime.
Thread::InitializationForManagedThreadInNative(pThread);

RunStartupHooks();

hr = RunMain(pMeth, 1, &iRetVal, stringArgs);

Thread::CleanUpForManagedThreadInNative(pThread);
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/coreclr/vm/ceemain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ void EEStartupHelper()
g_MiniMetaDataBuffMaxSize, MEM_COMMIT, PAGE_READWRITE);
#endif // FEATURE_MINIMETADATA_IN_TRIAGEDUMPS

#endif // CROSSGEN_COMPILE
#endif // !CROSSGEN_COMPILE

g_fEEStarted = TRUE;
g_EEStartupStatus = S_OK;
Expand All @@ -1061,7 +1061,6 @@ void EEStartupHelper()

// Perform CoreLib consistency check if requested
g_CoreLib.CheckExtended();

#endif // _DEBUG

#endif // !CROSSGEN_COMPILE
Expand Down
5 changes: 5 additions & 0 deletions src/coreclr/vm/corelib.h
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,11 @@ DEFINE_FIELD_U(_priority, ThreadBaseObject, m_Priority)
DEFINE_CLASS(THREAD, Threading, Thread)
DEFINE_METHOD(THREAD, INTERNAL_GET_CURRENT_THREAD, InternalGetCurrentThread, SM_RetIntPtr)
DEFINE_METHOD(THREAD, START_CALLBACK, StartCallback, IM_RetVoid)
#ifdef FEATURE_OBJCMARSHAL
DEFINE_CLASS(AUTORELEASEPOOL, Threading, AutoreleasePool)
DEFINE_METHOD(AUTORELEASEPOOL, CREATEAUTORELEASEPOOL, CreateAutoreleasePool, SM_RetVoid)
DEFINE_METHOD(AUTORELEASEPOOL, DRAINAUTORELEASEPOOL, DrainAutoreleasePool, SM_RetVoid)
#endif // FEATURE_OBJCMARSHAL

DEFINE_CLASS(IOCB_HELPER, Threading, _IOCompletionCallback)
DEFINE_METHOD(IOCB_HELPER, PERFORM_IOCOMPLETION_CALLBACK, PerformIOCompletionCallback, SM_UInt_UInt_PtrNativeOverlapped_RetVoid)
Expand Down
8 changes: 2 additions & 6 deletions src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h
Original file line number Diff line number Diff line change
Expand Up @@ -2745,12 +2745,8 @@ ep_rt_thread_setup (void)
{
STATIC_CONTRACT_NOTHROW;

EX_TRY
{
SetupThread ();
}
EX_CATCH {}
EX_END_CATCH(SwallowAllExceptions);
Thread* thread_handle = SetupThreadNoThrow ();
EP_ASSERT (thread_handle != NULL);
}

static
Expand Down
13 changes: 13 additions & 0 deletions src/coreclr/vm/finalizerthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ void FinalizerThread::WaitForFinalizerEvent (CLREvent *event)
}

static BOOL s_FinalizerThreadOK = FALSE;
static BOOL s_InitializedFinalizerThreadForPlatform = FALSE;

VOID FinalizerThread::FinalizerThreadWorker(void *args)
{
Expand Down Expand Up @@ -289,6 +290,15 @@ VOID FinalizerThread::FinalizerThreadWorker(void *args)
bPriorityBoosted = TRUE;
}

// The Finalizer thread is started very early in EE startup. We deferred
// some initialization until a point we are sure the EE is up and running. At
// this point we make a single attempt and if it fails won't try again.
if (!s_InitializedFinalizerThreadForPlatform)
{
s_InitializedFinalizerThreadForPlatform = TRUE;
Thread::InitializationForManagedThreadInNative(GetFinalizerThread());
}

JitHost::Reclaim();

GetFinalizerThread()->DisablePreemptiveGC();
Expand Down Expand Up @@ -330,6 +340,9 @@ VOID FinalizerThread::FinalizerThreadWorker(void *args)
// acceptable.
SignalFinalizationDone(TRUE);
}

if (s_InitializedFinalizerThreadForPlatform)
Thread::CleanUpForManagedThreadInNative(GetFinalizerThread());
}

DWORD WINAPI FinalizerThread::FinalizerThreadStart(void *args)
Expand Down
6 changes: 3 additions & 3 deletions src/coreclr/vm/gcenv.ee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1361,7 +1361,7 @@ namespace

EX_TRY
{
args.Thread = SetupUnstartedThread(FALSE);
args.Thread = SetupUnstartedThread(SUTF_ThreadStoreLockAlreadyTaken);
}
EX_CATCH
{
Expand All @@ -1382,7 +1382,7 @@ namespace
ClrFlsSetThreadType(ThreadType_GC);
args->Thread->SetGCSpecial(true);
STRESS_LOG_RESERVE_MEM(GC_STRESSLOG_MULTIPLY);
args->HasStarted = !!args->Thread->HasStarted(false);
args->HasStarted = !!args->Thread->HasStarted();

Thread* thread = args->Thread;
auto threadStart = args->ThreadStart;
Expand All @@ -1407,7 +1407,7 @@ namespace
return false;
}

args.Thread->SetBackground(TRUE, FALSE);
args.Thread->SetBackground(TRUE);
args.Thread->StartThread();

// Wait for the thread to be in its main loop
Expand Down
19 changes: 8 additions & 11 deletions src/coreclr/vm/proftoeeinterfaceimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9073,20 +9073,17 @@ HRESULT ProfToEEInterfaceImpl::SetupThreadForReJIT()
{
LIMITED_METHOD_CONTRACT;

HRESULT hr = S_OK;
EX_TRY
Thread* pThread = GetThreadNULLOk();
if (pThread == NULL)
{
if (GetThreadNULLOk() == NULL)
{
SetupThread();
}

Thread *pThread = GetThreadNULLOk();
pThread->SetProfilerCallbackStateFlags(COR_PRF_CALLBACKSTATE_REJIT_WAS_CALLED);
HRESULT hr = S_OK;
pThread = SetupThreadNoThrow(&hr);
if (pThread == NULL)
return hr;
}
EX_CATCH_HRESULT(hr);

return hr;
pThread->SetProfilerCallbackStateFlags(COR_PRF_CALLBACKSTATE_REJIT_WAS_CALLED);
return S_OK;
}

HRESULT ProfToEEInterfaceImpl::RequestReJIT(ULONG cFunctions, // in
Expand Down
Loading

0 comments on commit 60735c6

Please sign in to comment.