xref: /netbsd-src/share/man/man9/kthread.9 (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1.\"     $NetBSD: kthread.9,v 1.16 2007/11/21 23:11:06 xtraeme Exp $
2.\"
3.\" Copyright (c) 2000, 2007 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Gregory McGarry, and by Andrew Doran.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\" 3. All advertising materials mentioning features or use of this software
18.\"    must display the following acknowledgement:
19.\"        This product includes software developed by the NetBSD
20.\"        Foundation, Inc. and its contributors.
21.\" 4. Neither the name of The NetBSD Foundation nor the names of its
22.\"    contributors may be used to endorse or promote products derived
23.\"    from this software without specific prior written permission.
24.\"
25.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35.\" POSSIBILITY OF SUCH DAMAGE.
36.\"
37.Dd November 22, 2007
38.Dt KTHREAD 9
39.Os
40.Sh NAME
41.Nm kthread_create ,
42.Nm kthread_destroy ,
43.Nm kthread_exit
44.Nd kernel threads
45.Sh SYNOPSIS
46.In sys/kthread.h
47.Ft int
48.Fn kthread_create "pri_t pri" "int flags" "struct cpu_info *ci" \
49"void (*func)(void *)" "void *arg" "lwp_t **newlp" "const char *fmt" "..."
50.Ft void
51.Fn kthread_destroy "lwp_t *l"
52.Ft void
53.Fn kthread_exit "int ecode"
54.Sh DESCRIPTION
55Kernel threads are light-weight processes which execute entirely
56within the kernel.
57.Pp
58Any process can request the creation of a new kernel thread.
59Kernel threads are not swapped out during memory congestion.
60The VM space and limits are shared with proc0 (usually swapper).
61.Sh FUNCTIONS
62.Bl -tag -width compact
63.It Fn kthread_create "pri" "flags" "ci" "func" "arg" "newlp" "fmt" "..."
64Create a kernel thread.
65The arguments are as follows.
66.Bl -tag -width compact
67.It Fa pri
68Priority level for the thread.
69If no priority level is desired specify
70.Dv PRI_NONE ,
71causing
72.Fn kthread_create
73to select the default priority level.
74.It Fa flags
75Flags that can be logically ORed together to alter the thread's behaviour.
76.Pp
77.Dv KTHREAD_IDLE :
78causes the thread to be created in the
79.Dv LSIDL
80(idle) state.
81By default, the threads are created in the
82.Dv LSRUN
83(runnable) state, meaning they will begin execution shortly after creation.
84.Pp
85.Dv KTHREAD_MPSAFE :
86Specifies that the thread does its own locking and so is multiprocessor safe.
87If not specified, the global kernel lock will be held whenever the thread is
88running (unless explicitly dropped by the thread).
89.Pp
90.Dv KTHREAD_INTR :
91Specifies that the thread services device interrupts.
92This flag is intended for kernel internal use and should not normally be
93specified.
94.It Fa ci
95If non-NULL, the thread will be created bound to the CPU specified
96by
97.Fa ci ,
98meaning that it will only ever execute on that CPU.
99By default, the threads are free to execute on any CPU in the system.
100.It Fa func
101A function to be called when the thread begins executing.
102This function must not return.
103If the thread runs to completion, it must call
104.Fn kthread_exit
105to properly terminate itself.
106.It Fa arg
107An argument to be passed to
108.Fn func .
109May be NULL if not required.
110.It Fa newpl
111A pointer to receive the new lwp structure for the kernel thread.
112May be NULL if not required.
113.It Fa fmt
114A string containing format information used to display the kernel
115thread name.
116Must not be NULL.
117.El
118.It Fn kthread_destroy "l"
119From another thread executing in the kernel, cause a kthread to exit.
120The kthread must be in the
121.Dv LSIDL
122(idle) state.
123.It Fn kthread_exit "ecode"
124Exit from a kernel thread.
125Must only be called by a kernel thread.
126.El
127.Sh RETURN VALUES
128Upon successful completion,
129.Fn kthread_create
130returns 0.
131Otherwise, the following error values are returned:
132.Bl -tag -width [EAGAIN]
133.It Bq Er EAGAIN
134The limit on the total number of system processes would be exceeded.
135.It Bq Er EAGAIN
136The limit
137.Dv RLIMIT_NPROC
138on the total number of processes under execution by this
139user id would be exceeded.
140.El
141.Sh CODE REFERENCES
142This section describes places within the
143.Nx
144source tree where actual code implementing or using the kthread
145framework can be found.
146All pathnames are relative to
147.Pa /usr/src .
148.Pp
149The kthread framework itself is implemented within the file
150.Pa sys/kern/kern_kthread.c .
151Data structures and function prototypes for the framework are located in
152.Pa sys/sys/kthread.h .
153.Sh SEE ALSO
154.Xr driver 9
155.Sh HISTORY
156The kthread framework appeared in
157.Nx 1.4 .
158