]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/clock_nanosleep.2
9019fac2e3ef4451688879f5f4e935de9a1e05f8
[thirdparty/man-pages.git] / man2 / clock_nanosleep.2
1 .\" Copyright (c) 2008, Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .TH CLOCK_NANOSLEEP 2 2021-03-22 "Linux man-pages (unreleased)" "Linux Programmer's Manual"
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 .PP
14 Before glibc 2.17,
15 Real-time library
16 .RI ( librt ", " \-lrt )
17 .SH SYNOPSIS
18 .B #include <time.h>
19 .nf
20 .PP
21 .BI "int clock_nanosleep(clockid_t " clockid ", int " flags ,
22 .BI " const struct timespec *" request ,
23 .BI " struct timespec *" remain );
24 .fi
25 .PP
26 .RS -4
27 Feature Test Macro Requirements for glibc (see
28 .BR feature_test_macros (7)):
29 .RE
30 .PP
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 .PP
46 The time values passed to and returned by this call are specified using
47 .BR timespec (3)
48 structures.
49 .PP
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 ignoring 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 .PP
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 .PP
94 If
95 .I flags
96 is 0, then the value specified in
97 .I request
98 is interpreted as an interval relative to the current
99 value of the clock specified by
100 .IR clockid .
101 .PP
102 If
103 .I flags
104 is
105 .BR TIMER_ABSTIME ,
106 then
107 .I request
108 is interpreted as an absolute time as measured by the clock,
109 .IR clockid .
110 If
111 .I request
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 .PP
117 .BR clock_nanosleep ()
118 suspends the execution of the calling thread
119 until either at least the time specified by
120 .I request
121 has elapsed,
122 or a signal is delivered that causes a signal handler to be called or
123 that terminates the process.
124 .PP
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 request
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 to 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 VERSIONS
176 The
177 .BR clock_nanosleep ()
178 system call first appeared in Linux 2.6.
179 Support is available in glibc since version 2.1.
180 .SH STANDARDS
181 POSIX.1-2001, POSIX.1-2008.
182 .SH NOTES
183 If the interval specified in
184 .I request
185 is not an exact multiple of the granularity underlying clock (see
186 .BR time (7)),
187 then the interval will be rounded up to the next multiple.
188 Furthermore, after the sleep completes, there may still be a delay before
189 the CPU becomes free to once again execute the calling thread.
190 .PP
191 Using an absolute timer is useful for preventing
192 timer drift problems of the type described in
193 .BR nanosleep (2).
194 (Such problems are exacerbated in programs that try to restart
195 a relative sleep that is repeatedly interrupted by signals.)
196 To perform a relative sleep that avoids these problems, call
197 .BR clock_gettime (2)
198 for the desired clock,
199 add the desired interval to the returned time value,
200 and then call
201 .BR clock_nanosleep ()
202 with the
203 .B TIMER_ABSTIME
204 flag.
205 .PP
206 .BR clock_nanosleep ()
207 is never restarted after being interrupted by a signal handler,
208 regardless of the use of the
209 .BR sigaction (2)
210 .B SA_RESTART
211 flag.
212 .PP
213 The
214 .I remain
215 argument is unused, and unnecessary, when
216 .I flags
217 is
218 .BR TIMER_ABSTIME .
219 (An absolute sleep can be restarted using the same
220 .I request
221 argument.)
222 .PP
223 POSIX.1 specifies that
224 .BR clock_nanosleep ()
225 has no effect on signals dispositions or the signal mask.
226 .PP
227 POSIX.1 specifies that after changing the value of the
228 .B CLOCK_REALTIME
229 clock via
230 .BR clock_settime (2),
231 the new clock value shall be used to determine the time
232 at which a thread blocked on an absolute
233 .BR clock_nanosleep ()
234 will wake up;
235 if the new clock value falls past the end of the sleep interval, then the
236 .BR clock_nanosleep ()
237 call will return immediately.
238 .PP
239 POSIX.1 specifies that
240 changing the value of the
241 .B CLOCK_REALTIME
242 clock via
243 .BR clock_settime (2)
244 shall have no effect on a thread that is blocked on a relative
245 .BR clock_nanosleep ().
246 .SH SEE ALSO
247 .BR clock_getres (2),
248 .BR nanosleep (2),
249 .BR restart_syscall (2),
250 .BR timer_create (2),
251 .BR sleep (3),
252 .BR timespec (3),
253 .BR usleep (3),
254 .BR time (7)