Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move PendingTypeLoadTable from the ClassLoader object to a global structure #96653

Merged
merged 5 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/coreclr/vm/ceemain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
#include "disassembler.h"
#include "jithost.h"
#include "pgo.h"
#include "pendingload.h"

#ifndef TARGET_UNIX
#include "dwreport.h"
Expand Down Expand Up @@ -623,6 +624,8 @@ void EEStartupHelper()
// This needs to be done before the EE has started
InitializeStartupFlags();

PendingTypeLoadTable::Init();

IfFailGo(ExecutableAllocator::StaticInitialize(FatalErrorHandler));

Thread::StaticInitialize();
Expand Down
57 changes: 16 additions & 41 deletions src/coreclr/vm/clsload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1674,12 +1674,6 @@ ClassLoader::~ClassLoader()
}
CONTRACTL_END

#ifdef _DEBUG
// Do not walk m_pUnresolvedClassHash at destruct time as it is loaderheap allocated memory
// and may already have been deallocated via an AllocMemTracker.
m_pUnresolvedClassHash = (PendingTypeLoadTable*)(UINT_PTR)0xcccccccc;
#endif

#ifdef _DEBUG
// LOG((
// LF_CLASSLOADER,
Expand Down Expand Up @@ -1719,7 +1713,6 @@ ClassLoader::~ClassLoader()

FreeModules();

m_UnresolvedClassLock.Destroy();
m_AvailableClassLock.Destroy();
m_AvailableTypesLock.Destroy();
}
Expand All @@ -1744,7 +1737,6 @@ ClassLoader::ClassLoader(Assembly *pAssembly)

m_pAssembly = pAssembly;

m_pUnresolvedClassHash = NULL;
m_cUnhashedModules = 0;

#ifdef _DEBUG
Expand Down Expand Up @@ -1775,12 +1767,6 @@ VOID ClassLoader::Init(AllocMemTracker *pamTracker)
{
STANDARD_VM_CONTRACT;

m_pUnresolvedClassHash = PendingTypeLoadTable::Create(GetAssembly()->GetLowFrequencyHeap(),
UNRESOLVED_CLASS_HASH_BUCKETS,
pamTracker);

m_UnresolvedClassLock.Init(CrstUnresolvedClassLock);

// This lock is taken within the classloader whenever we have to enter a
// type in one of the modules governed by the loader.
// The process of creating these types may be reentrant. The ordering has
Expand Down Expand Up @@ -3033,8 +3019,7 @@ TypeHandle ClassLoader::LoadTypeHandleForTypeKey(TypeKey *pTypeKey,
SString name;
TypeString::AppendTypeKeyDebug(name, pTypeKey);
LOG((LF_CLASSLOADER, LL_INFO10000, "PHASEDLOAD: LoadTypeHandleForTypeKey for type %s to level %s\n", name.GetUTF8(), classLoadLevelName[targetLevel]));
CrstHolder unresolvedClassLockHolder(&m_UnresolvedClassLock);
m_pUnresolvedClassHash->Dump();
PendingTypeLoadTable::GetTable()->Dump();
}
#endif

Expand Down Expand Up @@ -3106,11 +3091,11 @@ TypeHandle ClassLoader::LoadTypeHandleForTypeKeyNoLock(TypeKey *pTypeKey,
class PendingTypeLoadHolder
{
Thread * m_pThread;
PendingTypeLoadEntry * m_pEntry;
PendingTypeLoadTable::Entry * m_pEntry;
PendingTypeLoadHolder * m_pPrevious;

public:
PendingTypeLoadHolder(PendingTypeLoadEntry * pEntry)
PendingTypeLoadHolder(PendingTypeLoadTable::Entry * pEntry)
{
LIMITED_METHOD_CONTRACT;

Expand All @@ -3129,7 +3114,7 @@ class PendingTypeLoadHolder
m_pThread->SetPendingTypeLoad(m_pPrevious);
}

static bool CheckForDeadLockOnCurrentThread(PendingTypeLoadEntry * pEntry)
static bool CheckForDeadLockOnCurrentThread(PendingTypeLoadTable::Entry * pEntry)
{
LIMITED_METHOD_CONTRACT;

Expand Down Expand Up @@ -3182,14 +3167,16 @@ ClassLoader::LoadTypeHandleForTypeKey_Body(
#endif
}

ReleaseHolder<PendingTypeLoadEntry> pLoadingEntry;
CrstHolderWithState unresolvedClassLockHolder(&m_UnresolvedClassLock, false);
ReleaseHolder<PendingTypeLoadTable::Entry> pLoadingEntry;
DWORD dwHashedTypeKey;
PendingTypeLoadTable::Shard *pPendingTypeLoadShard = PendingTypeLoadTable::GetTable()->GetShard(*pTypeKey, this, &dwHashedTypeKey);
CrstHolderWithState unresolvedClassLockHolder(pPendingTypeLoadShard->GetCrst(), false);

retry:
unresolvedClassLockHolder.Acquire();

// Is it in the hash of classes currently being loaded?
pLoadingEntry = m_pUnresolvedClassHash->GetValue(pTypeKey);
pLoadingEntry = pPendingTypeLoadShard->FindPendingTypeLoadEntry(dwHashedTypeKey, *pTypeKey);
if (pLoadingEntry)
{
pLoadingEntry->AddRef();
Expand Down Expand Up @@ -3234,15 +3221,8 @@ ClassLoader::LoadTypeHandleForTypeKey_Body(
goto retry;
}

{
// Wait for class to be loaded by another thread. This is where we start tracking the
// entry, so there is an implicit Acquire in our use of Assign here.
CrstHolder loadingEntryLockHolder(&pLoadingEntry->m_Crst);
_ASSERTE(pLoadingEntry->HasLock());
}

// Result of other thread loading the class
HRESULT hr = pLoadingEntry->m_hrResult;
HRESULT hr = pLoadingEntry->DelayForProgress();

if (FAILED(hr)) {

Expand Down Expand Up @@ -3278,7 +3258,7 @@ ClassLoader::LoadTypeHandleForTypeKey_Body(
}

// Get a pointer to the EEClass being loaded
typeHnd = pLoadingEntry->m_typeHandle;
typeHnd = pLoadingEntry->GetTypeHandle();

if (!typeHnd.IsNull())
{
Expand Down Expand Up @@ -3309,12 +3289,7 @@ ClassLoader::LoadTypeHandleForTypeKey_Body(

// It was not loaded, and it is not being loaded, so we must load it. Create a new LoadingEntry
// and acquire it immediately so that other threads will block.
pLoadingEntry = new PendingTypeLoadEntry(*pTypeKey, typeHnd); // this atomically creates a crst and acquires it

if (!(m_pUnresolvedClassHash->InsertValue(pLoadingEntry)))
{
COMPlusThrowOM();
}
pLoadingEntry = pPendingTypeLoadShard->InsertPendingTypeLoadEntry(dwHashedTypeKey, *pTypeKey, typeHnd); // this atomically creates a crst and acquires it

// Leave the global lock, so that other threads may now start waiting on our class's lock
unresolvedClassLockHolder.Release();
Expand Down Expand Up @@ -3352,7 +3327,7 @@ ClassLoader::LoadTypeHandleForTypeKey_Body(

// Unlink this class from the unresolved class list.
unresolvedClassLockHolder.Acquire();
m_pUnresolvedClassHash->DeleteValue(pTypeKey);
pPendingTypeLoadShard->RemovePendingTypeLoadEntry(pLoadingEntry);

// Release the lock before proceeding. The unhandled exception filters take number of locks that
// have ordering violations with this lock.
Expand All @@ -3365,13 +3340,13 @@ ClassLoader::LoadTypeHandleForTypeKey_Body(

// Unlink this class from the unresolved class list.
unresolvedClassLockHolder.Acquire();
m_pUnresolvedClassHash->DeleteValue(pTypeKey);
pPendingTypeLoadShard->RemovePendingTypeLoadEntry(pLoadingEntry);
unresolvedClassLockHolder.Release();

// Unblock any thread waiting to load same type as in TypeLoadEntry. This should be done
// after pLoadingEntry is removed from m_pUnresolvedClassHash. Otherwise the other thread
// after pLoadingEntry is removed from the PendingTypeLoadTable. Otherwise the other thread
// (which was waiting) will keep spinning for a while after waking up, till the current thread removes
// pLoadingEntry from m_pUnresolvedClassHash. This can cause hang in situation when the current
// pLoadingEntry from the PendingTypeLoadTable. This can cause hang in situation when the current
// thread is a background thread and so will get very less processor cycle to perform subsequent
// operations to remove the entry from hash later.
pLoadingEntry->UnblockWaiters();
Expand Down
7 changes: 1 addition & 6 deletions src/coreclr/vm/clsload.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class SystemDomain;
class Assembly;
class ClassLoader;
class TypeKey;
class PendingTypeLoadEntry;
class PendingTypeLoadTable;
class EEClass;
class Thread;
Expand Down Expand Up @@ -466,7 +465,7 @@ void DECLSPEC_NORETURN ThrowTypeAccessException(AccessCheckContext* pContext,
//
class ClassLoader
{
friend class PendingTypeLoadEntry;
friend class PendingTypeLoadTable;
friend class MethodTableBuilder;
friend class AppDomain;
friend class Assembly;
Expand All @@ -478,10 +477,6 @@ class ClassLoader
friend class COMModule;

private:
// Classes for which load is in progress
PendingTypeLoadTable * m_pUnresolvedClassHash;
CrstExplicitInit m_UnresolvedClassLock;

// Protects addition of elements to module's m_pAvailableClasses.
// (indeed thus protects addition of elements to any m_pAvailableClasses in any
// of the modules managed by this loader)
Expand Down
6 changes: 4 additions & 2 deletions src/coreclr/vm/crst.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ extern DWORD g_fEEShutDown;
extern Volatile<LONG> g_ShutdownCrstUsageCount;
extern Volatile<LONG> g_fForbidEnterEE;

class PendingTypeLoadTable;

AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved
// The CRST.
class CrstBase
{
Expand Down Expand Up @@ -134,9 +136,9 @@ friend class Crst;
friend class DbgTransportLock;
#endif // FEATURE_DBGIPC_TRANSPORT_VM

// PendingTypeLoadEntry acquires the lock during construction before anybody has a chance to see it to avoid
// PendingTypeLoadTable::Entry acquires the lock during construction before anybody has a chance to see it to avoid
// level violations.
friend class PendingTypeLoadEntry;
friend class PendingTypeLoadTable;

public:
#ifdef _DEBUG
Expand Down
Loading