kthread (9)
Leading comments
Copyright (c) 2000-2001 The Regents of the University of California. All rights reserved. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of condit...
NAME
kthread_start kthread_shutdown kthread_add kthread_exit kthread_resume kthread_suspend kthread_suspend_check - kernel threadsSYNOPSIS
In sys/kthread.h Ft void Fn kthread_start const void *udata Ft void Fn kthread_shutdown void *arg int howto Ft void Fn kthread_exit void Ft int Fn kthread_resume struct thread *td Ft int Fn kthread_suspend struct thread *td int timo Ft void Fn kthread_suspend_check void In sys/unistd.h Ft int Fo kthread_add Fa void (*func)(void *) void *arg struct proc *procp Fa struct thread **newtdpp int flags int pages Fa const char *fmt ... Fc Ft int Fo kproc_kthread_add Fa void (*func)(void *) void *arg Fa struct proc **procptr struct thread **tdptr Fa int flags int pages char * procname const char *fmt ... FcDESCRIPTION
In Fx 8.0 , the older family of Fn kthread_* 9 functions was renamed to be the Fn kproc_* 9 family of functions, as they were previously misnamed and actually produced kernel processes. This new family of Fn kthread_* 9 functions was added to produce real kernel threads. See the kproc(9) man page for more information on the renamed calls. Also note that the Fn kproc_kthread_add 9 function appears in both pages as its functionality is split.The function Fn kthread_start is used to start ``internal'' daemons such as bufdaemon , pagedaemon , vmdaemon and the syncer and is intended to be called from SYSINIT(9). The Fa udata argument is actually a pointer to a Vt struct kthread_desc which describes the kernel thread that should be created:
struct kthread_desc { char *arg0; void (*func)(void); struct thread **global_threadpp; };
The structure members are used by Fn kthread_start as follows:
- arg0
- String to be used for the name of the thread. This string will be copied into the td_name member of the new threads' Vt struct thread .
- func
- The main function for this kernel thread to run.
- global_threadpp
- A pointer to a Vt struct thread pointer that should be updated to point to the newly created thread's Vt thread structure. If this variable is NULL then it is ignored. The thread will be a subthread of proc0 (PID 0).
The Fn kthread_add function is used to create a kernel thread. The new thread runs in kernel mode only. It is added to the process specified by the Fa procp argument, or if that is NULL to proc0 The Fa func argument specifies the function that the thread should execute. The Fa arg argument is an arbitrary pointer that is passed in as the only argument to Fa func when it is called by the new thread. The Fa newtdpp pointer points to a Vt struct thread pointer that is to be updated to point to the newly created thread. If this argument is NULL then it is ignored. The Fa flags argument may be set to RFSTOPPED to leave the thread in a stopped state. The caller must call Fn sched_add to start the thread. The Fa pages argument specifies the size of the new kernel thread's stack in pages. If 0 is used, the default kernel stack size is allocated. The rest of the arguments form a printf(9) argument list that is used to build the name of the new thread and is stored in the td_name member of the new thread's Vt struct thread .
The Fn kproc_kthread_add function is much like the Fn kthread_add function above except that if the kproc does not already exist, it is created. This function is better documented in the kproc(9) manual page.
The Fn kthread_exit function is used to terminate kernel threads. It should be called by the main function of the kernel thread rather than letting the main function return to its caller.
The Fn kthread_resume , Fn kthread_suspend , and Fn kthread_suspend_check functions are used to suspend and resume a kernel thread. During the main loop of its execution, a kernel thread that wishes to allow itself to be suspended should call Fn kthread_suspend_check in order to check if the it has been asked to suspend. If it has, it will msleep(9) until it is told to resume. Once it has been told to resume it will return allowing execution of the kernel thread to continue. The other two functions are used to notify a kernel thread of a suspend or resume request. The Fa td argument points to the Vt struct thread of the kernel thread to suspend or resume. For Fn kthread_suspend , the Fa timo argument specifies a timeout to wait for the kernel thread to acknowledge the suspend request and suspend itself.
The Fn kthread_shutdown function is meant to be registered as a shutdown event for kernel threads that need to be suspended voluntarily during system shutdown so as not to interfere with system shutdown activities. The actual suspension of the kernel thread is done with Fn kthread_suspend .
RETURN VALUES
The Fn kthread_add , Fn kthread_resume , and Fn kthread_suspend functions return zero on success and non-zero on failure.EXAMPLES
This example demonstrates the use of a Vt struct kthread_desc and the functions Fn kthread_start , Fn kthread_shutdown , and Fn kthread_suspend_check to run the bufdaemon process.static struct thread *bufdaemonthread; static struct kthread_desc buf_kp = { "bufdaemon", buf_daemon, &bufdaemonthread }; SYSINIT(bufdaemon, SI_SUB_KTHREAD_BUF, SI_ORDER_FIRST, kthread_start, &buf_kp) static void buf_daemon() { ... /* * This process needs to be suspended prior to shutdown sync. */ EVENTHANDLER_REGISTER(shutdown_pre_sync, kthread_shutdown, bufdaemonthread, SHUTDOWN_PRI_LAST); ... for (;;) { kthread_suspend_check(); ... } }
ERRORS
The Fn kthread_resume and Fn kthread_suspend functions will fail if:- Bq Er EINVAL
- The Fa td argument does not reference a kernel thread.
The Fn kthread_add function will fail if:
- Bq Er ENOMEM
- Memory for a thread's stack could not be allocated.