summaryrefslogtreecommitdiffstats
path: root/man2/clock_nanosleep.2
blob: 3904954febb8e58c4ca8eb26fd2bf3182b324b31 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
.\" Copyright (c) 2008, Linux Foundation, written by Michael Kerrisk
.\" <mtk.manpages@gmail.com>
.\"
.\" %%%LICENSE_START(VERBATIM)
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.  The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\" %%%LICENSE_END
.\"
.TH CLOCK_NANOSLEEP 2 2020-04-11 "Linux" "Linux Programmer's Manual"
.SH NAME
clock_nanosleep \- high-resolution sleep with specifiable clock
.SH SYNOPSIS
.B #include <time.h>
.nf
.PP
.BI "int clock_nanosleep(clockid_t " clockid ", int " flags ,
.BI "                    const struct timespec *" request ,
.BI "                    struct timespec *" remain );
.fi
.PP
Link with \fI\-lrt\fP (only for glibc versions before 2.17).
.PP
.ad l
.RS -4
Feature Test Macro Requirements for glibc (see
.BR feature_test_macros (7)):
.RE
.PP
.BR clock_nanosleep ():
.RS 4
_POSIX_C_SOURCE\ >=\ 200112L
.RE
.ad
.SH DESCRIPTION
Like
.BR nanosleep (2),
.BR clock_nanosleep ()
allows the calling thread to sleep for an interval specified
with nanosecond precision.
It differs in allowing the caller to select the clock against
which the sleep interval is to be measured,
and in allowing the sleep interval to be specified as
either an absolute or a relative value.
.PP
The time values passed to and returned by this call are specified using
.I timespec
structures, defined as follows:
.PP
.in +4n
.EX
struct timespec {
    time_t tv_sec;        /* seconds */
    long   tv_nsec;       /* nanoseconds [0 .. 999999999] */
};
.EE
.in
.PP
The
.I clockid
argument specifies the clock against which the sleep interval
is to be measured.
This argument can have one of the following values:
.\" Look in time/posix-timers.c (kernel 5.6 sources) for the
.\" 'struct k_clock' structures that have an 'nsleep' method
.TP
.BR CLOCK_REALTIME
A settable system-wide real-time clock.
.TP
.BR CLOCK_TAI " (since Linux 3.10)"
A system-wide clock derived from wall-clock time but ignoring leap seconds.
.TP
.BR CLOCK_MONOTONIC
A nonsettable, monotonically increasing clock that measures time
since some unspecified point in the past that does not change after
system startup.
.\" On Linux this clock measures time since boot.
.TP
.BR CLOCK_BOOTIME " (since Linux 2.6.39)"
Identical to
.BR CLOCK_MONOTONIC ,
except that it also includes any time that the system is suspended.
.TP
.BR CLOCK_PROCESS_CPUTIME_ID
A settable per-process clock that measures CPU time consumed
by all threads in the process.
.\" There is some trickery between glibc and the kernel
.\" to deal with the CLOCK_PROCESS_CPUTIME_ID case.
.PP
See
.BR clock_getres (2)
for further details on these clocks.
In addition, the CPU clock IDs returned by
.BR clock_getcpuclockid (3)
and
.BR pthread_getcpuclockid (3)
can also be passed in
.IR clockid .
.\" Sleeping against CLOCK_REALTIME_ALARM and CLOCK_BOOTTIME_ALARM
.\" is also possible (tested), with CAP_WAKE_ALARM, but I'm not
.\" sure if this is useful or needs to be documented.
.PP
If
.I flags
is 0, then the value specified in
.I request
is interpreted as an interval relative to the current
value of the clock specified by
.IR clockid .
.PP
If
.I flags
is
.BR TIMER_ABSTIME ,
then
.I request
is interpreted as an absolute time as measured by the clock,
.IR clockid .
If
.I request
is less than or equal to the current value of the clock,
then
.BR clock_nanosleep ()
returns immediately without suspending the calling thread.
.PP
.BR clock_nanosleep ()
suspends the execution of the calling thread
until either at least the time specified by
.IR request
has elapsed,
or a signal is delivered that causes a signal handler to be called or
that terminates the process.
.PP
If the call is interrupted by a signal handler,
.BR clock_nanosleep ()
fails with the error
.BR EINTR .
In addition, if
.I remain
is not NULL, and
.I flags
was not
.BR TIMER_ABSTIME ,
it returns the remaining unslept time in
.IR remain .
This value can then be used to call
.BR clock_nanosleep ()
again and complete a (relative) sleep.
.SH RETURN VALUE
On successfully sleeping for the requested interval,
.BR clock_nanosleep ()
returns 0.
If the call is interrupted by a signal handler or encounters an error,
then it returns one of the positive error number listed in ERRORS.
.SH ERRORS
.TP
.B EFAULT
.I request
or
.I remain
specified an invalid address.
.TP
.B EINTR
The sleep was interrupted by a signal handler; see
.BR signal (7).
.TP
.B EINVAL
The value in the
.I tv_nsec
field was not in the range 0 to 999999999 or
.I tv_sec
was negative.
.TP
.B EINVAL
.I clockid
was invalid.
.RB ( CLOCK_THREAD_CPUTIME_ID
is not a permitted value for
.IR clockid .)
.TP
.B ENOTSUP
The kernel does not support sleeping against this
.IR clockid .
.SH VERSIONS
The
.BR clock_nanosleep ()
system call first appeared in Linux 2.6.
Support is available in glibc since version 2.1.
.SH CONFORMING TO
POSIX.1-2001, POSIX.1-2008.
.SH NOTES
If the interval specified in
.I request
is not an exact multiple of the granularity underlying clock (see
.BR time (7)),
then the interval will be rounded up to the next multiple.
Furthermore, after the sleep completes, there may still be a delay before
the CPU becomes free to once again execute the calling thread.
.PP
Using an absolute timer is useful for preventing
timer drift problems of the type described in
.BR nanosleep (2).
(Such problems are exacerbated in programs that try to restart
a relative sleep that is repeatedly interrupted by signals.)
To perform a relative sleep that avoids these problems, call
.BR clock_gettime (2)
for the desired clock,
add the desired interval to the returned time value,
and then call
.BR clock_nanosleep ()
with the
.B TIMER_ABSTIME
flag.
.PP
.BR clock_nanosleep ()
is never restarted after being interrupted by a signal handler,
regardless of the use of the
.BR sigaction (2)
.B SA_RESTART
flag.
.PP
The
.I remain
argument is unused, and unnecessary, when
.I flags
is
.BR TIMER_ABSTIME .
(An absolute sleep can be restarted using the same
.I request
argument.)
.PP
POSIX.1 specifies that
.BR clock_nanosleep ()
has no effect on signals dispositions or the signal mask.
.PP
POSIX.1 specifies that after changing the value of the
.B CLOCK_REALTIME
clock via
.BR clock_settime (2),
the new clock value shall be used to determine the time
at which a thread blocked on an absolute
.BR clock_nanosleep ()
will wake up;
if the new clock value falls past the end of the sleep interval, then the
.BR clock_nanosleep ()
call will return immediately.
.PP
POSIX.1 specifies that
changing the value of the
.B CLOCK_REALTIME
clock via
.BR clock_settime (2)
shall have no effect on a thread that is blocked on a relative
.BR clock_nanosleep ().
.SH SEE ALSO
.BR clock_getres (2),
.BR nanosleep (2),
.BR restart_syscall (2),
.BR timer_create (2),
.BR sleep (3),
.BR usleep (3),
.BR time (7)
.SH COLOPHON
This page is part of release 5.09 of the Linux
.I man-pages
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
\%https://www.kernel.org/doc/man\-pages/.