From 5b2a0166d655caedca57a510cd1b465ffbae9c71 Mon Sep 17 00:00:00 2001 From: "Jessica Wong (Seattle)" Date: Wed, 27 Nov 2024 13:23:53 -0800 Subject: [PATCH 01/14] Add threadid to AsyncStackRoot --- include/unifex/tracing/async_stack-inl.hpp | 3 ++- include/unifex/tracing/async_stack.hpp | 19 +++++++++++++++++-- source/async_stack.cpp | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/include/unifex/tracing/async_stack-inl.hpp b/include/unifex/tracing/async_stack-inl.hpp index add04bde..99e84987 100644 --- a/include/unifex/tracing/async_stack-inl.hpp +++ b/include/unifex/tracing/async_stack-inl.hpp @@ -136,9 +136,10 @@ inline AsyncStackFrame* AsyncStackRoot::getTopFrame() const noexcept { } inline void AsyncStackRoot::setStackFrameContext( - frame_ptr framePtr, instruction_ptr ip) noexcept { + frame_ptr framePtr, instruction_ptr ip, uint64_t tId) noexcept { stackFramePtr = framePtr; returnAddress = ip; + threadId = tId; } inline frame_ptr AsyncStackRoot::getStackFramePointer() const noexcept { diff --git a/include/unifex/tracing/async_stack.hpp b/include/unifex/tracing/async_stack.hpp index ec7f67d6..e8702ce0 100644 --- a/include/unifex/tracing/async_stack.hpp +++ b/include/unifex/tracing/async_stack.hpp @@ -24,6 +24,15 @@ #include #include +#include + +#if defined(__APPLE__) +#define UNIFEX_SYS_gettid SYS_thread_selfid +#elif defined(SYS_gettid) +#define UNIFEX_SYS_gettid SYS_gettid +#else +#define UNIFEX_SYS_gettid __NR_gettid +#endif namespace unifex { @@ -150,7 +159,11 @@ struct AsyncStackRoot; struct AsyncStackFrame; namespace detail { class ScopedAsyncStackRoot; -} +} // namespace detail + +namespace utils { + uint64_t getOSThreadID(); +} // namespace utils // Get access to the current thread's top-most AsyncStackRoot. // @@ -454,7 +467,8 @@ struct AsyncStackRoot { // normal stack-trace. void setStackFrameContext( frame_ptr fp = frame_ptr::read_frame_pointer(), - instruction_ptr ip = instruction_ptr::read_return_address()) noexcept; + instruction_ptr ip = instruction_ptr::read_return_address(), + uint64_t tId = utils::getOSThreadID()) noexcept; frame_ptr getStackFramePointer() const noexcept; instruction_ptr getReturnAddress() const noexcept; @@ -501,6 +515,7 @@ struct AsyncStackRoot { // Typically initialise with instruction_ptr::read_return_address() or // setStackFrameContext(). instruction_ptr returnAddress; + uint64_t threadId = 0; }; namespace detail { diff --git a/source/async_stack.cpp b/source/async_stack.cpp index 9cc0465b..4a2b5a99 100644 --- a/source/async_stack.cpp +++ b/source/async_stack.cpp @@ -145,6 +145,20 @@ static thread_local AsyncStackRootHolder currentThreadAsyncStackRoot; } // namespace +namespace utils { + uint64_t getOSThreadID() { + #if defined(__APPLE__) + uint64_t tid; + pthread_threadid_np(nullptr, &tid); + return tid; + #elif defined(_WIN32) + return uint64_t(GetCurrentThreadId()); + #else + return uint64_t(syscall(UNIFEX_SYS_gettid)); + #endif + } +} + AsyncStackRoot* tryGetCurrentAsyncStackRoot() noexcept { return currentThreadAsyncStackRoot.get(); } From b001c9190e142d7f03bb22da2d37e76b1a0059ff Mon Sep 17 00:00:00 2001 From: "Jessica Wong (Seattle)" Date: Wed, 27 Nov 2024 13:40:40 -0800 Subject: [PATCH 02/14] Add syscall.h --- include/unifex/tracing/async_stack.hpp | 1 - source/async_stack.cpp | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/include/unifex/tracing/async_stack.hpp b/include/unifex/tracing/async_stack.hpp index e8702ce0..2d09b2ed 100644 --- a/include/unifex/tracing/async_stack.hpp +++ b/include/unifex/tracing/async_stack.hpp @@ -24,7 +24,6 @@ #include #include -#include #if defined(__APPLE__) #define UNIFEX_SYS_gettid SYS_thread_selfid diff --git a/source/async_stack.cpp b/source/async_stack.cpp index 4a2b5a99..3ff5361f 100644 --- a/source/async_stack.cpp +++ b/source/async_stack.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #if !defined(UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD) #if defined(__linux__) From d4fc0fe1ad556fff8d6e5389c3eb6d69a07f8b5b Mon Sep 17 00:00:00 2001 From: "Jessica Wong (Seattle)" Date: Wed, 27 Nov 2024 13:52:13 -0800 Subject: [PATCH 03/14] fix build --- include/unifex/tracing/async_stack.hpp | 3 +++ source/async_stack.cpp | 17 ++++++++--------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/include/unifex/tracing/async_stack.hpp b/include/unifex/tracing/async_stack.hpp index 2d09b2ed..26e5a4bd 100644 --- a/include/unifex/tracing/async_stack.hpp +++ b/include/unifex/tracing/async_stack.hpp @@ -24,6 +24,9 @@ #include #include +#if defined(__linux__) +#include +#endif #if defined(__APPLE__) #define UNIFEX_SYS_gettid SYS_thread_selfid diff --git a/source/async_stack.cpp b/source/async_stack.cpp index 3ff5361f..5856cb6d 100644 --- a/source/async_stack.cpp +++ b/source/async_stack.cpp @@ -19,7 +19,6 @@ #include #include #include -#include #if !defined(UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD) #if defined(__linux__) @@ -148,15 +147,15 @@ static thread_local AsyncStackRootHolder currentThreadAsyncStackRoot; namespace utils { uint64_t getOSThreadID() { - #if defined(__APPLE__) - uint64_t tid; - pthread_threadid_np(nullptr, &tid); - return tid; - #elif defined(_WIN32) - return uint64_t(GetCurrentThreadId()); - #else + #if defined(__APPLE__) + uint64_t tid; + pthread_threadid_np(nullptr, &tid); + return tid; + #elif defined(_WIN32) + return uint64_t(GetCurrentThreadId()); + #else return uint64_t(syscall(UNIFEX_SYS_gettid)); - #endif + #endif } } From 5511d716ebb9493fc3177b1ee30472fe64db9df1 Mon Sep 17 00:00:00 2001 From: "Jessica Wong (Seattle)" Date: Wed, 27 Nov 2024 13:54:12 -0800 Subject: [PATCH 04/14] add unistd.h --- include/unifex/tracing/async_stack.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/include/unifex/tracing/async_stack.hpp b/include/unifex/tracing/async_stack.hpp index 26e5a4bd..f2881c46 100644 --- a/include/unifex/tracing/async_stack.hpp +++ b/include/unifex/tracing/async_stack.hpp @@ -25,6 +25,7 @@ #include #if defined(__linux__) +#include #include #endif From 0f2ebe743cbf7bfee6d9a91238978fec06898893 Mon Sep 17 00:00:00 2001 From: "Jessica Wong (Seattle)" Date: Wed, 27 Nov 2024 14:02:12 -0800 Subject: [PATCH 05/14] Add processthreadsapi.h --- source/async_stack.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/source/async_stack.cpp b/source/async_stack.cpp index 5856cb6d..7047a60d 100644 --- a/source/async_stack.cpp +++ b/source/async_stack.cpp @@ -20,6 +20,10 @@ #include #include +#if defined(_WIN32) +#include +#endif + #if !defined(UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD) #if defined(__linux__) # define UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD 1 From 1f1289df40f1471943b56d97af0c0fb6b7fd6fff Mon Sep 17 00:00:00 2001 From: "Jessica Wong (Seattle)" Date: Wed, 27 Nov 2024 14:51:34 -0800 Subject: [PATCH 06/14] Add windows.h --- source/async_stack.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/async_stack.cpp b/source/async_stack.cpp index 7047a60d..af069d46 100644 --- a/source/async_stack.cpp +++ b/source/async_stack.cpp @@ -21,7 +21,7 @@ #include #if defined(_WIN32) -#include +#include #endif #if !defined(UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD) From 8370068594afdf193acd0251289f0ab61dbe0dfd Mon Sep 17 00:00:00 2001 From: "Jessica Wong (Seattle)" Date: Mon, 2 Dec 2024 13:07:36 -0800 Subject: [PATCH 07/14] address comments --- include/unifex/tracing/async_stack-inl.hpp | 2 +- include/unifex/tracing/async_stack.hpp | 18 +++--------------- source/async_stack.cpp | 10 ++++++---- 3 files changed, 10 insertions(+), 20 deletions(-) diff --git a/include/unifex/tracing/async_stack-inl.hpp b/include/unifex/tracing/async_stack-inl.hpp index 99e84987..da316a5b 100644 --- a/include/unifex/tracing/async_stack-inl.hpp +++ b/include/unifex/tracing/async_stack-inl.hpp @@ -136,7 +136,7 @@ inline AsyncStackFrame* AsyncStackRoot::getTopFrame() const noexcept { } inline void AsyncStackRoot::setStackFrameContext( - frame_ptr framePtr, instruction_ptr ip, uint64_t tId) noexcept { + frame_ptr framePtr, instruction_ptr ip, std::uint64_t tId) noexcept { stackFramePtr = framePtr; returnAddress = ip; threadId = tId; diff --git a/include/unifex/tracing/async_stack.hpp b/include/unifex/tracing/async_stack.hpp index f2881c46..de59c07a 100644 --- a/include/unifex/tracing/async_stack.hpp +++ b/include/unifex/tracing/async_stack.hpp @@ -24,18 +24,6 @@ #include #include -#if defined(__linux__) -#include -#include -#endif - -#if defined(__APPLE__) -#define UNIFEX_SYS_gettid SYS_thread_selfid -#elif defined(SYS_gettid) -#define UNIFEX_SYS_gettid SYS_gettid -#else -#define UNIFEX_SYS_gettid __NR_gettid -#endif namespace unifex { @@ -165,7 +153,7 @@ class ScopedAsyncStackRoot; } // namespace detail namespace utils { - uint64_t getOSThreadID(); + uint64_t get_os_thread_id(); } // namespace utils // Get access to the current thread's top-most AsyncStackRoot. @@ -471,7 +459,7 @@ struct AsyncStackRoot { void setStackFrameContext( frame_ptr fp = frame_ptr::read_frame_pointer(), instruction_ptr ip = instruction_ptr::read_return_address(), - uint64_t tId = utils::getOSThreadID()) noexcept; + std::uint64_t tId = utils::get_os_thread_id()) noexcept; frame_ptr getStackFramePointer() const noexcept; instruction_ptr getReturnAddress() const noexcept; @@ -518,7 +506,7 @@ struct AsyncStackRoot { // Typically initialise with instruction_ptr::read_return_address() or // setStackFrameContext(). instruction_ptr returnAddress; - uint64_t threadId = 0; + std::uint64_t threadId = 0; }; namespace detail { diff --git a/source/async_stack.cpp b/source/async_stack.cpp index af069d46..c072332f 100644 --- a/source/async_stack.cpp +++ b/source/async_stack.cpp @@ -22,6 +22,8 @@ #if defined(_WIN32) #include +#elif defined(__linux__) +#include #endif #if !defined(UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD) @@ -150,15 +152,15 @@ static thread_local AsyncStackRootHolder currentThreadAsyncStackRoot; } // namespace namespace utils { - uint64_t getOSThreadID() { + std::uint64_t get_os_thread_id() { #if defined(__APPLE__) - uint64_t tid; + std::uint64_t tid; pthread_threadid_np(nullptr, &tid); return tid; #elif defined(_WIN32) - return uint64_t(GetCurrentThreadId()); + return std::uint64_t(GetCurrentThreadId()); #else - return uint64_t(syscall(UNIFEX_SYS_gettid)); + return std::uint64_t(gettid()) #endif } } From e1b8e2572e24afcae13bc27cddcb745d370710d9 Mon Sep 17 00:00:00 2001 From: "Jessica Wong (Seattle)" Date: Mon, 2 Dec 2024 13:12:06 -0800 Subject: [PATCH 08/14] address comments --- include/unifex/tracing/async_stack.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/unifex/tracing/async_stack.hpp b/include/unifex/tracing/async_stack.hpp index de59c07a..a6ebf474 100644 --- a/include/unifex/tracing/async_stack.hpp +++ b/include/unifex/tracing/async_stack.hpp @@ -153,7 +153,7 @@ class ScopedAsyncStackRoot; } // namespace detail namespace utils { - uint64_t get_os_thread_id(); + std::uint64_t get_os_thread_id(); } // namespace utils // Get access to the current thread's top-most AsyncStackRoot. From 650c043f3b7daa450837598fa749f4a6de2edf67 Mon Sep 17 00:00:00 2001 From: "Jessica Wong (Seattle)" Date: Mon, 2 Dec 2024 13:14:14 -0800 Subject: [PATCH 09/14] fix build --- source/async_stack.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/async_stack.cpp b/source/async_stack.cpp index c072332f..e2214e26 100644 --- a/source/async_stack.cpp +++ b/source/async_stack.cpp @@ -160,7 +160,7 @@ namespace utils { #elif defined(_WIN32) return std::uint64_t(GetCurrentThreadId()); #else - return std::uint64_t(gettid()) + return std::uint64_t(gettid()); #endif } } From 9002efa224bf7803bc337dbb11bb3df85603c331 Mon Sep 17 00:00:00 2001 From: "Jessica Wong (Seattle)" Date: Wed, 4 Dec 2024 09:32:14 -0800 Subject: [PATCH 10/14] move threadid to AsyncStackRootHolder --- include/unifex/tracing/async_stack-inl.hpp | 3 +- include/unifex/tracing/async_stack.hpp | 8 +----- source/async_stack.cpp | 32 ++++++++++++---------- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/include/unifex/tracing/async_stack-inl.hpp b/include/unifex/tracing/async_stack-inl.hpp index da316a5b..add04bde 100644 --- a/include/unifex/tracing/async_stack-inl.hpp +++ b/include/unifex/tracing/async_stack-inl.hpp @@ -136,10 +136,9 @@ inline AsyncStackFrame* AsyncStackRoot::getTopFrame() const noexcept { } inline void AsyncStackRoot::setStackFrameContext( - frame_ptr framePtr, instruction_ptr ip, std::uint64_t tId) noexcept { + frame_ptr framePtr, instruction_ptr ip) noexcept { stackFramePtr = framePtr; returnAddress = ip; - threadId = tId; } inline frame_ptr AsyncStackRoot::getStackFramePointer() const noexcept { diff --git a/include/unifex/tracing/async_stack.hpp b/include/unifex/tracing/async_stack.hpp index a6ebf474..8b2cdac3 100644 --- a/include/unifex/tracing/async_stack.hpp +++ b/include/unifex/tracing/async_stack.hpp @@ -152,10 +152,6 @@ namespace detail { class ScopedAsyncStackRoot; } // namespace detail -namespace utils { - std::uint64_t get_os_thread_id(); -} // namespace utils - // Get access to the current thread's top-most AsyncStackRoot. // // Returns nullptr if there is no active AsyncStackRoot. @@ -458,8 +454,7 @@ struct AsyncStackRoot { // normal stack-trace. void setStackFrameContext( frame_ptr fp = frame_ptr::read_frame_pointer(), - instruction_ptr ip = instruction_ptr::read_return_address(), - std::uint64_t tId = utils::get_os_thread_id()) noexcept; + instruction_ptr ip = instruction_ptr::read_return_address()) noexcept; frame_ptr getStackFramePointer() const noexcept; instruction_ptr getReturnAddress() const noexcept; @@ -506,7 +501,6 @@ struct AsyncStackRoot { // Typically initialise with instruction_ptr::read_return_address() or // setStackFrameContext(). instruction_ptr returnAddress; - std::uint64_t threadId = 0; }; namespace detail { diff --git a/source/async_stack.cpp b/source/async_stack.cpp index e2214e26..c32262d7 100644 --- a/source/async_stack.cpp +++ b/source/async_stack.cpp @@ -93,6 +93,20 @@ extern "C" { } #endif // UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD == 0 +namespace utils { + static std::uint64_t get_os_thread_id() { + #if defined(__APPLE__) + std::uint64_t tid; + pthread_threadid_np(nullptr, &tid); + return tid; + #elif defined(_WIN32) + return std::uint64_t(GetCurrentThreadId()); + #else + return std::uint64_t(gettid()); + #endif + } +} // namespace utils + namespace { // Folly's async stack library uses Folly's benchmarking tools to force a @@ -123,6 +137,7 @@ struct AsyncStackRootHolder { UNIFEX_ASSERT(result == 0); #else kUnifexAsyncStackRootHolderList->add(this); + threadId = utils::get_os_thread_id(); #endif } @@ -145,26 +160,15 @@ struct AsyncStackRootHolder { } std::atomic value{nullptr}; +#if !UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD + std::uint64_t threadId = 0; +#endif }; static thread_local AsyncStackRootHolder currentThreadAsyncStackRoot; } // namespace -namespace utils { - std::uint64_t get_os_thread_id() { - #if defined(__APPLE__) - std::uint64_t tid; - pthread_threadid_np(nullptr, &tid); - return tid; - #elif defined(_WIN32) - return std::uint64_t(GetCurrentThreadId()); - #else - return std::uint64_t(gettid()); - #endif - } -} - AsyncStackRoot* tryGetCurrentAsyncStackRoot() noexcept { return currentThreadAsyncStackRoot.get(); } From 3b1b1d853acc9851708cd5a91b8348646a1ae773 Mon Sep 17 00:00:00 2001 From: "Jessica Wong (Seattle)" Date: Wed, 4 Dec 2024 09:43:40 -0800 Subject: [PATCH 11/14] run files through clang-format --- include/unifex/tracing/async_stack.hpp | 6 +- source/async_stack.cpp | 129 +++++++++++++------------ 2 files changed, 69 insertions(+), 66 deletions(-) diff --git a/include/unifex/tracing/async_stack.hpp b/include/unifex/tracing/async_stack.hpp index 8b2cdac3..fc6fff35 100644 --- a/include/unifex/tracing/async_stack.hpp +++ b/include/unifex/tracing/async_stack.hpp @@ -150,7 +150,7 @@ struct AsyncStackRoot; struct AsyncStackFrame; namespace detail { class ScopedAsyncStackRoot; -} // namespace detail +} // namespace detail // Get access to the current thread's top-most AsyncStackRoot. // @@ -321,9 +321,7 @@ struct frame_ptr { return frame_ptr{p}; } - explicit operator void*() const noexcept { - return p_; - } + explicit operator void*() const noexcept { return p_; } private: void* p_; diff --git a/source/async_stack.cpp b/source/async_stack.cpp index c32262d7..89bcf7cb 100644 --- a/source/async_stack.cpp +++ b/source/async_stack.cpp @@ -20,19 +20,13 @@ #include #include -#if defined(_WIN32) -#include -#elif defined(__linux__) -#include -#endif - #if !defined(UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD) -#if defined(__linux__) -# define UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD 1 -#else +# if defined(__linux__) +# define UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD 1 +# else // defaults to using vector to store AsyncStackRoots instead of a pthread key -# define UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD 0 -#endif +# define UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD 0 +# endif #endif #if UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD @@ -47,9 +41,14 @@ extern "C" { // Initialise to some value that will be interpreted as an invalid key. inline pthread_key_t folly_async_stack_root_tls_key = 0xFFFF'FFFFu; } -#else -# include -#endif //UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD +#else +# include +# if defined(_WIN32) +# include +# elif defined(__linux__) +# include +# endif // defined(_WIN32) +#endif // UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD namespace unifex { @@ -64,48 +63,51 @@ struct AsyncStackRootHolderList { asyncStackRootHolders_.reserve(50); } -void add(void* holder) { - std::lock_guard lock(mutex_); - asyncStackRootHolders_.push_back(holder); -} + void add(void* holder) { + std::lock_guard lock(mutex_); + asyncStackRootHolders_.push_back(holder); + } -void remove(void* holder) { - std::lock_guard lock(mutex_); - auto it = std::find(asyncStackRootHolders_.begin(), asyncStackRootHolders_.end(), holder); - if (it != asyncStackRootHolders_.end()) { - std::swap(*it, asyncStackRootHolders_.back()); - asyncStackRootHolders_.pop_back(); + void remove(void* holder) { + std::lock_guard lock(mutex_); + auto it = std::find( + asyncStackRootHolders_.begin(), asyncStackRootHolders_.end(), holder); + if (it != asyncStackRootHolders_.end()) { + std::swap(*it, asyncStackRootHolders_.back()); + asyncStackRootHolders_.pop_back(); + } } -} -std::vector getAsyncStackRoots() { - if (!mutex_.try_lock()) { - // assume we crashed holding the lock and give up - return {}; + std::vector getAsyncStackRoots() { + if (!mutex_.try_lock()) { + // assume we crashed holding the lock and give up + return {}; + } + std::lock_guard lock(mutex_, std::adopt_lock); + return asyncStackRootHolders_; } - std::lock_guard lock(mutex_, std::adopt_lock); - return asyncStackRootHolders_; -} }; extern "C" { - auto* kUnifexAsyncStackRootHolderList = new AsyncStackRootHolderList(); +auto* kUnifexAsyncStackRootHolderList = new AsyncStackRootHolderList(); } -#endif // UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD == 0 +#endif // UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD == 0 +#if !UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD namespace utils { - static std::uint64_t get_os_thread_id() { - #if defined(__APPLE__) - std::uint64_t tid; - pthread_threadid_np(nullptr, &tid); - return tid; - #elif defined(_WIN32) - return std::uint64_t(GetCurrentThreadId()); - #else - return std::uint64_t(gettid()); - #endif - } -} // namespace utils +static std::uint64_t get_os_thread_id() { +# if defined(__APPLE__) + std::uint64_t tid; + pthread_threadid_np(nullptr, &tid); + return tid; +# elif defined(_WIN32) + return std::uint64_t(GetCurrentThreadId()); +# else + return std::uint64_t(gettid()); +# endif +} +} // namespace utils +#endif // !UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD namespace { @@ -128,24 +130,23 @@ static void ensureAsyncRootTlsKeyIsInitialised() noexcept { #endif struct AsyncStackRootHolder { - AsyncStackRootHolder() noexcept { - #if UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD - ensureAsyncRootTlsKeyIsInitialised(); - [[maybe_unused]] const int result = - pthread_setspecific(folly_async_stack_root_tls_key, this); - UNIFEX_ASSERT(result == 0); - #else - kUnifexAsyncStackRootHolderList->add(this); - threadId = utils::get_os_thread_id(); - #endif +#if UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD + ensureAsyncRootTlsKeyIsInitialised(); + [[maybe_unused]] const int result = + pthread_setspecific(folly_async_stack_root_tls_key, this); + UNIFEX_ASSERT(result == 0); +#else + kUnifexAsyncStackRootHolderList->add(this); + threadId = unifex::utils::get_os_thread_id(); +#endif } - #if !UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD - ~AsyncStackRootHolder() noexcept { - kUnifexAsyncStackRootHolderList->remove(this); - } - #endif +#if !UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD + ~AsyncStackRootHolder() noexcept { + kUnifexAsyncStackRootHolderList->remove(this); + } +#endif AsyncStackRoot* get() const noexcept { return value.load(std::memory_order_relaxed); @@ -159,6 +160,10 @@ struct AsyncStackRootHolder { value.store(root, std::memory_order_relaxed); } +#if !UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD + std::uint64_t get_thread_id() const noexcept { return threadId; } +#endif + std::atomic value{nullptr}; #if !UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD std::uint64_t threadId = 0; @@ -244,6 +249,6 @@ UNIFEX_NO_INLINE void resumeCoroutineWithNewAsyncStackRoot( h.resume(); } -#endif // FOLLY_HAS_COROUTINES +#endif // !UNIFEX_NO_COROUTINES } // namespace unifex From 6f28bfeb3ba64a16fbaf0000b3b0d1e1c13ada1f Mon Sep 17 00:00:00 2001 From: "Jessica Wong (Seattle)" Date: Wed, 4 Dec 2024 09:49:04 -0800 Subject: [PATCH 12/14] reduce the number of ifdefs --- source/async_stack.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/source/async_stack.cpp b/source/async_stack.cpp index 89bcf7cb..89d4df2f 100644 --- a/source/async_stack.cpp +++ b/source/async_stack.cpp @@ -91,9 +91,7 @@ struct AsyncStackRootHolderList { extern "C" { auto* kUnifexAsyncStackRootHolderList = new AsyncStackRootHolderList(); } -#endif // UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD == 0 -#if !UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD namespace utils { static std::uint64_t get_os_thread_id() { # if defined(__APPLE__) @@ -107,7 +105,7 @@ static std::uint64_t get_os_thread_id() { # endif } } // namespace utils -#endif // !UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD +#endif // UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD == 0 namespace { @@ -146,6 +144,9 @@ struct AsyncStackRootHolder { ~AsyncStackRootHolder() noexcept { kUnifexAsyncStackRootHolderList->remove(this); } + + std::uint64_t get_thread_id() const noexcept { return threadId; } + #endif AsyncStackRoot* get() const noexcept { @@ -160,10 +161,6 @@ struct AsyncStackRootHolder { value.store(root, std::memory_order_relaxed); } -#if !UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD - std::uint64_t get_thread_id() const noexcept { return threadId; } -#endif - std::atomic value{nullptr}; #if !UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD std::uint64_t threadId = 0; From 4f9f5f3f99d57f00004dba092d868fd75dcc4b84 Mon Sep 17 00:00:00 2001 From: jesswong <5247012+jesswong@users.noreply.github.com> Date: Wed, 4 Dec 2024 10:37:23 -0800 Subject: [PATCH 13/14] Remove external dir (#645) --- external/mpark-wg21 | 1 - 1 file changed, 1 deletion(-) delete mode 160000 external/mpark-wg21 diff --git a/external/mpark-wg21 b/external/mpark-wg21 deleted file mode 160000 index c556c469..00000000 --- a/external/mpark-wg21 +++ /dev/null @@ -1 +0,0 @@ -Subproject commit c556c469e19966ea65e8b5729b192f4c71838cf6 From af9b136c436c81b85c16025f6efdf8f19122b31c Mon Sep 17 00:00:00 2001 From: "Jessica Wong (Seattle)" Date: Wed, 4 Dec 2024 10:42:11 -0800 Subject: [PATCH 14/14] remove utils namespace --- source/async_stack.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/source/async_stack.cpp b/source/async_stack.cpp index 89d4df2f..f63d9e8b 100644 --- a/source/async_stack.cpp +++ b/source/async_stack.cpp @@ -92,7 +92,6 @@ extern "C" { auto* kUnifexAsyncStackRootHolderList = new AsyncStackRootHolderList(); } -namespace utils { static std::uint64_t get_os_thread_id() { # if defined(__APPLE__) std::uint64_t tid; @@ -104,7 +103,6 @@ static std::uint64_t get_os_thread_id() { return std::uint64_t(gettid()); # endif } -} // namespace utils #endif // UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD == 0 namespace { @@ -136,7 +134,7 @@ struct AsyncStackRootHolder { UNIFEX_ASSERT(result == 0); #else kUnifexAsyncStackRootHolderList->add(this); - threadId = unifex::utils::get_os_thread_id(); + threadId = unifex::get_os_thread_id(); #endif }