2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___SUPPORT_IBM_NANOSLEEP_H
11 #define _LIBCPP___SUPPORT_IBM_NANOSLEEP_H
15 inline 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
20 if (__req
->tv_sec
< 0 || __req
->tv_nsec
< 0 || __req
->tv_nsec
> 999999999) {
24 long __micro_sec
= (__req
->tv_nsec
+ 999) / 1000;
25 time_t __sec
= __req
->tv_sec
;
26 if (__micro_sec
> 999999) {
28 __micro_sec
-= 1000000;
30 __sec
= static_cast<time_t>(sleep(static_cast<unsigned int>(__sec
)));
33 // Updating the remaining time to sleep in case of unsuccessful call to sleep().
34 __rem
->tv_sec
= __sec
;
35 __rem
->tv_nsec
= __micro_sec
* 1000;
41 int __rt
= usleep(static_cast<unsigned int>(__micro_sec
));
42 if (__rt
!= 0 && __rem
) {
43 // The usleep() does not provide the amount of remaining time upon its failure,
44 // so the time slept will be ignored.
46 __rem
->tv_nsec
= __micro_sec
* 1000;
47 // The errno is already set.
55 #endif // _LIBCPP___SUPPORT_IBM_NANOSLEEP_H