Skip to content

[libc] Change LIBC_THREAD_LOCAL to be dependent on LIBC_THREAD_MODE #151527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmake/Modules/FindLibcCommonUtils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ if(NOT TARGET llvm-libc-common-utilities)
add_library(llvm-libc-common-utilities INTERFACE)
# TODO: Reorganize the libc shared section so that it can be included without
# adding the root "libc" directory to the include path.
if (NOT(LIBCXX_ENABLE_THREADS))
target_compile_definitions(llvm-libc-common-utilities INTERFACE LIBC_THREAD_MODE=LIBC_THREAD_MODE_SINGLE)
endif()
target_include_directories(llvm-libc-common-utilities INTERFACE ${libc_path})
target_compile_definitions(llvm-libc-common-utilities INTERFACE LIBC_NAMESPACE=__llvm_libc_common_utils)
target_compile_features(llvm-libc-common-utilities INTERFACE cxx_std_17)
Expand Down
27 changes: 26 additions & 1 deletion libc/src/__support/macros/attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,32 @@
#define LIBC_INLINE_ASM __asm__ __volatile__
#define LIBC_UNUSED __attribute__((unused))

#ifdef LIBC_TARGET_ARCH_IS_GPU
// Uses the platform specific specialization
#define LIBC_THREAD_MODE_PLATFORM 0

// Mutex guards nothing, used in single-threaded implementations
#define LIBC_THREAD_MODE_SINGLE 1

// Vendor provides implementation
#define LIBC_THREAD_MODE_EXTERNAL 2

// libcxx doesn't define LIBC_THREAD_MODE, unless that is passed in the command
// line in the CMake invocation. This defaults to the original implementation
// (before changes in https://github.com/llvm/llvm-project/pull/145358)
#ifndef LIBC_THREAD_MODE
#define LIBC_THREAD_MODE LIBC_THREAD_MODE_PLATFORM
#endif // LIBC_THREAD_MODE

#if LIBC_THREAD_MODE != LIBC_THREAD_MODE_PLATFORM && \
LIBC_THREAD_MODE != LIBC_THREAD_MODE_SINGLE && \
LIBC_THREAD_MODE != LIBC_THREAD_MODE_EXTERNAL
#error LIBC_THREAD_MODE must be one of the following values: \
LIBC_THREAD_MODE_PLATFORM, \
LIBC_THREAD_MODE_SINGLE, \
LIBC_THREAD_MODE_EXTERNAL.
#endif

#if LIBC_THREAD_MODE == LIBC_THREAD_MODE_SINGLE
#define LIBC_THREAD_LOCAL
#else
#define LIBC_THREAD_LOCAL thread_local
Expand Down
22 changes: 0 additions & 22 deletions libc/src/__support/threads/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,6 @@
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h"

// Uses the platform specific specialization
#define LIBC_THREAD_MODE_PLATFORM 0

// Mutex guards nothing, used in single-threaded implementations
#define LIBC_THREAD_MODE_SINGLE 1

// Vendor provides implementation
#define LIBC_THREAD_MODE_EXTERNAL 2

#if !defined(LIBC_THREAD_MODE)
#error LIBC_THREAD_MODE is undefined
#endif // LIBC_THREAD_MODE

#if LIBC_THREAD_MODE != LIBC_THREAD_MODE_PLATFORM && \
LIBC_THREAD_MODE != LIBC_THREAD_MODE_SINGLE && \
LIBC_THREAD_MODE != LIBC_THREAD_MODE_EXTERNAL
#error LIBC_THREAD_MODE must be one of the following values: \
LIBC_THREAD_MODE_PLATFORM, \
LIBC_THREAD_MODE_SINGLE, \
LIBC_THREAD_MODE_EXTERNAL.
#endif

#if LIBC_THREAD_MODE == LIBC_THREAD_MODE_PLATFORM

// Platform independent code will include this header file which pulls
Expand Down
Loading