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

Commit 92d9aee

Browse files
committed
Merge pull request #564 from janvorli/gc-os-interface
GC to OS interface refactoring
2 parents aaa49d2 + 9e0eb8b commit 92d9aee

38 files changed

+2797
-1980
lines changed

src/Native/Runtime/Crst.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,13 @@
1010
#include "holder.h"
1111
#include "Crst.h"
1212

13-
#ifndef DACCESS_COMPILE
14-
bool EEThreadId::IsSameThread()
15-
{
16-
return PalGetCurrentThreadId() == m_uiId;
17-
}
18-
#endif // DACCESS_COMPILE
19-
2013
void CrstStatic::Init(CrstType eType, CrstFlags eFlags)
2114
{
2215
UNREFERENCED_PARAMETER(eType);
2316
UNREFERENCED_PARAMETER(eFlags);
2417
#ifndef DACCESS_COMPILE
2518
#if defined(_DEBUG)
26-
m_uiOwnerId = UNOWNED;
19+
m_uiOwnerId.Clear();
2720
#endif // _DEBUG
2821
PalInitializeCriticalSectionEx(&m_sCritSec, 0, 0);
2922
#endif // !DACCESS_COMPILE
@@ -42,7 +35,7 @@ void CrstStatic::Enter(CrstStatic *pCrst)
4235
#ifndef DACCESS_COMPILE
4336
PalEnterCriticalSection(&pCrst->m_sCritSec);
4437
#if defined(_DEBUG)
45-
pCrst->m_uiOwnerId = PalGetCurrentThreadId();
38+
pCrst->m_uiOwnerId.SetToCurrentThread();
4639
#endif // _DEBUG
4740
#else
4841
UNREFERENCED_PARAMETER(pCrst);
@@ -54,7 +47,7 @@ void CrstStatic::Leave(CrstStatic *pCrst)
5447
{
5548
#ifndef DACCESS_COMPILE
5649
#if defined(_DEBUG)
57-
pCrst->m_uiOwnerId = UNOWNED;
50+
pCrst->m_uiOwnerId.Clear();
5851
#endif // _DEBUG
5952
PalLeaveCriticalSection(&pCrst->m_sCritSec);
6053
#else
@@ -66,14 +59,14 @@ void CrstStatic::Leave(CrstStatic *pCrst)
6659
bool CrstStatic::OwnedByCurrentThread()
6760
{
6861
#ifndef DACCESS_COMPILE
69-
return m_uiOwnerId == PalGetCurrentThreadId();
62+
return m_uiOwnerId.IsCurrentThread();
7063
#else
7164
return false;
7265
#endif
7366
}
7467

7568
EEThreadId CrstStatic::GetHolderThreadId()
7669
{
77-
return EEThreadId(m_uiOwnerId);
70+
return m_uiOwnerId;
7871
}
7972
#endif // _DEBUG

src/Native/Runtime/Crst.h

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,6 @@
33
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
44
//
55

6-
// Abstracted thread ID. This doesn't really belong in this file, but there is not currently any better place
7-
// for it.
8-
class EEThreadId
9-
{
10-
public:
11-
EEThreadId(uint32_t uiId) : m_uiId(uiId) {}
12-
#ifndef DACCESS_COMPILE
13-
bool IsSameThread();
14-
#endif
15-
16-
private:
17-
uint32_t m_uiId;
18-
};
19-
20-
216
//
227
// -----------------------------------------------------------------------------------------------------------
238
//
@@ -67,8 +52,7 @@ class CrstStatic
6752
private:
6853
CRITICAL_SECTION m_sCritSec;
6954
#if defined(_DEBUG)
70-
uint32_t m_uiOwnerId;
71-
static const uint32_t UNOWNED = 0;
55+
EEThreadId m_uiOwnerId;
7256
#endif // _DEBUG
7357
};
7458

src/Native/Runtime/PalRedhawk.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@ extern GCSystemInfo g_SystemInfo;
651651
#define REDHAWK_PALAPI __stdcall
652652
#endif // GCENV_INCLUDED
653653

654+
bool InitializeSystemInfo();
654655

655656
#ifndef DACCESS_COMPILE
656657

@@ -773,18 +774,13 @@ REDHAWK_PALIMPORT int __cdecl PalVSprintf(_Out_writes_z_(cchBuffer) char * szBuf
773774
#define ALLOW_CONSTANT_EXPR_BEGIN __pragma(warning(push)) __pragma(warning(disable:4127))
774775
#define ALLOW_CONSTANT_EXPR_END __pragma(warning(pop))
775776

776-
struct GCMemoryStatus;
777-
REDHAWK_PALIMPORT UInt32_BOOL REDHAWK_PALAPI PalGlobalMemoryStatusEx(_Out_ GCMemoryStatus* pBuffer);
778777
REDHAWK_PALIMPORT _Ret_maybenull_ _Post_writable_byte_size_(size) void* REDHAWK_PALAPI PalVirtualAlloc(_In_opt_ void* pAddress, UIntNative size, UInt32 allocationType, UInt32 protect);
779778
REDHAWK_PALIMPORT UInt32_BOOL REDHAWK_PALAPI PalVirtualFree(_In_ void* pAddress, UIntNative size, UInt32 freeType);
780779
REDHAWK_PALIMPORT void REDHAWK_PALAPI PalSleep(UInt32 milliseconds);
781780
REDHAWK_PALIMPORT UInt32_BOOL REDHAWK_PALAPI PalSwitchToThread();
782-
REDHAWK_PALIMPORT HANDLE REDHAWK_PALAPI PalCreateMutexW(_In_opt_ LPSECURITY_ATTRIBUTES pMutexAttributes, UInt32_BOOL initialOwner, _In_opt_z_ LPCWSTR pName);
783781
REDHAWK_PALIMPORT HANDLE REDHAWK_PALAPI PalCreateEventW(_In_opt_ LPSECURITY_ATTRIBUTES pEventAttributes, UInt32_BOOL manualReset, UInt32_BOOL initialState, _In_opt_z_ LPCWSTR pName);
784782
REDHAWK_PALIMPORT UInt32 REDHAWK_PALAPI PalGetTickCount();
785783
REDHAWK_PALIMPORT HANDLE REDHAWK_PALAPI PalCreateFileW(_In_z_ LPCWSTR pFileName, uint32_t desiredAccess, uint32_t shareMode, _In_opt_ void* pSecurityAttributes, uint32_t creationDisposition, uint32_t flagsAndAttributes, HANDLE hTemplateFile);
786-
REDHAWK_PALIMPORT UInt32 REDHAWK_PALAPI PalGetWriteWatch(UInt32 flags, _In_ void* pBaseAddress, UIntNative regionSize, _Out_writes_to_opt_(*pCount, *pCount) void** pAddresses, _Inout_opt_ UIntNative* pCount, _Inout_opt_ UInt32* pGranularity);
787-
REDHAWK_PALIMPORT UInt32 REDHAWK_PALAPI PalResetWriteWatch(_In_ void* pBaseAddress, UIntNative regionSize);
788784
REDHAWK_PALIMPORT HANDLE REDHAWK_PALAPI PalCreateLowMemoryNotification();
789785
REDHAWK_PALIMPORT void REDHAWK_PALAPI PalTerminateCurrentProcess(UInt32 exitCode);
790786
REDHAWK_PALIMPORT HANDLE REDHAWK_PALAPI PalGetModuleHandleFromPointer(_In_ void* pointer);

src/Native/Runtime/PalRedhawkFunctions.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,6 @@ inline UInt32_BOOL PalSetEvent(HANDLE arg1)
196196
return SetEvent(arg1);
197197
}
198198

199-
extern "C" UInt32_BOOL __stdcall SetFilePointerEx(HANDLE, LARGE_INTEGER, LARGE_INTEGER *, UInt32);
200-
inline UInt32_BOOL PalSetFilePointerEx(HANDLE arg1, LARGE_INTEGER arg2, LARGE_INTEGER * arg3, UInt32 arg4)
201-
{
202-
return SetFilePointerEx(arg1, arg2, arg3, arg4);
203-
}
204-
205199
extern "C" void __stdcall TerminateProcess(HANDLE, UInt32);
206200
inline void PalTerminateProcess(HANDLE arg1, UInt32 arg2)
207201
{

src/Native/Runtime/gcenv.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@
99
#pragma warning( disable: 4127 ) // conditional expression is constant -- common in GC
1010
#endif
1111

12+
typedef wchar_t WCHAR;
13+
#define W(str) L##str
14+
1215
#include "sal.h"
1316
#include "gcenv.structs.h"
17+
#include "gcenv.os.h"
18+
#include "gcenv.interlocked.h"
1419
#include "gcenv.base.h"
20+
#include "gcenv.ee.h"
1521

1622
#include "Crst.h"
1723
#include "event.h"
@@ -25,6 +31,7 @@
2531
#include "PalRedhawkCommon.h"
2632
#include "PalRedhawk.h"
2733
#include "gcrhinterface.h"
34+
#include "gcenv.interlocked.inl"
2835

2936
#ifdef FEATURE_ETW
3037

@@ -53,6 +60,12 @@
5360

5461
#endif // FEATURE_ETW
5562

63+
#define MAX_LONGPATH 1024
64+
65+
#ifndef YieldProcessor
66+
#define YieldProcessor PalYieldProcessor
67+
#endif
68+
5669
// Adapter for GC's view of Array
5770
class ArrayBase : Array
5871
{

src/Native/Runtime/gcrhenv.cpp

Lines changed: 10 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,10 @@ bool RedhawkGCInterface::InitializeSubsystems(GCType gcType)
169169
MICROSOFT_WINDOWS_REDHAWK_GC_PUBLIC_PROVIDER_Context.RegistrationHandle = Microsoft_Windows_Redhawk_GC_PublicHandle;
170170
#endif // FEATURE_ETW
171171

172-
InitializeSystemInfo();
172+
if (!InitializeSystemInfo())
173+
{
174+
return false;
175+
}
173176

174177
// Initialize the special EEType used to mark free list entries in the GC heap.
175178
g_FreeObjectEEType.InitializeAsGcFreeType();
@@ -479,7 +482,7 @@ void RedhawkGCInterface::ScanHeap(GcScanObjectFunction pfnScanCallback, void *pC
479482
// Carefully attempt to set the global callback function (careful in that we won't overwrite another scan
480483
// that's being scheduled or in-progress). If someone beat us to it back off and wait for the
481484
// corresponding GC to complete.
482-
while (FastInterlockCompareExchangePointer(&g_pfnHeapScan, pfnScanCallback, NULL) != NULL)
485+
while (Interlocked::CompareExchangePointer(&g_pfnHeapScan, pfnScanCallback, NULL) != NULL)
483486
{
484487
// Wait in pre-emptive mode to avoid stalling another thread that's attempting a collection.
485488
Thread * pCurThread = GetThread();
@@ -509,7 +512,7 @@ void RedhawkGCInterface::ScanHeap(GcScanObjectFunction pfnScanCallback, void *pC
509512

510513
// Release our hold on the global scanning pointers.
511514
g_pvHeapScanContext = NULL;
512-
FastInterlockExchangePointer(&g_pfnHeapScan, NULL);
515+
Interlocked::ExchangePointer(&g_pfnHeapScan, NULL);
513516
#else
514517
UNREFERENCED_PARAMETER(pfnScanCallback);
515518
UNREFERENCED_PARAMETER(pContext);
@@ -992,12 +995,12 @@ bool StartFinalizerThread()
992995
//
993996
static volatile Int32 fFinalizerThreadCreated;
994997

995-
if (FastInterlockExchange(&fFinalizerThreadCreated, 1) != 1)
998+
if (Interlocked::Exchange(&fFinalizerThreadCreated, 1) != 1)
996999
{
9971000
if (!PalStartFinalizerThread(FinalizerStart, (void*)FinalizerThread::GetFinalizerEvent()))
9981001
{
9991002
// Need to try again another time...
1000-
FastInterlockExchange(&fFinalizerThreadCreated, 0);
1003+
Interlocked::Exchange(&fFinalizerThreadCreated, 0);
10011004
}
10021005
}
10031006

@@ -1095,9 +1098,9 @@ bool FinalizerThread::WatchDog()
10951098

10961099
// Wait for any outstanding finalization run to complete. Time this initial operation so that it forms
10971100
// part of the overall timeout budget.
1098-
DWORD dwStartTime = GetTickCount();
1101+
DWORD dwStartTime = PalGetTickCount();
10991102
Wait(dwTimeout);
1100-
DWORD dwEndTime = GetTickCount();
1103+
DWORD dwEndTime = PalGetTickCount();
11011104

11021105
// In the exceedingly rare case that the tick count wrapped then we'll just reset the timeout to its
11031106
// initial value. Otherwise we'll subtract the time we waited from the timeout budget (being mindful
@@ -1156,60 +1159,14 @@ void FinalizerThread::Wait(DWORD timeout, bool allowReentrantWait)
11561159
#endif // FEATURE_PREMORTEM_FINALIZATION
11571160

11581161
#ifndef DACCESS_COMPILE
1159-
void GetProcessMemoryLoad(GCMemoryStatus* pGCMemStatus)
1160-
{
1161-
// @TODO: no way to communicate failure
1162-
PalGlobalMemoryStatusEx(pGCMemStatus);
1163-
}
11641162

11651163
bool __SwitchToThread(uint32_t /*dwSleepMSec*/, uint32_t /*dwSwitchCount*/)
11661164
{
11671165
return !!PalSwitchToThread();
11681166
}
11691167

1170-
void * ClrVirtualAlloc(
1171-
void * lpAddress,
1172-
size_t dwSize,
1173-
uint32_t flAllocationType,
1174-
uint32_t flProtect)
1175-
{
1176-
return PalVirtualAlloc(lpAddress, dwSize, flAllocationType, flProtect);
1177-
}
1178-
1179-
void * ClrVirtualAllocAligned(
1180-
void * lpAddress,
1181-
size_t dwSize,
1182-
uint32_t flAllocationType,
1183-
uint32_t flProtect,
1184-
size_t /*dwAlignment*/)
1185-
{
1186-
return PalVirtualAlloc(lpAddress, dwSize, flAllocationType, flProtect);
1187-
}
1188-
1189-
bool ClrVirtualFree(
1190-
void * lpAddress,
1191-
size_t dwSize,
1192-
uint32_t dwFreeType)
1193-
{
1194-
return !!PalVirtualFree(lpAddress, dwSize, dwFreeType);
1195-
}
11961168
#endif // DACCESS_COMPILE
11971169

1198-
bool
1199-
ClrVirtualProtect(
1200-
void * lpAddress,
1201-
size_t dwSize,
1202-
uint32_t flNewProtect,
1203-
uint32_t * lpflOldProtect)
1204-
{
1205-
UNREFERENCED_PARAMETER(lpAddress);
1206-
UNREFERENCED_PARAMETER(dwSize);
1207-
UNREFERENCED_PARAMETER(flNewProtect);
1208-
UNREFERENCED_PARAMETER(lpflOldProtect);
1209-
ASSERT(!"ClrVirtualProtect");
1210-
return false;
1211-
}
1212-
12131170
MethodTable * g_pFreeObjectMethodTable;
12141171
int32_t g_TrapReturningThreads;
12151172
bool g_fFinalizerRunOnShutDown;
@@ -1230,38 +1187,6 @@ VOID LogSpewAlways(const char * /*fmt*/, ...)
12301187
{
12311188
}
12321189

1233-
CLR_MUTEX_COOKIE ClrCreateMutex(CLR_MUTEX_ATTRIBUTES lpMutexAttributes, bool bInitialOwner, LPCWSTR lpName)
1234-
{
1235-
UNREFERENCED_PARAMETER(lpMutexAttributes);
1236-
UNREFERENCED_PARAMETER(bInitialOwner);
1237-
UNREFERENCED_PARAMETER(lpName);
1238-
ASSERT(!"ClrCreateMutex");
1239-
return NULL;
1240-
}
1241-
1242-
void ClrCloseMutex(CLR_MUTEX_COOKIE mutex)
1243-
{
1244-
UNREFERENCED_PARAMETER(mutex);
1245-
ASSERT(!"ClrCloseMutex");
1246-
}
1247-
1248-
bool ClrReleaseMutex(CLR_MUTEX_COOKIE mutex)
1249-
{
1250-
UNREFERENCED_PARAMETER(mutex);
1251-
ASSERT(!"ClrReleaseMutex");
1252-
return true;
1253-
}
1254-
1255-
uint32_t ClrWaitForMutex(CLR_MUTEX_COOKIE mutex, uint32_t dwMilliseconds, bool bAlertable)
1256-
{
1257-
UNREFERENCED_PARAMETER(mutex);
1258-
UNREFERENCED_PARAMETER(dwMilliseconds);
1259-
UNREFERENCED_PARAMETER(bAlertable);
1260-
ASSERT(!"ClrWaitForMutex");
1261-
return WAIT_OBJECT_0;
1262-
}
1263-
1264-
12651190
uint32_t CLRConfig::GetConfigValue(ConfigDWORDInfo eType)
12661191
{
12671192
switch (eType)

src/Native/Runtime/unix/PalRedhawkInline.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ FORCEINLINE Int32 PalInterlockedDecrement(_Inout_ _Interlocked_operand_ Int32 vo
1717

1818
FORCEINLINE UInt32 PalInterlockedOr(_Inout_ _Interlocked_operand_ UInt32 volatile *pDst, UInt32 iValue)
1919
{
20-
return __sync_fetch_and_or(pDst, iValue);
20+
return __sync_or_and_fetch(pDst, iValue);
2121
}
2222

2323
FORCEINLINE UInt32 PalInterlockedAnd(_Inout_ _Interlocked_operand_ UInt32 volatile *pDst, UInt32 iValue)
2424
{
25-
return __sync_fetch_and_and(pDst, iValue);
25+
return __sync_and_and_fetch(pDst, iValue);
2626
}
2727

2828
FORCEINLINE Int32 PalInterlockedExchange(_Inout_ _Interlocked_operand_ Int32 volatile *pDst, Int32 iValue)

0 commit comments

Comments
 (0)