]>
Commit | Line | Data |
---|---|---|
ea4d37b3 | 1 | /* Sleep for a given number of seconds. POSIX.1 version. |
d4697bc9 | 2 | Copyright (C) 1991-2014 Free Software Foundation, Inc. |
c84142e8 | 3 | This file is part of the GNU C Library. |
28f540f4 | 4 | |
c84142e8 | 5 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
28f540f4 | 9 | |
c84142e8 UD |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
28f540f4 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
28f540f4 | 18 | |
28f540f4 RM |
19 | #include <time.h> |
20 | #include <unistd.h> | |
21 | #include <errno.h> | |
ea4d37b3 | 22 | #include <sys/param.h> |
28f540f4 RM |
23 | |
24 | ||
28f540f4 RM |
25 | /* Make the process sleep for SECONDS seconds, or until a signal arrives |
26 | and is not ignored. The function returns the number of seconds less | |
27 | than SECONDS which it actually slept (zero if it slept the full time). | |
28 | If a signal handler does a `longjmp' or modifies the handling of the | |
29 | SIGALRM signal while inside `sleep' call, the handling of the SIGALRM | |
30 | signal afterwards is undefined. There is no return value to indicate | |
31 | error, but if `sleep' returns SECONDS, it probably didn't work. */ | |
32 | unsigned int | |
c84142e8 | 33 | __sleep (unsigned int seconds) |
28f540f4 | 34 | { |
ea4d37b3 | 35 | /* This is not necessary but some buggy programs depend on it. */ |
a1ffb40e | 36 | if (__glibc_unlikely (seconds == 0)) |
28f540f4 | 37 | { |
ea4d37b3 RM |
38 | #ifdef CANCELLATION_P |
39 | CANCELLATION_P (THREAD_SELF); | |
40 | #endif | |
41 | return 0; | |
28f540f4 | 42 | } |
28f540f4 | 43 | |
ea4d37b3 | 44 | int save_errno = errno; |
28f540f4 | 45 | |
ea4d37b3 RM |
46 | const unsigned int max |
47 | = (unsigned int) (((unsigned long int) (~((time_t) 0))) >> 1); | |
48 | struct timespec ts = { 0, 0 }; | |
49 | do | |
50 | { | |
51 | if (sizeof (ts.tv_sec) <= sizeof (seconds)) | |
52 | { | |
53 | /* Since SECONDS is unsigned assigning the value to .tv_sec can | |
54 | overflow it. In this case we have to wait in steps. */ | |
55 | ts.tv_sec += MIN (seconds, max); | |
56 | seconds -= (unsigned int) ts.tv_sec; | |
57 | } | |
58 | else | |
59 | { | |
60 | ts.tv_sec = (time_t) seconds; | |
61 | seconds = 0; | |
62 | } | |
63 | ||
64 | if (__nanosleep (&ts, &ts) < 0) | |
65 | /* We were interrupted. | |
66 | Return the number of (whole) seconds we have not yet slept. */ | |
67 | return seconds + ts.tv_sec; | |
28f540f4 | 68 | } |
ea4d37b3 | 69 | while (seconds > 0); |
28f540f4 | 70 | |
ea4d37b3 | 71 | __set_errno (save_errno); |
28f540f4 | 72 | |
ea4d37b3 | 73 | return 0; |
28f540f4 | 74 | } |
c84142e8 | 75 | weak_alias (__sleep, sleep) |