annotate lib/nanosleep.c @ 9309:bbbbbf4cd1c5

Change copyright notice from GPLv2+ to GPLv3+.
author Bruno Haible <bruno@clisp.org>
date Sun, 07 Oct 2007 19:14:58 +0200
parents b31580167c2b
children 51cc8a81238b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
1 /* Provide a replacement for the POSIX nanosleep function.
6795
9dc5bb3f3359 * lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6275
diff changeset
2
7897
b7a83a69ac23 * MODULES.html.sh (Support for systems lacking POSIX:2001): New
Paul Eggert <eggert@cs.ucla.edu>
parents: 7302
diff changeset
3 Copyright (C) 1999, 2000, 2002, 2004, 2005, 2006, 2007 Free
b7a83a69ac23 * MODULES.html.sh (Support for systems lacking POSIX:2001): New
Paul Eggert <eggert@cs.ucla.edu>
parents: 7302
diff changeset
4 Software Foundation, Inc.
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
5
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8146
diff changeset
6 This program is free software: you can redistribute it and/or modify
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
7 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8146
diff changeset
8 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8146
diff changeset
9 (at your option) any later version.
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
10
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
11 This program is distributed in the hope that it will be useful,
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
14 GNU General Public License for more details.
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
15
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8146
diff changeset
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
18
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
19 /* written by Jim Meyering */
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
20
7302
8a1a9361108c * _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents: 7298
diff changeset
21 #include <config.h>
2270
cf5eb0c29b04 #undef nanosleep.
Jim Meyering <jim@meyering.net>
parents: 2151
diff changeset
22
8146
b31580167c2b New module 'time', so that apps can include <time.h> as per
Paul Eggert <eggert@cs.ucla.edu>
parents: 7897
diff changeset
23 #include <time.h>
2270
cf5eb0c29b04 #undef nanosleep.
Jim Meyering <jim@meyering.net>
parents: 2151
diff changeset
24
7897
b7a83a69ac23 * MODULES.html.sh (Support for systems lacking POSIX:2001): New
Paul Eggert <eggert@cs.ucla.edu>
parents: 7302
diff changeset
25 #include "timespec.h"
b7a83a69ac23 * MODULES.html.sh (Support for systems lacking POSIX:2001): New
Paul Eggert <eggert@cs.ucla.edu>
parents: 7302
diff changeset
26
5159
a535859efd14 Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5060
diff changeset
27 #include <stdbool.h>
2053
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
28 #include <stdio.h>
7298
6948138d9f3a * nanosleep.c: Include <sys/types.h> before sys/select.h, to avoid
Jim Meyering <jim@meyering.net>
parents: 7237
diff changeset
29 #include <sys/types.h>
6795
9dc5bb3f3359 * lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6275
diff changeset
30 #if HAVE_SYS_SELECT_H
9dc5bb3f3359 * lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6275
diff changeset
31 # include <sys/select.h>
9dc5bb3f3359 * lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6275
diff changeset
32 #endif
2053
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
33 #include <signal.h>
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
34
7897
b7a83a69ac23 * MODULES.html.sh (Support for systems lacking POSIX:2001): New
Paul Eggert <eggert@cs.ucla.edu>
parents: 7302
diff changeset
35 #include <sys/time.h>
2057
50da17269b1b *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2056
diff changeset
36 #include <errno.h>
50da17269b1b *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2056
diff changeset
37
6275
fd0ccce602e4 Sync from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
38 #include <unistd.h>
2056
cf3d523888c5 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2053
diff changeset
39
8146
b31580167c2b New module 'time', so that apps can include <time.h> as per
Paul Eggert <eggert@cs.ucla.edu>
parents: 7897
diff changeset
40 #undef nanosleep
b31580167c2b New module 'time', so that apps can include <time.h> as per
Paul Eggert <eggert@cs.ucla.edu>
parents: 7897
diff changeset
41
7237
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
42 enum { BILLION = 1000 * 1000 * 1000 };
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
43
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
44 #if HAVE_BUG_BIG_NANOSLEEP
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
45
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
46 void
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
47 getnow (struct timespec *t)
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
48 {
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
49 # if defined CLOCK_MONOTONIC && HAVE_CLOCK_GETTIME
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
50 if (clock_gettime (CLOCK_MONOTONIC, t) == 0)
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
51 return;
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
52 # endif
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
53 gettime (t);
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
54 }
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
55
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
56 int
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
57 rpl_nanosleep (const struct timespec *requested_delay,
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
58 struct timespec *remaining_delay)
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
59 {
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
60 /* nanosleep mishandles large sleeps due to internal overflow
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
61 problems, so check that the proper amount of time has actually
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
62 elapsed. */
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
63
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
64 struct timespec delay = *requested_delay;
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
65 struct timespec t0;
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
66 getnow (&t0);
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
67
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
68 for (;;)
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
69 {
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
70 int r = nanosleep (&delay, remaining_delay);
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
71 if (r == 0)
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
72 {
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
73 time_t secs_sofar;
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
74 struct timespec now;
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
75 getnow (&now);
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
76
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
77 secs_sofar = now.tv_sec - t0.tv_sec;
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
78 if (requested_delay->tv_sec < secs_sofar)
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
79 return 0;
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
80 delay.tv_sec = requested_delay->tv_sec - secs_sofar;
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
81 delay.tv_nsec = requested_delay->tv_nsec - (now.tv_nsec - t0.tv_nsec);
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
82 if (delay.tv_nsec < 0)
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
83 {
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
84 if (delay.tv_sec == 0)
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
85 return 0;
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
86 delay.tv_nsec += BILLION;
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
87 delay.tv_sec--;
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
88 }
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
89 else if (BILLION <= delay.tv_nsec)
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
90 {
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
91 delay.tv_nsec -= BILLION;
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
92 delay.tv_sec++;
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
93 }
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
94 }
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
95 }
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
96 }
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
97
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
98 #else
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
99
2687
34703e371994 (SIGCONT): Define if not already defined.
Jim Meyering <jim@meyering.net>
parents: 2341
diff changeset
100 /* Some systems (MSDOS) don't have SIGCONT.
34703e371994 (SIGCONT): Define if not already defined.
Jim Meyering <jim@meyering.net>
parents: 2341
diff changeset
101 Using SIGTERM here turns the signal-handling code below
34703e371994 (SIGCONT): Define if not already defined.
Jim Meyering <jim@meyering.net>
parents: 2341
diff changeset
102 into a no-op on such systems. */
7237
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
103 # ifndef SIGCONT
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
104 # define SIGCONT SIGTERM
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
105 # endif
2687
34703e371994 (SIGCONT): Define if not already defined.
Jim Meyering <jim@meyering.net>
parents: 2341
diff changeset
106
7237
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
107 # if ! HAVE_SIGINTERRUPT
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
108 # define siginterrupt(sig, flag) /* empty */
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
109 # endif
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
110
5060
4e59cddb690f nanosleep merge from coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents: 3767
diff changeset
111 static sig_atomic_t volatile suspended;
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
112
2053
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
113 /* Handle SIGCONT. */
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
114
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
115 static void
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
116 sighandler (int sig)
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
117 {
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
118 suspended = 1;
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
119 }
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
120
6795
9dc5bb3f3359 * lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6275
diff changeset
121 /* Suspend execution for at least *TS_DELAY seconds. */
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
122
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
123 static void
2056
cf3d523888c5 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2053
diff changeset
124 my_usleep (const struct timespec *ts_delay)
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
125 {
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
126 struct timeval tv_delay;
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
127 tv_delay.tv_sec = ts_delay->tv_sec;
5060
4e59cddb690f nanosleep merge from coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents: 3767
diff changeset
128 tv_delay.tv_usec = (ts_delay->tv_nsec + 999) / 1000;
4e59cddb690f nanosleep merge from coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents: 3767
diff changeset
129 if (tv_delay.tv_usec == 1000000)
4e59cddb690f nanosleep merge from coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents: 3767
diff changeset
130 {
6795
9dc5bb3f3359 * lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6275
diff changeset
131 time_t t1 = tv_delay.tv_sec + 1;
9dc5bb3f3359 * lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6275
diff changeset
132 if (t1 < tv_delay.tv_sec)
9dc5bb3f3359 * lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6275
diff changeset
133 tv_delay.tv_usec = 1000000 - 1; /* close enough */
9dc5bb3f3359 * lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6275
diff changeset
134 else
9dc5bb3f3359 * lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6275
diff changeset
135 {
9dc5bb3f3359 * lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6275
diff changeset
136 tv_delay.tv_sec = t1;
9dc5bb3f3359 * lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6275
diff changeset
137 tv_delay.tv_usec = 0;
9dc5bb3f3359 * lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6275
diff changeset
138 }
5060
4e59cddb690f nanosleep merge from coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents: 3767
diff changeset
139 }
5813
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
140 select (0, NULL, NULL, NULL, &tv_delay);
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
141 }
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
142
6795
9dc5bb3f3359 * lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6275
diff changeset
143 /* Suspend execution for at least *REQUESTED_DELAY seconds. The
9dc5bb3f3359 * lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6275
diff changeset
144 *REMAINING_DELAY part isn't implemented yet. */
2058
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
145
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
146 int
2270
cf5eb0c29b04 #undef nanosleep.
Jim Meyering <jim@meyering.net>
parents: 2151
diff changeset
147 rpl_nanosleep (const struct timespec *requested_delay,
2341
12885d185e68 tweak indentation
Jim Meyering <jim@meyering.net>
parents: 2270
diff changeset
148 struct timespec *remaining_delay)
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
149 {
5159
a535859efd14 Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5060
diff changeset
150 static bool initialized;
5060
4e59cddb690f nanosleep merge from coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents: 3767
diff changeset
151
2058
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
152 /* set up sig handler */
5060
4e59cddb690f nanosleep merge from coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents: 3767
diff changeset
153 if (! initialized)
2058
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
154 {
7237
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
155 # ifdef SA_NOCLDSTOP
5813
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
156 struct sigaction oldact, newact;
2058
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
157 newact.sa_handler = sighandler;
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
158 sigemptyset (&newact.sa_mask);
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
159 newact.sa_flags = 0;
2053
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
160
2058
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
161 sigaction (SIGCONT, NULL, &oldact);
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
162 if (oldact.sa_handler != SIG_IGN)
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
163 sigaction (SIGCONT, &newact, NULL);
7237
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
164 # else
2058
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
165 if (signal (SIGCONT, SIG_IGN) != SIG_IGN)
5813
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
166 {
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
167 signal (SIGCONT, sighandler);
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
168 siginterrupt (SIGCONT, 1);
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
169 }
7237
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
170 # endif
5159
a535859efd14 Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5060
diff changeset
171 initialized = true;
2058
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
172 }
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
173
5813
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
174 suspended = 0;
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
175
2056
cf3d523888c5 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2053
diff changeset
176 my_usleep (requested_delay);
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
177
2056
cf3d523888c5 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2053
diff changeset
178 if (suspended)
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
179 {
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
180 /* Calculate time remaining. */
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
181 /* FIXME: the code in sleep doesn't use this, so there's no
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
182 rush to implement it. */
2058
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
183
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
184 errno = EINTR;
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
185 }
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
186
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
187 /* FIXME: Restore sig handler? */
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
188
2056
cf3d523888c5 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2053
diff changeset
189 return suspended;
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
190 }
7237
119870bc83d7 Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents: 6795
diff changeset
191 #endif