[libc] Add macro LIBC_THREAD_LOCAL.
It resolves to thread_local on all platform except for the GPUs on which
it resolves to nothing. The use of thread_local in the source code has been
replaced with the new macro.
Reviewed By: jhuber6
Differential Revision: https://reviews.llvm.org/D151486
GitOrigin-RevId: daeee56798c51e8f007e8e8e6e677a420b14c9ef
diff --git a/src/__support/StringUtil/CMakeLists.txt b/src/__support/StringUtil/CMakeLists.txt
index 52daa30..c053966 100644
--- a/src/__support/StringUtil/CMakeLists.txt
+++ b/src/__support/StringUtil/CMakeLists.txt
@@ -44,6 +44,7 @@
DEPENDS
.message_mapper
.platform_errors
+ libc.src.__support.common
libc.src.__support.CPP.span
libc.src.__support.CPP.string_view
libc.src.__support.CPP.stringstream
@@ -60,6 +61,7 @@
.message_mapper
.platform_signals
libc.include.signal
+ libc.src.__support.common
libc.src.__support.CPP.span
libc.src.__support.CPP.string_view
libc.src.__support.CPP.stringstream
diff --git a/src/__support/StringUtil/error_to_string.cpp b/src/__support/StringUtil/error_to_string.cpp
index 193d7d0..9662492 100644
--- a/src/__support/StringUtil/error_to_string.cpp
+++ b/src/__support/StringUtil/error_to_string.cpp
@@ -14,6 +14,7 @@
#include "src/__support/CPP/stringstream.h"
#include "src/__support/StringUtil/message_mapper.h"
#include "src/__support/integer_to_string.h"
+#include "src/__support/macros/attributes.h"
#include <stddef.h>
@@ -31,7 +32,7 @@
// This is to hold error strings that have to be custom built. It may be
// rewritten on every call to strerror (or other error to string function).
constexpr size_t ERR_BUFFER_SIZE = max_buff_size();
-thread_local char error_buffer[ERR_BUFFER_SIZE];
+LIBC_THREAD_LOCAL char error_buffer[ERR_BUFFER_SIZE];
constexpr size_t TOTAL_STR_LEN = total_str_len(PLATFORM_ERRORS);
diff --git a/src/__support/StringUtil/signal_to_string.cpp b/src/__support/StringUtil/signal_to_string.cpp
index c14123a..c3610de 100644
--- a/src/__support/StringUtil/signal_to_string.cpp
+++ b/src/__support/StringUtil/signal_to_string.cpp
@@ -14,6 +14,7 @@
#include "src/__support/CPP/stringstream.h"
#include "src/__support/StringUtil/message_mapper.h"
#include "src/__support/integer_to_string.h"
+#include "src/__support/macros/attributes.h"
#include <signal.h>
#include <stddef.h>
@@ -32,7 +33,7 @@
// This is to hold signal strings that have to be custom built. It may be
// rewritten on every call to strsignal (or other signal to string function).
constexpr size_t SIG_BUFFER_SIZE = max_buff_size();
-thread_local char signal_buffer[SIG_BUFFER_SIZE];
+LIBC_THREAD_LOCAL char signal_buffer[SIG_BUFFER_SIZE];
constexpr size_t TOTAL_STR_LEN = total_str_len(PLATFORM_SIGNALS);
diff --git a/src/__support/macros/attributes.h b/src/__support/macros/attributes.h
index 9a2fcd2..38b6b7f 100644
--- a/src/__support/macros/attributes.h
+++ b/src/__support/macros/attributes.h
@@ -17,8 +17,16 @@
#ifndef LLVM_LIBC_SUPPORT_MACROS_ATTRIBUTES_H
#define LLVM_LIBC_SUPPORT_MACROS_ATTRIBUTES_H
+#include "properties/architectures.h"
+
#define LIBC_INLINE inline
#define LIBC_INLINE_ASM __asm__ __volatile__
#define LIBC_UNUSED __attribute__((unused))
+#ifdef LIBC_TARGET_ARCH_IS_GPU
+#define LIBC_THREAD_LOCAL
+#else
+#define LIBC_THREAD_LOCAL thread_local
+#endif
+
#endif // LLVM_LIBC_SUPPORT_MACROS_ATTRIBUTES_H
diff --git a/src/__support/threads/CMakeLists.txt b/src/__support/threads/CMakeLists.txt
index b77eb0d..0feeda0 100644
--- a/src/__support/threads/CMakeLists.txt
+++ b/src/__support/threads/CMakeLists.txt
@@ -48,6 +48,7 @@
DEPENDS
.mutex
.${LIBC_TARGET_OS}.thread
+ libc.src.__support.common
libc.src.__support.fixedvector
libc.src.__support.CPP.array
libc.src.__support.CPP.optional
diff --git a/src/__support/threads/thread.cpp b/src/__support/threads/thread.cpp
index 03e93a4..3a6f30f 100644
--- a/src/__support/threads/thread.cpp
+++ b/src/__support/threads/thread.cpp
@@ -12,10 +12,11 @@
#include "src/__support/CPP/array.h"
#include "src/__support/CPP/optional.h"
#include "src/__support/fixedvector.h"
+#include "src/__support/macros/attributes.h"
namespace __llvm_libc {
-thread_local Thread self;
+LIBC_THREAD_LOCAL Thread self;
namespace {
@@ -99,7 +100,7 @@
: active(true), payload(p), dtor(d) {}
};
-static thread_local cpp::array<TSSValueUnit, TSS_KEY_COUNT> tss_values;
+static LIBC_THREAD_LOCAL cpp::array<TSSValueUnit, TSS_KEY_COUNT> tss_values;
} // anonymous namespace
@@ -128,7 +129,7 @@
}
};
-static thread_local ThreadAtExitCallbackMgr atexit_callback_mgr;
+static LIBC_THREAD_LOCAL ThreadAtExitCallbackMgr atexit_callback_mgr;
// The function __cxa_thread_atexit is provided by C++ runtimes like libcxxabi.
// It is used by thread local object runtime to register destructor calls. To
diff --git a/src/__support/threads/thread.h b/src/__support/threads/thread.h
index 4918163..0c88909 100644
--- a/src/__support/threads/thread.h
+++ b/src/__support/threads/thread.h
@@ -13,6 +13,7 @@
#include "src/__support/CPP/optional.h"
#include "src/__support/CPP/string_view.h"
#include "src/__support/CPP/stringstream.h"
+#include "src/__support/macros/attributes.h"
#include "src/__support/macros/properties/architectures.h"
#include <linux/param.h> // for exec_pagesize.
@@ -225,7 +226,7 @@
int get_name(cpp::StringStream &name) const;
};
-extern thread_local Thread self;
+extern LIBC_THREAD_LOCAL Thread self;
// Platforms should implement this function.
[[noreturn]] void thread_exit(ThreadReturnValue retval, ThreadStyle style);
diff --git a/src/errno/CMakeLists.txt b/src/errno/CMakeLists.txt
index 2fa1381..e8868dc 100644
--- a/src/errno/CMakeLists.txt
+++ b/src/errno/CMakeLists.txt
@@ -6,4 +6,5 @@
libc_errno.h # Include this
DEPENDS
libc.include.errno
+ libc.src.__support.common
)
diff --git a/src/errno/libc_errno.cpp b/src/errno/libc_errno.cpp
index a52e3af..005d9a9 100644
--- a/src/errno/libc_errno.cpp
+++ b/src/errno/libc_errno.cpp
@@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//
+#include "src/__support/macros/attributes.h"
#include "src/__support/macros/properties/architectures.h"
namespace __llvm_libc {
@@ -29,13 +30,13 @@
#ifdef LIBC_TARGET_ARCH_IS_GPU
ErrnoConsumer __llvmlibc_errno;
#else
-thread_local int __llvmlibc_errno;
+LIBC_THREAD_LOCAL int __llvmlibc_errno;
#endif // LIBC_TARGET_ARCH_IS_GPU
#else
#ifdef LIBC_TARGET_ARCH_IS_GPU
ErrnoConsumer __llvmlibc_internal_errno;
#else
-thread_local int __llvmlibc_internal_errno;
+LIBC_THREAD_LOCAL int __llvmlibc_internal_errno;
#endif // LIBC_TARGET_ARCH_IS_GPU
#endif
} // extern "C"
diff --git a/src/errno/libc_errno.h b/src/errno/libc_errno.h
index 585da15..0c473a5 100644
--- a/src/errno/libc_errno.h
+++ b/src/errno/libc_errno.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H
#define LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H
+#include "src/__support/macros/attributes.h"
#include "src/__support/macros/properties/architectures.h"
#include <errno.h>
@@ -42,7 +43,7 @@
extern "C" ErrnoConsumer __llvmlibc_internal_errno;
#else // LIBC_TARGET_ARCH_IS_GPU
extern "C" {
-extern thread_local int __llvmlibc_internal_errno;
+extern LIBC_THREAD_LOCAL int __llvmlibc_internal_errno;
} // extern "C"
#endif
diff --git a/src/stdlib/CMakeLists.txt b/src/stdlib/CMakeLists.txt
index 8067eeb..bffa45f 100644
--- a/src/stdlib/CMakeLists.txt
+++ b/src/stdlib/CMakeLists.txt
@@ -218,6 +218,8 @@
rand_util.cpp
HDRS
rand_util.h
+ DEPENDS
+ libc.src.__support.common
)
add_entrypoint_object(
diff --git a/src/stdlib/rand_util.cpp b/src/stdlib/rand_util.cpp
index afa6662..9c29eb8 100644
--- a/src/stdlib/rand_util.cpp
+++ b/src/stdlib/rand_util.cpp
@@ -7,9 +7,10 @@
//===----------------------------------------------------------------------===//
#include "src/stdlib/rand_util.h"
+#include "src/__support/macros/attributes.h"
namespace __llvm_libc {
-thread_local unsigned long rand_next;
+LIBC_THREAD_LOCAL unsigned long rand_next;
} // namespace __llvm_libc
diff --git a/src/stdlib/rand_util.h b/src/stdlib/rand_util.h
index 6179406..4cbfa14 100644
--- a/src/stdlib/rand_util.h
+++ b/src/stdlib/rand_util.h
@@ -9,9 +9,11 @@
#ifndef LLVM_LIBC_SRC_STDLIB_RAND_UTIL_H
#define LLVM_LIBC_SRC_STDLIB_RAND_UTIL_H
+#include "src/__support/macros/attributes.h"
+
namespace __llvm_libc {
-extern thread_local unsigned long rand_next;
+extern LIBC_THREAD_LOCAL unsigned long rand_next;
} // namespace __llvm_libc