1 /* Provide a replacement for the POSIX nanosleep function.
3 Copyright (C) 1999, 2000, 2002, 2004, 2005, 2006, 2007 Free
4 Software Foundation, Inc.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* written by Jim Meyering */
29 #include <sys/types.h>
31 # include <sys/select.h>
42 enum { BILLION = 1000 * 1000 * 1000 };
44 #if HAVE_BUG_BIG_NANOSLEEP
47 getnow (struct timespec *t)
49 # if defined CLOCK_MONOTONIC && HAVE_CLOCK_GETTIME
50 if (clock_gettime (CLOCK_MONOTONIC, t) == 0)
57 rpl_nanosleep (const struct timespec *requested_delay,
58 struct timespec *remaining_delay)
60 /* nanosleep mishandles large sleeps due to internal overflow
61 problems, so check that the proper amount of time has actually
64 struct timespec delay = *requested_delay;
70 int r = nanosleep (&delay, remaining_delay);
77 secs_sofar = now.tv_sec - t0.tv_sec;
78 if (requested_delay->tv_sec < secs_sofar)
80 delay.tv_sec = requested_delay->tv_sec - secs_sofar;
81 delay.tv_nsec = requested_delay->tv_nsec - (now.tv_nsec - t0.tv_nsec);
82 if (delay.tv_nsec < 0)
84 if (delay.tv_sec == 0)
86 delay.tv_nsec += BILLION;
89 else if (BILLION <= delay.tv_nsec)
91 delay.tv_nsec -= BILLION;
100 /* Some systems (MSDOS) don't have SIGCONT.
101 Using SIGTERM here turns the signal-handling code below
102 into a no-op on such systems. */
104 # define SIGCONT SIGTERM
107 # if ! HAVE_SIGINTERRUPT
108 # define siginterrupt(sig, flag) /* empty */
111 static sig_atomic_t volatile suspended;
113 /* Handle SIGCONT. */
121 /* Suspend execution for at least *TS_DELAY seconds. */
124 my_usleep (const struct timespec *ts_delay)
126 struct timeval tv_delay;
127 tv_delay.tv_sec = ts_delay->tv_sec;
128 tv_delay.tv_usec = (ts_delay->tv_nsec + 999) / 1000;
129 if (tv_delay.tv_usec == 1000000)
131 time_t t1 = tv_delay.tv_sec + 1;
132 if (t1 < tv_delay.tv_sec)
133 tv_delay.tv_usec = 1000000 - 1; /* close enough */
136 tv_delay.tv_sec = t1;
137 tv_delay.tv_usec = 0;
140 select (0, NULL, NULL, NULL, &tv_delay);
143 /* Suspend execution for at least *REQUESTED_DELAY seconds. The
144 *REMAINING_DELAY part isn't implemented yet. */
147 rpl_nanosleep (const struct timespec *requested_delay,
148 struct timespec *remaining_delay)
150 static bool initialized;
152 /* set up sig handler */
156 struct sigaction oldact, newact;
157 newact.sa_handler = sighandler;
158 sigemptyset (&newact.sa_mask);
161 sigaction (SIGCONT, NULL, &oldact);
162 if (oldact.sa_handler != SIG_IGN)
163 sigaction (SIGCONT, &newact, NULL);
165 if (signal (SIGCONT, SIG_IGN) != SIG_IGN)
167 signal (SIGCONT, sighandler);
168 siginterrupt (SIGCONT, 1);
176 my_usleep (requested_delay);
180 /* Calculate time remaining. */
181 /* FIXME: the code in sleep doesn't use this, so there's no
182 rush to implement it. */
187 /* FIXME: Restore sig handler? */