1 /* Provide a replacement for the POSIX nanosleep function.
3 Copyright (C) 1999, 2000, 2002, 2004, 2005, 2006, 2007, 2008 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>
30 #include <sys/select.h>
40 enum { BILLION = 1000 * 1000 * 1000 };
42 #if HAVE_BUG_BIG_NANOSLEEP
45 getnow (struct timespec *t)
47 # if defined CLOCK_MONOTONIC && HAVE_CLOCK_GETTIME
48 if (clock_gettime (CLOCK_MONOTONIC, t) == 0)
55 rpl_nanosleep (const struct timespec *requested_delay,
56 struct timespec *remaining_delay)
58 /* nanosleep mishandles large sleeps due to internal overflow
59 problems, so check that the proper amount of time has actually
62 struct timespec delay = *requested_delay;
68 int r = nanosleep (&delay, remaining_delay);
75 secs_sofar = now.tv_sec - t0.tv_sec;
76 if (requested_delay->tv_sec < secs_sofar)
78 delay.tv_sec = requested_delay->tv_sec - secs_sofar;
79 delay.tv_nsec = requested_delay->tv_nsec - (now.tv_nsec - t0.tv_nsec);
80 if (delay.tv_nsec < 0)
82 if (delay.tv_sec == 0)
84 delay.tv_nsec += BILLION;
87 else if (BILLION <= delay.tv_nsec)
89 delay.tv_nsec -= BILLION;
98 /* Some systems (MSDOS) don't have SIGCONT.
99 Using SIGTERM here turns the signal-handling code below
100 into a no-op on such systems. */
102 # define SIGCONT SIGTERM
105 # if ! HAVE_SIGINTERRUPT
106 # define siginterrupt(sig, flag) /* empty */
109 static sig_atomic_t volatile suspended;
111 /* Handle SIGCONT. */
119 /* Suspend execution for at least *TS_DELAY seconds. */
122 my_usleep (const struct timespec *ts_delay)
124 struct timeval tv_delay;
125 tv_delay.tv_sec = ts_delay->tv_sec;
126 tv_delay.tv_usec = (ts_delay->tv_nsec + 999) / 1000;
127 if (tv_delay.tv_usec == 1000000)
129 time_t t1 = tv_delay.tv_sec + 1;
130 if (t1 < tv_delay.tv_sec)
131 tv_delay.tv_usec = 1000000 - 1; /* close enough */
134 tv_delay.tv_sec = t1;
135 tv_delay.tv_usec = 0;
138 select (0, NULL, NULL, NULL, &tv_delay);
141 /* Suspend execution for at least *REQUESTED_DELAY seconds. The
142 *REMAINING_DELAY part isn't implemented yet. */
145 rpl_nanosleep (const struct timespec *requested_delay,
146 struct timespec *remaining_delay)
148 static bool initialized;
150 /* set up sig handler */
154 struct sigaction oldact, newact;
155 newact.sa_handler = sighandler;
156 sigemptyset (&newact.sa_mask);
159 sigaction (SIGCONT, NULL, &oldact);
160 if (oldact.sa_handler != SIG_IGN)
161 sigaction (SIGCONT, &newact, NULL);
163 if (signal (SIGCONT, SIG_IGN) != SIG_IGN)
165 signal (SIGCONT, sighandler);
166 siginterrupt (SIGCONT, 1);
174 my_usleep (requested_delay);
178 /* Calculate time remaining. */
179 /* FIXME: the code in sleep doesn't use this, so there's no
180 rush to implement it. */
185 /* FIXME: Restore sig handler? */