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