[libc-commits] [libc] [libc] Mutex implementation for single-threaded baremetal (PR #145358)
William Huynh via libc-commits
libc-commits at lists.llvm.org
Tue Jun 24 04:09:03 PDT 2025
https://github.com/saturn691 updated https://github.com/llvm/llvm-project/pull/145358
>From d47e596dbdf02e624f0ed3a80eb0ee1cf9507e8b Mon Sep 17 00:00:00 2001
From: William Huynh <William.Huynh at arm.com>
Date: Mon, 23 Jun 2025 17:30:31 +0100
Subject: [PATCH 1/2] [libc] Mutex implementation for single-threaded baremetal
---
.../threads/baremetal/CMakeLists.txt | 5 +++
libc/src/__support/threads/baremetal/mutex.h | 32 +++++++++++++++++++
libc/src/__support/threads/mutex.h | 2 ++
3 files changed, 39 insertions(+)
create mode 100644 libc/src/__support/threads/baremetal/CMakeLists.txt
create mode 100644 libc/src/__support/threads/baremetal/mutex.h
diff --git a/libc/src/__support/threads/baremetal/CMakeLists.txt b/libc/src/__support/threads/baremetal/CMakeLists.txt
new file mode 100644
index 0000000000000..ea89feb0c5c68
--- /dev/null
+++ b/libc/src/__support/threads/baremetal/CMakeLists.txt
@@ -0,0 +1,5 @@
+add_header_library(
+ mutex
+ HDRS
+ mutex.h
+)
diff --git a/libc/src/__support/threads/baremetal/mutex.h b/libc/src/__support/threads/baremetal/mutex.h
new file mode 100644
index 0000000000000..77a0b61ea9f5b
--- /dev/null
+++ b/libc/src/__support/threads/baremetal/mutex.h
@@ -0,0 +1,32 @@
+//===--- Implementation of a mutex class for baremetal ----------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_THREADS_BAREMETAL_MUTEX_H
+#define LLVM_LIBC_SRC___SUPPORT_THREADS_BAREMETAL_MUTEX_H
+
+#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/threads/mutex_common.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+/// Implementation of a simple passthrough mutex which guards nothing. For
+/// single threaded processors, this is the implementation.
+struct Mutex {
+ LIBC_INLINE constexpr Mutex(bool, bool, bool, bool) {}
+
+ LIBC_INLINE MutexError lock() { return MutexError::NONE; }
+ LIBC_INLINE MutexError unlock() { return MutexError::NONE; }
+ LIBC_INLINE MutexError reset() { return MutexError::NONE; }
+};
+
+// TODO: add multithreading support here
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_THREADS_BAREMETAL_MUTEX_H
diff --git a/libc/src/__support/threads/mutex.h b/libc/src/__support/threads/mutex.h
index 392b38984dc0a..a600e2ff017f5 100644
--- a/libc/src/__support/threads/mutex.h
+++ b/libc/src/__support/threads/mutex.h
@@ -41,6 +41,8 @@
#include "src/__support/threads/linux/mutex.h"
#elif defined(LIBC_TARGET_ARCH_IS_GPU)
#include "src/__support/threads/gpu/mutex.h"
+#elif defined(__ELF__)
+#include "src/__support/threads/baremetal/mutex.h"
#endif // __linux__
#endif // LLVM_LIBC_SRC___SUPPORT_THREADS_MUTEX_H
>From 0369eaeed4f474e9d1e568f24b107521c65a53ae Mon Sep 17 00:00:00 2001
From: William Huynh <William.Huynh at arm.com>
Date: Tue, 24 Jun 2025 12:08:52 +0100
Subject: [PATCH 2/2] Switch mutex implementation to match errno's
implementation
---
.../modules/LLVMLibCCompileOptionRules.cmake | 4 ++
libc/config/baremetal/config.json | 5 ++
libc/config/config.json | 6 ++
libc/config/gpu/amdgpu/config.json | 5 ++
libc/config/gpu/nvptx/config.json | 5 ++
libc/src/__support/threads/CMakeLists.txt | 8 +++
libc/src/__support/threads/gpu/CMakeLists.txt | 5 --
libc/src/__support/threads/gpu/mutex.h | 32 ----------
libc/src/__support/threads/mutex.h | 61 +++++++++++++++++--
9 files changed, 88 insertions(+), 43 deletions(-)
delete mode 100644 libc/src/__support/threads/gpu/CMakeLists.txt
delete mode 100644 libc/src/__support/threads/gpu/mutex.h
diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
index 82d06e2b9eb55..0752dfa8b19ea 100644
--- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
+++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
@@ -110,6 +110,10 @@ function(_get_compile_options_from_config output_var)
list(APPEND config_options "-DLIBC_ERRNO_MODE=${LIBC_CONF_ERRNO_MODE}")
endif()
+ if(LIBC_CONF_THREAD_MODE)
+ list(APPEND config_options "-DLIBC_THREAD_MODE=${LIBC_CONF_THREAD_MODE}")
+ endif()
+
set(${output_var} ${config_options} PARENT_SCOPE)
endfunction(_get_compile_options_from_config)
diff --git a/libc/config/baremetal/config.json b/libc/config/baremetal/config.json
index 105e417d165f0..f01e5084b9695 100644
--- a/libc/config/baremetal/config.json
+++ b/libc/config/baremetal/config.json
@@ -4,6 +4,11 @@
"value": "LIBC_ERRNO_MODE_EXTERNAL"
}
},
+ "threads": {
+ "LIBC_CONF_THREAD_MODE": {
+ "value": "LIBC_THREAD_MODE_SINGLE"
+ }
+ },
"printf": {
"LIBC_CONF_PRINTF_DISABLE_FIXED_POINT": {
"value": true
diff --git a/libc/config/config.json b/libc/config/config.json
index d53b2936edb07..1b0546980e6ba 100644
--- a/libc/config/config.json
+++ b/libc/config/config.json
@@ -5,6 +5,12 @@
"doc": "The implementation used for errno, acceptable values are LIBC_ERRNO_MODE_DEFAULT, LIBC_ERRNO_MODE_UNDEFINED, LIBC_ERRNO_MODE_THREAD_LOCAL, LIBC_ERRNO_MODE_SHARED, LIBC_ERRNO_MODE_EXTERNAL, LIBC_ERRNO_MODE_SYSTEM, and LIBC_ERRNO_MODE_SYSTEM_INLINE."
}
},
+ "threads": {
+ "LIBC_CONF_THREAD_MODE": {
+ "value": "LIBC_THREAD_MODE_PLATFORM",
+ "doc": "The implementation used for Mutex, acceptable values are LIBC_THREAD_MODE_PLATFORM, LIBC_THREAD_MODE_SINGLE, and LIBC_THREAD_MODE_EXTERNAL."
+ }
+ },
"printf": {
"LIBC_CONF_PRINTF_DISABLE_FLOAT": {
"value": false,
diff --git a/libc/config/gpu/amdgpu/config.json b/libc/config/gpu/amdgpu/config.json
index 30ae10e2cfd61..fa179b849dcf7 100644
--- a/libc/config/gpu/amdgpu/config.json
+++ b/libc/config/gpu/amdgpu/config.json
@@ -4,6 +4,11 @@
"value": "LIBC_ERRNO_MODE_SHARED"
}
},
+ "threads": {
+ "LIBC_CONF_THREAD_MODE": {
+ "value": "LIBC_THREAD_MODE_SINGLE"
+ }
+ },
"printf": {
"LIBC_CONF_PRINTF_DISABLE_FLOAT": {
"value": true
diff --git a/libc/config/gpu/nvptx/config.json b/libc/config/gpu/nvptx/config.json
index 30ae10e2cfd61..fa179b849dcf7 100644
--- a/libc/config/gpu/nvptx/config.json
+++ b/libc/config/gpu/nvptx/config.json
@@ -4,6 +4,11 @@
"value": "LIBC_ERRNO_MODE_SHARED"
}
},
+ "threads": {
+ "LIBC_CONF_THREAD_MODE": {
+ "value": "LIBC_THREAD_MODE_SINGLE"
+ }
+ },
"printf": {
"LIBC_CONF_PRINTF_DISABLE_FLOAT": {
"value": true
diff --git a/libc/src/__support/threads/CMakeLists.txt b/libc/src/__support/threads/CMakeLists.txt
index bd49bbb5ad2fe..a13d5f273e3b1 100644
--- a/libc/src/__support/threads/CMakeLists.txt
+++ b/libc/src/__support/threads/CMakeLists.txt
@@ -42,6 +42,14 @@ if(TARGET libc.src.__support.threads.${LIBC_TARGET_OS}.mutex)
.mutex
libc.src.__support.CPP.mutex
)
+else()
+ add_header_library(
+ mutex
+ HDRS
+ mutex.h
+ DEPENDS
+ .mutex_common
+ )
endif()
add_header_library(
diff --git a/libc/src/__support/threads/gpu/CMakeLists.txt b/libc/src/__support/threads/gpu/CMakeLists.txt
deleted file mode 100644
index ea89feb0c5c68..0000000000000
--- a/libc/src/__support/threads/gpu/CMakeLists.txt
+++ /dev/null
@@ -1,5 +0,0 @@
-add_header_library(
- mutex
- HDRS
- mutex.h
-)
diff --git a/libc/src/__support/threads/gpu/mutex.h b/libc/src/__support/threads/gpu/mutex.h
deleted file mode 100644
index c8c484ed2b794..0000000000000
--- a/libc/src/__support/threads/gpu/mutex.h
+++ /dev/null
@@ -1,32 +0,0 @@
-//===--- Implementation of a GPU mutex class --------------------*- C++ -*-===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_LIBC_SRC___SUPPORT_THREADS_GPU_MUTEX_H
-#define LLVM_LIBC_SRC___SUPPORT_THREADS_GPU_MUTEX_H
-
-#include "src/__support/macros/attributes.h"
-#include "src/__support/macros/config.h"
-#include "src/__support/threads/mutex_common.h"
-
-namespace LIBC_NAMESPACE_DECL {
-
-/// Implementation of a simple passthrough mutex which guards nothing. A
-/// complete Mutex locks in general cannot be implemented on the GPU. We simply
-/// define the Mutex interface and require that only a single thread executes
-/// code requiring a mutex lock.
-struct Mutex {
- LIBC_INLINE constexpr Mutex(bool, bool, bool, bool) {}
-
- LIBC_INLINE MutexError lock() { return MutexError::NONE; }
- LIBC_INLINE MutexError unlock() { return MutexError::NONE; }
- LIBC_INLINE MutexError reset() { return MutexError::NONE; }
-};
-
-} // namespace LIBC_NAMESPACE_DECL
-
-#endif // LLVM_LIBC_SRC___SUPPORT_THREADS_GPU_MUTEX_H
diff --git a/libc/src/__support/threads/mutex.h b/libc/src/__support/threads/mutex.h
index a600e2ff017f5..ef2dd6c5ac365 100644
--- a/libc/src/__support/threads/mutex.h
+++ b/libc/src/__support/threads/mutex.h
@@ -9,10 +9,35 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_THREADS_MUTEX_H
#define LLVM_LIBC_SRC___SUPPORT_THREADS_MUTEX_H
-#include "src/__support/macros/properties/architectures.h"
+#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
-// the platfrom specific specializations using platform macros.
+// the platform specific specializations using platform macros.
//
// The platform specific specializations should define a class by name
// Mutex with non-static methods having the following signature:
@@ -39,10 +64,34 @@
#if defined(__linux__)
#include "src/__support/threads/linux/mutex.h"
-#elif defined(LIBC_TARGET_ARCH_IS_GPU)
-#include "src/__support/threads/gpu/mutex.h"
-#elif defined(__ELF__)
-#include "src/__support/threads/baremetal/mutex.h"
+#else
+#warning "Mutex implementation is undefined for selected platform"
#endif // __linux__
+#elif LIBC_THREAD_MODE == LIBC_THREAD_MODE_SINGLE
+
+#include "src/__support/threads/mutex_common.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+/// Implementation of a simple passthrough mutex which guards nothing. A
+/// complete Mutex locks in general cannot be implemented on the GPU, or on some
+/// baremetal platforms. We simply define the Mutex interface and require that
+/// only a single thread executes code requiring a mutex lock.
+struct Mutex {
+ LIBC_INLINE constexpr Mutex(bool, bool, bool, bool) {}
+
+ LIBC_INLINE MutexError lock() { return MutexError::NONE; }
+ LIBC_INLINE MutexError unlock() { return MutexError::NONE; }
+ LIBC_INLINE MutexError reset() { return MutexError::NONE; }
+};
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#elif LIBC_THREAD_MODE == LIBC_THREAD_MODE_EXTERNAL
+
+// TODO: Implement the interfacing, if necessary, e.g. "extern struct Mutex;"
+
+#endif // LIBC_THREAD_MODE == LIBC_THREAD_MODE_PLATFORM
+
#endif // LLVM_LIBC_SRC___SUPPORT_THREADS_MUTEX_H
More information about the libc-commits
mailing list