Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Initial hookup of GC code to MRT code #35

Merged
merged 2 commits into from
Oct 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 27 additions & 9 deletions src/Native/Runtime/CommonMacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ char (*COUNTOF_helper(_CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray];
#define offsetof(s,m) (UIntNative)( (IntNative)&reinterpret_cast<const volatile char&>((((s *)0)->m)) )
#endif // offsetof

#ifndef GCENV_INCLUDED
#define FORCEINLINE __forceinline

inline UIntNative ALIGN_UP(UIntNative val, UIntNative alignment);
Expand All @@ -54,6 +55,7 @@ inline T* ALIGN_DOWN(T* val, UIntNative alignment);
inline bool IS_ALIGNED(UIntNative val, UIntNative alignment);
template <typename T>
inline bool IS_ALIGNED(T* val, UIntNative alignment);
#endif // GCENV_INCLUDED

#ifndef DACCESS_COMPILE
//
Expand Down Expand Up @@ -96,35 +98,50 @@ EXTERN_C int __cdecl memcmp(const void *,const void *,size_t);

#if defined(_AMD64_)

#define DATA_ALIGNMENT 8
#define OS_PAGE_SIZE 0x1000
#define VIRTUAL_ALLOC_RESERVE_GRANULARITY (64*1024) // 0x10000 (64 KB)
#define LOG2_PTRSIZE 3
#define POINTER_SIZE 8

#elif defined(_X86_)

#define VIRTUAL_ALLOC_RESERVE_GRANULARITY (64*1024) // 0x10000 (64 KB)
#define LOG2_PTRSIZE 2
#define POINTER_SIZE 4

#elif defined(_ARM_)

#define VIRTUAL_ALLOC_RESERVE_GRANULARITY (64*1024) // 0x10000 (64 KB)
#define LOG2_PTRSIZE 2
#define POINTER_SIZE 4

#else
#error Unsupported target architecture
#endif

#ifndef GCENV_INCLUDED
#if defined(_AMD64_)

#define DATA_ALIGNMENT 8
#define OS_PAGE_SIZE 0x1000

#elif defined(_X86_)

#define DATA_ALIGNMENT 4
#ifndef OS_PAGE_SIZE
#define OS_PAGE_SIZE 0x1000
#endif
#define VIRTUAL_ALLOC_RESERVE_GRANULARITY (64*1024) // 0x10000 (64 KB)
#define LOG2_PTRSIZE 2
#define POINTER_SIZE 4

#elif defined(_ARM_)

#define DATA_ALIGNMENT 4
#ifndef OS_PAGE_SIZE
#define OS_PAGE_SIZE 0x1000
#endif
#define VIRTUAL_ALLOC_RESERVE_GRANULARITY (64*1024) // 0x10000 (64 KB)
#define LOG2_PTRSIZE 2
#define POINTER_SIZE 4

#else
#error Unsupported target architecture
#endif
#endif // GCENV_INCLUDED

//
// Define an unmanaged function called from managed code that needs to execute in co-operative GC mode. (There
Expand Down Expand Up @@ -169,5 +186,6 @@ bool inline FitsInI4(__int64 val)
{
return val == (__int64)(__int32)val;
}

#ifndef GCENV_INCLUDED
#define C_ASSERT(e) typedef char __C_ASSERT__[(e)?1:-1]
#endif // GCENV_INCLUDED
2 changes: 2 additions & 0 deletions src/Native/Runtime/CommonTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ typedef UInt32 UInt32_BOOL; // windows 4-byte BOOL, 0 -> false,
#define UInt32_FALSE 0
#define UInt32_TRUE 1

#ifndef GCENV_INCLUDED
#define UNREFERENCED_PARAMETER(P) (P)
#endif // GCENV_INCLUDED

#define NULL 0

Expand Down
7 changes: 6 additions & 1 deletion src/Native/Runtime/Crst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
#include "common.h"
#include "rhcommon.h"
#ifdef DACCESS_COMPILE
#include "gcrhenv.h"
#endif // DACCESS_COMPILE
Expand All @@ -16,6 +16,11 @@
#include "crst.h"
#endif // !DACCESS_COMPILE

bool EEThreadId::IsSameThread()
{
return PalGetCurrentThreadId() == m_uiId;
}

void CrstStatic::Init(CrstType eType, CrstFlags eFlags)
{
#ifndef DACCESS_COMPILE
Expand Down
80 changes: 67 additions & 13 deletions src/Native/Runtime/Crst.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
class EEThreadId
{
public:
EEThreadId(UInt32 uiId) : m_uiId(uiId) {}
EEThreadId(uint32_t uiId) : m_uiId(uiId) {}
#ifndef DACCESS_COMPILE
bool IsSameThread() { return PalGetCurrentThreadId() == m_uiId; }
bool IsSameThread();
#endif

private:
UInt32 m_uiId;
uint32_t m_uiId;
};


Expand Down Expand Up @@ -44,7 +44,11 @@ enum CrstType

enum CrstFlags
{
CRST_DEFAULT = 0x0,
CRST_DEFAULT = 0x0,
CRST_REENTRANCY = 0x0,
CRST_UNSAFE_SAMELEVEL = 0x0,
CRST_UNSAFE_ANYMODE = 0x0,
CRST_DEBUGGER_THREAD = 0x0,
};

// Static version of Crst with no default constructor (user must call Init() before use).
Expand All @@ -54,6 +58,8 @@ class CrstStatic
void Init(CrstType eType, CrstFlags eFlags = CRST_DEFAULT);
bool InitNoThrow(CrstType eType, CrstFlags eFlags = CRST_DEFAULT) { Init(eType, eFlags); return true; }
void Destroy();
void Enter() { CrstStatic::Enter(this); }
void Leave() { CrstStatic::Leave(this); }
static void Enter(CrstStatic *pCrst);
static void Leave(CrstStatic *pCrst);
#if defined(_DEBUG)
Expand All @@ -64,8 +70,8 @@ class CrstStatic
private:
CRITICAL_SECTION m_sCritSec;
#if defined(_DEBUG)
UInt32 m_uiOwnerId;
static const UInt32 UNOWNED = 0;
uint32_t m_uiOwnerId;
static const uint32_t UNOWNED = 0;
#endif // _DEBUG
};

Expand All @@ -79,14 +85,62 @@ class Crst : public CrstStatic
};

// Holder for a Crst instance.
class CrstHolder : public Holder<CrstStatic*, CrstStatic::Enter, CrstStatic::Leave>
class CrstHolder
{
CrstStatic * m_pLock;

public:
CrstHolder(CrstStatic *pCrst, bool fTake = true) : Holder(pCrst, fTake) {}
~CrstHolder() {}
CrstHolder(CrstStatic * pLock)
: m_pLock(pLock)
{
m_pLock->Enter();
}

~CrstHolder()
{
m_pLock->Leave();
}
};

// The CLR has split the Crst holders into CrstHolder which only supports acquire on construction/release on
// destruction semantics and CrstHolderWithState, with the old, fully flexible semantics. We don't support the
// split yet so both types are equivalent.
typedef CrstHolder CrstHolderWithState;
class CrstHolderWithState
{
CrstStatic * m_pLock;
bool m_fAcquired;

public:
CrstHolderWithState(CrstStatic * pLock, bool fAcquire = true)
: m_pLock(pLock), m_fAcquired(fAcquire)
{
if (fAcquire)
m_pLock->Enter();
}

~CrstHolderWithState()
{
if (m_fAcquired)
m_pLock->Leave();
}

void Acquire()
{
if (!m_fAcquired)
{
m_pLock->Enter();
m_fAcquired = true;
}
}

void Release()
{
if (m_fAcquired)
{
m_pLock->Leave();
m_fAcquired = false;
}
}

CrstStatic * GetValue()
{
return m_pLock;
}
};
2 changes: 1 addition & 1 deletion src/Native/Runtime/DebugEventSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
#include "common.h"
#include "rhcommon.h"
#ifdef DACCESS_COMPILE
#include "gcrhenv.h"
#endif // DACCESS_COMPILE
Expand Down
1 change: 1 addition & 0 deletions src/Native/Runtime/EHHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
#ifndef DACCESS_COMPILE
#include "rhcommon.h"
#include "commontypes.h"
#include "daccess.h"
#include "commonmacros.h"
Expand Down
27 changes: 22 additions & 5 deletions src/Native/Runtime/FinalizerHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,24 @@
//
// Unmanaged helpers called by the managed finalizer thread.
//

#include "gcrhenv.h"
#include "RuntimeInstance.h"
#include "common.h"
#include "gcenv.h"
#include "gc.h"

#include "slist.h"
#include "gcrhinterface.h"
#include "rwlock.h"
#include "runtimeinstance.h"
#include "module.h"

// Block the current thread until at least one object needs to be finalized (returns true) or memory is low
// (returns false and the finalizer thread should initiate a garbage collection).
EXTERN_C REDHAWK_API UInt32_BOOL __cdecl RhpWaitForFinalizerRequest()
{
#ifdef USE_PORTABLE_HELPERS
ASSERT(!"@TODO: FINALIZER THREAD NYI");
return FALSE;
#else
// We can wait for two events; finalization queue has been populated and low memory resource notification.
// But if the latter is signalled we shouldn't wait on it again immediately -- if the garbage collection
// the finalizer thread initiates as a result is not sufficient to remove the low memory condition the
Expand All @@ -30,8 +39,9 @@ EXTERN_C REDHAWK_API UInt32_BOOL __cdecl RhpWaitForFinalizerRequest()
// two second timeout expires.
do
{
HANDLE rgWaitHandles[] = { pHeap->GetFinalizerEvent(), pHeap->GetLowMemoryNotificationEvent() };
UInt32 cWaitHandles = (fLastEventWasLowMemory || pHeap->GetLowMemoryNotificationEvent() == NULL) ? 1 : 2;
HANDLE lowMemEvent = pHeap->GetLowMemoryNotificationEvent();
HANDLE rgWaitHandles[] = { pHeap->GetFinalizerEvent(), lowMemEvent };
UInt32 cWaitHandles = (fLastEventWasLowMemory || (lowMemEvent == NULL)) ? 1 : 2;
UInt32 uTimeout = fLastEventWasLowMemory ? 2000 : INFINITE;

UInt32 uResult = PalWaitForMultipleObjectsEx(cWaitHandles, rgWaitHandles, FALSE, uTimeout, FALSE);
Expand Down Expand Up @@ -59,14 +69,20 @@ EXTERN_C REDHAWK_API UInt32_BOOL __cdecl RhpWaitForFinalizerRequest()
return FALSE;
}
} while (true);
#endif
}

// Indicate that the current round of finalizations is complete.
EXTERN_C REDHAWK_API void __cdecl RhpSignalFinalizationComplete()
{
#ifdef USE_PORTABLE_HELPERS
ASSERT(!"@TODO: FINALIZER THREAD NYI");
#else
GCHeap::GetGCHeap()->SignalFinalizationDone(TRUE);
#endif
}

#ifdef FEATURE_PREMORTEM_FINALIZATION
// Enable a last pass of the finalizer during (clean) runtime shutdown. Specify the number of milliseconds
// we'll wait before giving up a proceeding with the shutdown (INFINITE is an allowable value).
COOP_PINVOKE_HELPER(void, RhEnableShutdownFinalization, (UInt32 uiTimeout))
Expand All @@ -80,6 +96,7 @@ COOP_PINVOKE_HELPER(UInt8, RhHasShutdownStarted, ())
{
return g_fShutdownHasStarted ? 1 : 0;
}
#endif // FEATURE_PREMORTEM_FINALIZATION

//
// The following helpers are special in that they interact with internal GC state or directly manipulate
Expand Down
19 changes: 17 additions & 2 deletions src/Native/Runtime/GCHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,25 @@
// Unmanaged helpers exposed by the System.GC managed class.
//

#include "gcrhenv.h"
#include "common.h"
#include "gcenv.h"
#include "gc.h"
#include "restrictedcallouts.h"

#include "gcrhinterface.h"

#include "palredhawkcommon.h"
#include "slist.h"
#include "varint.h"
#include "regdisplay.h"
#include "stackframeiterator.h"

#include "thread.h"

COOP_PINVOKE_HELPER(void, RhSuppressFinalize, (OBJECTREF refObj))
{
if (!refObj->get_EEType()->HasFinalizer())
return;

GCHeap::GetGCHeap()->SetFinalizationRun(refObj);
}

Expand All @@ -24,7 +35,11 @@ EXTERN_C REDHAWK_API void __cdecl RhWaitForPendingFinalizers(BOOL allowReentrant
// called in cooperative mode.
ASSERT(!GetThread()->PreemptiveGCDisabled());

#ifdef USE_PORTABLE_HELPERS
ASSERT(!"@TODO: FINALIZER THREAD NYI");
#else
GCHeap::GetGCHeap()->FinalizerThreadWait(INFINITE, allowReentrantWait);
#endif
}

COOP_PINVOKE_HELPER(Int32, RhGetMaxGcGeneration, ())
Expand Down
2 changes: 1 addition & 1 deletion src/Native/Runtime/GcStressControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
#include "common.h"
#include "rhcommon.h"

#if defined(FEATURE_GC_STRESS) & !defined(DACCESS_COMPILE)

Expand Down
6 changes: 4 additions & 2 deletions src/Native/Runtime/HandleTableHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
// binder has special knowledge of these methods and doesn't generate the normal code to transition out of the
// runtime prior to the call.
//

#include "gcrhenv.h"
#include "common.h"
#include "gcenv.h"
#include "objecthandle.h"
#include "restrictedcallouts.h"


COOP_PINVOKE_HELPER(OBJECTHANDLE, RhpHandleAlloc, (Object *pObject, int type))
{
return CreateTypedHandle(g_HandleTableMap.pBuckets[0]->pTable[GetCurrentThreadHomeHeapNumber()], pObject, type);
Expand Down
1 change: 1 addition & 0 deletions src/Native/Runtime/InstanceStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
#include "rhcommon.h"
#include "commontypes.h"
#include "daccess.h"
#include "commonmacros.h"
Expand Down
Loading