From 3aecc7fd451c69b2aec829b0ad70c28772c348c3 Mon Sep 17 00:00:00 2001 From: Sam McCall Date: Tue, 6 Feb 2018 14:25:02 +0000 Subject: [PATCH] [clangd] Don't try pthread, just use thread_local. Reverts r323949. The pthread solution here breaks standalone builds, which don't have the relevant cmake magic for feature-detection. The original reason for trying pthread was fear of libgcc without support for thread_local (e.g. on the clang-x86_64-linux-selfhost-modules bot). However the earliest supported GCC is 4.8, and this has __cxa_thread_atexit. This will probably break that bot, it's not running a supported GCC and needs to be upgraded. I'll try to find out how to do this. llvm-svn: 324351 --- clang-tools-extra/clangd/Context.cpp | 46 +++------------------------- 1 file changed, 5 insertions(+), 41 deletions(-) diff --git a/clang-tools-extra/clangd/Context.cpp b/clang-tools-extra/clangd/Context.cpp index 23006778f550..7ecaced52c48 100644 --- a/clang-tools-extra/clangd/Context.cpp +++ b/clang-tools-extra/clangd/Context.cpp @@ -8,49 +8,8 @@ //===---------------------------------------------------------------------===// #include "Context.h" -#include "llvm/Config/config.h" #include -// The thread-local Context is scoped in a function to avoid init-order issues. -// It's created by currentContext() when first needed. - -#ifdef HAVE_PTHREAD_GETSPECIFIC -// We'd love to use thread_local everywhere. -// It requires support from the runtime: __cxa_thread_atexit. -// Rather than detect this, we use the pthread API where available. -#include -#include -static clang::clangd::Context ¤tContext() { - using clang::clangd::Context; - static pthread_key_t CtxKey; - - // Once (across threads), set up pthread TLS for Context, and its destructor. - static int Dummy = [] { // Create key only once, for all threads. - if (auto Err = pthread_key_create(&CtxKey, /*destructor=*/+[](void *Ctx) { - delete reinterpret_cast(Ctx); - })) - llvm_unreachable(strerror(Err)); - return 0; - }(); - (void)Dummy; - - // Now grab the current context from TLS, and create it if it doesn't exist. - void *Ctx = pthread_getspecific(CtxKey); - if (!Ctx) { - Ctx = new Context(); - if (auto Err = pthread_setspecific(CtxKey, Ctx)) - llvm_unreachable(strerror(Err)); - } - return *reinterpret_cast(Ctx); -} -#else -// Only supported platform without pthread is windows, and thread_local works. -static clang::clangd::Context ¤tContext() { - static thread_local auto C = clang::clangd::Context::empty(); - return C; -} -#endif - namespace clang { namespace clangd { @@ -61,6 +20,11 @@ Context::Context(std::shared_ptr DataPtr) Context Context::clone() const { return Context(DataPtr); } +static Context ¤tContext() { + static thread_local auto C = Context::empty(); + return C; +} + const Context &Context::current() { return currentContext(); } Context Context::swapCurrent(Context Replacement) { -- 2.34.1