1 /* Provide a replacement for the POSIX nanosleep function.
3 Copyright (C) 1999-2000, 2002, 2004-2011 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* written by Jim Meyering
19 and Bruno Haible for the Woe32 part */
26 #include "sig-handler.h"
31 #include <sys/types.h>
32 #include <sys/select.h>
41 enum { BILLION = 1000 * 1000 * 1000 };
43 #if HAVE_BUG_BIG_NANOSLEEP
46 nanosleep (const struct timespec *requested_delay,
47 struct timespec *remaining_delay)
50 /* nanosleep mishandles large sleeps due to internal overflow problems.
51 The worst known case of this is Linux 2.6.9 with glibc 2.3.4, which
52 can't sleep more than 24.85 days (2^31 milliseconds). Similarly,
53 cygwin 1.5.x, which can't sleep more than 49.7 days (2^32 milliseconds).
54 Solve this by breaking the sleep up into smaller chunks. */
56 if (requested_delay->tv_nsec < 0 || BILLION <= requested_delay->tv_nsec)
63 /* Verify that time_t is large enough. */
64 verify (TYPE_MAXIMUM (time_t) / 24 / 24 / 60 / 60);
65 const time_t limit = 24 * 24 * 60 * 60;
66 time_t seconds = requested_delay->tv_sec;
67 struct timespec intermediate;
68 intermediate.tv_nsec = 0;
70 while (limit < seconds)
73 intermediate.tv_sec = limit;
74 result = nanosleep (&intermediate, remaining_delay);
80 remaining_delay->tv_sec += seconds;
81 remaining_delay->tv_nsec += requested_delay->tv_nsec;
82 if (BILLION <= requested_delay->tv_nsec)
84 remaining_delay->tv_sec++;
85 remaining_delay->tv_nsec -= BILLION;
91 intermediate.tv_sec = seconds;
92 intermediate.tv_nsec = requested_delay->tv_nsec;
93 return nanosleep (&intermediate, remaining_delay);
97 #elif (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
98 /* Windows platforms. */
100 # define WIN32_LEAN_AND_MEAN
101 # include <windows.h>
103 /* The Win32 function Sleep() has a resolution of about 15 ms and takes
104 at least 5 ms to execute. We use this function for longer time periods.
105 Additionally, we use busy-looping over short time periods, to get a
106 resolution of about 0.01 ms. In order to measure such short timespans,
107 we use the QueryPerformanceCounter() function. */
110 nanosleep (const struct timespec *requested_delay,
111 struct timespec *remaining_delay)
113 static bool initialized;
114 /* Number of performance counter increments per nanosecond,
115 or zero if it could not be determined. */
116 static double ticks_per_nanosecond;
118 if (requested_delay->tv_nsec < 0 || BILLION <= requested_delay->tv_nsec)
124 /* For requested delays of one second or more, 15ms resolution is
126 if (requested_delay->tv_sec == 0)
130 /* Initialize ticks_per_nanosecond. */
131 LARGE_INTEGER ticks_per_second;
133 if (QueryPerformanceFrequency (&ticks_per_second))
134 ticks_per_nanosecond =
135 (double) ticks_per_second.QuadPart / 1000000000.0;
139 if (ticks_per_nanosecond)
141 /* QueryPerformanceFrequency worked. We can use
142 QueryPerformanceCounter. Use a combination of Sleep and
144 /* Number of milliseconds to pass to the Sleep function.
145 Since Sleep can take up to 8 ms less or 8 ms more than requested
146 (or maybe more if the system is loaded), we subtract 10 ms. */
147 int sleep_millis = (int) requested_delay->tv_nsec / 1000000 - 10;
148 /* Determine how many ticks to delay. */
149 LONGLONG wait_ticks = requested_delay->tv_nsec * ticks_per_nanosecond;
151 LARGE_INTEGER counter_before;
152 if (QueryPerformanceCounter (&counter_before))
154 /* Wait until the performance counter has reached this value.
155 We don't need to worry about overflow, because the performance
156 counter is reset at reboot, and with a frequency of 3.6E6
157 ticks per second 63 bits suffice for over 80000 years. */
158 LONGLONG wait_until = counter_before.QuadPart + wait_ticks;
159 /* Use Sleep for the longest part. */
160 if (sleep_millis > 0)
161 Sleep (sleep_millis);
162 /* Busy-loop for the rest. */
165 LARGE_INTEGER counter_after;
166 if (!QueryPerformanceCounter (&counter_after))
167 /* QueryPerformanceCounter failed, but succeeded earlier.
168 Should not happen. */
170 if (counter_after.QuadPart >= wait_until)
171 /* The requested time has elapsed. */
178 /* Implementation for long delays and as fallback. */
179 Sleep (requested_delay->tv_sec * 1000 + requested_delay->tv_nsec / 1000000);
182 /* Sleep is not interruptible. So there is no remaining delay. */
183 if (remaining_delay != NULL)
185 remaining_delay->tv_sec = 0;
186 remaining_delay->tv_nsec = 0;
192 /* Unix platforms lacking nanosleep. */
194 /* Some systems (MSDOS) don't have SIGCONT.
195 Using SIGTERM here turns the signal-handling code below
196 into a no-op on such systems. */
198 # define SIGCONT SIGTERM
201 static sig_atomic_t volatile suspended;
203 /* Handle SIGCONT. */
211 /* Suspend execution for at least *TS_DELAY seconds. */
214 my_usleep (const struct timespec *ts_delay)
216 struct timeval tv_delay;
217 tv_delay.tv_sec = ts_delay->tv_sec;
218 tv_delay.tv_usec = (ts_delay->tv_nsec + 999) / 1000;
219 if (tv_delay.tv_usec == 1000000)
221 time_t t1 = tv_delay.tv_sec + 1;
222 if (t1 < tv_delay.tv_sec)
223 tv_delay.tv_usec = 1000000 - 1; /* close enough */
226 tv_delay.tv_sec = t1;
227 tv_delay.tv_usec = 0;
230 select (0, NULL, NULL, NULL, &tv_delay);
233 /* Suspend execution for at least *REQUESTED_DELAY seconds. The
234 *REMAINING_DELAY part isn't implemented yet. */
237 nanosleep (const struct timespec *requested_delay,
238 struct timespec *remaining_delay)
240 static bool initialized;
242 if (requested_delay->tv_nsec < 0 || BILLION <= requested_delay->tv_nsec)
248 /* set up sig handler */
251 struct sigaction oldact;
253 sigaction (SIGCONT, NULL, &oldact);
254 if (get_handler (&oldact) != SIG_IGN)
256 struct sigaction newact;
258 newact.sa_handler = sighandler;
259 sigemptyset (&newact.sa_mask);
261 sigaction (SIGCONT, &newact, NULL);
268 my_usleep (requested_delay);
272 /* Calculate time remaining. */
273 /* FIXME: the code in sleep doesn't use this, so there's no
274 rush to implement it. */
279 /* FIXME: Restore sig handler? */