Skip to content

Commit

Permalink
Remove #define Sleep (#70868)
Browse files Browse the repository at this point in the history
* Remove #define Sleep
  • Loading branch information
AaronRobinsonMSFT committed Jun 17, 2022
1 parent 051b482 commit 0d0fd75
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 95 deletions.
11 changes: 0 additions & 11 deletions src/coreclr/gc/env/gcenv.os.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,6 @@
#ifndef __GCENV_OS_H__
#define __GCENV_OS_H__

#ifdef Sleep
// This is a funny workaround for the fact that "common.h" defines Sleep to be
// Dont_Use_Sleep, with the hope of causing linker errors whenever someone tries to use sleep.
//
// However, GCToOSInterface defines a function called Sleep, which (due to this define) becomes
// "Dont_Use_Sleep", which the GC in turn happily uses. The symbol that GCToOSInterface actually
// exported was called "GCToOSInterface::Dont_Use_Sleep". While we progress in making the GC standalone,
// we'll need to break the dependency on common.h (the VM header) and this problem will become moot.
#undef Sleep
#endif // Sleep

#ifdef HAS_SYSTEM_YIELDPROCESSOR
// YieldProcessor is defined to Dont_Use_YieldProcessor. Restore it to the system-default implementation for the GC.
#undef YieldProcessor
Expand Down
3 changes: 0 additions & 3 deletions src/coreclr/vm/comsynchronizable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,6 @@ FCIMPL2(FC_BOOL_RET, ThreadNative::Join, ThreadBaseObject* pThisUNSAFE, INT32 Ti
}
FCIMPLEND

#undef Sleep
FCIMPL1(void, ThreadNative::Sleep, INT32 iTime)
{
FCALL_CONTRACT;
Expand All @@ -476,8 +475,6 @@ FCIMPL1(void, ThreadNative::Sleep, INT32 iTime)
}
FCIMPLEND

#define Sleep(dwMilliseconds) Dont_Use_Sleep(dwMilliseconds)

extern "C" void QCALLTYPE ThreadNative_UninterruptibleSleep0()
{
QCALL_CONTRACT;
Expand Down
2 changes: 0 additions & 2 deletions src/coreclr/vm/comsynchronizable.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ friend class ThreadBaseObject;
static FCDECL1(void, Interrupt, ThreadBaseObject* pThisUNSAFE);
static FCDECL1(FC_BOOL_RET, IsAlive, ThreadBaseObject* pThisUNSAFE);
static FCDECL2(FC_BOOL_RET, Join, ThreadBaseObject* pThisUNSAFE, INT32 Timeout);
#undef Sleep
static FCDECL1(void, Sleep, INT32 iTime);
#define Sleep(a) Dont_Use_Sleep(a)
static FCDECL1(void, Initialize, ThreadBaseObject* pThisUNSAFE);
static FCDECL2(void, SetBackground, ThreadBaseObject* pThisUNSAFE, CLR_BOOL isBackground);
static FCDECL1(FC_BOOL_RET, IsBackground, ThreadBaseObject* pThisUNSAFE);
Expand Down
2 changes: 0 additions & 2 deletions src/coreclr/vm/ecalllist.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,7 @@ FCFuncEnd()

FCFuncStart(gThreadFuncs)
FCFuncElement("InternalGetCurrentThread", GetThread)
#undef Sleep
FCFuncElement("SleepInternal", ThreadNative::Sleep)
#define Sleep(a) Dont_Use_Sleep(a)
FCFuncElement("Initialize", ThreadNative::Initialize)
FCFuncElement("SpinWaitInternal", ThreadNative::SpinWait)
FCFuncElement("GetCurrentThreadNative", ThreadNative::GetCurrentThread)
Expand Down
51 changes: 7 additions & 44 deletions src/coreclr/vm/hosting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "corhost.h"
#include "threads.h"

#undef VirtualAlloc
LPVOID ClrVirtualAlloc(LPVOID lpAddress, SIZE_T dwSize, DWORD flAllocationType, DWORD flProtect) {
CONTRACTL
{
Expand Down Expand Up @@ -78,44 +77,25 @@ LPVOID ClrVirtualAlloc(LPVOID lpAddress, SIZE_T dwSize, DWORD flAllocationType,
}

}
#define VirtualAlloc(lpAddress, dwSize, flAllocationType, flProtect) Dont_Use_VirtualAlloc(lpAddress, dwSize, flAllocationType, flProtect)

#undef VirtualFree
BOOL ClrVirtualFree(LPVOID lpAddress, SIZE_T dwSize, DWORD dwFreeType) {
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;

BOOL ClrVirtualFree(LPVOID lpAddress, SIZE_T dwSize, DWORD dwFreeType)
{
WRAPPER_NO_CONTRACT;
return (BOOL)(BYTE)::VirtualFree (lpAddress, dwSize, dwFreeType);
}
#define VirtualFree(lpAddress, dwSize, dwFreeType) Dont_Use_VirtualFree(lpAddress, dwSize, dwFreeType)

#undef VirtualQuery
SIZE_T ClrVirtualQuery(LPCVOID lpAddress, PMEMORY_BASIC_INFORMATION lpBuffer, SIZE_T dwLength)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;

{
return ::VirtualQuery(lpAddress, lpBuffer, dwLength);
}
WRAPPER_NO_CONTRACT;
return ::VirtualQuery(lpAddress, lpBuffer, dwLength);
}
#define VirtualQuery(lpAddress, lpBuffer, dwLength) Dont_Use_VirtualQuery(lpAddress, lpBuffer, dwLength)

#if defined(_DEBUG) && !defined(TARGET_UNIX)
static VolatilePtr<BYTE> s_pStartOfUEFSection = NULL;
static VolatilePtr<BYTE> s_pEndOfUEFSectionBoundary = NULL;
static Volatile<DWORD> s_dwProtection = 0;
#endif // _DEBUG && !TARGET_UNIX

#undef VirtualProtect
BOOL ClrVirtualProtect(LPVOID lpAddress, SIZE_T dwSize, DWORD flNewProtect, PDWORD lpflOldProtect)
{
CONTRACTL
Expand Down Expand Up @@ -235,29 +215,12 @@ BOOL ClrVirtualProtect(LPVOID lpAddress, SIZE_T dwSize, DWORD flNewProtect, PDWO

return ::VirtualProtect(lpAddress, dwSize, flNewProtect, lpflOldProtect);
}
#define VirtualProtect(lpAddress, dwSize, flNewProtect, lpflOldProtect) Dont_Use_VirtualProtect(lpAddress, dwSize, flNewProtect, lpflOldProtect)

#undef SleepEx
DWORD ClrSleepEx(DWORD dwMilliseconds, BOOL bAlertable)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
}
CONTRACTL_END;

DWORD res;

{
res = ::SleepEx(dwMilliseconds, bAlertable);
}

return res;
WRAPPER_NO_CONTRACT;
return ::SleepEx(dwMilliseconds, bAlertable);
}
#define SleepEx(dwMilliseconds,bAlertable) \
Dont_Use_SleepEx(dwMilliseconds,bAlertable)

// non-zero return value if this function causes the OS to switch to another thread
// See file:spinlock.h#SwitchToThreadSpinning for an explanation of dwSwitchCount
Expand Down
8 changes: 1 addition & 7 deletions src/coreclr/vm/threads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1006,10 +1006,8 @@ HRESULT Thread::DetachThread(BOOL fDLLThreadDetach)
while (m_dwThreadHandleBeingUsed > 0)
{
// Another thread is using the handle now.
#undef Sleep
// We can not call __SwitchToThread since we can not go back to host.
::Sleep(10);
#define Sleep(a) Dont_Use_Sleep(a)
ClrSleepEx(10, FALSE);
}
if (m_WeOwnThreadHandle && m_ThreadHandleForClose == INVALID_HANDLE_VALUE)
{
Expand Down Expand Up @@ -6748,11 +6746,7 @@ BOOL Thread::DoesRegionContainGuardPage(UINT_PTR uLowAddress, UINT_PTR uHighAddr

while (uStartOfCurrentRegion < uHighAddress)
{
#undef VirtualQuery
// This code can run below YieldTask, which means that it must not call back into the host.
// The reason is that YieldTask is invoked by the host, and the host needs not be reentrant.
dwRes = VirtualQuery((const void *)uStartOfCurrentRegion, &meminfo, sizeof(meminfo));
#define VirtualQuery(lpAddress, lpBuffer, dwLength) Dont_Use_VirtualQuery(lpAddress, lpBuffer, dwLength)

// If the query fails then assume we have no guard page.
if (sizeof(meminfo) != dwRes)
Expand Down
6 changes: 2 additions & 4 deletions src/coreclr/vm/threadsuspend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5198,17 +5198,15 @@ BOOL ThreadCaughtInKernelModeExceptionHandling(Thread *pThread, CONTEXT *ctx)
// still has page guard bit set. We can't hit the race in such case so we just leave. Besides, we can't access the
// memory with page guard flag or not committed.
MEMORY_BASIC_INFORMATION mbi;
#undef VirtualQuery
// This code can run below YieldTask, which means that it must not call back into the host.
// The reason is that YieldTask is invoked by the host, and the host needs not be reentrant.
if (VirtualQuery((LPCVOID)(UINT_PTR)ctx->Esp, &mbi, sizeof(mbi)) == sizeof(mbi))
{
if (!(mbi.State & MEM_COMMIT) || (mbi.Protect & PAGE_GUARD))
return FALSE;
}
else
{
STRESS_LOG0 (LF_SYNC, ERROR, "VirtualQuery failed!");
#define VirtualQuery(lpAddress, lpBuffer, dwLength) Dont_Use_VirtualQuery(lpAddress, lpBuffer, dwLength)
}

// The first two values on the stack should be a pointer to the EXCEPTION_RECORD and a pointer to the CONTEXT.
UINT_PTR Esp = (UINT_PTR)ctx->Esp;
Expand Down
25 changes: 3 additions & 22 deletions src/coreclr/vm/win32threadpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2456,15 +2456,7 @@ DWORD WINAPI ThreadpoolMgr::WaitThreadStart(LPVOID lpArgs)

if (threadCB->NumActiveWaits == 0)
{

#undef SleepEx
// <TODO>@TODO Consider doing a sleep for an idle period and terminating the thread if no activity</TODO>
//We use SleepEx instead of CLRSLeepEx because CLRSleepEx calls into SQL(or other hosts) in hosted
//scenarios. SQL does not deliver APC's, and the waithread wait insertion/deletion logic depends on
//APC's being delivered.
status = SleepEx(INFINITE,TRUE);
#define SleepEx(a,b) Dont_Use_SleepEx(a,b)

status = ClrSleepEx(INFINITE,TRUE);
_ASSERTE(status == WAIT_IO_COMPLETION);
}
else if (IsWaitThreadAPCPending())
Expand All @@ -2474,15 +2466,7 @@ DWORD WINAPI ThreadpoolMgr::WaitThreadStart(LPVOID lpArgs)
//allow the thread to enter alertable wait and thus cause the APC to fire.

ResetWaitThreadAPCPending();

//We use SleepEx instead of CLRSLeepEx because CLRSleepEx calls into SQL(or other hosts) in hosted
//scenarios. SQL does not deliver APC's, and the waithread wait insertion/deletion logic depends on
//APC's being delivered.

#undef SleepEx
status = SleepEx(0,TRUE);
#define SleepEx(a,b) Dont_Use_SleepEx(a,b)

status = ClrSleepEx(0,TRUE);
continue;
}
else
Expand Down Expand Up @@ -4480,10 +4464,7 @@ void ThreadpoolMgr::TimerThreadFire()

EX_TRY {
DWORD timeout = FireTimers();

#undef SleepEx
SleepEx(timeout, TRUE);
#define SleepEx(a,b) Dont_Use_SleepEx(a,b)
ClrSleepEx(timeout, TRUE);

// the thread could wake up either because an APC completed or the sleep timeout
// in both case, we need to sweep the timer queue, firing timers, and readjusting
Expand Down

0 comments on commit 0d0fd75

Please sign in to comment.