diff --git a/libc/src/sched/linux/CMakeLists.txt b/libc/src/sched/linux/CMakeLists.txt --- a/libc/src/sched/linux/CMakeLists.txt +++ b/libc/src/sched/linux/CMakeLists.txt @@ -30,4 +30,16 @@ ../sched_getcpucount.h DEPENDS libc.include.sched + ) + +add_entrypoint_object( + sched_yield + SRCS + sched_yield.cpp + HDRS + ../sched_yield.h + DEPENDS + libc.include.sys_syscall + libc.src.__support.OSUtil.osutil + libc.src.errno.errno ) diff --git a/libc/src/sched/linux/sched_yield.cpp b/libc/src/sched/linux/sched_yield.cpp new file mode 100644 --- /dev/null +++ b/libc/src/sched/linux/sched_yield.cpp @@ -0,0 +1,28 @@ +//===-- Implementation of sched_yield -------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "src/sched/sched_yield.h" + +#include "src/__support/OSUtil/syscall.h" // For internal syscall function. +#include "src/__support/common.h" + +#include // For syscall numbers. + +namespace __llvm_libc { + +LLVM_LIBC_FUNCTION(int, sched_yield, ()) { + long ret = __llvm_libc::syscall_impl(SYS_sched_yield); + // As of writing this, yield() cannot fail + if (ret < 0) { + libc_errno = -ret; + return -1; + } + return 0; +} + +} // namespace __llvm_libc diff --git a/libc/src/sched/sched_yield.h b/libc/src/sched/sched_yield.h new file mode 100644 --- /dev/null +++ b/libc/src/sched/sched_yield.h @@ -0,0 +1,18 @@ +//===-- Implementation header for sched_yield -------------------*- 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_SCHED_SCHED_YIELD_H +#define LLVM_LIBC_SRC_SCHED_SCHED_YIELD_H + +namespace __llvm_libc { + +int sched_yield(void); + +} // namespace __llvm_libc + +#endif // LLVM_LIBC_SRC_SCHED_SCHED_YIELD_H