1 /* Provide a replacement for the POSIX nanosleep function.
2 Copyright (C) 1999, 2000, 2002, 2004, 2005 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* written by Jim Meyering */
22 /* Undefine nanosleep here so any prototype is not redefined to be a
23 prototype for rpl_nanosleep. (they'd conflict e.g., on alpha-dec-osf3.2) */
28 #include <sys/types.h>
39 /* Some systems (MSDOS) don't have SIGCONT.
40 Using SIGTERM here turns the signal-handling code below
41 into a no-op on such systems. */
43 # define SIGCONT SIGTERM
46 #if ! HAVE_SIGINTERRUPT
47 # define siginterrupt(sig, flag) /* empty */
50 static sig_atomic_t volatile suspended;
63 my_usleep (const struct timespec *ts_delay)
65 struct timeval tv_delay;
66 tv_delay.tv_sec = ts_delay->tv_sec;
67 tv_delay.tv_usec = (ts_delay->tv_nsec + 999) / 1000;
68 if (tv_delay.tv_usec == 1000000)
73 select (0, NULL, NULL, NULL, &tv_delay);
79 rpl_nanosleep (const struct timespec *requested_delay,
80 struct timespec *remaining_delay)
82 static bool initialized;
84 /* set up sig handler */
88 struct sigaction oldact, newact;
89 newact.sa_handler = sighandler;
90 sigemptyset (&newact.sa_mask);
93 sigaction (SIGCONT, NULL, &oldact);
94 if (oldact.sa_handler != SIG_IGN)
95 sigaction (SIGCONT, &newact, NULL);
97 if (signal (SIGCONT, SIG_IGN) != SIG_IGN)
99 signal (SIGCONT, sighandler);
100 siginterrupt (SIGCONT, 1);
108 my_usleep (requested_delay);
112 /* Calculate time remaining. */
113 /* FIXME: the code in sleep doesn't use this, so there's no
114 rush to implement it. */
119 /* FIXME: Restore sig handler? */