Debian Codegraph @

upstream/debian-trixie/man2/clock_nanosleep.2

raw
1 .\" Copyright (c) 2008, Linux Foundation, written by Michael Kerrisk
2 .\" <[email protected]>
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .TH clock_nanosleep 2 2024-05-02 "Linux man-pages 6.9.1"
7 .SH NAME
8 clock_nanosleep \- high-resolution sleep with specifiable clock
9 .SH LIBRARY
10 Standard C library
11 .RI ( libc ", " \-lc ),
12 since glibc 2.17
13 .P
14 Before glibc 2.17,
15 Real-time library
16 .RI ( librt ", " \-lrt )
17 .SH SYNOPSIS
18 .B #include <time.h>
19 .nf
20 .P
21 .BI "int clock_nanosleep(clockid_t " clockid ", int " flags ,
22 .BI " const struct timespec *" t ,
23 .BI " struct timespec *_Nullable " remain );
24 .fi
25 .P
26 .RS -4
27 Feature Test Macro Requirements for glibc (see
28 .BR feature_test_macros (7)):
29 .RE
30 .P
31 .BR clock_nanosleep ():
32 .nf
33 _POSIX_C_SOURCE >= 200112L
34 .fi
35 .SH DESCRIPTION
36 Like
37 .BR nanosleep (2),
38 .BR clock_nanosleep ()
39 allows the calling thread to sleep for an interval specified
40 with nanosecond precision.
41 It differs in allowing the caller to select the clock against
42 which the sleep interval is to be measured,
43 and in allowing the sleep interval to be specified as
44 either an absolute or a relative value.
45 .P
46 The time values passed to and returned by this call are specified using
47 .BR timespec (3)
48 structures.
49 .P
50 The
51 .I clockid
52 argument specifies the clock against which the sleep interval
53 is to be measured.
54 This argument can have one of the following values:
55 .\" Look in time/posix-timers.c (kernel 5.6 sources) for the
56 .\" 'struct k_clock' structures that have an 'nsleep' method
57 .TP
58 .B CLOCK_REALTIME
59 A settable system-wide real-time clock.
60 .TP
61 .BR CLOCK_TAI " (since Linux 3.10)"
62 A system-wide clock derived from wall-clock time but counting leap seconds.
63 .TP
64 .B CLOCK_MONOTONIC
65 A nonsettable, monotonically increasing clock that measures time
66 since some unspecified point in the past that does not change after
67 system startup.
68 .\" On Linux this clock measures time since boot.
69 .TP
70 .BR CLOCK_BOOTTIME " (since Linux 2.6.39)"
71 Identical to
72 .BR CLOCK_MONOTONIC ,
73 except that it also includes any time that the system is suspended.
74 .TP
75 .B CLOCK_PROCESS_CPUTIME_ID
76 A settable per-process clock that measures CPU time consumed
77 by all threads in the process.
78 .\" There is some trickery between glibc and the kernel
79 .\" to deal with the CLOCK_PROCESS_CPUTIME_ID case.
80 .P
81 See
82 .BR clock_getres (2)
83 for further details on these clocks.
84 In addition, the CPU clock IDs returned by
85 .BR clock_getcpuclockid (3)
86 and
87 .BR pthread_getcpuclockid (3)
88 can also be passed in
89 .IR clockid .
90 .\" Sleeping against CLOCK_REALTIME_ALARM and CLOCK_BOOTTIME_ALARM
91 .\" is also possible (tested), with CAP_WAKE_ALARM, but I'm not
92 .\" sure if this is useful or needs to be documented.
93 .P
94 If
95 .I flags
96 is 0, then the value specified in
97 .I t
98 is interpreted as an interval relative to the current
99 value of the clock specified by
100 .IR clockid .
101 .P
102 If
103 .I flags
104 is
105 .BR TIMER_ABSTIME ,
106 then
107 .I t
108 is interpreted as an absolute time as measured by the clock,
109 .IR clockid .
110 If
111 .I t
112 is less than or equal to the current value of the clock,
113 then
114 .BR clock_nanosleep ()
115 returns immediately without suspending the calling thread.
116 .P
117 .BR clock_nanosleep ()
118 suspends the execution of the calling thread
119 until either at least the time specified by
120 .I t
121 has elapsed,
122 or a signal is delivered that causes a signal handler to be called or
123 that terminates the process.
124 .P
125 If the call is interrupted by a signal handler,
126 .BR clock_nanosleep ()
127 fails with the error
128 .BR EINTR .
129 In addition, if
130 .I remain
131 is not NULL, and
132 .I flags
133 was not
134 .BR TIMER_ABSTIME ,
135 it returns the remaining unslept time in
136 .IR remain .
137 This value can then be used to call
138 .BR clock_nanosleep ()
139 again and complete a (relative) sleep.
140 .SH RETURN VALUE
141 On successfully sleeping for the requested interval,
142 .BR clock_nanosleep ()
143 returns 0.
144 If the call is interrupted by a signal handler or encounters an error,
145 then it returns one of the positive error number listed in ERRORS.
146 .SH ERRORS
147 .TP
148 .B EFAULT
149 .I t
150 or
151 .I remain
152 specified an invalid address.
153 .TP
154 .B EINTR
155 The sleep was interrupted by a signal handler; see
156 .BR signal (7).
157 .TP
158 .B EINVAL
159 The value in the
160 .I tv_nsec
161 field was not in the range [0, 999999999] or
162 .I tv_sec
163 was negative.
164 .TP
165 .B EINVAL
166 .I clockid
167 was invalid.
168 .RB ( CLOCK_THREAD_CPUTIME_ID
169 is not a permitted value for
170 .IR clockid .)
171 .TP
172 .B ENOTSUP
173 The kernel does not support sleeping against this
174 .IR clockid .
175 .SH STANDARDS
176 POSIX.1-2008.
177 .SH HISTORY
178 POSIX.1-2001.
179 Linux 2.6,
180 glibc 2.1.
181 .SH NOTES
182 If the interval specified in
183 .I t
184 is not an exact multiple of the granularity underlying clock (see
185 .BR time (7)),
186 then the interval will be rounded up to the next multiple.
187 Furthermore, after the sleep completes, there may still be a delay before
188 the CPU becomes free to once again execute the calling thread.
189 .P
190 Using an absolute timer is useful for preventing
191 timer drift problems of the type described in
192 .BR nanosleep (2).
193 (Such problems are exacerbated in programs that try to restart
194 a relative sleep that is repeatedly interrupted by signals.)
195 To perform a relative sleep that avoids these problems, call
196 .BR clock_gettime (2)
197 for the desired clock,
198 add the desired interval to the returned time value,
199 and then call
200 .BR clock_nanosleep ()
201 with the
202 .B TIMER_ABSTIME
203 flag.
204 .P
205 .BR clock_nanosleep ()
206 is never restarted after being interrupted by a signal handler,
207 regardless of the use of the
208 .BR sigaction (2)
209 .B SA_RESTART
210 flag.
211 .P
212 The
213 .I remain
214 argument is unused, and unnecessary, when
215 .I flags
216 is
217 .BR TIMER_ABSTIME .
218 (An absolute sleep can be restarted using the same
219 .I t
220 argument.)
221 .P
222 POSIX.1 specifies that
223 .BR clock_nanosleep ()
224 has no effect on signals dispositions or the signal mask.
225 .P
226 POSIX.1 specifies that after changing the value of the
227 .B CLOCK_REALTIME
228 clock via
229 .BR clock_settime (2),
230 the new clock value shall be used to determine the time
231 at which a thread blocked on an absolute
232 .BR clock_nanosleep ()
233 will wake up;
234 if the new clock value falls past the end of the sleep interval, then the
235 .BR clock_nanosleep ()
236 call will return immediately.
237 .P
238 POSIX.1 specifies that
239 changing the value of the
240 .B CLOCK_REALTIME
241 clock via
242 .BR clock_settime (2)
243 shall have no effect on a thread that is blocked on a relative
244 .BR clock_nanosleep ().
245 .SH SEE ALSO
246 .BR clock_getres (2),
247 .BR nanosleep (2),
248 .BR restart_syscall (2),
249 .BR timer_create (2),
250 .BR sleep (3),
251 .BR timespec (3),
252 .BR usleep (3),
253 .BR time (7)