1.\" $NetBSD: nanosleep.2,v 1.23 2016/11/14 10:40:59 wiz Exp $ 2.\" 3.\" Copyright (c) 1986, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.\" @(#)sleep.3 8.1 (Berkeley) 6/4/93 31.\" 32.Dd April 3, 2022 33.Dt NANOSLEEP 2 34.Os 35.Sh NAME 36.Nm nanosleep 37.Nd high resolution sleep 38.Sh LIBRARY 39.Lb libc 40.Sh SYNOPSIS 41.In time.h 42.Ft int 43.Fo clock_nanosleep 44.Fa "clockid_t clock_id" 45.Fa "int flags" 46.Fa "const struct timespec *rqtp" 47.Fa "struct timespec *rmtp" 48.Fc 49.Ft int 50.Fo nanosleep 51.Fa "const struct timespec *rqtp" 52.Fa "struct timespec *rmtp" 53.Fc 54.Sh DESCRIPTION 55If the 56.Dv TIMER_ABSTIME 57flag is not set in the 58.Fa flags 59argument, then 60.Fn clock_nanosleep 61suspends execution of the calling thread until either the 62time interval specified by the 63.Fa rqtp 64argument has elapsed, 65or a signal is delivered to the calling process and its 66action is to invoke a signal-catching function or to terminate the 67process. 68The clock used to measure the time is specified by the 69.Fa clock_id 70argument. 71.Pp 72If the 73.Dv TIMER_ABSTIME 74flag is set in the 75.Fa flags 76argument, then 77.Fn clock_nanosleep 78suspends execution of the calling thread until either the value 79of the clock specified by the 80.Fa clock_id 81argument reaches the absolute time specified by the 82.Fa rqtp 83argument, 84or a signal is delivered to the calling process and its 85action is to invoke a signal-catching function or to terminate the 86process. 87If, at the time of the call, the time value specified by 88.Fa rqtp 89is less than or equal to the time value of the specified clock, then 90.Fn clock_nanosleep 91returns immediately and the calling thread is not suspended. 92.Pp 93The suspension time may be longer than requested due to the 94scheduling of other activity by the system. 95It is also subject to the allowed time interval deviation 96specified by the 97.Va kern.timecounter.alloweddeviation 98.Xr sysctl 8 99variable. 100An unmasked signal will terminate the sleep early, regardless of the 101.Dv SA_RESTART 102value on the interrupting signal. 103The 104.Fa rqtp 105and 106.Fa rmtp 107arguments can point to the same object. 108.Pp 109The following 110.Fa clock_id 111values are supported: 112.Pp 113.Bl -item -compact -offset indent 114.It 115CLOCK_MONOTONIC 116.It 117CLOCK_MONOTONIC_FAST 118.It 119CLOCK_MONOTONIC_PRECISE 120.It 121CLOCK_REALTIME 122.It 123CLOCK_REALTIME_FAST 124.It 125CLOCK_REALTIME_PRECISE 126.It 127CLOCK_SECOND 128.It 129CLOCK_UPTIME 130.It 131CLOCK_UPTIME_FAST 132.It 133CLOCK_UPTIME_PRECISE 134.El 135.Pp 136The 137.Fn nanosleep 138function behaves like 139.Fn clock_nanosleep 140with a 141.Fa clock_id 142argument of 143.Dv CLOCK_REALTIME 144and without the 145.Dv TIMER_ABSTIME 146flag in the 147.Fa flags 148argument. 149.Sh RETURN VALUES 150These functions return zero when the requested time has elapsed. 151.Pp 152If these functions return for any other reason, then 153.Fn clock_nanosleep 154will directly return the error number, and 155.Fn nanosleep 156will return \-1 with the global variable 157.Va errno 158set to indicate the error. 159If a relative sleep is interrupted by a signal and 160.Fa rmtp 161is 162.Pf non- Dv NULL , 163the timespec structure it references is updated to contain the 164unslept amount (the request time minus the time actually slept). 165.Sh ERRORS 166These functions can fail with the following errors. 167.Bl -tag -width Er 168.It Bq Er EFAULT 169Either 170.Fa rqtp 171or 172.Fa rmtp 173points to memory that is not a valid part of the process 174address space. 175.It Bq Er EINTR 176The function was interrupted by the delivery of a signal. 177.It Bq Er EINVAL 178The 179.Fa rqtp 180argument specified a nanosecond value less than zero 181or greater than or equal to 1000 million. 182.It Bq Er EINVAL 183The 184.Fa flags 185argument contained an invalid flag. 186.It Bq Er EINVAL 187The 188.Fa clock_id 189argument was 190.Dv CLOCK_THREAD_CPUTIME_ID 191or an unrecognized value. 192.It Bq Er ENOTSUP 193The 194.Fa clock_id 195argument was valid but not supported by this implementation of 196.Fn clock_nanosleep . 197.El 198.Sh SEE ALSO 199.Xr clock_gettime 2 , 200.Xr sigaction 2 , 201.Xr sleep 3 202.Sh STANDARDS 203These functions conform to 204.St -p1003.1-2008 . 205.Sh HISTORY 206The predecessor of this system call, 207.Fn sleep , 208appeared in 209.At v3 , 210but was removed when 211.Xr alarm 3 212was introduced into 213.At v7 . 214The 215.Fn nanosleep 216system call has been available since 217.Nx 1.3 218and was ported to 219.Ox 2.1 220and 221.Fx 3.0 . 222