summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam McCall <sam.mccall@gmail.com>2018-02-01 10:01:25 +0000
committerSam McCall <sam.mccall@gmail.com>2018-02-01 10:01:25 +0000
commite0a3dec9fbf62ecb891f61e0f773e8e38180ea0c (patch)
treef41c796e29c89ed020d3df2fca00404efabc78b5
parent705e26a24310d046bd6c3d29b72f30af045e4a1f (diff)
downloadbcm5719-llvm-e0a3dec9fbf62ecb891f61e0f773e8e38180ea0c.tar.gz
bcm5719-llvm-e0a3dec9fbf62ecb891f61e0f773e8e38180ea0c.zip
[clangd] Use pthread instead of thread_local to support more runtimes.
Summary: thread_local has nice syntax and semantics, but requires __cxa_thread_atexit, and some not-ancient runtime libraries don't provide it. The clang-x86_64-linux-selfhost-modules buildbot is one example :-) It works on windows, and the other platforms clang-tools-extra supports should all have the relevant pthread API. So we just use that if it's available, falling back to thread_local (so if a platform has neither, we'll fail to link). The fallback should really be the other way, that would require cmake changes. Reviewers: ilya-biryukov, bkramer Subscribers: klimek, jkorous-apple, ioeric, cfe-commits Differential Revision: https://reviews.llvm.org/D42742 llvm-svn: 323949
-rw-r--r--clang-tools-extra/clangd/Context.cpp47
1 files changed, 40 insertions, 7 deletions
diff --git a/clang-tools-extra/clangd/Context.cpp b/clang-tools-extra/clangd/Context.cpp
index 5a161216a25..0a1bacd7061 100644
--- a/clang-tools-extra/clangd/Context.cpp
+++ b/clang-tools-extra/clangd/Context.cpp
@@ -8,8 +8,48 @@
//===---------------------------------------------------------------------===//
#include "Context.h"
+#include "llvm/Config/config.h"
#include <cassert>
+// 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 <pthread.h>
+static clang::clangd::Context &currentContext() {
+ 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<Context *>(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<Context *>(Ctx);
+}
+#else
+// Only supported platform without pthread is windows, and thread_local works.
+static clang::clangd::Context &currentContext() {
+ static thread_local auto C = clang::clangd::Context::empty();
+ return C;
+}
+#endif
+
namespace clang {
namespace clangd {
@@ -20,13 +60,6 @@ Context::Context(std::shared_ptr<const Data> DataPtr)
Context Context::clone() const { return Context(DataPtr); }
-// The thread-local Context is scoped in a function to avoid
-// initialization-order issues. It's created when first needed.
-static Context &currentContext() {
- static thread_local Context C = Context::empty();
- return C;
-}
-
const Context &Context::current() { return currentContext(); }
Context Context::swapCurrent(Context Replacement) {
OpenPOWER on IntegriCloud