[libc-commits] [libc] bfe364d - [libc] Implement thrd_sleep and thrd_yield in threads (#232919)

via libc-commits libc-commits at lists.llvm.org
Mon Oct 26 01:59:57 PDT 2026


Author: Aman Maurya
Date: 2026-10-26T14:29:52+05:30
New Revision: bfe364d01eb44ac4eb896583dc009bbbdb69945d

URL: https://github.com/llvm/llvm-project/commit/bfe364d01eb44ac4eb896583dc009bbbdb69945d
DIFF: https://github.com/llvm/llvm-project/commit/bfe364d01eb44ac4eb896583dc009bbbdb69945d.diff

LOG: [libc] Implement thrd_sleep and thrd_yield in threads (#232919)

Implement the standard C11/C23 thread functions `thrd_sleep` and
`thrd_yield` in `<threads.h>`.

Fixes #232378.

Assisted by Antigravity and reviewed by Aman Maurya.

Added: 
    libc/src/threads/linux/thrd_sleep.cpp
    libc/src/threads/linux/thrd_yield.cpp
    libc/src/threads/thrd_sleep.h
    libc/src/threads/thrd_yield.h
    libc/test/integration/src/threads/thrd_sleep_test.cpp
    libc/test/integration/src/threads/thrd_yield_test.cpp

Modified: 
    libc/config/linux/aarch64/entrypoints.txt
    libc/config/linux/riscv/entrypoints.txt
    libc/config/linux/x86_64/entrypoints.txt
    libc/include/threads.yaml
    libc/src/threads/CMakeLists.txt
    libc/src/threads/linux/CMakeLists.txt
    libc/test/integration/src/threads/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index b193733a02dc5f..93e4a812f39c53 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1425,6 +1425,8 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.threads.thrd_equal
     libc.src.threads.thrd_exit
     libc.src.threads.thrd_join
+    libc.src.threads.thrd_sleep
+    libc.src.threads.thrd_yield
     libc.src.threads.tss_create
     libc.src.threads.tss_delete
     libc.src.threads.tss_get

diff  --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 273b865173bcec..89560e59f10c52 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1611,6 +1611,8 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.threads.thrd_equal
     libc.src.threads.thrd_exit
     libc.src.threads.thrd_join
+    libc.src.threads.thrd_sleep
+    libc.src.threads.thrd_yield
     libc.src.threads.tss_create
     libc.src.threads.tss_delete
     libc.src.threads.tss_get

diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 1769cfbfaa9b43..7fa1fb4bb69da5 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1637,6 +1637,8 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.threads.thrd_equal
     libc.src.threads.thrd_exit
     libc.src.threads.thrd_join
+    libc.src.threads.thrd_sleep
+    libc.src.threads.thrd_yield
     libc.src.threads.tss_create
     libc.src.threads.tss_delete
     libc.src.threads.tss_get

diff  --git a/libc/include/threads.yaml b/libc/include/threads.yaml
index 6b41524e587c2e..c4d9cfd0322eae 100644
--- a/libc/include/threads.yaml
+++ b/libc/include/threads.yaml
@@ -152,6 +152,19 @@ functions:
     arguments:
       - type: thrd_t
       - type: int *
+  - name: thrd_sleep
+    standards:
+      - stdc
+    return_type: int
+    arguments:
+      - type: const struct timespec *
+      - type: struct timespec *
+  - name: thrd_yield
+    standards:
+      - stdc
+    return_type: void
+    arguments:
+      - type: void
   - name: tss_create
     standards:
       - stdc

diff  --git a/libc/src/threads/CMakeLists.txt b/libc/src/threads/CMakeLists.txt
index 56247204c30f90..0a971fe20b304d 100644
--- a/libc/src/threads/CMakeLists.txt
+++ b/libc/src/threads/CMakeLists.txt
@@ -220,3 +220,17 @@ add_entrypoint_object(
   DEPENDS
     .${LIBC_TARGET_OS}.cnd_broadcast
 )
+
+add_entrypoint_object(
+  thrd_sleep
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.thrd_sleep
+)
+
+add_entrypoint_object(
+  thrd_yield
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.thrd_yield
+)

diff  --git a/libc/src/threads/linux/CMakeLists.txt b/libc/src/threads/linux/CMakeLists.txt
index e4b38621bd6990..1defcb289c64ae 100644
--- a/libc/src/threads/linux/CMakeLists.txt
+++ b/libc/src/threads/linux/CMakeLists.txt
@@ -83,3 +83,30 @@ add_entrypoint_object(
     libc.include.threads
     libc.src.__support.threads.CndVar
 )
+
+add_entrypoint_object(
+  thrd_sleep
+  SRCS
+    thrd_sleep.cpp
+  HDRS
+    ../thrd_sleep.h
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.types.struct_timespec
+    libc.src.__support.common
+    libc.src.__support.macros.config
+    libc.src.__support.macros.null_check
+    libc.src.__support.OSUtil.linux.syscall_wrappers.nanosleep
+)
+
+add_entrypoint_object(
+  thrd_yield
+  SRCS
+    thrd_yield.cpp
+  HDRS
+    ../thrd_yield.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.macros.config
+    libc.src.__support.OSUtil.linux.syscall_wrappers.sched_yield
+)

diff  --git a/libc/src/threads/linux/thrd_sleep.cpp b/libc/src/threads/linux/thrd_sleep.cpp
new file mode 100644
index 00000000000000..76cc8ebc737556
--- /dev/null
+++ b/libc/src/threads/linux/thrd_sleep.cpp
@@ -0,0 +1,39 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Linux implementation of thrd_sleep.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/threads/thrd_sleep.h"
+
+#include "hdr/errno_macros.h"
+#include "hdr/types/struct_timespec.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/nanosleep.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, thrd_sleep,
+                   (const struct timespec *duration,
+                    struct timespec *remaining)) {
+  LIBC_CRASH_ON_NULLPTR(duration);
+
+  auto result = linux_syscalls::nanosleep(duration, remaining);
+  if (!result) {
+    if (result.error() == EINTR)
+      return -1;
+    return -2;
+  }
+  return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/src/threads/linux/thrd_yield.cpp b/libc/src/threads/linux/thrd_yield.cpp
new file mode 100644
index 00000000000000..10ae3bd49d2f0f
--- /dev/null
+++ b/libc/src/threads/linux/thrd_yield.cpp
@@ -0,0 +1,24 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Linux implementation of thrd_yield.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/threads/thrd_yield.h"
+
+#include "src/__support/OSUtil/linux/syscall_wrappers/sched_yield.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(void, thrd_yield, ()) { linux_syscalls::sched_yield(); }
+
+} // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/src/threads/thrd_sleep.h b/libc/src/threads/thrd_sleep.h
new file mode 100644
index 00000000000000..c7aef93fe189a9
--- /dev/null
+++ b/libc/src/threads/thrd_sleep.h
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 thrd_sleep.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_THREADS_THRD_SLEEP_H
+#define LLVM_LIBC_SRC_THREADS_THRD_SLEEP_H
+
+#include "hdr/types/struct_timespec.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int thrd_sleep(const struct timespec *duration, struct timespec *remaining);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_THREADS_THRD_SLEEP_H

diff  --git a/libc/src/threads/thrd_yield.h b/libc/src/threads/thrd_yield.h
new file mode 100644
index 00000000000000..7b83bdf6255bab
--- /dev/null
+++ b/libc/src/threads/thrd_yield.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 thrd_yield.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_THREADS_THRD_YIELD_H
+#define LLVM_LIBC_SRC_THREADS_THRD_YIELD_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+void thrd_yield(void);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_THREADS_THRD_YIELD_H

diff  --git a/libc/test/integration/src/threads/CMakeLists.txt b/libc/test/integration/src/threads/CMakeLists.txt
index f5af0dc56f1ebb..c74084e30e0709 100644
--- a/libc/test/integration/src/threads/CMakeLists.txt
+++ b/libc/test/integration/src/threads/CMakeLists.txt
@@ -123,3 +123,27 @@ add_integration_test(
     libc.src.threads.linux.threads_utils
     libc.src.time.timespec_get
 )
+
+add_integration_test(
+  thrd_sleep_test
+  SUITE
+    libc-threads-integration-tests
+  SRCS
+    thrd_sleep_test.cpp
+  DEPENDS
+    libc.hdr.types.struct_timespec
+    libc.src.threads.thrd_sleep
+)
+
+add_integration_test(
+  thrd_yield_test
+  SUITE
+    libc-threads-integration-tests
+  SRCS
+    thrd_yield_test.cpp
+  DEPENDS
+    libc.include.threads
+    libc.src.threads.thrd_create
+    libc.src.threads.thrd_join
+    libc.src.threads.thrd_yield
+)

diff  --git a/libc/test/integration/src/threads/thrd_sleep_test.cpp b/libc/test/integration/src/threads/thrd_sleep_test.cpp
new file mode 100644
index 00000000000000..dda99d83925a91
--- /dev/null
+++ b/libc/test/integration/src/threads/thrd_sleep_test.cpp
@@ -0,0 +1,40 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Integration test for thrd_sleep.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/threads/thrd_sleep.h"
+
+#include "hdr/types/struct_timespec.h"
+#include "test/IntegrationTest/test.h"
+
+void test_sleep_zero() {
+  struct timespec req = {0, 0};
+  struct timespec rem = {123, 456};
+  ASSERT_EQ(LIBC_NAMESPACE::thrd_sleep(&req, &rem), 0);
+}
+
+void test_sleep_small() {
+  struct timespec req = {0, 1000000}; // 1 ms
+  ASSERT_EQ(LIBC_NAMESPACE::thrd_sleep(&req, nullptr), 0);
+}
+
+void test_sleep_invalid() {
+  struct timespec req = {0, -1};
+  ASSERT_TRUE(LIBC_NAMESPACE::thrd_sleep(&req, nullptr) < 0);
+}
+
+TEST_MAIN() {
+  test_sleep_zero();
+  test_sleep_small();
+  test_sleep_invalid();
+  return 0;
+}

diff  --git a/libc/test/integration/src/threads/thrd_yield_test.cpp b/libc/test/integration/src/threads/thrd_yield_test.cpp
new file mode 100644
index 00000000000000..f63cc9ed44ffb3
--- /dev/null
+++ b/libc/test/integration/src/threads/thrd_yield_test.cpp
@@ -0,0 +1,42 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Integration test for thrd_yield.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/threads/thrd_create.h"
+#include "src/threads/thrd_join.h"
+#include "src/threads/thrd_yield.h"
+
+#include "test/IntegrationTest/test.h"
+
+#include <threads.h>
+
+static int worker(void *) {
+  LIBC_NAMESPACE::thrd_yield();
+  return 0;
+}
+
+void test_yield() {
+  LIBC_NAMESPACE::thrd_yield();
+
+  thrd_t thread;
+  ASSERT_EQ(LIBC_NAMESPACE::thrd_create(&thread, worker, nullptr),
+            static_cast<int>(thrd_success));
+  int retval = -1;
+  ASSERT_EQ(LIBC_NAMESPACE::thrd_join(thread, &retval),
+            static_cast<int>(thrd_success));
+  ASSERT_EQ(retval, 0);
+}
+
+TEST_MAIN() {
+  test_yield();
+  return 0;
+}


        


More information about the libc-commits mailing list