]>
Commit | Line | Data |
---|---|---|
3537ecb4 | 1 | /* clock_nanosleep - high-resolution sleep with specifiable clock. |
dff8da6b | 2 | Copyright (C) 2002-2024 Free Software Foundation, Inc. |
c000cdad UD |
3 | This file is part of the GNU C Library. |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
c000cdad UD |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
c000cdad | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
c000cdad | 18 | |
c000cdad | 19 | #include <errno.h> |
3537ecb4 | 20 | #include <mach.h> |
8be918b7 | 21 | #include <time.h> |
3537ecb4 AZ |
22 | #include <unistd.h> |
23 | #include <posix-timer.h> | |
7b5af2d8 | 24 | #include <shlib-compat.h> |
7e718225 | 25 | #include <sysdep-cancel.h> |
c000cdad | 26 | |
3537ecb4 AZ |
27 | static int |
28 | nanosleep_call (const struct timespec *req, struct timespec *rem) | |
29 | { | |
30 | mach_port_t recv; | |
31 | struct timespec before; | |
32 | error_t err; | |
33 | ||
34 | const mach_msg_timeout_t ms | |
35 | = req->tv_sec * 1000 | |
36 | + (req->tv_nsec + 999999) / 1000000; | |
37 | ||
38 | recv = __mach_reply_port (); | |
39 | ||
40 | if (rem != NULL) | |
41 | __clock_gettime (CLOCK_REALTIME, &before); | |
42 | ||
7e718225 | 43 | int cancel_oldtype = LIBC_CANCEL_ASYNC(); |
3537ecb4 AZ |
44 | err = __mach_msg (NULL, MACH_RCV_MSG|MACH_RCV_TIMEOUT|MACH_RCV_INTERRUPT, |
45 | 0, 0, recv, ms, MACH_PORT_NULL); | |
7e718225 ST |
46 | LIBC_CANCEL_RESET (cancel_oldtype); |
47 | ||
3537ecb4 | 48 | __mach_port_destroy (mach_task_self (), recv); |
7e718225 | 49 | |
3537ecb4 AZ |
50 | if (err == EMACH_RCV_INTERRUPTED) |
51 | { | |
52 | if (rem != NULL) | |
53 | { | |
54 | struct timespec after, elapsed; | |
55 | __clock_gettime (CLOCK_REALTIME, &after); | |
56 | timespec_sub (&elapsed, &after, &before); | |
57 | timespec_sub (rem, req, &elapsed); | |
58 | } | |
59 | ||
60 | return EINTR; | |
61 | } | |
62 | ||
63 | return 0; | |
64 | } | |
65 | ||
c000cdad | 66 | int |
89fb6835 SP |
67 | __clock_nanosleep (clockid_t clock_id, int flags, const struct timespec *req, |
68 | struct timespec *rem) | |
c000cdad | 69 | { |
3537ecb4 AZ |
70 | if (clock_id != CLOCK_REALTIME |
71 | || !valid_nanoseconds (req->tv_nsec) | |
72 | || (flags != 0 && flags != TIMER_ABSTIME)) | |
c000cdad UD |
73 | return EINVAL; |
74 | ||
3537ecb4 | 75 | struct timespec now; |
ad0e8eb0 | 76 | |
c000cdad UD |
77 | /* If we got an absolute time, remap it. */ |
78 | if (flags == TIMER_ABSTIME) | |
79 | { | |
80 | long int nsec; | |
81 | long int sec; | |
82 | ||
83 | /* Make sure we use safe data types. */ | |
84 | assert (sizeof (sec) >= sizeof (now.tv_sec)); | |
85 | ||
86 | /* Get the current time for this clock. */ | |
38cc11da | 87 | if (__clock_gettime (clock_id, &now) != 0) |
c000cdad UD |
88 | return errno; |
89 | ||
90 | /* Compute the difference. */ | |
91 | nsec = req->tv_nsec - now.tv_nsec; | |
92 | sec = req->tv_sec - now.tv_sec - (nsec < 0); | |
93 | if (sec < 0) | |
94 | /* The time has already elapsed. */ | |
95 | return 0; | |
96 | ||
97 | now.tv_sec = sec; | |
98 | now.tv_nsec = nsec + (nsec < 0 ? 1000000000 : 0); | |
99 | ||
100 | /* From now on this is our time. */ | |
101 | req = &now; | |
102 | ||
103 | /* Make sure we are not modifying the struct pointed to by REM. */ | |
104 | rem = NULL; | |
105 | } | |
c000cdad | 106 | |
3537ecb4 | 107 | return nanosleep_call (req, rem); |
c000cdad | 108 | } |
3537ecb4 | 109 | libc_hidden_def (__clock_nanosleep) |
7b5af2d8 ZW |
110 | versioned_symbol (libc, __clock_nanosleep, clock_nanosleep, GLIBC_2_17); |
111 | /* clock_nanosleep moved to libc in version 2.17; | |
112 | old binaries may expect the symbol version it had in librt. */ | |
113 | #if SHLIB_COMPAT (libc, GLIBC_2_2, GLIBC_2_17) | |
114 | strong_alias (__clock_nanosleep, __clock_nanosleep_2); | |
115 | compat_symbol (libc, __clock_nanosleep_2, clock_nanosleep, GLIBC_2_2); | |
116 | #endif |