1.\" $NetBSD: kthread.9,v 1.28 2015/04/21 11:10:29 pooka Exp $ 2.\" 3.\" Copyright (c) 2000, 2007, 2008 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.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28.\" POSSIBILITY OF SUCH DAMAGE. 29.\" 30.Dd April 21, 2015 31.Dt KTHREAD 9 32.Os 33.Sh NAME 34.Nm kthread_create , 35.Nm kthread_exit , 36.Nm kthread_join 37.Nd kernel threads 38.Sh SYNOPSIS 39.In sys/kthread.h 40.Ft int 41.Fn kthread_create "pri_t pri" "int flags" "struct cpu_info *ci" \ 42"void (*func)(void *)" "void *arg" "lwp_t **newlp" "const char *fmt" "..." 43.Ft void 44.Fn kthread_exit "int ecode" 45.Ft int 46.Fn kthread_join "lwp_t *l" 47.Sh DESCRIPTION 48Kernel threads are light-weight processes which execute entirely 49within the kernel. 50.Pp 51Any process can request the creation of a new kernel thread. 52Kernel threads are not swapped out during memory congestion. 53The VM space and limits are shared with proc0 (usually swapper). 54.Sh FUNCTIONS 55.Bl -tag -width compact 56.It Fn kthread_create "pri" "flags" "ci" "func" "arg" "newlp" "fmt" "..." 57Create a kernel thread. 58The arguments are as follows. 59.Bl -tag -width compact 60.It Fa pri 61Priority level for the thread. 62If no priority level is desired specify 63.Dv PRI_NONE , 64causing 65.Fn kthread_create 66to select the default priority level. 67.It Fa flags 68Flags that can be logically ORed together to alter the thread's behaviour. 69.It Fa ci 70If 71.No non- Ns Dv NULL , 72the thread will be created bound to the CPU specified by 73.Fa ci , 74meaning that it will only ever execute on that CPU. 75By default, the threads are free to execute on any CPU in the system. 76.It Fa func 77A function to be called when the thread begins executing. 78This function must not return. 79If the thread runs to completion, it must call 80.Fn kthread_exit 81to properly terminate itself. 82.It Fa arg 83An argument to be passed to 84.Fn func . 85May be 86.Dv NULL 87if not required. 88.It Fa newlp 89A pointer to receive the new LWP structure for the kernel thread. 90May be 91.Dv NULL , 92unless 93.Dv KTHREAD_MUSTJOIN 94is specified in 95.Fa flags . 96.It Fa fmt 97A string containing format information used to display the kernel 98thread name. 99Must not be 100.Dv NULL . 101.El 102.Pp 103The following 104.Va flags 105are defined. 106.Bl -tag -width KTHREAD_MUSTJOIN 107.It Dv KTHREAD_IDLE 108Causes the thread to be created in the 109.Dv LSIDL 110(idle) state. 111By default, the threads are created in the 112.Dv LSRUN 113(runnable) state, meaning they will begin execution shortly after creation. 114.It Dv KTHREAD_MPSAFE 115Specifies that the thread does its own locking and so is multiprocessor safe. 116If not specified, the global kernel lock will be held whenever the thread is 117running (unless explicitly dropped by the thread). 118.It Dv KTHREAD_INTR 119Specifies that the thread services device interrupts. 120This flag is intended for kernel internal use and should not normally be 121specified. 122.It Dv KTHREAD_TS 123Causes the kthread to be created in the 124.Dv SCHED_OTHER 125class (timeshared). 126The thread's priority will be dynamically adjusted by the scheduler. 127Increased activity by the kthread will cause its priority to fall; 128decreased activity will cause its priority to rise. 129By default, kthreads are created in the 130.Dv SCHED_RR 131class, with a fixed 132priority specified by 133.Ar pri . 134Threads in the 135.Dv SCHED_RR 136class do not have their priority dynamically 137adjusted by the scheduler. 138.It Dv KTHREAD_MUSTJOIN 139Indicates that created kthread must be joined. 140In such case 141.Fn kthread_exit 142will wait until 143.Fn kthread_join 144will be called. 145.El 146.It Fn kthread_exit "ecode" 147Exit from a kernel thread. 148Must only be called by a kernel thread. 149.It Fn kthread_join "l" 150Suspend execution of calling thread until the target kthread terminates. 151Conceptually the function can be compared to the user space 152.Xr pthread_join 3 , 153however it must be called only once for kernel thread which was 154created using the 155.Dv KTHREAD_MUSTJOIN 156flag and would wait on 157.Fa kthread_exit . 158.El 159.Sh RETURN VALUES 160Upon successful completion, 161.Fn kthread_create 162returns 0. 163Otherwise, the following error values are returned: 164.Bl -tag -width [EAGAIN] 165.It Bq Er EAGAIN 166The limit on the total number of system processes would be exceeded. 167.It Bq Er EAGAIN 168The limit 169.Dv RLIMIT_NPROC 170on the total number of processes under execution by this 171user id would be exceeded. 172.El 173.Sh CODE REFERENCES 174The kthread framework itself is implemented within the file 175.Pa sys/kern/kern_kthread.c . 176Data structures and function prototypes for the framework are located in 177.Pa sys/sys/kthread.h . 178.Sh SEE ALSO 179.Xr condvar 9 , 180.Xr driver 9 , 181.Xr softint 9 , 182.Xr workqueue 9 183.Sh HISTORY 184The kthread framework appeared in 185.Nx 1.4 . 186