Skip to content
Snippets Groups Projects
Commit f1488bae authored by Fred Wright's avatar Fred Wright Committed by Christopher Nielsen
Browse files

pthread_get_stacksize_np: Cache dlsym() result.

This avoids repeatedly looking up the symbol for the OS call.

Also cleans up indentation.

TESTED:
Passes tests on all platforms.
parent 8292b6c1
Branches
No related tags found
No related merge requests found
...@@ -43,29 +43,33 @@ size_t pthread_get_stacksize_np(pthread_t t) { ...@@ -43,29 +43,33 @@ size_t pthread_get_stacksize_np(pthread_t t) {
} *thread = (void*) t; } *thread = (void*) t;
int is_main_thread = ((thread->detached & 4) == 4); int is_main_thread = ((thread->detached & 4) == 4);
#endif #endif
if ( is_main_thread ) { if ( is_main_thread ) {
/* use LLVM workaround */ /* use LLVM workaround */
/* see https://github.com/llvm/llvm-project/blob/617a15a9eac96088ae5e9134248d8236e34b91b1/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp#L414 */ /* see https://github.com/llvm/llvm-project/blob/617a15a9eac96088ae5e9134248d8236e34b91b1/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp#L414 */
/* OpenJDK also has a workaround */ /* OpenJDK also has a workaround */
/* see https://github.com/openjdk/jdk/blob/e833bfc8ac6104522d037e7eb300f5aa112688bb/src/hotspot/os_cpu/bsd_x86/os_bsd_x86.cpp#L715 */ /* see https://github.com/openjdk/jdk/blob/e833bfc8ac6104522d037e7eb300f5aa112688bb/src/hotspot/os_cpu/bsd_x86/os_bsd_x86.cpp#L715 */
struct rlimit limit; struct rlimit limit;
if( getrlimit(RLIMIT_STACK, &limit) ) { if( getrlimit(RLIMIT_STACK, &limit) ) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if( limit.rlim_cur < kMaxThreadStackSize ) { if( limit.rlim_cur < kMaxThreadStackSize ) {
return limit.rlim_cur; return limit.rlim_cur;
} else { } else {
return kMaxThreadStackSize; return kMaxThreadStackSize;
} }
} else { } else {
/* bug only affects main thread */ /* bug only affects main thread */
size_t (*real_pthread_get_stacksize_np)(pthread_t); static size_t (*os_pthread_get_stacksize_np)(pthread_t);
real_pthread_get_stacksize_np = dlsym(RTLD_NEXT, "pthread_get_stacksize_np"); if (!os_pthread_get_stacksize_np) {
if (real_pthread_get_stacksize_np == NULL) { os_pthread_get_stacksize_np =
exit(EXIT_FAILURE); dlsym(RTLD_NEXT, "pthread_get_stacksize_np");
} /* Something's badly broken if this fails */
return real_pthread_get_stacksize_np(t); if (!os_pthread_get_stacksize_np) {
} abort();
}
}
return (*os_pthread_get_stacksize_np)(t);
}
} }
#endif /* __MPLS_LIB_SUPPORT_PTHREAD_GET_STACKSIZE_NP_FIX__ */ #endif /* __MPLS_LIB_SUPPORT_PTHREAD_GET_STACKSIZE_NP_FIX__ */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment