From f1488baef95e8d18a1c95e09e8d2a9122ce84128 Mon Sep 17 00:00:00 2001
From: Fred Wright <fw@fwright.net>
Date: Fri, 15 Nov 2024 15:50:16 -0800
Subject: [PATCH] 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.
---
 src/pthread_get_stacksize_np.c | 50 ++++++++++++++++++----------------
 1 file changed, 27 insertions(+), 23 deletions(-)

diff --git a/src/pthread_get_stacksize_np.c b/src/pthread_get_stacksize_np.c
index 1bbd720..4ffb38b 100644
--- a/src/pthread_get_stacksize_np.c
+++ b/src/pthread_get_stacksize_np.c
@@ -43,29 +43,33 @@ size_t pthread_get_stacksize_np(pthread_t t) {
      } *thread = (void*) t;
      int is_main_thread = ((thread->detached & 4) == 4);
 #endif
-     if ( is_main_thread ) {
-	  /* use LLVM workaround */
-	  /* see https://github.com/llvm/llvm-project/blob/617a15a9eac96088ae5e9134248d8236e34b91b1/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp#L414 */
-         /* OpenJDK also has a workaround */
-         /* see https://github.com/openjdk/jdk/blob/e833bfc8ac6104522d037e7eb300f5aa112688bb/src/hotspot/os_cpu/bsd_x86/os_bsd_x86.cpp#L715 */
-	  struct rlimit limit;
-	  if( getrlimit(RLIMIT_STACK, &limit) ) {
-	       exit(EXIT_FAILURE);
-	  }
-	  if( limit.rlim_cur < kMaxThreadStackSize ) {
-	       return limit.rlim_cur;
-	  } else {
-	       return kMaxThreadStackSize;
-	  }
-     } else {
-	  /* bug only affects main thread */
-	  size_t (*real_pthread_get_stacksize_np)(pthread_t);
-	  real_pthread_get_stacksize_np = dlsym(RTLD_NEXT, "pthread_get_stacksize_np");
-	  if (real_pthread_get_stacksize_np == NULL) {
-	       exit(EXIT_FAILURE);
-	  }
-	  return real_pthread_get_stacksize_np(t);
-     }
+    if ( is_main_thread ) {
+        /* use LLVM workaround */
+        /* see https://github.com/llvm/llvm-project/blob/617a15a9eac96088ae5e9134248d8236e34b91b1/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp#L414 */
+       /* OpenJDK also has a workaround */
+       /* see https://github.com/openjdk/jdk/blob/e833bfc8ac6104522d037e7eb300f5aa112688bb/src/hotspot/os_cpu/bsd_x86/os_bsd_x86.cpp#L715 */
+        struct rlimit limit;
+        if( getrlimit(RLIMIT_STACK, &limit) ) {
+             exit(EXIT_FAILURE);
+        }
+        if( limit.rlim_cur < kMaxThreadStackSize ) {
+             return limit.rlim_cur;
+        } else {
+             return kMaxThreadStackSize;
+        }
+    } else {
+        /* bug only affects main thread */
+        static size_t (*os_pthread_get_stacksize_np)(pthread_t);
+        if (!os_pthread_get_stacksize_np) {
+            os_pthread_get_stacksize_np =
+                dlsym(RTLD_NEXT, "pthread_get_stacksize_np");
+            /* Something's badly broken if this fails */
+            if (!os_pthread_get_stacksize_np) {
+                abort();
+            }
+        }
+        return (*os_pthread_get_stacksize_np)(t);
+    }
 }
 
 #endif /* __MPLS_LIB_SUPPORT_PTHREAD_GET_STACKSIZE_NP_FIX__ */
-- 
GitLab