[libc-commits] [libc] [libc] Implement clone(2) and use it in thread spawning (PR #224257)
Pavel Labath via libc-commits
libc-commits at lists.llvm.org
Fri Sep 18 01:14:35 PDT 2026
================
@@ -0,0 +1,69 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for clone.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_CLONE_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_CLONE_H
+
+#include "hdr/stdint_proxy.h"
+#include "hdr/types/pid_t.h"
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/architectures.h"
+
+#if defined(LIBC_TARGET_ARCH_IS_X86_64)
+#include "src/__support/OSUtil/linux/syscall_wrappers/x86_64/clone.h"
+#elif defined(LIBC_TARGET_ARCH_IS_AARCH64)
+#include "src/__support/OSUtil/linux/syscall_wrappers/aarch64/clone.h"
+#elif defined(LIBC_TARGET_ARCH_IS_ANY_RISCV)
+#include "src/__support/OSUtil/linux/syscall_wrappers/riscv/clone.h"
+#else
+#error "clone is not supported for this architecture"
+#endif
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+struct ChildStackFrame {
+ int (*func)(void *);
+ void *arg;
+};
+
+LIBC_INLINE ErrorOr<pid_t> clone(int (*func)(void *), void *stack, int flags,
+ void *arg, pid_t *parent_tid = nullptr,
+ void *tls = nullptr,
+ pid_t *child_tid = nullptr) {
+ // Set up func and arg at the top of the child stack in C++ so the assembly
+ // only needs to pass the standard system call arguments.
+ uintptr_t sp = reinterpret_cast<uintptr_t>(stack);
----------------
labath wrote:
Added. I've also made the required stack alignment an explicit constant (I've made it arch-specific, though I think that ~all architectures have that at 16 nowadays).
https://github.com/llvm/llvm-project/pull/224257
More information about the libc-commits
mailing list