1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_SUPPORT_IBM_NANOSLEEP_H
11#define _LIBCPP_SUPPORT_IBM_NANOSLEEP_H
12
13#include <unistd.h>
14
15inline int nanosleep(const struct timespec* __req, struct timespec* __rem) {
16  // The nanosleep() function is not available on z/OS. Therefore, we will call
17  // sleep() to sleep for whole seconds and usleep() to sleep for any remaining
18  // fraction of a second. Any remaining nanoseconds will round up to the next
19  // microsecond.
20  if (__req->tv_sec < 0 || __req->tv_nsec < 0 || __req->tv_nsec > 999999999) {
21    errno = EINVAL;
22    return -1;
23  }
24  useconds_t __micro_sec =
25      static_cast<useconds_t>((__req->tv_nsec + 999) / 1000);
26  time_t __sec = __req->tv_sec;
27  if (__micro_sec > 999999) {
28    ++__sec;
29    __micro_sec -= 1000000;
30  }
31  __sec = sleep(static_cast<unsigned int>(__sec));
32  if (__sec) {
33    if (__rem) {
34      // Updating the remaining time to sleep in case of unsuccessful call to sleep().
35      __rem->tv_sec = __sec;
36      __rem->tv_nsec = __micro_sec * 1000;
37    }
38    errno = EINTR;
39    return -1;
40  }
41  if (__micro_sec) {
42    int __rt = usleep(__micro_sec);
43    if (__rt != 0 && __rem) {
44      // The usleep() does not provide the amount of remaining time upon its failure,
45      // so the time slept will be ignored.
46      __rem->tv_sec = 0;
47      __rem->tv_nsec = __micro_sec * 1000;
48      // The errno is already set.
49      return -1;
50    }
51    return __rt;
52  }
53  return 0;
54}
55
56#endif // _LIBCPP_SUPPORT_IBM_NANOSLEEP_H
57