* _fpending.c: Include <config.h> unconditionally, since we no
[gnulib.git] / lib / nanosleep.c
1 /* Provide a replacement for the POSIX nanosleep function.
2
3    Copyright (C) 1999, 2000, 2002, 2004, 2005, 2006 Free Software
4    Foundation, Inc.
5
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)
9    any later version.
10
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.
15
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.  */
19
20 /* written by Jim Meyering */
21
22 #include <config.h>
23
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)  */
26 #undef nanosleep
27
28 #include <stdbool.h>
29 #include <stdio.h>
30 #include <sys/types.h>
31 #if HAVE_SYS_SELECT_H
32 # include <sys/select.h>
33 #endif
34 #include <signal.h>
35
36 #if TIME_WITH_SYS_TIME
37 # include <sys/time.h>
38 # include <time.h>
39 #else
40 # if HAVE_SYS_TIME_H
41 #  include <sys/time.h>
42 # else
43 #  include <time.h>
44 # endif
45 #endif
46
47 #include <errno.h>
48
49 #include <unistd.h>
50
51 #include "timespec.h"
52
53 enum { BILLION = 1000 * 1000 * 1000 };
54
55 #if HAVE_BUG_BIG_NANOSLEEP
56
57 void
58 getnow (struct timespec *t)
59 {
60 # if defined CLOCK_MONOTONIC && HAVE_CLOCK_GETTIME
61   if (clock_gettime (CLOCK_MONOTONIC, t) == 0)
62     return;
63 # endif
64   gettime (t);
65 }
66
67 int
68 rpl_nanosleep (const struct timespec *requested_delay,
69                struct timespec *remaining_delay)
70 {
71   /* nanosleep mishandles large sleeps due to internal overflow
72      problems, so check that the proper amount of time has actually
73      elapsed.  */
74
75   struct timespec delay = *requested_delay;
76   struct timespec t0;
77   getnow (&t0);
78
79   for (;;)
80     {
81       int r = nanosleep (&delay, remaining_delay);
82       if (r == 0)
83         {
84           time_t secs_sofar;
85           struct timespec now;
86           getnow (&now);
87
88           secs_sofar = now.tv_sec - t0.tv_sec;
89           if (requested_delay->tv_sec < secs_sofar)
90             return 0;
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)
94             {
95               if (delay.tv_sec == 0)
96                 return 0;
97               delay.tv_nsec += BILLION;
98               delay.tv_sec--;
99             }
100           else if (BILLION <= delay.tv_nsec)
101             {
102               delay.tv_nsec -= BILLION;
103               delay.tv_sec++;
104             }
105         }
106     }
107 }
108
109 #else
110
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. */
114 # ifndef SIGCONT
115 #  define SIGCONT SIGTERM
116 # endif
117
118 # if ! HAVE_SIGINTERRUPT
119 #  define siginterrupt(sig, flag) /* empty */
120 # endif
121
122 static sig_atomic_t volatile suspended;
123
124 /* Handle SIGCONT. */
125
126 static void
127 sighandler (int sig)
128 {
129   suspended = 1;
130 }
131
132 /* Suspend execution for at least *TS_DELAY seconds.  */
133
134 static void
135 my_usleep (const struct timespec *ts_delay)
136 {
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)
141     {
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 */
145       else
146         {
147           tv_delay.tv_sec = t1;
148           tv_delay.tv_usec = 0;
149         }
150     }
151   select (0, NULL, NULL, NULL, &tv_delay);
152 }
153
154 /* Suspend execution for at least *REQUESTED_DELAY seconds.  The
155    *REMAINING_DELAY part isn't implemented yet.  */
156
157 int
158 rpl_nanosleep (const struct timespec *requested_delay,
159                struct timespec *remaining_delay)
160 {
161   static bool initialized;
162
163   /* set up sig handler */
164   if (! initialized)
165     {
166 # ifdef SA_NOCLDSTOP
167       struct sigaction oldact, newact;
168       newact.sa_handler = sighandler;
169       sigemptyset (&newact.sa_mask);
170       newact.sa_flags = 0;
171
172       sigaction (SIGCONT, NULL, &oldact);
173       if (oldact.sa_handler != SIG_IGN)
174         sigaction (SIGCONT, &newact, NULL);
175 # else
176       if (signal (SIGCONT, SIG_IGN) != SIG_IGN)
177         {
178           signal (SIGCONT, sighandler);
179           siginterrupt (SIGCONT, 1);
180         }
181 # endif
182       initialized = true;
183     }
184
185   suspended = 0;
186
187   my_usleep (requested_delay);
188
189   if (suspended)
190     {
191       /* Calculate time remaining.  */
192       /* FIXME: the code in sleep doesn't use this, so there's no
193          rush to implement it.  */
194
195       errno = EINTR;
196     }
197
198   /* FIXME: Restore sig handler?  */
199
200   return suspended;
201 }
202 #endif