Skip to content

Commit

Permalink
Move handle store and remaining crsts from BaseDomain to AppDomain (
Browse files Browse the repository at this point in the history
dotnet#107208)

- The handle store was always the global handle store
  - Make all the `Create*Handle` functions go through the `AppDomain`
- `m_crstLoaderAllocatorReferences` is only used for collectible loader allocators - so only assembly loader allocators (which correspond to `AppDomain`), not the global loader allocator (which corresponds to `SystemDomain`)
- Remove unnecessary `BaseDomain` on `PinnedHeapHandleTable`
  • Loading branch information
elinor-fung authored and sirntar committed Sep 30, 2024
1 parent 3c8e3ba commit 174467b
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 176 deletions.
73 changes: 17 additions & 56 deletions src/coreclr/vm/appdomain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ SPTR_IMPL(SystemDomain, SystemDomain, m_pSystemDomain);

#ifndef DACCESS_COMPILE

// Base Domain Statics
CrstStatic BaseDomain::m_MethodTableExposedClassObjectCrst;

// System Domain Statics
GlobalStringLiteralMap* SystemDomain::m_pGlobalStringLiteralMap = NULL;
FrozenObjectHeapManager* SystemDomain::m_FrozenObjectHeapManager = NULL;
Expand All @@ -104,7 +101,7 @@ CrstStatic SystemDomain::m_SystemDomainCrst;
CrstStatic SystemDomain::m_DelayedUnloadCrst;

// Constructor for the PinnedHeapHandleBucket class.
PinnedHeapHandleBucket::PinnedHeapHandleBucket(PinnedHeapHandleBucket *pNext, PTRARRAYREF pinnedHandleArrayObj, DWORD size, BaseDomain *pDomain)
PinnedHeapHandleBucket::PinnedHeapHandleBucket(PinnedHeapHandleBucket *pNext, PTRARRAYREF pinnedHandleArrayObj, DWORD size)
: m_pNext(pNext)
, m_ArraySize(size)
, m_CurrentPos(0)
Expand All @@ -115,7 +112,6 @@ PinnedHeapHandleBucket::PinnedHeapHandleBucket(PinnedHeapHandleBucket *pNext, PT
THROWS;
GC_NOTRIGGER;
MODE_COOPERATIVE;
PRECONDITION(CheckPointer(pDomain));
INJECT_FAULT(COMPlusThrowOM(););
}
CONTRACTL_END;
Expand All @@ -125,7 +121,7 @@ PinnedHeapHandleBucket::PinnedHeapHandleBucket(PinnedHeapHandleBucket *pNext, PT
m_pArrayDataPtr = (OBJECTREF *)pinnedHandleArrayObj->GetDataPtr();

// Store the array in a strong handle to keep it alive.
m_hndHandleArray = pDomain->CreateStrongHandle((OBJECTREF)pinnedHandleArrayObj);
m_hndHandleArray = AppDomain::GetCurrentDomain()->CreateStrongHandle((OBJECTREF)pinnedHandleArrayObj);
}


Expand Down Expand Up @@ -215,9 +211,8 @@ void PinnedHeapHandleBucket::EnumStaticGCRefs(promote_func* fn, ScanContext* sc)
#define MAX_BUCKETSIZE (16384 - 4)

// Constructor for the PinnedHeapHandleTable class.
PinnedHeapHandleTable::PinnedHeapHandleTable(BaseDomain *pDomain, DWORD InitialBucketSize)
PinnedHeapHandleTable::PinnedHeapHandleTable(DWORD InitialBucketSize)
: m_pHead(NULL)
, m_pDomain(pDomain)
, m_NextBucketSize(InitialBucketSize)
, m_pFreeSearchHint(NULL)
, m_cEmbeddedFree(0)
Expand All @@ -227,7 +222,6 @@ PinnedHeapHandleTable::PinnedHeapHandleTable(BaseDomain *pDomain, DWORD InitialB
THROWS;
GC_TRIGGERS;
MODE_COOPERATIVE;
PRECONDITION(CheckPointer(pDomain));
INJECT_FAULT(COMPlusThrowOM(););
}
CONTRACTL_END;
Expand Down Expand Up @@ -359,7 +353,7 @@ OBJECTREF* PinnedHeapHandleTable::AllocateHandles(DWORD nRequested)
m_pHead->ConsumeRemaining();
}

m_pHead = new PinnedHeapHandleBucket(m_pHead, pinnedHandleArrayObj, newBucketSize, m_pDomain);
m_pHead = new PinnedHeapHandleBucket(m_pHead, pinnedHandleArrayObj, newBucketSize);

// we already computed nextBucketSize to be double the previous size above, but it is possible that
// other threads increased m_NextBucketSize while the lock was unheld. We want to ensure
Expand Down Expand Up @@ -439,10 +433,6 @@ void PinnedHeapHandleTable::EnumStaticGCRefs(promote_func* fn, ScanContext* sc)
//*****************************************************************************
// BaseDomain
//*****************************************************************************
void BaseDomain::Attach()
{
m_MethodTableExposedClassObjectCrst.Init(CrstMethodTableExposedObject);
}

BaseDomain::BaseDomain()
{
Expand All @@ -459,29 +449,8 @@ BaseDomain::BaseDomain()

// Make sure the container is set to NULL so that it gets loaded when it is used.
m_pPinnedHeapHandleTable = NULL;

// Note that m_handleStore is overridden by app domains
m_handleStore = GCHandleUtilities::GetGCHandleManager()->GetGlobalHandleStore();
} //BaseDomain::BaseDomain

//*****************************************************************************
void BaseDomain::Init()
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_ANY;
INJECT_FAULT(COMPlusThrowOM(););
}
CONTRACTL_END;

//
// Initialize the domain locks
//
m_crstLoaderAllocatorReferences.Init(CrstLoaderAllocatorReferences);
}

#undef LOADERHEAP_PROFILE_COUNTER

void AppDomain::ClearBinderContext()
Expand Down Expand Up @@ -733,7 +702,7 @@ void BaseDomain::InitPinnedHeapHandleTable()
}
CONTRACTL_END;

PinnedHeapHandleTable* pTable = new PinnedHeapHandleTable(this, STATIC_OBJECT_TABLE_BUCKET_SIZE);
PinnedHeapHandleTable* pTable = new PinnedHeapHandleTable(STATIC_OBJECT_TABLE_BUCKET_SIZE);
if(InterlockedCompareExchangeT<PinnedHeapHandleTable*>(&m_pPinnedHeapHandleTable, pTable, NULL) != NULL)
{
// another thread beat us to initializing the field, delete our copy
Expand Down Expand Up @@ -797,9 +766,6 @@ void SystemDomain::Attach()
"Created system domain at %p\n",
m_pSystemDomain));

// We need to initialize the memory pools etc. for the system domain.
m_pSystemDomain->BaseDomain::Init(); // Setup the memory heaps

// Create the one and only app domain
AppDomain::Create();

Expand Down Expand Up @@ -869,7 +835,7 @@ void SystemDomain::PreallocateSpecialObjects()
_ASSERTE(g_pPreallocatedSentinelObject == NULL);

OBJECTREF pPreallocatedSentinelObject = AllocateObject(g_pObjectClass);
g_pPreallocatedSentinelObject = CreatePinningHandle( pPreallocatedSentinelObject );
g_pPreallocatedSentinelObject = AppDomain::GetCurrentDomain()->CreatePinningHandle( pPreallocatedSentinelObject );
}

void SystemDomain::CreatePreallocatedExceptions()
Expand All @@ -887,27 +853,30 @@ void SystemDomain::CreatePreallocatedExceptions()
pOutOfMemory->SetHResult(COR_E_OUTOFMEMORY);
pOutOfMemory->SetXCode(EXCEPTION_COMPLUS);
_ASSERTE(g_pPreallocatedOutOfMemoryException == NULL);
g_pPreallocatedOutOfMemoryException = CreateHandle(pOutOfMemory);
g_pPreallocatedOutOfMemoryException = AppDomain::GetCurrentDomain()->CreateHandle(pOutOfMemory);


EXCEPTIONREF pStackOverflow = (EXCEPTIONREF)AllocateObject(g_pStackOverflowExceptionClass);
pStackOverflow->SetHResult(COR_E_STACKOVERFLOW);
pStackOverflow->SetXCode(EXCEPTION_COMPLUS);
_ASSERTE(g_pPreallocatedStackOverflowException == NULL);
g_pPreallocatedStackOverflowException = CreateHandle(pStackOverflow);
g_pPreallocatedStackOverflowException = AppDomain::GetCurrentDomain()->CreateHandle(pStackOverflow);


EXCEPTIONREF pExecutionEngine = (EXCEPTIONREF)AllocateObject(g_pExecutionEngineExceptionClass);
pExecutionEngine->SetHResult(COR_E_EXECUTIONENGINE);
pExecutionEngine->SetXCode(EXCEPTION_COMPLUS);
_ASSERTE(g_pPreallocatedExecutionEngineException == NULL);
g_pPreallocatedExecutionEngineException = CreateHandle(pExecutionEngine);
g_pPreallocatedExecutionEngineException = AppDomain::GetCurrentDomain()->CreateHandle(pExecutionEngine);
}

void SystemDomain::Init()
{
STANDARD_VM_CONTRACT;

// The AppDomain should have already been created
_ASSERTE(AppDomain::GetCurrentDomain() != NULL);

HRESULT hr = S_OK;

#ifdef _DEBUG
Expand All @@ -927,10 +896,6 @@ void SystemDomain::Init()
));
#endif // _DEBUG

// The base domain is initialized in SystemDomain::Attach()
// to allow stub caches to use the memory pool. Do not
// initialize it here!

m_pSystemPEAssembly = NULL;
m_pSystemAssembly = NULL;

Expand Down Expand Up @@ -1537,16 +1502,14 @@ void AppDomain::Create()
{
STANDARD_VM_CONTRACT;

AppDomainRefHolder pDomain(new AppDomain());
_ASSERTE(m_pTheAppDomain == NULL);

AppDomainRefHolder pDomain(new AppDomain());
pDomain->Init();

pDomain->SetStage(AppDomain::STAGE_OPEN);
pDomain->CreateDefaultBinder();

pDomain.SuppressRelease();

m_pTheAppDomain = pDomain;
m_pTheAppDomain = pDomain.Extract();

LOG((LF_CLASSLOADER | LF_CORDB,
LL_INFO10,
Expand Down Expand Up @@ -1760,17 +1723,15 @@ void AppDomain::Init()
m_crstAssemblyList.Init(CrstAssemblyList, CrstFlags(
CRST_GC_NOTRIGGER_WHEN_TAKEN | CRST_DEBUGGER_THREAD | CRST_TAKEN_DURING_SHUTDOWN));

BaseDomain::Init();
m_crstLoaderAllocatorReferences.Init(CrstLoaderAllocatorReferences);
m_MethodTableExposedClassObjectCrst.Init(CrstMethodTableExposedObject);

// Set up the binding caches
m_AssemblyCache.Init(&m_DomainCacheCrst, GetHighFrequencyHeap());

m_MemoryPressure = 0;


// Default domain reuses the handletablemap that was created during EEStartup
m_handleStore = GCHandleUtilities::GetGCHandleManager()->GetGlobalHandleStore();

if (!m_handleStore)
{
COMPlusThrowOM();
Expand Down
Loading

0 comments on commit 174467b

Please sign in to comment.