Unverified Commit f17d11aa authored by Sergiu Deitsch's avatar Sergiu Deitsch Committed by GitHub

Merge pull request #161 from mpercy/thread-local-libunwind-2

Use thread local for libunwind GetStackTrace() reentrancy protection
parents 1227cf4d f581614a
...@@ -49,7 +49,9 @@ _START_GOOGLE_NAMESPACE_ ...@@ -49,7 +49,9 @@ _START_GOOGLE_NAMESPACE_
// recursive request, we'd end up with infinite recursion or deadlock. // recursive request, we'd end up with infinite recursion or deadlock.
// Luckily, it's safe to ignore those subsequent traces. In such // Luckily, it's safe to ignore those subsequent traces. In such
// cases, we return 0 to indicate the situation. // cases, we return 0 to indicate the situation.
static bool g_now_entering = false; // We can use the GCC __thread syntax here since libunwind is not supported on
// Windows.
static __thread bool g_tl_entered; // Initialized to false.
// If you change this function, also change GetStackFrames below. // If you change this function, also change GetStackFrames below.
int GetStackTrace(void** result, int max_depth, int skip_count) { int GetStackTrace(void** result, int max_depth, int skip_count) {
...@@ -58,9 +60,10 @@ int GetStackTrace(void** result, int max_depth, int skip_count) { ...@@ -58,9 +60,10 @@ int GetStackTrace(void** result, int max_depth, int skip_count) {
unw_cursor_t cursor; unw_cursor_t cursor;
unw_context_t uc; unw_context_t uc;
if (sync_val_compare_and_swap(&g_now_entering, false, true)) { if (g_tl_entered) {
return 0; return 0;
} }
g_tl_entered = true;
unw_getcontext(&uc); unw_getcontext(&uc);
RAW_CHECK(unw_init_local(&cursor, &uc) >= 0, "unw_init_local failed"); RAW_CHECK(unw_init_local(&cursor, &uc) >= 0, "unw_init_local failed");
...@@ -80,7 +83,7 @@ int GetStackTrace(void** result, int max_depth, int skip_count) { ...@@ -80,7 +83,7 @@ int GetStackTrace(void** result, int max_depth, int skip_count) {
break; break;
} }
g_now_entering = false; g_tl_entered = false;
return n; return n;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment