Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 3b5550f

Browse files
janvorlijkotas
authored andcommitted
Refactor GC background thread creation (#6384)
This change modifies the GCToEEInterface::CreateBackgroundThread so that it returns a fully initialized and running thread.
1 parent bfe11c1 commit 3b5550f

File tree

8 files changed

+85
-150
lines changed

8 files changed

+85
-150
lines changed

src/gc/env/gcenv.base.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -463,13 +463,6 @@ class FinalizerThread
463463
static HANDLE GetFinalizerEvent();
464464
};
465465

466-
#ifdef FEATURE_REDHAWK
467-
typedef uint32_t (__stdcall *BackgroundCallback)(void* pCallbackContext);
468-
REDHAWK_PALIMPORT bool REDHAWK_PALAPI PalStartBackgroundGCThread(BackgroundCallback callback, void* pCallbackContext);
469-
#endif // FEATURE_REDHAWK
470-
471-
void DestroyThread(Thread * pThread);
472-
473466
bool IsGCSpecialThread();
474467

475468
inline bool dbgOnly_IsSpecialEEThread()

src/gc/env/gcenv.ee.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,12 @@ class GCToEEInterface
7474
static void EnablePreemptiveGC(Thread * pThread);
7575
static void DisablePreemptiveGC(Thread * pThread);
7676

77-
static void SetGCSpecial(Thread * pThread);
7877
static alloc_context * GetAllocContext(Thread * pThread);
7978
static bool CatchAtSafePoint(Thread * pThread);
8079

8180
static void GcEnumAllocContexts(enum_alloc_context_func* fn, void* param);
8281

83-
static bool CreateBackgroundThread(Thread** thread, GCBackgroundThreadFunction threadStart, void* arg);
82+
static Thread* CreateBackgroundThread(GCBackgroundThreadFunction threadStart, void* arg);
8483
};
8584

8685
#endif // __GCENV_EE_H__

src/gc/gc.cpp

Lines changed: 4 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -2530,8 +2530,6 @@ gc_history_per_heap gc_heap::bgc_data_per_heap;
25302530

25312531
BOOL gc_heap::bgc_thread_running;
25322532

2533-
CLREvent gc_heap::background_gc_create_event;
2534-
25352533
CLRCriticalSection gc_heap::bgc_threads_timeout_cs;
25362534

25372535
CLREvent gc_heap::gc_lh_block_event;
@@ -7136,7 +7134,7 @@ int gc_heap::grow_brick_card_tables (uint8_t* start,
71367134
dprintf (GC_TABLE_LOG, ("Table alloc for %Id bytes: [%Ix, %Ix[",
71377135
alloc_size, (size_t)mem, (size_t)((uint8_t*)mem+alloc_size)));
71387136

7139-
{
7137+
{
71407138
// mark array will be committed separately (per segment).
71417139
size_t commit_size = alloc_size - ms;
71427140

@@ -24851,27 +24849,6 @@ void __stdcall gc_heap::gc_thread_stub (void* arg)
2485124849
uint32_t __stdcall gc_heap::bgc_thread_stub (void* arg)
2485224850
{
2485324851
gc_heap* heap = (gc_heap*)arg;
24854-
24855-
// TODO: need to check if we are still fine for these APIs:
24856-
// Thread::BeginThreadAffinity
24857-
// Thread::EndThreadAffinity()
24858-
// since now GC threads can be managed threads.
24859-
ClrFlsSetThreadType (ThreadType_GC);
24860-
assert (heap->bgc_thread != NULL);
24861-
GCToEEInterface::SetGCSpecial(heap->bgc_thread);
24862-
STRESS_LOG_RESERVE_MEM (GC_STRESSLOG_MULTIPLY);
24863-
24864-
// We commit the thread's entire stack to ensure we're robust in low memory conditions.
24865-
/*
24866-
BOOL fSuccess = Thread::CommitThreadStack();
24867-
24868-
if (!fSuccess)
24869-
{
24870-
// For background GC we revert to doing a blocking GC.
24871-
return 0;
24872-
}
24873-
*/
24874-
2487524852
return heap->bgc_thread_function();
2487624853
}
2487724854
#ifdef _MSC_VER
@@ -26652,39 +26629,10 @@ BOOL gc_heap::create_bgc_thread(gc_heap* gh)
2665226629

2665326630
//dprintf (2, ("Creating BGC thread"));
2665426631

26655-
if (GCToEEInterface::CreateBackgroundThread(&gh->bgc_thread, gh->bgc_thread_stub, gh))
26656-
{
26657-
dprintf (2, ("waiting for the thread to reach its main loop"));
26658-
// In chk builds this can easily time out since
26659-
// now we need to set the thread up into a managed thead.
26660-
// And since it's a managed thread we also need to make sure that we don't
26661-
// clean up here and are still executing code on that thread (it'll
26662-
// trigger all sorts of asserts.
26663-
//uint32_t res = gh->background_gc_create_event.Wait(20,FALSE);
26664-
uint32_t res = gh->background_gc_create_event.Wait(INFINITE,FALSE);
26665-
if (res == WAIT_TIMEOUT)
26666-
{
26667-
dprintf (2, ("waiting for the thread to reach its main loop Timeout."));
26668-
goto cleanup;
26669-
}
26670-
if (!gh->bgc_thread_running)
26671-
{
26672-
dprintf(2, ("background GC thread failed to start."));
26673-
goto cleanup;
26674-
}
26675-
//dprintf (2, ("waiting for the thread to reach its main loop Done."));
26632+
gh->bgc_thread = GCToEEInterface::CreateBackgroundThread(gh->bgc_thread_stub, gh);
26633+
gh->bgc_thread_running = (gh->bgc_thread != NULL);
2667626634

26677-
return TRUE;
26678-
}
26679-
26680-
cleanup:
26681-
26682-
if (gh->bgc_thread)
26683-
{
26684-
gh->bgc_thread = 0;
26685-
}
26686-
26687-
return FALSE;
26635+
return gh->bgc_thread_running;
2668826636
}
2668926637

2669026638
BOOL gc_heap::create_bgc_threads_support (int number_of_heaps)
@@ -26751,11 +26699,6 @@ BOOL gc_heap::create_bgc_thread_support()
2675126699
goto cleanup;
2675226700
}
2675326701

26754-
if (!background_gc_create_event.CreateAutoEventNoThrow(FALSE))
26755-
{
26756-
goto cleanup;
26757-
}
26758-
2675926702
//needs to have room for enough smallest objects fitting on a page
2676026703
parr = new (nothrow) (uint8_t* [1 + page_size / MIN_OBJECT_SIZE]);
2676126704
if (!parr)
@@ -26775,10 +26718,6 @@ BOOL gc_heap::create_bgc_thread_support()
2677526718
{
2677626719
gc_lh_block_event.CloseEvent();
2677726720
}
26778-
if (background_gc_create_event.IsValid())
26779-
{
26780-
background_gc_create_event.CloseEvent();
26781-
}
2678226721
}
2678326722

2678426723
return ret;
@@ -26860,7 +26799,6 @@ void gc_heap::kill_gc_thread()
2686026799
// In the first stage, we do minimum work, and call ExitProcess at the end.
2686126800
// In the secodn stage, we have the Loader lock and only one thread is
2686226801
// alive. Hence we do not need to kill gc thread.
26863-
DestroyThread (bgc_thread);
2686426802
background_gc_done_event.CloseEvent();
2686526803
gc_lh_block_event.CloseEvent();
2686626804
bgc_start_event.CloseEvent();
@@ -26877,28 +26815,11 @@ uint32_t gc_heap::bgc_thread_function()
2687726815
dprintf (3, ("gc_thread thread starting..."));
2687826816

2687926817
BOOL do_exit = FALSE;
26880-
Thread* thread_to_destroy = 0;
2688126818

26882-
#ifndef FEATURE_REDHAWK
26883-
// see comments in create_bgc_thread - we need
26884-
// to make sure that thread doesn't clean up this thread
26885-
// while we run code here.
26886-
if (!bgc_thread->HasStarted(FALSE))
26887-
{
26888-
dprintf (2, ("HasStarted failed"));
26889-
bgc_thread_running = FALSE;
26890-
background_gc_create_event.Set();
26891-
return 0;
26892-
}
26893-
#endif //FEATURE_REDHAWK
26894-
26895-
bgc_thread_running = TRUE;
2689626819
Thread* current_thread = GetThread();
2689726820
BOOL cooperative_mode = TRUE;
2689826821
bgc_thread_id.SetToCurrentThread();
2689926822
dprintf (1, ("bgc_thread_id is set to %x", (uint32_t)GCToOSInterface::GetCurrentThreadIdForLogging()));
26900-
//this also indicates that the thread is ready.
26901-
background_gc_create_event.Set();
2690226823
while (1)
2690326824
{
2690426825
// Wait for work to do...
@@ -26938,10 +26859,6 @@ uint32_t gc_heap::bgc_thread_function()
2693826859
{
2693926860
dprintf (2, ("GC thread exiting"));
2694026861
bgc_thread_running = FALSE;
26941-
// We can't call DestroyThread here 'cause EnterCriticalSection
26942-
// increases the thread's m_dwLockCount and DestroyThread will
26943-
// assert if the lock count is not 0.
26944-
thread_to_destroy = bgc_thread;
2694526862
bgc_thread = 0;
2694626863
bgc_thread_id.Clear();
2694726864
do_exit = TRUE;
@@ -27045,11 +26962,6 @@ uint32_t gc_heap::bgc_thread_function()
2704526962
//gc_heap::disable_preemptive (current_thread, TRUE);
2704626963
}
2704726964

27048-
if (thread_to_destroy)
27049-
{
27050-
DestroyThread(thread_to_destroy);
27051-
}
27052-
2705326965
FireEtwGCTerminateConcurrentThread_V1(GetClrInstanceId());
2705426966

2705526967
dprintf (3, ("bgc_thread thread exiting"));

src/gc/gcpriv.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3104,9 +3104,6 @@ class gc_heap
31043104
PER_HEAP_ISOLATED
31053105
CLREvent background_gc_done_event;
31063106

3107-
PER_HEAP
3108-
CLREvent background_gc_create_event;
3109-
31103107
PER_HEAP_ISOLATED
31113108
CLREvent ee_proceed_event;
31123109

src/gc/sample/gcenv.ee.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,6 @@ void GCToEEInterface::DisablePreemptiveGC(Thread * pThread)
184184
pThread->DisablePreemptiveGC();
185185
}
186186

187-
void GCToEEInterface::SetGCSpecial(Thread * pThread)
188-
{
189-
pThread->SetGCSpecial(true);
190-
}
191-
192187
alloc_context * GCToEEInterface::GetAllocContext(Thread * pThread)
193188
{
194189
return pThread->GetAllocContext();
@@ -220,10 +215,10 @@ void GCToEEInterface::SyncBlockCachePromotionsGranted(int /*max_gen*/)
220215
{
221216
}
222217

223-
bool GCToEEInterface::CreateBackgroundThread(Thread** thread, GCBackgroundThreadFunction threadStart, void* arg)
218+
Thread* GCToEEInterface::CreateBackgroundThread(GCBackgroundThreadFunction threadStart, void* arg)
224219
{
225220
// TODO: Implement for background GC
226-
return false;
221+
return NULL;
227222
}
228223

229224
void FinalizerThread::EnableFinalization()

src/gc/sample/gcenv.unix.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,6 @@ uint32_t CLREventStatic::Wait(uint32_t dwMilliseconds, bool bAlertable)
164164
}
165165
#endif // 0
166166

167-
void DestroyThread(Thread * pThread)
168-
{
169-
// TODO: implement
170-
}
171-
172167
bool __SwitchToThread(uint32_t dwSleepMSec, uint32_t dwSwitchCount)
173168
{
174169
return sched_yield() == 0;
@@ -245,10 +240,6 @@ void ThreadStore::AttachCurrentThread(bool fAcquireThreadStoreLock)
245240
g_pThreadList = pThread;
246241
}
247242
#endif // 0
248-
void DestroyThread(Thread * pThread)
249-
{
250-
// TODO: Implement
251-
}
252243

253244
#if 0
254245
void GCToEEInterface::SuspendEE(GCToEEInterface::SUSPEND_REASON reason)
@@ -292,12 +283,6 @@ void FinalizerThread::EnableFinalization()
292283
// TODO: Implement for finalization
293284
}
294285

295-
bool PalStartBackgroundGCThread(BackgroundCallback callback, void* pCallbackContext)
296-
{
297-
// TODO: Implement for background GC
298-
return false;
299-
}
300-
301286
bool IsGCSpecialThread()
302287
{
303288
// TODO: Implement for background GC

src/gc/sample/gcenv.windows.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,3 @@ void CLRCriticalSection::Leave()
455455
{
456456
::LeaveCriticalSection(&m_cs);
457457
}
458-
459-
void DestroyThread(Thread * pThread)
460-
{
461-
// TODO: implement
462-
}

0 commit comments

Comments
 (0)