From 2a56c4a700231045fa7003b1f149766c2a59d47a Mon Sep 17 00:00:00 2001 From: yangxiaobing Date: Wed, 26 Nov 2025 18:28:12 +0800 Subject: [PATCH] Conditionally compile the pthread_t member in emscripten::val val adds a `pthread_t` data member for later thread checks, but it is only used when the code is compiled with pthread support. Following the "what you don't use, you don't pay for" principle, this change wraps the `pthread_t` member with the same `#ifdef` guard used by the thread-checking code. For single-threaded builds, the member is omitted entirely, thus avoiding unnecessary memory overhead. --- system/include/emscripten/val.h | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/system/include/emscripten/val.h b/system/include/emscripten/val.h index 92997e333eca3..3d58535065cd3 100644 --- a/system/include/emscripten/val.h +++ b/system/include/emscripten/val.h @@ -355,7 +355,12 @@ class EMBIND_VISIBILITY_DEFAULT val { // it just moves a value and doesn't need to go via incref/decref. // This means it's safe to move values across threads - an error will // only arise if you access or free it from the wrong thread later. - val(val&& v) : handle(v.handle), thread(v.thread) { + val(val&& v) + : +#ifdef _REENTRANT + thread(v.thread), +#endif + handle(v.handle) { v.handle = 0; } @@ -558,8 +563,12 @@ class EMBIND_VISIBILITY_DEFAULT val { private: // takes ownership, assumes handle already incref'd and lives on the same thread explicit val(EM_VAL handle) - : handle(handle), thread(pthread_self()) - {} + : +#ifdef _REENTRANT + thread(pthread_self()), +#endif + handle(handle) { + } // Whether this value is a uses incref/decref (true) or is a special reserved // value (false). @@ -625,7 +634,10 @@ class EMBIND_VISIBILITY_DEFAULT val { return v; } +#ifdef _REENTRANT + // It's used for thread checks and only under pthread environment. pthread_t thread; +#endif EM_VAL handle; template