1 /* Provide a replacement for the POSIX nanosleep function.
3 Copyright (C) 1999, 2000, 2002, 2004, 2005, 2006 Free Software
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 2, or (at your option)
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, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 /* written by Jim Meyering */
26 /* Undefine nanosleep here so any prototype is not redefined to be a
27 prototype for rpl_nanosleep. (they'd conflict e.g., on alpha-dec-osf3.2) */
32 #include <sys/types.h>
34 # include <sys/select.h>
38 #if TIME_WITH_SYS_TIME
39 # include <sys/time.h>
43 # include <sys/time.h>
55 enum { BILLION = 1000 * 1000 * 1000 };
57 #if HAVE_BUG_BIG_NANOSLEEP
60 getnow (struct timespec *t)
62 # if defined CLOCK_MONOTONIC && HAVE_CLOCK_GETTIME
63 if (clock_gettime (CLOCK_MONOTONIC, t) == 0)
70 rpl_nanosleep (const struct timespec *requested_delay,
71 struct timespec *remaining_delay)
73 /* nanosleep mishandles large sleeps due to internal overflow
74 problems, so check that the proper amount of time has actually
77 struct timespec delay = *requested_delay;
83 int r = nanosleep (&delay, remaining_delay);
90 secs_sofar = now.tv_sec - t0.tv_sec;
91 if (requested_delay->tv_sec < secs_sofar)
93 delay.tv_sec = requested_delay->tv_sec - secs_sofar;
94 delay.tv_nsec = requested_delay->tv_nsec - (now.tv_nsec - t0.tv_nsec);
95 if (delay.tv_nsec < 0)
97 if (delay.tv_sec == 0)
99 delay.tv_nsec += BILLION;
102 else if (BILLION <= delay.tv_nsec)
104 delay.tv_nsec -= BILLION;
113 /* Some systems (MSDOS) don't have SIGCONT.
114 Using SIGTERM here turns the signal-handling code below
115 into a no-op on such systems. */
117 # define SIGCONT SIGTERM
120 # if ! HAVE_SIGINTERRUPT
121 # define siginterrupt(sig, flag) /* empty */
124 static sig_atomic_t volatile suspended;
126 /* Handle SIGCONT. */
134 /* Suspend execution for at least *TS_DELAY seconds. */
137 my_usleep (const struct timespec *ts_delay)
139 struct timeval tv_delay;
140 tv_delay.tv_sec = ts_delay->tv_sec;
141 tv_delay.tv_usec = (ts_delay->tv_nsec + 999) / 1000;
142 if (tv_delay.tv_usec == 1000000)
144 time_t t1 = tv_delay.tv_sec + 1;
145 if (t1 < tv_delay.tv_sec)
146 tv_delay.tv_usec = 1000000 - 1; /* close enough */
149 tv_delay.tv_sec = t1;
150 tv_delay.tv_usec = 0;
153 select (0, NULL, NULL, NULL, &tv_delay);
156 /* Suspend execution for at least *REQUESTED_DELAY seconds. The
157 *REMAINING_DELAY part isn't implemented yet. */
160 rpl_nanosleep (const struct timespec *requested_delay,
161 struct timespec *remaining_delay)
163 static bool initialized;
165 /* set up sig handler */
169 struct sigaction oldact, newact;
170 newact.sa_handler = sighandler;
171 sigemptyset (&newact.sa_mask);
174 sigaction (SIGCONT, NULL, &oldact);
175 if (oldact.sa_handler != SIG_IGN)
176 sigaction (SIGCONT, &newact, NULL);
178 if (signal (SIGCONT, SIG_IGN) != SIG_IGN)
180 signal (SIGCONT, sighandler);
181 siginterrupt (SIGCONT, 1);
189 my_usleep (requested_delay);
193 /* Calculate time remaining. */
194 /* FIXME: the code in sleep doesn't use this, so there's no
195 rush to implement it. */
200 /* FIXME: Restore sig handler? */