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 */
30 #include <sys/types.h>
32 # include <sys/select.h>
43 enum { BILLION = 1000 * 1000 * 1000 };
45 #if HAVE_BUG_BIG_NANOSLEEP
48 getnow (struct timespec *t)
50 # if defined CLOCK_MONOTONIC && HAVE_CLOCK_GETTIME
51 if (clock_gettime (CLOCK_MONOTONIC, t) == 0)
58 rpl_nanosleep (const struct timespec *requested_delay,
59 struct timespec *remaining_delay)
61 /* nanosleep mishandles large sleeps due to internal overflow
62 problems, so check that the proper amount of time has actually
65 struct timespec delay = *requested_delay;
71 int r = nanosleep (&delay, remaining_delay);
78 secs_sofar = now.tv_sec - t0.tv_sec;
79 if (requested_delay->tv_sec < secs_sofar)
81 delay.tv_sec = requested_delay->tv_sec - secs_sofar;
82 delay.tv_nsec = requested_delay->tv_nsec - (now.tv_nsec - t0.tv_nsec);
83 if (delay.tv_nsec < 0)
85 if (delay.tv_sec == 0)
87 delay.tv_nsec += BILLION;
90 else if (BILLION <= delay.tv_nsec)
92 delay.tv_nsec -= BILLION;
101 /* Some systems (MSDOS) don't have SIGCONT.
102 Using SIGTERM here turns the signal-handling code below
103 into a no-op on such systems. */
105 # define SIGCONT SIGTERM
108 # if ! HAVE_SIGINTERRUPT
109 # define siginterrupt(sig, flag) /* empty */
112 static sig_atomic_t volatile suspended;
114 /* Handle SIGCONT. */
122 /* Suspend execution for at least *TS_DELAY seconds. */
125 my_usleep (const struct timespec *ts_delay)
127 struct timeval tv_delay;
128 tv_delay.tv_sec = ts_delay->tv_sec;
129 tv_delay.tv_usec = (ts_delay->tv_nsec + 999) / 1000;
130 if (tv_delay.tv_usec == 1000000)
132 time_t t1 = tv_delay.tv_sec + 1;
133 if (t1 < tv_delay.tv_sec)
134 tv_delay.tv_usec = 1000000 - 1; /* close enough */
137 tv_delay.tv_sec = t1;
138 tv_delay.tv_usec = 0;
141 select (0, NULL, NULL, NULL, &tv_delay);
144 /* Suspend execution for at least *REQUESTED_DELAY seconds. The
145 *REMAINING_DELAY part isn't implemented yet. */
148 rpl_nanosleep (const struct timespec *requested_delay,
149 struct timespec *remaining_delay)
151 static bool initialized;
153 /* set up sig handler */
157 struct sigaction oldact, newact;
158 newact.sa_handler = sighandler;
159 sigemptyset (&newact.sa_mask);
162 sigaction (SIGCONT, NULL, &oldact);
163 if (oldact.sa_handler != SIG_IGN)
164 sigaction (SIGCONT, &newact, NULL);
166 if (signal (SIGCONT, SIG_IGN) != SIG_IGN)
168 signal (SIGCONT, sighandler);
169 siginterrupt (SIGCONT, 1);
177 my_usleep (requested_delay);
181 /* Calculate time remaining. */
182 /* FIXME: the code in sleep doesn't use this, so there's no
183 rush to implement it. */
188 /* FIXME: Restore sig handler? */