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