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 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 19 // microsecond. 20 if (__req->tv_sec < 0 || __req->tv_nsec < 0 || __req->tv_nsec > 999999999) { 21 errno = EINVAL; 22 return -1; 23 } 24 long __micro_sec = (__req->tv_nsec + 999) / 1000; 25 time_t __sec = __req->tv_sec; 26 if (__micro_sec > 999999) { 27 ++__sec; 28 __micro_sec -= 1000000; 29 } 30 __sec = static_cast<time_t>(sleep(static_cast<unsigned int>(__sec))); 31 if (__sec) { 32 if (__rem) { 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; 36 } 37 errno = EINTR; 38 return -1; 39 } 40 if (__micro_sec) { 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. 45 __rem->tv_sec = 0; 46 __rem->tv_nsec = __micro_sec * 1000; 47 // The errno is already set. 48 return -1; 49 } 50 return __rt; 51 } 52 return 0; 53 } 54 55 #endif // _LIBCPP___SUPPORT_IBM_NANOSLEEP_H 56