xref: /openbsd-src/share/man/man9/tsleep.9 (revision 41ce3b17e73f6b7d2d9e1a3d961e4bab2d895cb5)
1*41ce3b17Snaddy.\"	$OpenBSD: tsleep.9,v 1.16 2022/03/31 17:27:23 naddy Exp $
216b67135Smk.\"	$NetBSD: sleep.9,v 1.11 1999/03/24 06:15:12 mycroft Exp $
316b67135Smk.\"
416b67135Smk.\" Copyright (c) 1996 The NetBSD Foundation, Inc.
516b67135Smk.\" All rights reserved.
616b67135Smk.\"
716b67135Smk.\" This code is derived from software contributed to The NetBSD Foundation
816b67135Smk.\" by Paul Kranenburg.
916b67135Smk.\"
1016b67135Smk.\" Redistribution and use in source and binary forms, with or without
1116b67135Smk.\" modification, are permitted provided that the following conditions
1216b67135Smk.\" are met:
1316b67135Smk.\" 1. Redistributions of source code must retain the above copyright
1416b67135Smk.\"    notice, this list of conditions and the following disclaimer.
1516b67135Smk.\" 2. Redistributions in binary form must reproduce the above copyright
1616b67135Smk.\"    notice, this list of conditions and the following disclaimer in the
1716b67135Smk.\"    documentation and/or other materials provided with the distribution.
1816b67135Smk.\"
1916b67135Smk.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2016b67135Smk.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2116b67135Smk.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2216b67135Smk.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2316b67135Smk.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2416b67135Smk.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2516b67135Smk.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2616b67135Smk.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2716b67135Smk.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2816b67135Smk.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2916b67135Smk.\" POSSIBILITY OF SUCH DAMAGE.
3016b67135Smk.\"
31*41ce3b17Snaddy.Dd $Mdocdate: March 31 2022 $
3216b67135Smk.Dt TSLEEP 9
3316b67135Smk.Os
3416b67135Smk.Sh NAME
3516b67135Smk.Nm tsleep ,
364bc97b15Scheloha.Nm tsleep_nsec ,
3716c19d6fSart.Nm msleep ,
384bc97b15Scheloha.Nm msleep_nsec ,
39c4a692e1Smpi.Nm rwsleep ,
404bc97b15Scheloha.Nm rwsleep_nsec ,
41afb7b041Sjmc.Nm wakeup ,
42afb7b041Sjmc.Nm wakeup_n ,
43afb7b041Sjmc.Nm wakeup_one
4416b67135Smk.Nd process context sleep and wakeup
4516b67135Smk.Sh SYNOPSIS
46dddd2645Sschwarze.In sys/param.h
47dddd2645Sschwarze.In sys/systm.h
484bc97b15Scheloha.Fd #define INFSLP	UINT64_MAX
49375723adScheloha.Fd #define MAXTSLP	(UINT64_MAX - 1)
5016b67135Smk.Ft int
514bc97b15Scheloha.Fo tsleep
524bc97b15Scheloha.Fa "void *ident"
534bc97b15Scheloha.Fa "int priority"
544bc97b15Scheloha.Fa "const char *wmesg"
554bc97b15Scheloha.Fa "int timo"
564bc97b15Scheloha.Fc
5716c19d6fSart.Ft int
584bc97b15Scheloha.Fo tsleep_nsec
594bc97b15Scheloha.Fa "void *ident"
604bc97b15Scheloha.Fa "int priority"
614bc97b15Scheloha.Fa "const char *wmesg"
624bc97b15Scheloha.Fa "uint64_t nsecs"
634bc97b15Scheloha.Fc
64c4a692e1Smpi.Ft int
654bc97b15Scheloha.Fo msleep
664bc97b15Scheloha.Fa "void *ident"
674bc97b15Scheloha.Fa "struct mutex *mtx"
684bc97b15Scheloha.Fa "int priority"
694bc97b15Scheloha.Fa "const char *wmesg"
704bc97b15Scheloha.Fa "int timo"
714bc97b15Scheloha.Fc
724bc97b15Scheloha.Ft int
734bc97b15Scheloha.Fo msleep_nsec
744bc97b15Scheloha.Fa "void *ident"
754bc97b15Scheloha.Fa "struct mutex *mtx"
764bc97b15Scheloha.Fa "int priority"
774bc97b15Scheloha.Fa "const char *wmesg"
784bc97b15Scheloha.Fa "uint64_t nsecs"
794bc97b15Scheloha.Fc
804bc97b15Scheloha.Ft int
814bc97b15Scheloha.Fo rwsleep
824bc97b15Scheloha.Fa "void *ident"
834bc97b15Scheloha.Fa "struct rwlock *rwl"
844bc97b15Scheloha.Fa "int priority"
854bc97b15Scheloha.Fa "const char *wmesg"
864bc97b15Scheloha.Fa "int timo"
874bc97b15Scheloha.Fc
884bc97b15Scheloha.Ft int
894bc97b15Scheloha.Fo rwsleep_nsec
904bc97b15Scheloha.Fa "void *ident"
914bc97b15Scheloha.Fa "struct rwlock *rwl"
924bc97b15Scheloha.Fa "int priority"
934bc97b15Scheloha.Fa "const char *wmesg"
944bc97b15Scheloha.Fa "uint64_t nsecs"
954bc97b15Scheloha.Fc
9616b67135Smk.Ft void
975dfee228Sschwarze.Fn wakeup "void *ident"
984e58d6f3Stedu.Ft void
995dfee228Sschwarze.Fn wakeup_n "void *ident" "int count"
1004e58d6f3Stedu.Ft void
1015dfee228Sschwarze.Fn wakeup_one "void *ident"
10216b67135Smk.Sh DESCRIPTION
10316b67135SmkThese functions implement voluntary context switching.
104c4a692e1Smpi.Fn tsleep ,
105e617ab10Sjmc.Fn msleep
106c4a692e1Smpiand
107c4a692e1Smpi.Fn rwsleep
108e617ab10Sjmcare used throughout the kernel whenever processing in the current context
10916b67135Smkcannot continue for any of the following reasons:
11016b67135Smk.Bl -bullet -offset indent
11116b67135Smk.It
11216b67135SmkThe current process needs to await the results of a pending I/O operation.
11316b67135Smk.It
11416b67135SmkThe current process needs resources
115e617ab10Sjmc.Pq e.g. memory
11616b67135Smkwhich are temporarily unavailable.
11716b67135Smk.It
11816b67135SmkThe current process wants access to data structures which are locked by
11916b67135Smkother processes.
12016b67135Smk.El
12116b67135Smk.Pp
122afb7b041SjmcThe
123afb7b041Sjmc.Fn wakeup ,
124afb7b041Sjmc.Fn wakeup_n ,
125afb7b041Sjmcand
126afb7b041Sjmc.Fn wakeup_one
127afb7b041Sjmcfunctions are used to notify sleeping processes of possible changes to the
128afb7b041Sjmccondition that caused them to go to sleep.
12916b67135SmkTypically, an awakened process will -- after it has acquired a context
13016b67135Smkagain -- retry the action that blocked its operation to see if the
13116b67135Smk.Dq blocking
13216b67135Smkcondition has cleared.
13316b67135Smk.Pp
13416b67135SmkThe
13516b67135Smk.Fn tsleep
13616b67135Smkfunction takes the following arguments:
13716b67135Smk.Bl -tag -width priority
13816b67135Smk.It Fa ident
13916b67135SmkAn identifier of the
14016b67135Smk.Dq wait channel
14116b67135Smkrepresenting the resource for which the current process needs to wait.
14216b67135SmkThis typically is the virtual address of some kernel data structure related
14316b67135Smkto the resource for which the process is contending.
14416b67135SmkThe same identifier must be used in a call to
14516b67135Smk.Fn wakeup
14616b67135Smkto get the process going again.
14716b67135Smk.Fa ident
14816b67135Smkshould not be
14916b67135Smk.Dv NULL .
15016b67135Smk.It Fa priority
15116b67135SmkThe process priority to be used when the process is awakened and put on
15216b67135Smkthe queue of runnable processes.
15316b67135SmkThis mechanism is used to optimize
15416b67135Smk.Dq throughput
15516b67135Smkof processes executing in kernel mode.
15616b67135SmkIf the flag
15716b67135Smk.Dv PCATCH
15816b67135Smkis OR'ed into
159*41ce3b17Snaddy.Fa priority ,
16016b67135Smkthe process checks for posted signals before and after sleeping.
16116b67135Smk.It Fa wmesg
16216b67135SmkA pointer to a character string indicating the reason a process is sleeping.
16316b67135SmkThe kernel does not use the string, but makes it available
16416b67135Smk.Pq through the process structure field Li p_wmesg
16516b67135Smkfor user level utilities such as
16616b67135Smk.Xr ps 1 .
16716b67135Smk.It Fa timo
16816b67135SmkIf non-zero, the process will sleep for at most
16916b67135Smk.Li timo/hz
17016b67135Smkseconds.
17116b67135SmkIf this amount of time elapses and no
17216b67135Smk.Fn wakeup "ident"
17316b67135Smkhas occurred, and no signal
17416b67135Smk.Pq if Dv PCATCH No was set
17516b67135Smkwas posted,
17616b67135Smk.Fn tsleep
17716b67135Smkwill return
17816b67135Smk.Er EWOULDBLOCK .
17916b67135Smk.El
18016b67135Smk.Pp
18116b67135SmkThe
18216c19d6fSart.Fn msleep
18316c19d6fSartfunction behaves just like
18416c19d6fSart.Fn tsleep ,
185e617ab10Sjmcbut takes an additional argument:
186e617ab10Sjmc.Bl -tag -width priority
187e617ab10Sjmc.It Fa mtx
188e617ab10SjmcA mutex that will be unlocked when the process is safely
18916c19d6fSarton the sleep queue.
190fd293a6bSogaThe mutex will be relocked at the end of msleep unless the
19116c19d6fSart.Dv PNORELOCK
19216c19d6fSartflag is set in the
19316c19d6fSart.Fa priority
19416c19d6fSartargument.
195e617ab10Sjmc.El
19616c19d6fSart.Pp
19716c19d6fSartThe
198c4a692e1Smpi.Fn rwsleep
199c4a692e1Smpifunction behaves just like
200c4a692e1Smpi.Fn tsleep ,
201c4a692e1Smpibut takes an additional argument:
202c4a692e1Smpi.Bl -tag -width priority
203722c41fbScheloha.It Fa rwl
204722c41fbSchelohaA read- or write-lock that will be unlocked when the process is safely
205c4a692e1Smpion the sleep queue.
206722c41fbSchelohaThe lock will be relocked at the end of rwsleep unless the
207c4a692e1Smpi.Dv PNORELOCK
208c4a692e1Smpiflag is set in the
209c4a692e1Smpi.Fa priority
210c4a692e1Smpiargument.
211c4a692e1Smpi.El
212c4a692e1Smpi.Pp
213c4a692e1SmpiThe
2144bc97b15Scheloha.Fn tsleep_nsec ,
2154bc97b15Scheloha.Fn msleep_nsec ,
2164bc97b15Schelohaand
2174bc97b15Scheloha.Fn rwsleep_nsec
2184bc97b15Schelohafunctions behave like their unsuffixed counterparts except that they
2194bc97b15Schelohaaccept a timeout in terms of nanoseconds.
2204bc97b15SchelohaThese functions will always sleep for at least one tick,
2214bc97b15Schelohaeven if
2224bc97b15Scheloha.Fa nsecs
2234bc97b15Schelohais zero.
2244bc97b15SchelohaIf
2254bc97b15Scheloha.Fa nsecs
2264bc97b15Schelohais equal to
2274bc97b15Scheloha.Dv INFSLP
2284bc97b15Schelohathese functions do not time out,
229ce07d858Schelohaotherwise they sleep for at least
2304bc97b15Scheloha.Fa nsecs
2314bc97b15Schelohananoseconds.
2324bc97b15Scheloha.Pp
2334bc97b15SchelohaThe
23416b67135Smk.Fn wakeup
23516b67135Smkfunction will mark all processes which are currently sleeping on the identifier
23616b67135Smk.Fa ident
23716b67135Smkas runnable.
23816b67135SmkEventually, each of the processes will resume execution in the kernel
23916b67135Smkcontext, causing a return from
24016b67135Smk.Fn tsleep .
24116b67135SmkNote that processes returning from sleep should always re-evaluate the
24216b67135Smkconditions that blocked them, since a call to
24316b67135Smk.Fn wakeup
24416b67135Smkmerely signals a
24516b67135Smk.Em possible
24616b67135Smkchange to the blocking conditions.
24716b67135SmkFor example, when two or more processes are waiting for an exclusive lock,
24816b67135Smkonly one of them will succeed in acquiring the lock when it is released.
24916b67135SmkAll others will have to go back to sleep and wait for the next opportunity.
2504e58d6f3Stedu.Pp
2514e58d6f3SteduThe
2524e58d6f3Stedu.Fn wakeup_n
2534e58d6f3Steduand
2544e58d6f3Stedu.Fn wakeup_one
2554e58d6f3Stedufunctions behave similarly to
2564e58d6f3Stedu.Fn wakeup
2574e58d6f3Steduexcept that only
2584e58d6f3Stedu.Fa count
2594e58d6f3Steduor one process, respectively, is marked runnable.
26016b67135Smk.Sh RETURN VALUES
261c4a692e1Smpi.Fn tsleep ,
2624bc97b15Scheloha.Fn tsleep_nsec ,
2634bc97b15Scheloha.Fn msleep ,
2644bc97b15Scheloha.Fn msleep_nsec ,
2654bc97b15Scheloha.Fn rwsleep ,
266c4a692e1Smpiand
2674bc97b15Scheloha.Fn rwsleep_nsec
268e617ab10Sjmcreturn 0 if they return as a result of a
26916b67135Smk.Fn wakeup .
270e617ab10SjmcIf they return as a result of a signal, the return value is
27116b67135Smk.Er ERESTART
27216b67135Smkif the signal has the
27316b67135Smk.Dv SA_RESTART
27416b67135Smkproperty
27516b67135Smk.Pq see Xr sigaction 2 ,
27616b67135Smkand
27716b67135Smk.Er EINTR
27816b67135Smkotherwise.
279e617ab10SjmcIf they return as a result of a timeout, the return value is
28016b67135Smk.Er EWOULDBLOCK .
28116b67135Smk.Sh CODE REFERENCES
28216b67135SmkThese functions are implemented in the file
28316b67135Smk.Pa sys/kern/kern_synch.c .
28416b67135Smk.Sh SEE ALSO
28516b67135Smk.Xr hz 9 ,
28616b67135Smk.Xr mi_switch 9 ,
28716b67135Smk.Xr timeout 9
288