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 */
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) */
30 #include <sys/types.h>
32 # include <sys/select.h>
36 #if TIME_WITH_SYS_TIME
37 # include <sys/time.h>
41 # include <sys/time.h>
53 enum { BILLION = 1000 * 1000 * 1000 };
55 #if HAVE_BUG_BIG_NANOSLEEP
58 getnow (struct timespec *t)
60 # if defined CLOCK_MONOTONIC && HAVE_CLOCK_GETTIME
61 if (clock_gettime (CLOCK_MONOTONIC, t) == 0)
68 rpl_nanosleep (const struct timespec *requested_delay,
69 struct timespec *remaining_delay)
71 /* nanosleep mishandles large sleeps due to internal overflow
72 problems, so check that the proper amount of time has actually
75 struct timespec delay = *requested_delay;
81 int r = nanosleep (&delay, remaining_delay);
88 secs_sofar = now.tv_sec - t0.tv_sec;
89 if (requested_delay->tv_sec < secs_sofar)
91 delay.tv_sec = requested_delay->tv_sec - secs_sofar;
92 delay.tv_nsec = requested_delay->tv_nsec - (now.tv_nsec - t0.tv_nsec);
93 if (delay.tv_nsec < 0)
95 if (delay.tv_sec == 0)
97 delay.tv_nsec += BILLION;
100 else if (BILLION <= delay.tv_nsec)
102 delay.tv_nsec -= BILLION;
111 /* Some systems (MSDOS) don't have SIGCONT.
112 Using SIGTERM here turns the signal-handling code below
113 into a no-op on such systems. */
115 # define SIGCONT SIGTERM
118 # if ! HAVE_SIGINTERRUPT
119 # define siginterrupt(sig, flag) /* empty */
122 static sig_atomic_t volatile suspended;
124 /* Handle SIGCONT. */
132 /* Suspend execution for at least *TS_DELAY seconds. */
135 my_usleep (const struct timespec *ts_delay)
137 struct timeval tv_delay;
138 tv_delay.tv_sec = ts_delay->tv_sec;
139 tv_delay.tv_usec = (ts_delay->tv_nsec + 999) / 1000;
140 if (tv_delay.tv_usec == 1000000)
142 time_t t1 = tv_delay.tv_sec + 1;
143 if (t1 < tv_delay.tv_sec)
144 tv_delay.tv_usec = 1000000 - 1; /* close enough */
147 tv_delay.tv_sec = t1;
148 tv_delay.tv_usec = 0;
151 select (0, NULL, NULL, NULL, &tv_delay);
154 /* Suspend execution for at least *REQUESTED_DELAY seconds. The
155 *REMAINING_DELAY part isn't implemented yet. */
158 rpl_nanosleep (const struct timespec *requested_delay,
159 struct timespec *remaining_delay)
161 static bool initialized;
163 /* set up sig handler */
167 struct sigaction oldact, newact;
168 newact.sa_handler = sighandler;
169 sigemptyset (&newact.sa_mask);
172 sigaction (SIGCONT, NULL, &oldact);
173 if (oldact.sa_handler != SIG_IGN)
174 sigaction (SIGCONT, &newact, NULL);
176 if (signal (SIGCONT, SIG_IGN) != SIG_IGN)
178 signal (SIGCONT, sighandler);
179 siginterrupt (SIGCONT, 1);
187 my_usleep (requested_delay);
191 /* Calculate time remaining. */
192 /* FIXME: the code in sleep doesn't use this, so there's no
193 rush to implement it. */
198 /* FIXME: Restore sig handler? */