*-map tests: Fix compilation error.
[gnulib.git] / lib / usleep.c
blobf7e248027fbf98387d83df7657c9302930b9d8c3
1 /* Pausing execution of the current thread.
2 Copyright (C) 2009-2019 Free Software Foundation, Inc.
3 Written by Eric Blake <ebb9@byu.net>, 2009.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* This file is _intentionally_ light-weight. Rather than using
19 select or nanosleep, both of which drag in external libraries on
20 some platforms, this merely rounds up to the nearest second if
21 usleep() does not exist. If sub-second resolution is important,
22 then use a more powerful interface to begin with. */
24 #include <config.h>
26 /* Specification. */
27 #include <unistd.h>
29 #include <errno.h>
31 #ifndef HAVE_USLEEP
32 # define HAVE_USLEEP 0
33 #endif
35 /* Sleep for MICRO microseconds, which can be greater than 1 second.
36 Return -1 and set errno to EINVAL on range error (about 4295
37 seconds), or 0 on success. Interaction with SIGALARM is
38 unspecified. */
40 int
41 usleep (useconds_t micro)
43 unsigned int seconds = micro / 1000000;
44 if (sizeof seconds < sizeof micro && micro / 1000000 != seconds)
46 errno = EINVAL;
47 return -1;
49 if (!HAVE_USLEEP && micro % 1000000)
50 seconds++;
51 while ((seconds = sleep (seconds)) != 0);
53 #undef usleep
54 #if !HAVE_USLEEP
55 # define usleep(x) 0
56 #endif
57 return usleep (micro % 1000000);