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

Commit

Permalink
[Local GC] Add GetThread to the GC/EE interface
Browse files Browse the repository at this point in the history
  • Loading branch information
swgillespie committed Jun 1, 2017
1 parent dde63bc commit d309182
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/gc/env/gcenv.ee.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class GCToEEInterface
static bool IsPreemptiveGCDisabled(Thread * pThread);
static void EnablePreemptiveGC(Thread * pThread);
static void DisablePreemptiveGC(Thread * pThread);
static Thread* GetThread();

static gc_alloc_context * GetAllocContext(Thread * pThread);
static bool CatchAtSafePoint(Thread * pThread);
Expand Down
54 changes: 27 additions & 27 deletions src/gc/gc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ class t_join
dprintf (JOIN_LOG, ("join%d(%d): Join() hard wait on reset event %d, join_lock is now %d",
flavor, join_id, color, (int32_t)(join_struct.join_lock)));

//Thread* current_thread = GetThread();
//Thread* current_thread = GCToEEInterface::GetThread();
//BOOL cooperative_mode = gc_heap::enable_preemptive (current_thread);
uint32_t dwJoinWait = join_struct.joined_event[color].Wait(INFINITE, FALSE);
//gc_heap::disable_preemptive (current_thread, cooperative_mode);
Expand Down Expand Up @@ -1285,7 +1285,7 @@ void recursive_gc_sync::begin_foreground()
assert (foreground_allowed.IsValid());
assert (foreground_complete.IsValid());

current_thread = GetThread();
current_thread = GCToEEInterface::GetThread();
cooperative_mode = gc_heap::enable_preemptive (current_thread);

foreground_allowed.Wait(INFINITE, FALSE);
Expand Down Expand Up @@ -1496,7 +1496,7 @@ inline bool can_use_write_watch_for_card_table()
void WaitLongerNoInstru (int i)
{
// every 8th attempt:
Thread *pCurThread = GetThread();
Thread *pCurThread = GCToEEInterface::GetThread();
bool bToggleGC = false;
if (pCurThread)
{
Expand Down Expand Up @@ -1556,7 +1556,7 @@ void WaitLongerNoInstru (int i)
inline
static void safe_switch_to_thread()
{
Thread* current_thread = GetThread();
Thread* current_thread = GCToEEInterface::GetThread();
BOOL cooperative_mode = gc_heap::enable_preemptive(current_thread);

GCToOSInterface::YieldThread(0);
Expand Down Expand Up @@ -1631,34 +1631,34 @@ static void enter_spin_lock(GCSpinLock *pSpinLock)
{
enter_spin_lock_noinstru(&pSpinLock->lock);
assert (pSpinLock->holding_thread == (Thread*)-1);
pSpinLock->holding_thread = GetThread();
pSpinLock->holding_thread = GCToEEInterface::GetThread();
}

inline
static BOOL try_enter_spin_lock(GCSpinLock *pSpinLock)
{
BOOL ret = try_enter_spin_lock_noinstru(&pSpinLock->lock);
if (ret)
pSpinLock->holding_thread = GetThread();
pSpinLock->holding_thread = GCToEEInterface::GetThread();
return ret;
}

inline
static void leave_spin_lock(GCSpinLock *pSpinLock)
{
BOOL gc_thread_p = IsGCSpecialThread();
// _ASSERTE((pSpinLock->holding_thread == GetThread()) || gc_thread_p || pSpinLock->released_by_gc_p);
// _ASSERTE((pSpinLock->holding_thread == GCToEEInterface::GetThread()) || gc_thread_p || pSpinLock->released_by_gc_p);
pSpinLock->released_by_gc_p = gc_thread_p;
pSpinLock->holding_thread = (Thread*) -1;
if (pSpinLock->lock != -1)
leave_spin_lock_noinstru(&pSpinLock->lock);
}

#define ASSERT_HOLDING_SPIN_LOCK(pSpinLock) \
_ASSERTE((pSpinLock)->holding_thread == GetThread());
_ASSERTE((pSpinLock)->holding_thread == GCToEEInterface::GetThread());

#define ASSERT_NOT_HOLDING_SPIN_LOCK(pSpinLock) \
_ASSERTE((pSpinLock)->holding_thread != GetThread());
_ASSERTE((pSpinLock)->holding_thread != GCToEEInterface::GetThread());

#else //_DEBUG

Expand All @@ -1675,7 +1675,7 @@ void WaitLonger (int i
#endif //SYNCHRONIZATION_STATS

// every 8th attempt:
Thread *pCurThread = GetThread();
Thread *pCurThread = GCToEEInterface::GetThread();
bool bToggleGC = false;
if (pCurThread)
{
Expand Down Expand Up @@ -1759,7 +1759,7 @@ static void enter_spin_lock (GCSpinLock* spin_lock)
#ifdef SYNCHRONIZATION_STATS
(spin_lock->num_switch_thread)++;
#endif //SYNCHRONIZATION_STATS
Thread* current_thread = GetThread();
Thread* current_thread = GCToEEInterface::GetThread();
BOOL cooperative_mode = gc_heap::enable_preemptive (current_thread);

GCToOSInterface::YieldThread(0);
Expand Down Expand Up @@ -9562,7 +9562,7 @@ const size_t ww_reset_quantum = 128*1024*1024;
inline
void gc_heap::switch_one_quantum()
{
Thread* current_thread = GetThread();
Thread* current_thread = GCToEEInterface::GetThread();
enable_preemptive (current_thread);
GCToOSInterface::Sleep (1);
disable_preemptive (current_thread, TRUE);
Expand Down Expand Up @@ -10207,7 +10207,7 @@ gc_heap* gc_heap::make_gc_heap (
uint32_t
gc_heap::wait_for_gc_done(int32_t timeOut)
{
Thread* current_thread = GetThread();
Thread* current_thread = GCToEEInterface::GetThread();
BOOL cooperative_mode = enable_preemptive (current_thread);

uint32_t dwWaitResult = NOERROR;
Expand Down Expand Up @@ -12300,7 +12300,7 @@ BOOL gc_heap::allocate_small (int gen_number,
background_soh_alloc_count++;
if ((background_soh_alloc_count % bgc_alloc_spin_count) == 0)
{
Thread* current_thread = GetThread();
Thread* current_thread = GCToEEInterface::GetThread();
add_saved_spinlock_info (me_release, mt_alloc_small);
dprintf (SPINLOCK_LOG, ("[%d]spin Lmsl", heap_number));
leave_spin_lock (&more_space_lock);
Expand Down Expand Up @@ -12829,7 +12829,7 @@ BOOL gc_heap::allocate_large (int gen_number,
{
if (!bgc_alloc_spin_loh)
{
Thread* current_thread = GetThread();
Thread* current_thread = GCToEEInterface::GetThread();
add_saved_spinlock_info (me_release, mt_alloc_large);
dprintf (SPINLOCK_LOG, ("[%d]spin Lmsl loh", heap_number));
leave_spin_lock (&more_space_lock);
Expand Down Expand Up @@ -15585,7 +15585,7 @@ void gc_heap::gc1()
)
{
#ifdef BACKGROUND_GC
Thread* current_thread = GetThread();
Thread* current_thread = GCToEEInterface::GetThread();
BOOL cooperative_mode = TRUE;

if (settings.concurrent)
Expand Down Expand Up @@ -25089,7 +25089,7 @@ void gc_heap::recover_bgc_settings()

void gc_heap::allow_fgc()
{
assert (bgc_thread == GetThread());
assert (bgc_thread == GCToEEInterface::GetThread());

if (GCToEEInterface::IsPreemptiveGCDisabled(bgc_thread) && GCToEEInterface::CatchAtSafePoint(bgc_thread))
{
Expand Down Expand Up @@ -25535,7 +25535,7 @@ void gc_heap::background_mark_phase ()
sc.concurrent = FALSE;

THREAD_FROM_HEAP;
Thread* current_thread = GetThread();
Thread* current_thread = GCToEEInterface::GetThread();
BOOL cooperative_mode = TRUE;
#ifndef MULTIPLE_HEAPS
const int thread = heap_number;
Expand Down Expand Up @@ -26853,7 +26853,7 @@ uint32_t gc_heap::bgc_thread_function()

BOOL do_exit = FALSE;

Thread* current_thread = GetThread();
Thread* current_thread = GCToEEInterface::GetThread();
BOOL cooperative_mode = TRUE;
bgc_thread_id.SetToCurrentThread();
dprintf (1, ("bgc_thread_id is set to %x", (uint32_t)GCToOSInterface::GetCurrentThreadIdForLogging()));
Expand Down Expand Up @@ -31203,7 +31203,7 @@ void gc_heap::background_ephemeral_sweep()

void gc_heap::background_sweep()
{
Thread* current_thread = GetThread();
Thread* current_thread = GCToEEInterface::GetThread();
generation* gen = generation_of (max_generation);
dynamic_data* dd = dynamic_data_of (max_generation);
// For SOH segments we go backwards.
Expand Down Expand Up @@ -33978,7 +33978,7 @@ bool GCHeap::StressHeap(gc_alloc_context * context)
return FALSE;

#ifdef _DEBUG
if (g_pConfig->FastGCStressLevel() && !GetThread()->StressHeapIsEnabled()) {
if (g_pConfig->FastGCStressLevel() && !GCToEEInterface::GetThread()->StressHeapIsEnabled()) {
return FALSE;
}

Expand Down Expand Up @@ -34975,7 +34975,7 @@ GCHeap::GarbageCollectGeneration (unsigned int gen, gc_reason reason)
#else
gc_heap* hpt = 0;
#endif //MULTIPLE_HEAPS
Thread* current_thread = GetThread();
Thread* current_thread = GCToEEInterface::GetThread();
BOOL cooperative_mode = TRUE;
dynamic_data* dd = hpt->dynamic_data_of (gen);
size_t localCount = dd_collection_count (dd);
Expand Down Expand Up @@ -35294,7 +35294,7 @@ int GCHeap::GetNumberOfHeaps ()
int GCHeap::GetHomeHeapNumber ()
{
#ifdef MULTIPLE_HEAPS
Thread *pThread = GetThread();
Thread *pThread = GCToEEInterface::GetThread();
for (int i = 0; i < gc_heap::n_heaps; i++)
{
if (pThread)
Expand Down Expand Up @@ -35751,8 +35751,8 @@ inline
void CFinalize::EnterFinalizeLock()
{
_ASSERTE(dbgOnly_IsSpecialEEThread() ||
GetThread() == 0 ||
GCToEEInterface::IsPreemptiveGCDisabled(GetThread()));
GCToEEInterface::GetThread() == 0 ||
GCToEEInterface::IsPreemptiveGCDisabled(GCToEEInterface::GetThread()));

retry:
if (Interlocked::Exchange (&lock, 0) >= 0)
Expand All @@ -35778,8 +35778,8 @@ inline
void CFinalize::LeaveFinalizeLock()
{
_ASSERTE(dbgOnly_IsSpecialEEThread() ||
GetThread() == 0 ||
GCToEEInterface::IsPreemptiveGCDisabled(GetThread()));
GCToEEInterface::GetThread() == 0 ||
GCToEEInterface::IsPreemptiveGCDisabled(GCToEEInterface::GetThread()));

#ifdef _DEBUG
lockowner_threadid.Clear();
Expand Down
4 changes: 2 additions & 2 deletions src/gc/gcee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ void gc_heap::fire_etw_allocation_event (size_t allocation_amount, int gen_numbe

EX_TRY
{
TypeHandle th = GetThread()->GetTHAllocContextObj();
TypeHandle th = GCToEEInterface::GetThread()->GetTHAllocContextObj();

if (th != 0)
{
Expand Down Expand Up @@ -533,7 +533,7 @@ uint32_t gc_heap::user_thread_wait (GCEvent *event, BOOL no_mode_change, int tim

if (!no_mode_change)
{
pCurThread = GetThread();
pCurThread = GCToEEInterface::GetThread();
mode = pCurThread ? GCToEEInterface::IsPreemptiveGCDisabled(pCurThread) : false;
if (mode)
{
Expand Down
6 changes: 6 additions & 0 deletions src/gc/gcenv.ee.standalone.inl
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ ALWAYS_INLINE void GCToEEInterface::DisablePreemptiveGC(Thread * pThread)
g_theGCToCLR->DisablePreemptiveGC(pThread);
}

ALWAYS_INLINE Thread* GCToEEInterface::GetThread()
{
assert(g_theGCToCLR != nullptr);
return g_theGCToCLR->GetThread();
}

ALWAYS_INLINE gc_alloc_context * GCToEEInterface::GetAllocContext(Thread * pThread)
{
assert(g_theGCToCLR != nullptr);
Expand Down
5 changes: 5 additions & 0 deletions src/gc/gcinterface.ee.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ class IGCToCLR {
virtual
void DisablePreemptiveGC(Thread * pThread) = 0;

// Gets the Thread instance for the current thread, or null if no thread
// instance is associated with this thread.
virtual
Thread* GetThread() = 0;

// Retrieves the alloc context associated with a given thread.
virtual
gc_alloc_context * GetAllocContext(Thread * pThread) = 0;
Expand Down
1 change: 0 additions & 1 deletion src/gc/handletable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,6 @@ BOOL Ref_HandleAsyncPinHandles()
{
NOTHROW;
GC_NOTRIGGER;
if (GetThread()) {MODE_COOPERATIVE;} else {DISABLED(MODE_COOPERATIVE);}
}
CONTRACTL_END;

Expand Down
1 change: 0 additions & 1 deletion src/gc/objecthandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,6 @@ void CALLBACK ScanPointerForProfilerAndETW(_UNCHECKED_OBJECTREF *pObjRef, uintpt
{
NOTHROW;
GC_NOTRIGGER;
if (GetThreadNULLOk()) { MODE_COOPERATIVE; }
}
CONTRACTL_END;
#endif // FEATURE_REDHAWK
Expand Down
5 changes: 5 additions & 0 deletions src/gc/sample/gcenv.ee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ void GCToEEInterface::DisablePreemptiveGC(Thread * pThread)
pThread->DisablePreemptiveGC();
}

Thread* GCToEEInterface::GetThread()
{
return ::GetThread();
}

gc_alloc_context * GCToEEInterface::GetAllocContext(Thread * pThread)
{
return pThread->GetAllocContext();
Expand Down
7 changes: 7 additions & 0 deletions src/vm/gcenv.ee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,13 @@ void GCToEEInterface::DisablePreemptiveGC(Thread * pThread)
pThread->DisablePreemptiveGC();
}

Thread* GCToEEInterface::GetThread()
{
WRAPPER_NO_CONTRACT;

return ::GetThread();
}

struct BackgroundThreadStubArgs
{
Thread* thread;
Expand Down
1 change: 1 addition & 0 deletions src/vm/gcenv.ee.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class GCToEEInterface : public IGCToCLR {
bool IsPreemptiveGCDisabled(Thread * pThread);
void EnablePreemptiveGC(Thread * pThread);
void DisablePreemptiveGC(Thread * pThread);
Thread* GetThread();
gc_alloc_context * GetAllocContext(Thread * pThread);
bool CatchAtSafePoint(Thread * pThread);
void GcEnumAllocContexts(enum_alloc_context_func* fn, void* param);
Expand Down

0 comments on commit d309182

Please sign in to comment.