Coverage Report

Created: 2026-11-12 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wget2/lib/nanosleep.c
Line
Count
Source
1
/* Provide a replacement for the POSIX nanosleep function.
2
3
   Copyright (C) 1999-2000, 2002, 2004-2026 Free Software Foundation, Inc.
4
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.
9
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.
14
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/>.  */
17
18
/* written by Jim Meyering
19
   and Bruno Haible for the native Windows part */
20
21
#include <config.h>
22
23
#include <time.h>
24
25
#include "intprops.h"
26
27
#include <stdio.h>
28
#include <sys/types.h>
29
#include <sys/select.h>
30
#include <signal.h>
31
32
#include <errno.h>
33
34
#include <unistd.h>
35
36
37
enum { BILLION = 1000 * 1000 * 1000 };
38
39
#if HAVE_BUG_BIG_NANOSLEEP
40
41
int
42
nanosleep (const struct timespec *requested_delay,
43
           struct timespec *remaining_delay)
44
# undef nanosleep
45
0
{
46
  /* nanosleep mishandles large sleeps due to internal overflow problems.
47
     The worst known case of this is Linux 2.6.9 with glibc 2.3.4, which
48
     can't sleep more than 24.85 days (2^31 milliseconds).  Similarly,
49
     cygwin 1.5.x, which can't sleep more than 49.7 days (2^32 milliseconds).
50
     Solve this by breaking the sleep up into smaller chunks.  */
51
52
0
  if (requested_delay->tv_nsec < 0 || BILLION <= requested_delay->tv_nsec)
53
0
    {
54
0
      errno = EINVAL;
55
0
      return -1;
56
0
    }
57
58
0
  {
59
    /* Verify that time_t is large enough.  */
60
0
    static_assert (TYPE_MAXIMUM (time_t) / 24 / 24 / 60 / 60);
61
0
    const time_t limit = 24 * 24 * 60 * 60;
62
0
    time_t seconds = requested_delay->tv_sec;
63
0
    struct timespec intermediate = *requested_delay;
64
65
0
    while (limit < seconds)
66
0
      {
67
0
        intermediate.tv_sec = limit;
68
0
        int result = nanosleep (&intermediate, remaining_delay);
69
0
        seconds -= limit;
70
0
        if (result)
71
0
          {
72
0
            if (remaining_delay)
73
0
              remaining_delay->tv_sec += seconds;
74
0
            return result;
75
0
          }
76
0
        intermediate.tv_nsec = 0;
77
0
      }
78
0
    intermediate.tv_sec = seconds;
79
0
    return nanosleep (&intermediate, remaining_delay);
80
0
  }
81
0
}
82
83
#elif defined _WIN32 && ! defined __CYGWIN__
84
/* Native Windows platforms.  */
85
86
# define WIN32_LEAN_AND_MEAN
87
# include <windows.h>
88
89
/* The Windows API function Sleep() has a resolution of about 15 ms and takes
90
   at least 5 ms to execute.  We use this function for longer time periods.
91
   Additionally, we use busy-looping over short time periods, to get a
92
   resolution of about 0.01 ms.  In order to measure such short timespans,
93
   we use the QueryPerformanceCounter() function.  */
94
95
int
96
nanosleep (const struct timespec *requested_delay,
97
           struct timespec *remaining_delay)
98
{
99
  static bool initialized;
100
  /* Number of performance counter increments per nanosecond,
101
     or zero if it could not be determined.  */
102
  static double ticks_per_nanosecond;
103
104
  if (requested_delay->tv_nsec < 0 || BILLION <= requested_delay->tv_nsec)
105
    {
106
      errno = EINVAL;
107
      return -1;
108
    }
109
110
  /* For requested delays of one second or more, 15ms resolution is
111
     sufficient.  */
112
  if (requested_delay->tv_sec == 0)
113
    {
114
      if (!initialized)
115
        {
116
          /* Initialize ticks_per_nanosecond.  */
117
          LARGE_INTEGER ticks_per_second;
118
119
          if (QueryPerformanceFrequency (&ticks_per_second))
120
            ticks_per_nanosecond =
121
              (double) ticks_per_second.QuadPart / 1000000000.0;
122
123
          initialized = true;
124
        }
125
      if (ticks_per_nanosecond)
126
        {
127
          /* QueryPerformanceFrequency worked.  We can use
128
             QueryPerformanceCounter.  Use a combination of Sleep and
129
             busy-looping.  */
130
          /* Number of milliseconds to pass to the Sleep function.
131
             Since Sleep can take up to 8 ms less or 8 ms more than requested
132
             (or maybe more if the system is loaded), we subtract 10 ms.  */
133
          int sleep_millis = (int) requested_delay->tv_nsec / 1000000 - 10;
134
          /* Determine how many ticks to delay.  */
135
          LONGLONG wait_ticks = requested_delay->tv_nsec * ticks_per_nanosecond;
136
          /* Start.  */
137
          LARGE_INTEGER counter_before;
138
          if (QueryPerformanceCounter (&counter_before))
139
            {
140
              /* Wait until the performance counter has reached this value.
141
                 We don't need to worry about overflow, because the performance
142
                 counter is reset at reboot, and with a frequency of 3.6E6
143
                 ticks per second 63 bits suffice for over 80000 years.  */
144
              LONGLONG wait_until = counter_before.QuadPart + wait_ticks;
145
              /* Use Sleep for the longest part.  */
146
              if (sleep_millis > 0)
147
                Sleep (sleep_millis);
148
              /* Busy-loop for the rest.  */
149
              for (;;)
150
                {
151
                  LARGE_INTEGER counter_after;
152
                  if (!QueryPerformanceCounter (&counter_after))
153
                    /* QueryPerformanceCounter failed, but succeeded earlier.
154
                       Should not happen.  */
155
                    break;
156
                  if (counter_after.QuadPart >= wait_until)
157
                    /* The requested time has elapsed.  */
158
                    break;
159
                }
160
              goto done;
161
            }
162
        }
163
    }
164
  /* Implementation for long delays and as fallback.  */
165
  Sleep (requested_delay->tv_sec * 1000 + requested_delay->tv_nsec / 1000000);
166
167
 done:
168
  /* Sleep is not interruptible.  So there is no remaining delay.  */
169
  if (remaining_delay != NULL)
170
    {
171
      remaining_delay->tv_sec = 0;
172
      remaining_delay->tv_nsec = 0;
173
    }
174
  return 0;
175
}
176
177
#else
178
/* Other platforms lacking nanosleep.
179
   It's not clear whether these are still practical porting targets.
180
   For now, just fall back on pselect.  */
181
182
/* Suspend execution for at least *REQUESTED_DELAY seconds.  The
183
   *REMAINING_DELAY part isn't implemented yet.  */
184
185
int
186
nanosleep (const struct timespec *requested_delay,
187
           struct timespec *remaining_delay)
188
{
189
  return pselect (0, NULL, NULL, NULL, requested_delay, NULL);
190
}
191
#endif