aboutsummaryrefslogtreecommitdiff
path: root/libc/src/threads
AgeCommit message (Collapse)AuthorFilesLines
2025-12-16[libc] Add Darwin mutex support via os_sync primitives (#167722)Shreeyash Pandey1-1/+1
This patch implements the generic mutex and raw_mutex interfaces on macOS. A new Futex class is provided that relies on os_sync_wait and os_sync_wake to emulate futex‑like wait and wake semantics. The OS‑specific part is moved into futex_utils, which now contains the Darwin implementation.
2025-10-08[libc] Refactor AUXV handling with new auxv.h header library (#162326)Schrodinger ZHU Yifan1-2/+1
Closes https://github.com/llvm/llvm-project/issues/153666 This patch introduces a new centralized AUXV (auxiliary vector) handling mechanism for LLVM libc on Linux, replacing the previous scattered implementation across multiple files. ## Key Changes: ### New Files: - **libc/src/__support/OSUtil/linux/auxv.h**: New header library providing a clean interface for AUXV access with: - `auxv::Entry` struct for AUXV entries (type and value) - `auxv::Vector` class with iterator support for traversing AUXV - `auxv::get()` function for retrieving specific AUXV values - Thread-safe initialization with fallback mechanisms (prctl and /proc/self/auxv) ### Modified Files: 1. **libc/src/__support/OSUtil/linux/CMakeLists.txt**: - Added `auxv` header library declaration with proper dependencies: - libc.hdr.fcntl_macros - libc.src.__support.OSUtil.osutil - libc.src.__support.common - libc.src.__support.CPP.optional - libc.src.__support.threads.callonce 2. **libc/config/linux/app.h**: - Removed `AuxEntry` struct (moved to auxv.h as `auxv::Entry`) - Removed `auxv_ptr` from `AppProperties` struct - Simplified application properties structure 3. **libc/src/sys/auxv/linux/getauxval.cpp**: - Completely refactored to use new auxv.h interface - Removed ~200 lines of complex initialization code - Simplified to just call `auxv::get()` function - Removed dependencies to external symbols (mman, prctl, fcntl, read, close, open) 4. **libc/src/sys/auxv/linux/CMakeLists.txt**: - Updated dependencies to use new auxv header library - Removed dependencies to external symbols (prctl, mman, fcntl, unistd, etc.) 5. **libc/startup/linux/do_start.cpp**: - Updated to use new `auxv::Vector` interface - Changed from pointer-based to iterator-based AUXV traversal - Updated field names (`aux_entry->id` → `aux_entry.type`, `aux_entry->value` → `aux_entry.val`) - Added call to `auxv::Vector::initialize_unsafe()` for early AUXV setup 6. **libc/startup/linux/CMakeLists.txt**: - Added dependency on `libc.src.__support.OSUtil.linux.auxv`
2025-06-11[libc] Move libc_errno.h to libc/src/__support and make ↵lntue1-1/+1
LIBC_ERRNO_MODE_SYSTEM to be header-only. (#143187) This is the first step in preparation for: https://discourse.llvm.org/t/rfc-make-clang-builtin-math-functions-constexpr-with-llvm-libc-to-support-c-23-constexpr-math-functions/86450
2024-11-26[libc] suppress more clang-cl warnings (#117718)Schrodinger ZHU Yifan1-1/+1
- migrate more `-O3` to `${libc_opt_high_flag}` - workaround a issue with `LLP64` in test. The overflow testing is guarded by a constexpr but the literal overflow itself will still trigger warnings. Notice that for math smoke test, for some reasons, the `${libc_opt_high_flag}` will be passed into `lld-link` which confuses the linker so there are still some warnings leftover there. I can investigate more when I have time.
2024-10-05[libc] remove errno.h includes (#110934)Job Henandez Lara2-1/+2
2024-07-12[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace declaration (#98597)Petr Hosek41-82/+123
This is a part of #97655.
2024-07-12Revert "[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace ↵Mehdi Amini41-123/+82
declaration" (#98593) Reverts llvm/llvm-project#98075 bots are broken
2024-07-11[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace declaration (#98075)Petr Hosek41-82/+123
This is a part of #97655.
2024-05-31[libc] rework mutex (#92168)Schrodinger ZHU Yifan2-1/+3
2024-05-21[libc][__support] move CndVar to __support (#89329)Nick Desaulniers (paternity leave)7-169/+38
We should be able to reuse this between the implementation of C11 cnd_t condition variables and POSIX pthread_cond_t condition variables. The current implementation is hyper linux specific, making use of Futex. That obviously wont work outside of linux, so split the OS specific functions off into their own source outside of the header. Modifies the return values of the to-be-shared impl to return 0 on success and -1 on error. This pattern was shamelessly stolen from Bionic's [__bionic_thrd_error](https://android.googlesource.com/platform/bionic/+/refs/heads/main/libc/include/bits/threads_inlines.h#41). Fixes: #88580 Link: #88583
2024-05-09[libc] Replace `MutexLock` with `cpp::lock_guard` (#89340)Vlad Mishel2-2/+4
This PR address issue #89002. #### Changes in this PR * Added a simple implementation of `cpp::lock_guard` (an equivalent of `std::lock_guard`) in libc/src/__support/CPP inspired by the libstdc++ implementation * Added tests for `cpp::lock_guard` in /libc/test/src/__support/CPP/mutex_test.cpp * Replaced all references to `MutexLock` with `cpp::lock_guard` --------- Co-authored-by: Guillaume Chatelet <gchatelet@google.com>
2024-05-07[libc] clean up futex usage (#91163)Schrodinger ZHU Yifan2-5/+6
# Motivation Futex syscalls are widely used in our codebase as synchronization mechanism. Hence, it may be worthy to abstract out the most common routines (wait and wake). On the other hand, C++20 also provides `std::atomic_notify_one/std::atomic_wait/std::atomic_notify_all` which align with such functionalities. This PR introduces `Futex` as a subtype of `cpp::Atomic<FutexWordType>` with additional `notify_one/notify_all/wait` operations. Providing such wrappers also make future porting easier. For example, FreeBSD's `_umtx_op` and Darwin's `ulock` can be wrapped in a similar manner. ### Similar Examples 1. [bionic futex](https://android.googlesource.com/platform/bionic/+/refs/heads/main/libc/bionic/bionic_futex.cpp) 2. [futex in Rust's std](https://github.com/rust-lang/rust/blob/8cef37dbb67e9c80702925f19cf298c4203991e4/library/std/src/sys/pal/unix/futex.rs#L21)
2024-01-23[libc] remove redundant call_once (#79226)Nick Desaulniers2-78/+0
Missed cleanup from https://reviews.llvm.org/D134716. Fixes: #79220
2023-09-26[libc] Mass replace enclosing namespace (#67032)Guillaume Chatelet43-104/+104
This is step 4 of https://discourse.llvm.org/t/rfc-customizable-namespace-to-allow-testing-the-libc-when-the-system-libc-is-also-llvms-libc/73079
2023-08-29[libc] Fix the remaining old style includesPetr Hosek1-2/+2
These were omitted from previous cleanup changes. Differential Revision: https://reviews.llvm.org/D159066
2023-08-07[libc][cleanup] Fix most conversion warningsMichael Jones1-4/+4
This patch is large, but is almost entirely just adding casts to calls to syscall_impl. Much of the work was done programatically, with human checking when the syntax or types got confusing. Reviewed By: mcgrathr Differential Revision: https://reviews.llvm.org/D156950
2023-08-03[libc] Add support to compile some syscalls on 32 bit platformMikhail R. Gadelha2-6/+7
This patch adds a bunch of ifdefs to handle the 32 bit versions of some syscalls, which often only append a 64 to the name of the syscall (with exception of SYS_lseek -> SYS_llseek and SYS_futex -> SYS_futex_time64) This patch also tries to handle cases where wait4 is not available (as in riscv32): to implement wait, wait4 and waitpid when wait4 is not available, we check for alternative wait calls and ultimately rely on waitid to implement them all. In riscv32, only waitid is available, so we need it to support this platform. Reviewed By: michaelrj Differential Revision: https://reviews.llvm.org/D148371
2023-07-05[libc] Use the new style includesPetr Hosek19-20/+24
We should be using the standard includes. Differential Revision: https://reviews.llvm.org/D154529
2023-05-22Support custom attributes in pthread_createNoah Goldstein1-1/+1
Only functional for stack growsdown (same as before), but custom `stack`, `stacksize`, `guardsize`, and `detachstate` all should be working. Differential Revision: https://reviews.llvm.org/D148290
2023-03-09[libc][Obvious] Fix bad include and type in threads/tss_get.h.Siva Chandra Reddy1-2/+2
2023-02-09[libc][NFC] Move architectures.h to properties subfolderGuillaume Chatelet1-1/+1
2023-02-07[libc][NFC] Rename macrosGuillaume Chatelet1-1/+2
2023-02-07[libc][NFC] Rename architecture macros and move to macros folderGuillaume Chatelet1-2/+2
2022-09-30[libc] add syscall functionMichael Jones2-10/+11
Add the syscall wrapper function and tests. It's implemented using a macro to guarantee the minimum number of arguments. Reviewed By: sivachandra, lntue Differential Revision: https://reviews.llvm.org/D134919
2022-09-28[libc] Add implementation of pthread_once.Siva Chandra Reddy4-17/+30
The existing thrd_once function has been refactored so that the implementation can be shared between thrd_once and pthread_once functions. Reviewed By: michaelrj Differential Revision: https://reviews.llvm.org/D134716
2022-08-12[libc] Add implemementations of thread specific data related API.Siva Chandra Reddy9-0/+217
Specifically, POSIX functions pthread_key_create, pthread_key_delete, pthread_setspecific and pthread_getspecific have been added. The C standard equivalents tss_create, tss_delete, tss_set and tss_get have also been added. Reviewed By: lntue, michaelrj Differential Revision: https://reviews.llvm.org/D131647
2022-08-11[libc][Obvious] Fix thrd_join's first arg.Siva Chandra Reddy2-3/+3
First argument to thrd_join was incorrectly listed as a pointer to a thrd_t value. It should instead be a thrd_t value argument.
2022-08-10[libc] Add implementation of pthread_exit and thrd_exit.Siva Chandra Reddy3-0/+55
Reviewed By: michaelrj Differential Revision: https://reviews.llvm.org/D131451
2022-08-04[libc][NFC] Add a few compiler warning flags.Siva Chandra Reddy1-1/+1
A bunch of cleanup to supress the new warnings is also done. Reviewed By: abrachet Differential Revision: https://reviews.llvm.org/D130723
2022-07-14[libc] Add implementations of pthread_equal and pthread_self.Siva Chandra Reddy5-0/+114
Reviewed By: michaelrj, lntue Differential Revision: https://reviews.llvm.org/D129729
2022-06-24[libc][NFC] Remove the templatization from the linux implementation of thread.Siva Chandra Reddy3-9/+9
This enables setting up a single "self" thread object to be returned by API like thrd_self and pthread_self.
2022-06-11[libc] Add pthread_detach and thrd_detach.Siva Chandra Reddy3-0/+57
Tests for pthread_detach and thrd_detach have not been added. Instead, a test for the underlying implementation has been added as it makes use of an internal wait method to synchronize with detached threads. Reviewed By: lntue, michaelrj Differential Revision: https://reviews.llvm.org/D127479
2022-06-01[libc] Keep all thread state information separate from the thread structure.Siva Chandra Reddy1-6/+2
The state is now stored on the thread's stack memory. This enables implementing pthread API like pthread_detach which takes the pthread_t structure argument by value. Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D126716
2022-04-07[libc] Add a linux Thread class in __support/threads.Siva Chandra Reddy9-264/+83
This change is essentially a mechanical change which moves the thread creation and join implementations from src/threads/linux to src/__support/threads/linux/thread.h. The idea being that, in future, a pthread implementation can reuse the common thread implementations in src/__support/threads. Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D123287
2022-04-02[libc][NFC] Do not call mmap and munmap from thread functions.Siva Chandra Reddy3-20/+34
Instead, memory is allocated and deallocated using mmap and munmap syscalls directly. Reviewed By: lntue, michaelrj Differential Revision: https://reviews.llvm.org/D122876
2022-03-31[libc] Enable threads.h functions on aarch64.Siva Chandra4-44/+43
Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D122788
2022-03-18[libc][NFC] Add the platform independent file target only if mutex is available.Siva Chandra Reddy2-6/+6
The platform independent file implementation is not an entrypoint so it cannot be excluded via the entrypoints.txt file. Hence, we need a special treatment to exclude it from the build. Reviewed By: michaelrj Differential Revision: https://reviews.llvm.org/D121947
2022-03-16[libc] Remove references to the std threads library from __support/threads.Siva Chandra Reddy1-1/+6
2022-03-15[libc][NFC] Fix typos and reduntent code triggering compiler warinings.Siva Chandra Reddy1-1/+1
2022-03-04[libc][NFC] Add a platform independent thread support library.Siva Chandra Reddy9-194/+48
The idea is that, other parts of the libc which require thread/lock support will be able to use this platform independent setup. With this change, only the linux implementation of a mutex type has been moved to the new library. Because of this, there is some duplication between the new library and src/threads/linux. A follow up change will move all of src/threads/linux to the new library. The duplication should be eliminated with that move. Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D120795
2022-03-04[libc] Make the errno macro resolve to the thread local variable directly.Siva Chandra Reddy1-1/+1
With modern architectures having a thread pointer and language supporting thread locals, there is no reason to use a function intermediary to access the thread local errno value. The entrypoint corresponding to errno has been replaced with an object library as there is no formal entrypoint for errno anymore. Reviewed By: jeffbailey, michaelrj Differential Revision: https://reviews.llvm.org/D120920
2022-03-01[libc] Remove the remaining uses of stdatomic.h.Siva Chandra Reddy7-44/+48
New methods to the Atomic class have been added as required. Futex related types have been consolidated at a common place. Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D120705
2022-02-28[libc] Add a class "Atomic" as a simple equivalent of std::atomic.Siva Chandra Reddy3-31/+36
Only the methods currently required by the libc have been added. Most of the existing uses of atomic operations have been switched over to this new class. A future change will clean up the rest of uses. This change now allows building mutex and condition variable code with a C++ compiler which does not have stdatomic.h, for example g++. Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D120642
2021-12-22[libc] Move the x86_64 syscall functions to OSUtil.Siva Chandra Reddy6-22/+22
Reviewed By: michaelrj, lntue Differential Revision: https://reviews.llvm.org/D116177
2021-12-07[libc] apply new lint rulesMichael Jones3-10/+10
This patch applies the lint rules described in the previous patch. There was also a significant amount of effort put into manually fixing things, since all of the templated functions, or structs defined in /spec, were not updated and had to be handled manually. Reviewed By: sivachandra, lntue Differential Revision: https://reviews.llvm.org/D114302
2021-09-28[libc] Add implementations of the C standard condition variable functions.Siva Chandra Reddy7-6/+175
Reviewed By: michaelrj Differential Revision: https://reviews.llvm.org/D108948
2021-09-01[libc] Add a skeleton for C standard condition variable functions.Siva Chandra Reddy12-0/+272
This patch adds a skeleton as a preparatory step for the next patch which adds the actual implementations of the condition variable functions. Reviewed By: michaelrj Differential Revision: https://reviews.llvm.org/D108947
2021-08-30[libc] Add mtx_destroy which does nothing.Siva Chandra Reddy4-0/+56
There is not cleanup to be done for the mutex type so mtx_destroy does nothing.
2021-08-30[libc] Ensure the result of the clone syscall is not on stack in thrd_create.Siva Chandra Reddy2-1/+4
Also, added a call to munmap on error in thrd_create.
2021-08-30[libc][NFC] Add a check to catch mismatch in internal and public mutex types.Siva Chandra Reddy1-0/+4