exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / usleep.c
blob4d325016a5669258393a7e94952757140bfbd7d4
1 /* Pausing execution of the current thread.
2 Copyright (C) 2009-2024 Free Software Foundation, Inc.
3 Written by Eric Blake <ebb9@byu.net>, 2009.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 This file 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser 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 #if defined _WIN32 && ! defined __CYGWIN__
32 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
33 # include <windows.h>
34 #endif
36 #ifndef HAVE_USLEEP
37 # define HAVE_USLEEP 0
38 #endif
40 /* Sleep for MICRO microseconds, which can be greater than 1 second.
41 Return -1 and set errno to EINVAL on range error (about 4295
42 seconds), or 0 on success. Interaction with SIGALARM is
43 unspecified. */
45 int
46 usleep (useconds_t micro)
47 #undef usleep
49 #if defined _WIN32 && ! defined __CYGWIN__
50 unsigned int milliseconds = micro / 1000;
51 if (sizeof milliseconds < sizeof micro && micro / 1000 != milliseconds)
53 errno = EINVAL;
54 return -1;
56 if (micro % 1000)
57 milliseconds++;
58 Sleep (milliseconds);
59 return 0;
60 #else
61 unsigned int seconds = micro / 1000000;
62 if (sizeof seconds < sizeof micro && micro / 1000000 != seconds)
64 errno = EINVAL;
65 return -1;
67 if (!HAVE_USLEEP && micro % 1000000)
68 seconds++;
69 while ((seconds = sleep (seconds)) != 0);
71 # if !HAVE_USLEEP
72 # define usleep(x) 0
73 # endif
74 return usleep (micro % 1000000);
75 #endif