xref: /llvm-project/libc/src/time/linux/nanosleep.cpp (revision ac11430983d0d89b7ccd8e10a1a3d02ad7e2208d)
1ca10bc4fSJoseph Huber //===-- Linux implementation of nanosleep function ------------------------===//
2ca10bc4fSJoseph Huber //
3ca10bc4fSJoseph Huber // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4ca10bc4fSJoseph Huber // See https://llvm.org/LICENSE.txt for license information.
5ca10bc4fSJoseph Huber // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ca10bc4fSJoseph Huber //
7ca10bc4fSJoseph Huber //===----------------------------------------------------------------------===//
8ca10bc4fSJoseph Huber 
9ca10bc4fSJoseph Huber #include "src/time/nanosleep.h"
10*ac114309SMikhail R. Gadelha #include "hdr/time_macros.h"
11ca10bc4fSJoseph Huber #include "src/__support/OSUtil/syscall.h" // For syscall functions.
12ca10bc4fSJoseph Huber #include "src/__support/common.h"
135ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
14ca10bc4fSJoseph Huber #include "src/errno/libc_errno.h"
15ca10bc4fSJoseph Huber 
1675398f28SMikhail R. Gadelha #include <stdint.h>      // For int64_t.
17ca10bc4fSJoseph Huber #include <sys/syscall.h> // For syscall numbers.
18ca10bc4fSJoseph Huber 
195ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
20ca10bc4fSJoseph Huber 
21ca10bc4fSJoseph Huber LLVM_LIBC_FUNCTION(int, nanosleep,
22ca10bc4fSJoseph Huber                    (const struct timespec *req, struct timespec *rem)) {
23ca10bc4fSJoseph Huber #if SYS_nanosleep
24b6bc9d72SGuillaume Chatelet   int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_nanosleep, req, rem);
25ca10bc4fSJoseph Huber #elif defined(SYS_clock_nanosleep_time64)
2675398f28SMikhail R. Gadelha   static_assert(
2775398f28SMikhail R. Gadelha       sizeof(time_t) == sizeof(int64_t),
2875398f28SMikhail R. Gadelha       "SYS_clock_gettime64 requires struct timespec with 64-bit members.");
29b6bc9d72SGuillaume Chatelet   int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_nanosleep_time64,
3075398f28SMikhail R. Gadelha                                               CLOCK_REALTIME, 0, req, rem);
31ca10bc4fSJoseph Huber #else
32ca10bc4fSJoseph Huber #error "SYS_nanosleep and SYS_clock_nanosleep_time64 syscalls not available."
33ca10bc4fSJoseph Huber #endif
34ca10bc4fSJoseph Huber 
35ca10bc4fSJoseph Huber   if (ret < 0) {
36ca10bc4fSJoseph Huber     libc_errno = -ret;
37ca10bc4fSJoseph Huber     return -1;
38ca10bc4fSJoseph Huber   }
39ca10bc4fSJoseph Huber   return ret;
40ca10bc4fSJoseph Huber }
41ca10bc4fSJoseph Huber 
425ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
43