Skip to content

[libc] Mutex implementation for single-threaded baremetal #145358

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 5 additions & 0 deletions libc/config/baremetal/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions libc/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions libc/config/gpu/amdgpu/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions libc/config/gpu/nvptx/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions libc/docs/configure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,7 @@ to learn about the defaults for your platform and target.
* **"string" options**
- ``LIBC_CONF_MEMSET_X86_USE_SOFTWARE_PREFETCHING``: Inserts prefetch for write instructions (PREFETCHW) for memset on x86 to recover performance when hardware prefetcher is disabled.
- ``LIBC_CONF_STRING_UNSAFE_WIDE_READ``: Read more than a byte at a time to perform byte-string operations like strlen.
* **"threads" options**
- ``LIBC_CONF_THREAD_MODE``: The implementation used for Mutex, acceptable values are LIBC_THREAD_MODE_PLATFORM, LIBC_THREAD_MODE_SINGLE, and LIBC_THREAD_MODE_EXTERNAL.
* **"time" options**
- ``LIBC_CONF_TIME_64BIT``: Force the size of time_t to 64 bits, even on platforms where compatibility considerations would otherwise make it 32-bit.
8 changes: 8 additions & 0 deletions libc/src/__support/threads/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ if(TARGET libc.src.__support.threads.${LIBC_TARGET_OS}.mutex)
.mutex
libc.src.__support.CPP.mutex
)
elseif(NOT (LIBC_CONF_THREAD_MODE STREQUAL LIBC_THREAD_MODE_PLATFORM))
add_header_library(
mutex
HDRS
mutex.h
DEPENDS
.mutex_common
)
endif()

add_header_library(
Expand Down
5 changes: 0 additions & 5 deletions libc/src/__support/threads/gpu/CMakeLists.txt

This file was deleted.

32 changes: 0 additions & 32 deletions libc/src/__support/threads/gpu/mutex.h

This file was deleted.

57 changes: 53 additions & 4 deletions libc/src/__support/threads/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -39,8 +64,32 @@

#if defined(__linux__)
#include "src/__support/threads/linux/mutex.h"
#elif defined(LIBC_TARGET_ARCH_IS_GPU)
#include "src/__support/threads/gpu/mutex.h"
#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
Loading