Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:42

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___CXX03___SUPPORT_IBM_NANOSLEEP_H
0011 #define _LIBCPP___CXX03___SUPPORT_IBM_NANOSLEEP_H
0012 
0013 #include <__cxx03/unistd.h>
0014 
0015 inline int nanosleep(const struct timespec* __req, struct timespec* __rem) {
0016   // The nanosleep() function is not available on z/OS. Therefore, we will call
0017   // sleep() to sleep for whole seconds and usleep() to sleep for any remaining
0018   // fraction of a second. Any remaining nanoseconds will round up to the next
0019   // microsecond.
0020   if (__req->tv_sec < 0 || __req->tv_nsec < 0 || __req->tv_nsec > 999999999) {
0021     errno = EINVAL;
0022     return -1;
0023   }
0024   long __micro_sec = (__req->tv_nsec + 999) / 1000;
0025   time_t __sec     = __req->tv_sec;
0026   if (__micro_sec > 999999) {
0027     ++__sec;
0028     __micro_sec -= 1000000;
0029   }
0030   __sec = static_cast<time_t>(sleep(static_cast<unsigned int>(__sec)));
0031   if (__sec) {
0032     if (__rem) {
0033       // Updating the remaining time to sleep in case of unsuccessful call to sleep().
0034       __rem->tv_sec  = __sec;
0035       __rem->tv_nsec = __micro_sec * 1000;
0036     }
0037     errno = EINTR;
0038     return -1;
0039   }
0040   if (__micro_sec) {
0041     int __rt = usleep(static_cast<unsigned int>(__micro_sec));
0042     if (__rt != 0 && __rem) {
0043       // The usleep() does not provide the amount of remaining time upon its failure,
0044       // so the time slept will be ignored.
0045       __rem->tv_sec  = 0;
0046       __rem->tv_nsec = __micro_sec * 1000;
0047       // The errno is already set.
0048       return -1;
0049     }
0050     return __rt;
0051   }
0052   return 0;
0053 }
0054 
0055 #endif // _LIBCPP___CXX03___SUPPORT_IBM_NANOSLEEP_H