annotate lib/sleep.c @ 423:079d43b6967b 3.0-beta2 3.0-beta2

Added missing file
author lost@l-w.ca
date Sun, 19 Sep 2010 01:44:47 -0600
parents 3c2e1f24cc7d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
421
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1 /* Pausing execution of the current thread.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2 Copyright (C) 2007, 2009, 2010 Free Software Foundation, Inc.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2007.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
8 (at your option) any later version.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
9
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
13 GNU General Public License for more details.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
14
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
17
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
18 #include <config.h>
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
19
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
20 /* Specification. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
21 #include <unistd.h>
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
22
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
23 #include <limits.h>
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
24
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
25 #include "verify.h"
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
26
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
27 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
28
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
29 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
30 # include <windows.h>
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
31
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
32 unsigned int
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
33 sleep (unsigned int seconds)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
34 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
35 unsigned int remaining;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
36
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
37 /* Sleep for 1 second many times, because
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
38 1. Sleep is not interruptiple by Ctrl-C,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
39 2. we want to avoid arithmetic overflow while multiplying with 1000. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
40 for (remaining = seconds; remaining > 0; remaining--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
41 Sleep (1000);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
42
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
43 return remaining;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
44 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
45
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
46 #elif HAVE_SLEEP
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
47
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
48 # undef sleep
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
49
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
50 /* Guarantee unlimited sleep and a reasonable return value. Cygwin
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
51 1.5.x rejects attempts to sleep more than 49.7 days (2**32
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
52 milliseconds), but uses uninitialized memory which results in a
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
53 garbage answer. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
54 unsigned int
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
55 rpl_sleep (unsigned int seconds)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
56 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
57 /* This requires int larger than 16 bits. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
58 verify (UINT_MAX / 49 / 24 / 60 / 60);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
59 const unsigned int limit = 49 * 24 * 60 * 60;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
60 while (limit < seconds)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
61 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
62 unsigned int result;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
63 seconds -= limit;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
64 result = sleep (limit);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
65 if (result)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
66 return seconds + result;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
67 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
68 return sleep (seconds);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
69 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
70
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
71 #else /* !HAVE_SLEEP */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
72
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
73 #error "Please port gnulib sleep.c to your platform, possibly using usleep() or select(), then report this to bug-gnulib."
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
74
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
75 #endif