[libcxx-commits] [libcxx] 916093f - [SystemZ][z/OS] correct rc and errno within nanosleep()
Zbigniew Sarbinowski via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Apr 6 08:37:03 PDT 2021
Author: Zbigniew Sarbinowski
Date: 2021-04-06T15:36:58Z
New Revision: 916093f49fafa54a7a4a8473ec4704eca3dc5a67
URL: https://github.com/llvm/llvm-project/commit/916093f49fafa54a7a4a8473ec4704eca3dc5a67
DIFF: https://github.com/llvm/llvm-project/commit/916093f49fafa54a7a4a8473ec4704eca3dc5a67.diff
LOG: [SystemZ][z/OS] correct rc and errno within nanosleep()
This patch fixes rc and errno within nanosleep(). It also updates __rem parameter as well it introduces cast to handle conversions from long into unsigned int to avoid warnings.
Reviewed By: Mordante
Differential Revision: https://reviews.llvm.org/D99373
Added:
Modified:
libcxx/include/__support/ibm/nanosleep.h
Removed:
################################################################################
diff --git a/libcxx/include/__support/ibm/nanosleep.h b/libcxx/include/__support/ibm/nanosleep.h
index c82f4eb0cd5b..ec6379b8a9fa 100644
--- a/libcxx/include/__support/ibm/nanosleep.h
+++ b/libcxx/include/__support/ibm/nanosleep.h
@@ -12,27 +12,45 @@
#include <unistd.h>
-inline int nanosleep(const struct timespec* req, struct timespec* rem)
-{
- // The nanosleep() function is not available on z/OS. Therefore, we will call
- // sleep() to sleep for whole seconds and usleep() to sleep for any remaining
- // fraction of a second. Any remaining nanoseconds will round up to the next
- // microsecond.
-
- useconds_t __micro_sec = (rem->tv_nsec + 999) / 1000;
- if (__micro_sec > 999999)
- {
- ++rem->tv_sec;
- __micro_sec -= 1000000;
- }
- while (rem->tv_sec)
- rem->tv_sec = sleep(rem->tv_sec);
- if (__micro_sec) {
- rem->tv_nsec = __micro_sec * 1000;
- return usleep(__micro_sec);
- }
- rem->tv_nsec = 0;
- return 0;
+inline int nanosleep(const struct timespec* __req, struct timespec* __rem) {
+ // The nanosleep() function is not available on z/OS. Therefore, we will call
+ // sleep() to sleep for whole seconds and usleep() to sleep for any remaining
+ // fraction of a second. Any remaining nanoseconds will round up to the next
+ // microsecond.
+ if (__req->tv_sec < 0 || __req->tv_nsec < 0 || __req->tv_nsec > 999999999) {
+ errno = EINVAL;
+ return -1;
+ }
+ useconds_t __micro_sec =
+ static_cast<useconds_t>((__req->tv_nsec + 999) / 1000);
+ time_t __sec = __req->tv_sec;
+ if (__micro_sec > 999999) {
+ ++__sec;
+ __micro_sec -= 1000000;
+ }
+ __sec = sleep(static_cast<unsigned int>(__sec));
+ if (__sec) {
+ if (__rem) {
+ // Updating the remaining time to sleep in case of unsuccessful call to sleep().
+ __rem->tv_sec = __sec;
+ __rem->tv_nsec = __micro_sec * 1000;
+ }
+ errno = EINTR;
+ return -1;
+ }
+ if (__micro_sec) {
+ int __rt = usleep(__micro_sec);
+ if (__rt != 0 && __rem) {
+ // The usleep() does not provide the amount of remaining time upon its failure,
+ // so the time slept will be ignored.
+ __rem->tv_sec = 0;
+ __rem->tv_nsec = __micro_sec * 1000;
+ // The errno is already set.
+ return -1;
+ }
+ return __rt;
+ }
+ return 0;
}
#endif // _LIBCPP_SUPPORT_IBM_NANOSLEEP_H
More information about the libcxx-commits
mailing list