Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/nanosleep.c @ 2687:34703e371994
(SIGCONT): Define if not already defined.
author | Jim Meyering <jim@meyering.net> |
---|---|
date | Sun, 02 Jul 2000 18:23:51 +0000 |
parents | 12885d185e68 |
children | e22be96e00a6 |
rev | line source |
---|---|
2052 | 1 /* Provide a replacement for the POSIX nanosleep function. |
2077 | 2 Copyright (C) 1999, 2000 Free Software Foundation, Inc. |
2052 | 3 |
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) | |
7 any later version. | |
8 | |
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. | |
13 | |
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
17 | |
18 /* written by Jim Meyering */ | |
19 | |
20 #include <config.h> | |
2270 | 21 |
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) */ | |
24 #undef nanosleep | |
25 | |
2053 | 26 #include <stdio.h> |
2052 | 27 #include <sys/types.h> |
2053 | 28 #include <signal.h> |
2052 | 29 |
2057 | 30 #include <errno.h> |
31 #ifndef errno | |
32 extern int errno; | |
33 #endif | |
34 | |
2056 | 35 #if HAVE_UNISTD_H |
36 # include <unistd.h> | |
37 #endif | |
38 | |
2687
34703e371994
(SIGCONT): Define if not already defined.
Jim Meyering <jim@meyering.net>
parents:
2341
diff
changeset
|
39 /* Some systems (MSDOS) don't have SIGCONT. |
34703e371994
(SIGCONT): Define if not already defined.
Jim Meyering <jim@meyering.net>
parents:
2341
diff
changeset
|
40 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
|
41 into a no-op on such systems. */ |
34703e371994
(SIGCONT): Define if not already defined.
Jim Meyering <jim@meyering.net>
parents:
2341
diff
changeset
|
42 #ifndef SIGCONT |
34703e371994
(SIGCONT): Define if not already defined.
Jim Meyering <jim@meyering.net>
parents:
2341
diff
changeset
|
43 # define SIGCONT SIGTERM |
34703e371994
(SIGCONT): Define if not already defined.
Jim Meyering <jim@meyering.net>
parents:
2341
diff
changeset
|
44 #endif |
34703e371994
(SIGCONT): Define if not already defined.
Jim Meyering <jim@meyering.net>
parents:
2341
diff
changeset
|
45 |
2077 | 46 #include "nanosleep.h" |
2052 | 47 |
2056 | 48 static int suspended; |
2058 | 49 int first_call = 1; |
2052 | 50 |
2053 | 51 /* Handle SIGCONT. */ |
52 | |
53 static void | |
54 sighandler (int sig) | |
55 { | |
56 suspended = 1; | |
57 } | |
58 | |
2058 | 59 /* FIXME: comment */ |
2052 | 60 |
61 static void | |
2056 | 62 my_usleep (const struct timespec *ts_delay) |
2052 | 63 { |
64 struct timeval tv_delay; | |
65 tv_delay.tv_sec = ts_delay->tv_sec; | |
2058 | 66 tv_delay.tv_usec = ts_delay->tv_nsec / 1000; |
2056 | 67 select (0, (void *) 0, (void *) 0, (void *) 0, &tv_delay); |
2052 | 68 } |
69 | |
2058 | 70 /* FIXME: comment */ |
71 | |
2052 | 72 int |
2270 | 73 rpl_nanosleep (const struct timespec *requested_delay, |
2341 | 74 struct timespec *remaining_delay) |
2052 | 75 { |
2151 | 76 #ifdef SA_NOCLDSTOP |
2053 | 77 struct sigaction oldact, newact; |
78 #endif | |
79 | |
2056 | 80 suspended = 0; |
2052 | 81 |
2058 | 82 /* set up sig handler */ |
83 if (first_call) | |
84 { | |
2151 | 85 #ifdef SA_NOCLDSTOP |
2058 | 86 newact.sa_handler = sighandler; |
87 sigemptyset (&newact.sa_mask); | |
88 newact.sa_flags = 0; | |
2053 | 89 |
2058 | 90 sigaction (SIGCONT, NULL, &oldact); |
91 if (oldact.sa_handler != SIG_IGN) | |
92 sigaction (SIGCONT, &newact, NULL); | |
2053 | 93 #else |
2058 | 94 if (signal (SIGCONT, SIG_IGN) != SIG_IGN) |
95 signal (SIGCONT, sighandler); | |
2053 | 96 #endif |
2058 | 97 first_call = 0; |
98 } | |
2052 | 99 |
2056 | 100 my_usleep (requested_delay); |
2052 | 101 |
2056 | 102 if (suspended) |
2052 | 103 { |
104 /* Calculate time remaining. */ | |
105 /* FIXME: the code in sleep doesn't use this, so there's no | |
106 rush to implement it. */ | |
2058 | 107 |
108 errno = EINTR; | |
2052 | 109 } |
110 | |
111 /* FIXME: Restore sig handler? */ | |
112 | |
2056 | 113 return suspended; |
2052 | 114 } |