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 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 */
24 /* Undefine nanosleep here so any prototype is not redefined to be a
25 prototype for rpl_nanosleep. (they'd conflict e.g., on alpha-dec-osf3.2) */
32 #include <sys/types.h>
34 # include <sys/select.h>
45 enum { BILLION = 1000 * 1000 * 1000 };
47 #if HAVE_BUG_BIG_NANOSLEEP
50 getnow (struct timespec *t)
52 # if defined CLOCK_MONOTONIC && HAVE_CLOCK_GETTIME
53 if (clock_gettime (CLOCK_MONOTONIC, t) == 0)
60 rpl_nanosleep (const struct timespec *requested_delay,
61 struct timespec *remaining_delay)
63 /* nanosleep mishandles large sleeps due to internal overflow
64 problems, so check that the proper amount of time has actually
67 struct timespec delay = *requested_delay;
73 int r = nanosleep (&delay, remaining_delay);
80 secs_sofar = now.tv_sec - t0.tv_sec;
81 if (requested_delay->tv_sec < secs_sofar)
83 delay.tv_sec = requested_delay->tv_sec - secs_sofar;
84 delay.tv_nsec = requested_delay->tv_nsec - (now.tv_nsec - t0.tv_nsec);
85 if (delay.tv_nsec < 0)
87 if (delay.tv_sec == 0)
89 delay.tv_nsec += BILLION;
92 else if (BILLION <= delay.tv_nsec)
94 delay.tv_nsec -= BILLION;
103 /* Some systems (MSDOS) don't have SIGCONT.
104 Using SIGTERM here turns the signal-handling code below
105 into a no-op on such systems. */
107 # define SIGCONT SIGTERM
110 # if ! HAVE_SIGINTERRUPT
111 # define siginterrupt(sig, flag) /* empty */
114 static sig_atomic_t volatile suspended;
116 /* Handle SIGCONT. */
124 /* Suspend execution for at least *TS_DELAY seconds. */
127 my_usleep (const struct timespec *ts_delay)
129 struct timeval tv_delay;
130 tv_delay.tv_sec = ts_delay->tv_sec;
131 tv_delay.tv_usec = (ts_delay->tv_nsec + 999) / 1000;
132 if (tv_delay.tv_usec == 1000000)
134 time_t t1 = tv_delay.tv_sec + 1;
135 if (t1 < tv_delay.tv_sec)
136 tv_delay.tv_usec = 1000000 - 1; /* close enough */
139 tv_delay.tv_sec = t1;
140 tv_delay.tv_usec = 0;
143 select (0, NULL, NULL, NULL, &tv_delay);
146 /* Suspend execution for at least *REQUESTED_DELAY seconds. The
147 *REMAINING_DELAY part isn't implemented yet. */
150 rpl_nanosleep (const struct timespec *requested_delay,
151 struct timespec *remaining_delay)
153 static bool initialized;
155 /* set up sig handler */
159 struct sigaction oldact, newact;
160 newact.sa_handler = sighandler;
161 sigemptyset (&newact.sa_mask);
164 sigaction (SIGCONT, NULL, &oldact);
165 if (oldact.sa_handler != SIG_IGN)
166 sigaction (SIGCONT, &newact, NULL);
168 if (signal (SIGCONT, SIG_IGN) != SIG_IGN)
170 signal (SIGCONT, sighandler);
171 siginterrupt (SIGCONT, 1);
179 my_usleep (requested_delay);
183 /* Calculate time remaining. */
184 /* FIXME: the code in sleep doesn't use this, so there's no
185 rush to implement it. */
190 /* FIXME: Restore sig handler? */