@@ -862,6 +862,7 @@ void EEStartupHelper()
862
862
863
863
#ifdef PROFILING_SUPPORTED
864
864
// Initialize the profiling services.
865
+ // This must happen before Thread::HasStarted() that fires profiler notifications is called on the finalizer thread.
865
866
hr = ProfilingAPIUtility::InitializeProfiling ();
866
867
867
868
_ASSERTE (SUCCEEDED (hr));
@@ -891,7 +892,12 @@ void EEStartupHelper()
891
892
892
893
// This isn't done as part of InitializeGarbageCollector() above because
893
894
// debugger must be initialized before creating EE thread objects
895
+ #ifdef TARGET_WINDOWS
896
+ // Create the finalizer thread on windows earlier, as we will need to wait for
897
+ // the completion of its initialization part that initializes COM as that has to be done
898
+ // before the first Thread is attached. Thus we want to give the thread a bit more time.
894
899
FinalizerThread::FinalizerThreadCreate ();
900
+ #endif
895
901
896
902
InitPreStubManager ();
897
903
@@ -906,8 +912,6 @@ void EEStartupHelper()
906
912
InitJITHelpers1 ();
907
913
InitJITHelpers2 ();
908
914
909
- SyncBlockCache::Attach ();
910
-
911
915
// Set up the sync block
912
916
SyncBlockCache::Start ();
913
917
@@ -922,6 +926,48 @@ void EEStartupHelper()
922
926
923
927
IfFailGo (hr);
924
928
929
+ InitializeExceptionHandling ();
930
+
931
+ //
932
+ // Install our global exception filter
933
+ //
934
+ if (!InstallUnhandledExceptionFilter ())
935
+ {
936
+ IfFailGo (E_FAIL);
937
+ }
938
+
939
+ #ifdef TARGET_WINDOWS
940
+ // g_pGCHeap->Initialize() above could take nontrivial time, so by now the finalizer thread
941
+ // should have initialized FLS slot for thread cleanup notifications.
942
+ // And ensured that COM is initialized (must happen before allocating FLS slot).
943
+ // Make sure that this was done before we start creating Thread objects
944
+ // Ex: The call to SetupThread below will create and attach a Thread object.
945
+ // Event pipe might also do that.
946
+ FinalizerThread::WaitForFinalizerThreadStart ();
947
+ #endif
948
+
949
+ // throws on error
950
+ _ASSERTE (GetThreadNULLOk () == NULL );
951
+ SetupThread ();
952
+
953
+ #ifdef DEBUGGING_SUPPORTED
954
+ // Notify debugger once the first thread is created to finish initialization.
955
+ if (g_pDebugInterface != NULL )
956
+ {
957
+ g_pDebugInterface->StartupPhase2 (GetThread ());
958
+ }
959
+ #endif
960
+
961
+ #ifndef TARGET_WINDOWS
962
+ // This isn't done as part of InitializeGarbageCollector() above because
963
+ // debugger must be initialized before creating EE thread objects
964
+ FinalizerThread::FinalizerThreadCreate ();
965
+ #else
966
+ // On windows the finalizer thread is already partially created and is waiting
967
+ // right before doing HasStarted(). We will release it now.
968
+ FinalizerThread::EnableFinalization ();
969
+ #endif
970
+
925
971
#ifdef FEATURE_PERFTRACING
926
972
// Finish setting up rest of EventPipe - specifically enable SampleProfiler if it was requested at startup.
927
973
// SampleProfiler needs to cooperate with the GC which hasn't fully finished setting up in the first part of the
@@ -982,12 +1028,6 @@ void EEStartupHelper()
982
1028
g_MiniMetaDataBuffMaxSize, MEM_COMMIT, PAGE_READWRITE);
983
1029
#endif // FEATURE_MINIMETADATA_IN_TRIAGEDUMPS
984
1030
985
- #ifdef TARGET_WINDOWS
986
- // By now finalizer thread should have initialized FLS slot for thread cleanup notifications.
987
- // And ensured that COM is initialized (must happen before allocating FLS slot).
988
- // Make sure that this was done.
989
- FinalizerThread::WaitForFinalizerThreadStart ();
990
- #endif
991
1031
g_fEEStarted = TRUE ;
992
1032
g_EEStartupStatus = S_OK;
993
1033
hr = S_OK;
@@ -1794,6 +1834,8 @@ void InitFlsSlot()
1794
1834
// thread - thread to attach
1795
1835
static void OsAttachThread (void * thread)
1796
1836
{
1837
+ _ASSERTE (g_flsIndex != FLS_OUT_OF_INDEXES);
1838
+
1797
1839
if (t_flsState == FLS_STATE_INVOKED)
1798
1840
{
1799
1841
_ASSERTE_ALL_BUILDS (!" Attempt to execute managed code after the .NET runtime thread state has been destroyed." );
0 commit comments