Skip to content

Commit 0b16395

Browse files
authored
[libc] Change LIBC_THREAD_LOCAL to be dependent on LIBC_THREAD_MODE (#151527)
When single-threaded mode is selected, all instances of the keyword `LIBC_THREAD_LOCAL` are stubbed out, similar to how it currently works on the GPU. This allows baremetal builds to avoid using thread_local. However, libcxx uses shared headers, so we need to be careful there. Thankfully, there is already an option to disable multithreading in libcxx, so a flag is added such that single-threaded mode is propagated down to libc.
1 parent c4f6d34 commit 0b16395

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

cmake/Modules/FindLibcCommonUtils.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ if(NOT TARGET llvm-libc-common-utilities)
1212
add_library(llvm-libc-common-utilities INTERFACE)
1313
# TODO: Reorganize the libc shared section so that it can be included without
1414
# adding the root "libc" directory to the include path.
15+
if (NOT(LIBCXX_ENABLE_THREADS))
16+
target_compile_definitions(llvm-libc-common-utilities INTERFACE LIBC_THREAD_MODE=LIBC_THREAD_MODE_SINGLE)
17+
endif()
1518
target_include_directories(llvm-libc-common-utilities INTERFACE ${libc_path})
1619
target_compile_definitions(llvm-libc-common-utilities INTERFACE LIBC_NAMESPACE=__llvm_libc_common_utils)
1720
target_compile_features(llvm-libc-common-utilities INTERFACE cxx_std_17)

libc/src/__support/macros/attributes.h

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,32 @@
2828
#define LIBC_INLINE_ASM __asm__ __volatile__
2929
#define LIBC_UNUSED __attribute__((unused))
3030

31-
#ifdef LIBC_TARGET_ARCH_IS_GPU
31+
// Uses the platform specific specialization
32+
#define LIBC_THREAD_MODE_PLATFORM 0
33+
34+
// Mutex guards nothing, used in single-threaded implementations
35+
#define LIBC_THREAD_MODE_SINGLE 1
36+
37+
// Vendor provides implementation
38+
#define LIBC_THREAD_MODE_EXTERNAL 2
39+
40+
// libcxx doesn't define LIBC_THREAD_MODE, unless that is passed in the command
41+
// line in the CMake invocation. This defaults to the original implementation
42+
// (before changes in https://github.com/llvm/llvm-project/pull/145358)
43+
#ifndef LIBC_THREAD_MODE
44+
#define LIBC_THREAD_MODE LIBC_THREAD_MODE_PLATFORM
45+
#endif // LIBC_THREAD_MODE
46+
47+
#if LIBC_THREAD_MODE != LIBC_THREAD_MODE_PLATFORM && \
48+
LIBC_THREAD_MODE != LIBC_THREAD_MODE_SINGLE && \
49+
LIBC_THREAD_MODE != LIBC_THREAD_MODE_EXTERNAL
50+
#error LIBC_THREAD_MODE must be one of the following values: \
51+
LIBC_THREAD_MODE_PLATFORM, \
52+
LIBC_THREAD_MODE_SINGLE, \
53+
LIBC_THREAD_MODE_EXTERNAL.
54+
#endif
55+
56+
#if LIBC_THREAD_MODE == LIBC_THREAD_MODE_SINGLE
3257
#define LIBC_THREAD_LOCAL
3358
#else
3459
#define LIBC_THREAD_LOCAL thread_local

libc/src/__support/threads/mutex.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,6 @@
1212
#include "src/__support/macros/attributes.h"
1313
#include "src/__support/macros/config.h"
1414

15-
// Uses the platform specific specialization
16-
#define LIBC_THREAD_MODE_PLATFORM 0
17-
18-
// Mutex guards nothing, used in single-threaded implementations
19-
#define LIBC_THREAD_MODE_SINGLE 1
20-
21-
// Vendor provides implementation
22-
#define LIBC_THREAD_MODE_EXTERNAL 2
23-
24-
#if !defined(LIBC_THREAD_MODE)
25-
#error LIBC_THREAD_MODE is undefined
26-
#endif // LIBC_THREAD_MODE
27-
28-
#if LIBC_THREAD_MODE != LIBC_THREAD_MODE_PLATFORM && \
29-
LIBC_THREAD_MODE != LIBC_THREAD_MODE_SINGLE && \
30-
LIBC_THREAD_MODE != LIBC_THREAD_MODE_EXTERNAL
31-
#error LIBC_THREAD_MODE must be one of the following values: \
32-
LIBC_THREAD_MODE_PLATFORM, \
33-
LIBC_THREAD_MODE_SINGLE, \
34-
LIBC_THREAD_MODE_EXTERNAL.
35-
#endif
36-
3715
#if LIBC_THREAD_MODE == LIBC_THREAD_MODE_PLATFORM
3816

3917
// Platform independent code will include this header file which pulls

0 commit comments

Comments
 (0)