Skip to content

Commit 62b872c

Browse files
committed
[MERGE #2679 @obastemur] xplat: cache thread stack limit
Merge pull request #2679 from obastemur:cache_stackl
2 parents e01f5a5 + 877c6d7 commit 62b872c

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

pal/src/thread/pal_thread.cpp

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2834,8 +2834,27 @@ int CorUnix::CThreadMachExceptionHandlers::GetIndexOfHandler(exception_mask_t bm
28342834

28352835
#endif // HAVE_MACH_EXCEPTIONS
28362836

2837+
#ifndef __APPLE__
2838+
#define THREAD_LOCAL thread_local
2839+
#else
2840+
#define THREAD_LOCAL _Thread_local
2841+
#endif
2842+
2843+
#ifndef __IOS__
2844+
static THREAD_LOCAL ULONG_PTR s_cachedHighLimit = 0;
2845+
static THREAD_LOCAL ULONG_PTR s_cachedLowLimit = 0;
2846+
#endif
2847+
28372848
void GetCurrentThreadStackLimits(ULONG_PTR* lowLimit, ULONG_PTR* highLimit)
28382849
{
2850+
#ifndef __IOS__
2851+
if (s_cachedLowLimit)
2852+
{
2853+
*lowLimit = s_cachedLowLimit;
2854+
*highLimit = s_cachedHighLimit;
2855+
return;
2856+
}
2857+
#endif
28392858
pthread_t currentThreadHandle = pthread_self();
28402859

28412860
#ifdef __APPLE__
@@ -2866,14 +2885,12 @@ void GetCurrentThreadStackLimits(ULONG_PTR* lowLimit, ULONG_PTR* highLimit)
28662885
*lowLimit = (ULONG_PTR) stackend;
28672886
*highLimit = (ULONG_PTR) stackbase;
28682887
#endif
2869-
}
28702888

2871-
#ifndef __APPLE__
2872-
#define THREAD_LOCAL thread_local
2873-
#else
2874-
#define THREAD_LOCAL _Thread_local
2889+
#ifndef __IOS__
2890+
s_cachedLowLimit = *lowLimit;
2891+
s_cachedHighLimit = *highLimit;
28752892
#endif
2876-
static THREAD_LOCAL ULONG_PTR s_cachedThreadStackHighLimit = 0;
2893+
}
28772894

28782895
bool IsAddressOnStack(ULONG_PTR address)
28792896
{
@@ -2882,17 +2899,12 @@ bool IsAddressOnStack(ULONG_PTR address)
28822899
// bounds to speed up checking if a given address is on the stack
28832900
// The semantics of IsAddressOnStack is that we care if a given address is
28842901
// in the range of the current stack pointer
2885-
if (s_cachedThreadStackHighLimit == 0)
2886-
{
2887-
ULONG_PTR lowLimit, highLimit;
2888-
GetCurrentThreadStackLimits(&lowLimit, &highLimit);
2889-
2890-
s_cachedThreadStackHighLimit = highLimit;
2891-
}
2902+
ULONG_PTR lowLimit, highLimit;
2903+
GetCurrentThreadStackLimits(&lowLimit, &highLimit);
28922904

28932905
ULONG_PTR currentStackPtr = GetCurrentSP();
28942906

2895-
if (currentStackPtr <= address && address < s_cachedThreadStackHighLimit)
2907+
if (currentStackPtr <= address && address < highLimit)
28962908
{
28972909
return true;
28982910
}

0 commit comments

Comments
 (0)