42 lines · cpp
1//===-- Linux implementation of nanosleep function ------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/time/nanosleep.h"10#include "hdr/stdint_proxy.h" // For int64_t.11#include "hdr/time_macros.h"12#include "src/__support/OSUtil/syscall.h" // For syscall functions.13#include "src/__support/common.h"14#include "src/__support/libc_errno.h"15#include "src/__support/macros/config.h"16 17#include <sys/syscall.h> // For syscall numbers.18 19namespace LIBC_NAMESPACE_DECL {20 21LLVM_LIBC_FUNCTION(int, nanosleep, (const timespec *req, timespec *rem)) {22#if SYS_nanosleep23 int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_nanosleep, req, rem);24#elif defined(SYS_clock_nanosleep_time64)25 static_assert(26 sizeof(time_t) == sizeof(int64_t),27 "SYS_clock_gettime64 requires struct timespec with 64-bit members.");28 int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_nanosleep_time64,29 CLOCK_REALTIME, 0, req, rem);30#else31#error "SYS_nanosleep and SYS_clock_nanosleep_time64 syscalls not available."32#endif33 34 if (ret < 0) {35 libc_errno = -ret;36 return -1;37 }38 return ret;39}40 41} // namespace LIBC_NAMESPACE_DECL42