Mercurial > hg > octave-kai > gnulib-hg
comparison lib/nanosleep.c @ 5060:4e59cddb690f
nanosleep merge from coreutils
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Thu, 13 May 2004 22:20:53 +0000 |
parents | e22be96e00a6 |
children | a535859efd14 |
comparison
equal
deleted
inserted
replaced
5059:eb3efcec6ae2 | 5060:4e59cddb690f |
---|---|
1 /* Provide a replacement for the POSIX nanosleep function. | 1 /* Provide a replacement for the POSIX nanosleep function. |
2 Copyright (C) 1999, 2000, 2002 Free Software Foundation, Inc. | 2 Copyright (C) 1999, 2000, 2002, 2004 Free Software Foundation, Inc. |
3 | 3 |
4 This program is free software; you can redistribute it and/or modify | 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 | 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) | 6 the Free Software Foundation; either version 2, or (at your option) |
7 any later version. | 7 any later version. |
43 # define SIGCONT SIGTERM | 43 # define SIGCONT SIGTERM |
44 #endif | 44 #endif |
45 | 45 |
46 #include "timespec.h" | 46 #include "timespec.h" |
47 | 47 |
48 static int suspended; | 48 static sig_atomic_t volatile suspended; |
49 int first_call = 1; | |
50 | 49 |
51 /* Handle SIGCONT. */ | 50 /* Handle SIGCONT. */ |
52 | 51 |
53 static void | 52 static void |
54 sighandler (int sig) | 53 sighandler (int sig) |
61 static void | 60 static void |
62 my_usleep (const struct timespec *ts_delay) | 61 my_usleep (const struct timespec *ts_delay) |
63 { | 62 { |
64 struct timeval tv_delay; | 63 struct timeval tv_delay; |
65 tv_delay.tv_sec = ts_delay->tv_sec; | 64 tv_delay.tv_sec = ts_delay->tv_sec; |
66 tv_delay.tv_usec = ts_delay->tv_nsec / 1000; | 65 tv_delay.tv_usec = (ts_delay->tv_nsec + 999) / 1000; |
66 if (tv_delay.tv_usec == 1000000) | |
67 { | |
68 tv_delay.tv_sec++; | |
69 tv_delay.tv_usec = 0; | |
70 } | |
67 select (0, (void *) 0, (void *) 0, (void *) 0, &tv_delay); | 71 select (0, (void *) 0, (void *) 0, (void *) 0, &tv_delay); |
68 } | 72 } |
69 | 73 |
70 /* FIXME: comment */ | 74 /* FIXME: comment */ |
71 | 75 |
72 int | 76 int |
73 rpl_nanosleep (const struct timespec *requested_delay, | 77 rpl_nanosleep (const struct timespec *requested_delay, |
74 struct timespec *remaining_delay) | 78 struct timespec *remaining_delay) |
75 { | 79 { |
80 static int initialized; | |
81 | |
76 #ifdef SA_NOCLDSTOP | 82 #ifdef SA_NOCLDSTOP |
77 struct sigaction oldact, newact; | 83 struct sigaction oldact, newact; |
78 #endif | 84 #endif |
79 | 85 |
80 suspended = 0; | 86 suspended = 0; |
81 | 87 |
82 /* set up sig handler */ | 88 /* set up sig handler */ |
83 if (first_call) | 89 if (! initialized) |
84 { | 90 { |
85 #ifdef SA_NOCLDSTOP | 91 #ifdef SA_NOCLDSTOP |
86 newact.sa_handler = sighandler; | 92 newact.sa_handler = sighandler; |
87 sigemptyset (&newact.sa_mask); | 93 sigemptyset (&newact.sa_mask); |
88 newact.sa_flags = 0; | 94 newact.sa_flags = 0; |
92 sigaction (SIGCONT, &newact, NULL); | 98 sigaction (SIGCONT, &newact, NULL); |
93 #else | 99 #else |
94 if (signal (SIGCONT, SIG_IGN) != SIG_IGN) | 100 if (signal (SIGCONT, SIG_IGN) != SIG_IGN) |
95 signal (SIGCONT, sighandler); | 101 signal (SIGCONT, sighandler); |
96 #endif | 102 #endif |
97 first_call = 0; | 103 initialized = 1; |
98 } | 104 } |
99 | 105 |
100 my_usleep (requested_delay); | 106 my_usleep (requested_delay); |
101 | 107 |
102 if (suspended) | 108 if (suspended) |