18269e767SBrooks Davis.\" $NetBSD: nanosleep.2,v 1.23 2016/11/14 10:40:59 wiz Exp $ 28269e767SBrooks Davis.\" 38269e767SBrooks Davis.\" Copyright (c) 1986, 1991, 1993 48269e767SBrooks Davis.\" The Regents of the University of California. All rights reserved. 58269e767SBrooks Davis.\" 68269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without 78269e767SBrooks Davis.\" modification, are permitted provided that the following conditions 88269e767SBrooks Davis.\" are met: 98269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright 108269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer. 118269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright 128269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer in the 138269e767SBrooks Davis.\" documentation and/or other materials provided with the distribution. 148269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors 158269e767SBrooks Davis.\" may be used to endorse or promote products derived from this software 168269e767SBrooks Davis.\" without specific prior written permission. 178269e767SBrooks Davis.\" 188269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 198269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 208269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 218269e767SBrooks Davis.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 228269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 238269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 248269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 258269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 268269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 278269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 288269e767SBrooks Davis.\" SUCH DAMAGE. 298269e767SBrooks Davis.\" 30*7b7ba785SNathan Whitehorn.Dd May 3, 2025 318269e767SBrooks Davis.Dt NANOSLEEP 2 328269e767SBrooks Davis.Os 338269e767SBrooks Davis.Sh NAME 348269e767SBrooks Davis.Nm nanosleep 358269e767SBrooks Davis.Nd high resolution sleep 368269e767SBrooks Davis.Sh LIBRARY 378269e767SBrooks Davis.Lb libc 388269e767SBrooks Davis.Sh SYNOPSIS 398269e767SBrooks Davis.In time.h 408269e767SBrooks Davis.Ft int 418269e767SBrooks Davis.Fo clock_nanosleep 428269e767SBrooks Davis.Fa "clockid_t clock_id" 438269e767SBrooks Davis.Fa "int flags" 448269e767SBrooks Davis.Fa "const struct timespec *rqtp" 458269e767SBrooks Davis.Fa "struct timespec *rmtp" 468269e767SBrooks Davis.Fc 478269e767SBrooks Davis.Ft int 488269e767SBrooks Davis.Fo nanosleep 498269e767SBrooks Davis.Fa "const struct timespec *rqtp" 508269e767SBrooks Davis.Fa "struct timespec *rmtp" 518269e767SBrooks Davis.Fc 528269e767SBrooks Davis.Sh DESCRIPTION 538269e767SBrooks DavisIf the 548269e767SBrooks Davis.Dv TIMER_ABSTIME 558269e767SBrooks Davisflag is not set in the 568269e767SBrooks Davis.Fa flags 578269e767SBrooks Davisargument, then 588269e767SBrooks Davis.Fn clock_nanosleep 598269e767SBrooks Davissuspends execution of the calling thread until either the 608269e767SBrooks Davistime interval specified by the 618269e767SBrooks Davis.Fa rqtp 628269e767SBrooks Davisargument has elapsed, 638269e767SBrooks Davisor a signal is delivered to the calling process and its 648269e767SBrooks Davisaction is to invoke a signal-catching function or to terminate the 658269e767SBrooks Davisprocess. 668269e767SBrooks DavisThe clock used to measure the time is specified by the 678269e767SBrooks Davis.Fa clock_id 688269e767SBrooks Davisargument. 698269e767SBrooks Davis.Pp 708269e767SBrooks DavisIf the 718269e767SBrooks Davis.Dv TIMER_ABSTIME 728269e767SBrooks Davisflag is set in the 738269e767SBrooks Davis.Fa flags 748269e767SBrooks Davisargument, then 758269e767SBrooks Davis.Fn clock_nanosleep 768269e767SBrooks Davissuspends execution of the calling thread until either the value 778269e767SBrooks Davisof the clock specified by the 788269e767SBrooks Davis.Fa clock_id 798269e767SBrooks Davisargument reaches the absolute time specified by the 808269e767SBrooks Davis.Fa rqtp 818269e767SBrooks Davisargument, 828269e767SBrooks Davisor a signal is delivered to the calling process and its 838269e767SBrooks Davisaction is to invoke a signal-catching function or to terminate the 848269e767SBrooks Davisprocess. 858269e767SBrooks DavisIf, at the time of the call, the time value specified by 868269e767SBrooks Davis.Fa rqtp 878269e767SBrooks Davisis less than or equal to the time value of the specified clock, then 888269e767SBrooks Davis.Fn clock_nanosleep 898269e767SBrooks Davisreturns immediately and the calling thread is not suspended. 908269e767SBrooks DavisAn unmasked signal will terminate the sleep early, regardless of the 918269e767SBrooks Davis.Dv SA_RESTART 928269e767SBrooks Davisvalue on the interrupting signal. 938269e767SBrooks DavisThe 948269e767SBrooks Davis.Fa rqtp 958269e767SBrooks Davisand 968269e767SBrooks Davis.Fa rmtp 978269e767SBrooks Davisarguments can point to the same object. 988269e767SBrooks Davis.Pp 998269e767SBrooks DavisThe following 1008269e767SBrooks Davis.Fa clock_id 1018269e767SBrooks Davisvalues are supported: 1028269e767SBrooks Davis.Pp 1038269e767SBrooks Davis.Bl -item -compact -offset indent 1048269e767SBrooks Davis.It 1058269e767SBrooks DavisCLOCK_MONOTONIC 1068269e767SBrooks Davis.It 1078269e767SBrooks DavisCLOCK_MONOTONIC_FAST 1088269e767SBrooks Davis.It 1098269e767SBrooks DavisCLOCK_MONOTONIC_PRECISE 1108269e767SBrooks Davis.It 1118269e767SBrooks DavisCLOCK_REALTIME 1128269e767SBrooks Davis.It 1138269e767SBrooks DavisCLOCK_REALTIME_FAST 1148269e767SBrooks Davis.It 1158269e767SBrooks DavisCLOCK_REALTIME_PRECISE 1168269e767SBrooks Davis.It 1178269e767SBrooks DavisCLOCK_SECOND 1188269e767SBrooks Davis.It 119*7b7ba785SNathan WhitehornCLOCK_TAI 120*7b7ba785SNathan Whitehorn.It 1218269e767SBrooks DavisCLOCK_UPTIME 1228269e767SBrooks Davis.It 1238269e767SBrooks DavisCLOCK_UPTIME_FAST 1248269e767SBrooks Davis.It 1258269e767SBrooks DavisCLOCK_UPTIME_PRECISE 1268269e767SBrooks Davis.El 1278269e767SBrooks Davis.Pp 128626ea75eSGleb SmirnoffThe suspension time may be longer than requested due to the 129626ea75eSGleb Smirnoffscheduling of other activity by the system. 130626ea75eSGleb SmirnoffThe clocks with the 131626ea75eSGleb Smirnoff.Dv _FAST 132626ea75eSGleb Smirnoffsuffix and the 133626ea75eSGleb Smirnoff.Dv CLOCK_SECOND 134626ea75eSGleb Smirnoffare subject to the allowed time interval deviation specified by the 135626ea75eSGleb Smirnoff.Va kern.timecounter.alloweddeviation 136626ea75eSGleb Smirnoff.Xr sysctl 8 137626ea75eSGleb Smirnoffvariable. 138626ea75eSGleb SmirnoffThe clocks with the 139626ea75eSGleb Smirnoff.Dv _PRECISE 140626ea75eSGleb Smirnoffsuffix are always as precise as possible. 141626ea75eSGleb SmirnoffThe 142626ea75eSGleb Smirnoff.Dv CLOCK_MONOTONIC , 143626ea75eSGleb Smirnoff.Dv CLOCK_REALTIME 144626ea75eSGleb Smirnoffand 145626ea75eSGleb Smirnoff.Dv CLOCK_UPTIME 146626ea75eSGleb Smirnoffare precise by default. 147626ea75eSGleb SmirnoffSetting the 148626ea75eSGleb Smirnoff.Va kern.timecounter.nanosleep_precise 149626ea75eSGleb Smirnoff.Xr sysctl 8 150626ea75eSGleb Smirnoffto a false value would make those clocks to behave like the 151626ea75eSGleb Smirnoff.Dv _FAST 152626ea75eSGleb Smirnoffclocks. 153626ea75eSGleb Smirnoff.Pp 1548269e767SBrooks DavisThe 1558269e767SBrooks Davis.Fn nanosleep 1568269e767SBrooks Davisfunction behaves like 1578269e767SBrooks Davis.Fn clock_nanosleep 1588269e767SBrooks Daviswith a 1598269e767SBrooks Davis.Fa clock_id 1608269e767SBrooks Davisargument of 1618269e767SBrooks Davis.Dv CLOCK_REALTIME 1628269e767SBrooks Davisand without the 1638269e767SBrooks Davis.Dv TIMER_ABSTIME 1648269e767SBrooks Davisflag in the 1658269e767SBrooks Davis.Fa flags 1668269e767SBrooks Davisargument. 1678269e767SBrooks Davis.Sh RETURN VALUES 1688269e767SBrooks DavisThese functions return zero when the requested time has elapsed. 1698269e767SBrooks Davis.Pp 1708269e767SBrooks DavisIf these functions return for any other reason, then 1718269e767SBrooks Davis.Fn clock_nanosleep 1728269e767SBrooks Daviswill directly return the error number, and 1738269e767SBrooks Davis.Fn nanosleep 1748269e767SBrooks Daviswill return \-1 with the global variable 1758269e767SBrooks Davis.Va errno 1768269e767SBrooks Davisset to indicate the error. 1778269e767SBrooks DavisIf a relative sleep is interrupted by a signal and 1788269e767SBrooks Davis.Fa rmtp 1798269e767SBrooks Davisis 1808269e767SBrooks Davis.Pf non- Dv NULL , 1818269e767SBrooks Davisthe timespec structure it references is updated to contain the 1828269e767SBrooks Davisunslept amount (the request time minus the time actually slept). 1838269e767SBrooks Davis.Sh ERRORS 1848269e767SBrooks DavisThese functions can fail with the following errors. 1858269e767SBrooks Davis.Bl -tag -width Er 1868269e767SBrooks Davis.It Bq Er EFAULT 1878269e767SBrooks DavisEither 1888269e767SBrooks Davis.Fa rqtp 1898269e767SBrooks Davisor 1908269e767SBrooks Davis.Fa rmtp 1918269e767SBrooks Davispoints to memory that is not a valid part of the process 1928269e767SBrooks Davisaddress space. 1938269e767SBrooks Davis.It Bq Er EINTR 1948269e767SBrooks DavisThe function was interrupted by the delivery of a signal. 1958269e767SBrooks Davis.It Bq Er EINVAL 1968269e767SBrooks DavisThe 1978269e767SBrooks Davis.Fa rqtp 1988269e767SBrooks Davisargument specified a nanosecond value less than zero 1998269e767SBrooks Davisor greater than or equal to 1000 million. 2008269e767SBrooks Davis.It Bq Er EINVAL 2018269e767SBrooks DavisThe 2028269e767SBrooks Davis.Fa flags 2038269e767SBrooks Davisargument contained an invalid flag. 2048269e767SBrooks Davis.It Bq Er EINVAL 2058269e767SBrooks DavisThe 2068269e767SBrooks Davis.Fa clock_id 2078269e767SBrooks Davisargument was 2088269e767SBrooks Davis.Dv CLOCK_THREAD_CPUTIME_ID 2098269e767SBrooks Davisor an unrecognized value. 2108269e767SBrooks Davis.It Bq Er ENOTSUP 2118269e767SBrooks DavisThe 2128269e767SBrooks Davis.Fa clock_id 2138269e767SBrooks Davisargument was valid but not supported by this implementation of 2148269e767SBrooks Davis.Fn clock_nanosleep . 2158269e767SBrooks Davis.El 2168269e767SBrooks Davis.Sh SEE ALSO 2178269e767SBrooks Davis.Xr clock_gettime 2 , 2188269e767SBrooks Davis.Xr sigaction 2 , 2198269e767SBrooks Davis.Xr sleep 3 2208269e767SBrooks Davis.Sh STANDARDS 2218269e767SBrooks DavisThese functions conform to 2228269e767SBrooks Davis.St -p1003.1-2008 . 2238269e767SBrooks Davis.Sh HISTORY 2248269e767SBrooks DavisThe predecessor of this system call, 2258269e767SBrooks Davis.Fn sleep , 2268269e767SBrooks Davisappeared in 2278269e767SBrooks Davis.At v3 , 2288269e767SBrooks Davisbut was removed when 2298269e767SBrooks Davis.Xr alarm 3 2308269e767SBrooks Daviswas introduced into 2318269e767SBrooks Davis.At v7 . 2328269e767SBrooks DavisThe 2338269e767SBrooks Davis.Fn nanosleep 2348269e767SBrooks Davissystem call has been available since 2358269e767SBrooks Davis.Nx 1.3 2368269e767SBrooks Davisand was ported to 2378269e767SBrooks Davis.Ox 2.1 2388269e767SBrooks Davisand 2398269e767SBrooks Davis.Fx 3.0 . 240626ea75eSGleb SmirnoffThe 241626ea75eSGleb Smirnoff.Fn clock_nanosleep 242626ea75eSGleb Smirnoffsystem call has been available since 243626ea75eSGleb Smirnoff.Fx 11.1 . 244626ea75eSGleb Smirnoff.Pp 245626ea75eSGleb SmirnoffIn 246626ea75eSGleb Smirnoff.Fx 15.0 247626ea75eSGleb Smirnoffthe default behavior of 248626ea75eSGleb Smirnoff.Fn clock_nanosleep 249626ea75eSGleb Smirnoffwith 250626ea75eSGleb Smirnoff.Dv CLOCK_MONOTONIC , 251626ea75eSGleb Smirnoff.Dv CLOCK_REALTIME , 252626ea75eSGleb Smirnoff.Dv CLOCK_UPTIME 253626ea75eSGleb Smirnoffclocks and 254626ea75eSGleb Smirnoff.Fn nanosleep 255626ea75eSGleb Smirnoffhas been switched to use precise clock. 256