1*dab59af3SLi-Wen Hsu.\" Copyright (c) 2016 The FreeBSD Foundation 28269e767SBrooks Davis.\" 38269e767SBrooks Davis.\" This documentation was written by 48269e767SBrooks Davis.\" Konstantin Belousov <kib@FreeBSD.org> under sponsorship 58269e767SBrooks Davis.\" from the FreeBSD Foundation. 68269e767SBrooks Davis.\" 78269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without 88269e767SBrooks Davis.\" modification, are permitted provided that the following conditions 98269e767SBrooks Davis.\" are met: 108269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright 118269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer. 128269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright 138269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer in the 148269e767SBrooks Davis.\" documentation and/or other materials provided with the distribution. 158269e767SBrooks Davis.\" 168269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 178269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 188269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 198269e767SBrooks Davis.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 208269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 218269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 228269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 238269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 248269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 258269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 268269e767SBrooks Davis.\" SUCH DAMAGE. 278269e767SBrooks Davis.\" 288269e767SBrooks Davis.Dd May 5, 2020 298269e767SBrooks Davis.Dt THR_NEW 2 308269e767SBrooks Davis.Os 318269e767SBrooks Davis.Sh NAME 328269e767SBrooks Davis.Nm thr_new 338269e767SBrooks Davis.Nd create new thread of execution 348269e767SBrooks Davis.Sh LIBRARY 358269e767SBrooks Davis.Lb libc 368269e767SBrooks Davis.Sh SYNOPSIS 378269e767SBrooks Davis.In sys/thr.h 388269e767SBrooks Davis.Ft int 398269e767SBrooks Davis.Fn thr_new "struct thr_param *param" "int param_size" 408269e767SBrooks Davis.Sh DESCRIPTION 418269e767SBrooks Davis.Bf -symbolic 428269e767SBrooks DavisThis function is intended for implementing threading. 438269e767SBrooks DavisNormal applications should call 448269e767SBrooks Davis.Xr pthread_create 3 458269e767SBrooks Davisinstead. 468269e767SBrooks Davis.Ef 478269e767SBrooks Davis.Pp 488269e767SBrooks DavisThe 498269e767SBrooks Davis.Fn thr_new 508269e767SBrooks Davissystem call creates a new kernel-scheduled thread of execution in the context 518269e767SBrooks Davisof the current process. 528269e767SBrooks DavisThe newly created thread shares all attributes of the process with the 538269e767SBrooks Davisexisting kernel-scheduled threads in the process, but has private processor 548269e767SBrooks Davisexecution state. 558269e767SBrooks DavisThe machine context for the new thread is copied from the creating thread's 568269e767SBrooks Daviscontext, including coprocessor state. 578269e767SBrooks DavisFPU state and specific machine registers are excluded from the copy. 588269e767SBrooks DavisThese are set according to ABI requirements and syscall parameters. 598269e767SBrooks DavisThe FPU state for the new thread is reinitialized to clean. 608269e767SBrooks Davis.Pp 618269e767SBrooks DavisThe 628269e767SBrooks Davis.Fa param 638269e767SBrooks Davisstructure supplies parameters affecting the thread creation. 648269e767SBrooks DavisThe structure is defined in the 658269e767SBrooks Davis.In sys/thr.h 668269e767SBrooks Davisheader as follows 678269e767SBrooks Davis.Bd -literal 688269e767SBrooks Davisstruct thr_param { 698269e767SBrooks Davis void (*start_func)(void *); 708269e767SBrooks Davis void *arg; 718269e767SBrooks Davis char *stack_base; 728269e767SBrooks Davis size_t stack_size; 738269e767SBrooks Davis char *tls_base; 748269e767SBrooks Davis size_t tls_size; 758269e767SBrooks Davis long *child_tid; 768269e767SBrooks Davis long *parent_tid; 778269e767SBrooks Davis int flags; 788269e767SBrooks Davis struct rtprio *rtp; 798269e767SBrooks Davis}; 808269e767SBrooks Davis.Ed 818269e767SBrooks Davisand contains the following fields: 828269e767SBrooks Davis.Bl -tag -width ".Va parent_tid" 838269e767SBrooks Davis.It Va start_func 848269e767SBrooks DavisPointer to the thread entry function. 858269e767SBrooks DavisThe kernel arranges for the new thread to start executing the function 868269e767SBrooks Davisupon the first return to userspace. 878269e767SBrooks Davis.It Va arg 888269e767SBrooks DavisOpaque argument supplied to the entry function. 898269e767SBrooks Davis.It Va stack_base 908269e767SBrooks DavisStack base address. 918269e767SBrooks DavisThe stack must be allocated by the caller. 928269e767SBrooks DavisOn some architectures, the ABI might require that the system put information 938269e767SBrooks Davison the stack to ensure the execution environment for 948269e767SBrooks Davis.Va start_func . 958269e767SBrooks Davis.It Va stack_size 968269e767SBrooks DavisStack size. 978269e767SBrooks Davis.It Va tls_base 988269e767SBrooks DavisTLS base address. 998269e767SBrooks DavisThe value of TLS base is loaded into the ABI-defined machine register 1008269e767SBrooks Davisin the new thread context. 1018269e767SBrooks Davis.It Va tls_size 1028269e767SBrooks DavisTLS size. 1038269e767SBrooks Davis.It Va child_tid 1048269e767SBrooks DavisAddress to store the new thread identifier, for the child's use. 1058269e767SBrooks Davis.It Va parent_tid 1068269e767SBrooks DavisAddress to store the new thread identifier, for the parent's use. 1078269e767SBrooks Davis.Pp 1088269e767SBrooks DavisBoth 1098269e767SBrooks Davis.Va child_tid 1108269e767SBrooks Davisand 1118269e767SBrooks Davis.Va parent_tid 1128269e767SBrooks Davisare provided, with the intent that 1138269e767SBrooks Davis.Va child_tid 1148269e767SBrooks Davisis used by the new thread to get its thread identifier without 1158269e767SBrooks Davisissuing the 1168269e767SBrooks Davis.Xr thr_self 2 1178269e767SBrooks Davissyscall, while 1188269e767SBrooks Davis.Va parent_tid 1198269e767SBrooks Davisis used by the thread creator. 1208269e767SBrooks DavisThe latter is separate from 1218269e767SBrooks Davis.Va child_tid 1228269e767SBrooks Davisbecause the new thread might exit and free its thread data before the parent 1238269e767SBrooks Davishas a chance to execute far enough to access it. 1248269e767SBrooks Davis.It Va flags 1258269e767SBrooks DavisThread creation flags. 1268269e767SBrooks DavisThe 1278269e767SBrooks Davis.Va flags 1288269e767SBrooks Davismember may specify the following flags: 1298269e767SBrooks Davis.Bl -tag -width ".Dv THR_SYSTEM_SCOPE" 1308269e767SBrooks Davis.It Dv THR_SUSPENDED 1318269e767SBrooks DavisCreate the new thread in the suspended state. 1328269e767SBrooks DavisThe flag is not currently implemented. 1338269e767SBrooks Davis.It Dv THR_SYSTEM_SCOPE 1348269e767SBrooks DavisCreate the system scope thread. 1358269e767SBrooks DavisThe flag is not currently implemented. 1368269e767SBrooks Davis.El 1378269e767SBrooks Davis.It Va rtp 1388269e767SBrooks DavisReal-time scheduling priority for the new thread. 1398269e767SBrooks DavisMay be 1408269e767SBrooks Davis.Dv NULL 1418269e767SBrooks Davisto inherit the priority from the 1428269e767SBrooks Daviscreating thread. 1438269e767SBrooks Davis.El 1448269e767SBrooks Davis.Pp 1458269e767SBrooks DavisThe 1468269e767SBrooks Davis.Fa param_size 1478269e767SBrooks Davisargument should be set to the size of the 1488269e767SBrooks Davis.Fa param 1498269e767SBrooks Davisstructure. 1508269e767SBrooks Davis.Pp 1518269e767SBrooks DavisAfter the first successful creation of an additional thread, 1528269e767SBrooks Davisthe process is marked by the kernel as multi-threaded. 1538269e767SBrooks DavisIn particular, the 1548269e767SBrooks Davis.Dv P_HADTHREADS 1558269e767SBrooks Davisflag is set in the process' 1568269e767SBrooks Davis.Dv p_flag 1578269e767SBrooks Davis(visible in the 1588269e767SBrooks Davis.Xr ps 1 1598269e767SBrooks Davisoutput), and several operations are executed in multi-threaded mode. 1608269e767SBrooks DavisFor instance, the 1618269e767SBrooks Davis.Xr execve 2 1628269e767SBrooks Davissystem call terminates all threads but the calling one on successful 1638269e767SBrooks Davisexecution. 1648269e767SBrooks Davis.Sh RETURN VALUES 1658269e767SBrooks DavisIf successful, 1668269e767SBrooks Davis.Fn thr_new 1678269e767SBrooks Daviswill return zero, otherwise \-1 is returned, and 1688269e767SBrooks Davis.Va errno 1698269e767SBrooks Davisis set to indicate the error. 1708269e767SBrooks Davis.Sh ERRORS 1718269e767SBrooks DavisThe 1728269e767SBrooks Davis.Fn thr_new 1738269e767SBrooks Davisoperation returns the following errors: 1748269e767SBrooks Davis.Bl -tag -width Er 1758269e767SBrooks Davis.\" When changing this list, consider updating share/man/man3/pthread_create.3, 1768269e767SBrooks Davis.\" since that function can return any of these errors. 1778269e767SBrooks Davis.It Bq Er EFAULT 1788269e767SBrooks DavisThe memory pointed to by the 1798269e767SBrooks Davis.Fa param 1808269e767SBrooks Davisargument is not valid. 1818269e767SBrooks Davis.It Bq Er EFAULT 1828269e767SBrooks DavisThe memory pointed to by the 1838269e767SBrooks Davis.Fa param 1848269e767SBrooks Davisstructure 1858269e767SBrooks Davis.Fa child_tid , parent_tid 1868269e767SBrooks Davisor 1878269e767SBrooks Davis.Fa rtp 1888269e767SBrooks Davisarguments is not valid. 1898269e767SBrooks Davis.It Bq Er EFAULT 1908269e767SBrooks DavisThe specified stack base is invalid, or the kernel was unable to put required 1918269e767SBrooks Davisinitial data on the stack. 1928269e767SBrooks Davis.It Bq Er EINVAL 1938269e767SBrooks DavisThe 1948269e767SBrooks Davis.Fa param_size 1958269e767SBrooks Davisargument specifies a negative value, or the value is greater than the 1968269e767SBrooks Davislargest 1978269e767SBrooks Davis.Fa struct param 1988269e767SBrooks Davissize the kernel can interpret. 1998269e767SBrooks Davis.It Bq Er EINVAL 2008269e767SBrooks DavisThe 2018269e767SBrooks Davis.Fa rtp 2028269e767SBrooks Davismember is not 2038269e767SBrooks Davis.Dv NULL 2048269e767SBrooks Davisand specifies invalid scheduling parameters. 2058269e767SBrooks Davis.It Bq Er EINVAL 2068269e767SBrooks DavisThe specified TLS base is invalid. 2078269e767SBrooks Davis.It Bq Er EPERM 2088269e767SBrooks DavisThe caller does not have permission to set the scheduling parameters or 2098269e767SBrooks Davisscheduling policy. 2108269e767SBrooks Davis.It Bq Er EPROCLIM 2118269e767SBrooks DavisCreation of the new thread would exceed the 2128269e767SBrooks Davis.Dv RACCT_NTHR 2138269e767SBrooks Davislimit, see 2148269e767SBrooks Davis.Xr rctl_get_racct 2 . 2158269e767SBrooks Davis.It Bq Er EPROCLIM 2168269e767SBrooks DavisCreation of the new thread would exceed the 2178269e767SBrooks Davis.Dv kern.threads.max_threads_per_proc 2188269e767SBrooks Davis.Xr sysctl 3 2198269e767SBrooks Davislimit. 2208269e767SBrooks Davis.It Bq Er ENOMEM 2218269e767SBrooks DavisThere was not enough kernel memory to allocate the new thread structures. 2228269e767SBrooks Davis.El 2238269e767SBrooks Davis.Sh SEE ALSO 2248269e767SBrooks Davis.Xr ps 1 , 2258269e767SBrooks Davis.Xr _umtx_op 2 , 2268269e767SBrooks Davis.Xr execve 2 , 2278269e767SBrooks Davis.Xr rctl_get_racct 2 , 2288269e767SBrooks Davis.Xr thr_exit 2 , 2298269e767SBrooks Davis.Xr thr_kill 2 , 2308269e767SBrooks Davis.Xr thr_kill2 2 , 2318269e767SBrooks Davis.Xr thr_self 2 , 2328269e767SBrooks Davis.Xr thr_set_name 2 , 2338269e767SBrooks Davis.Xr pthread_create 3 2348269e767SBrooks Davis.Sh STANDARDS 2358269e767SBrooks DavisThe 2368269e767SBrooks Davis.Fn thr_new 2378269e767SBrooks Davissystem call is non-standard and is used by the 2388269e767SBrooks Davis.Lb libthr 2398269e767SBrooks Davisto implement 2408269e767SBrooks Davis.St -p1003.1-2001 2418269e767SBrooks Davis.Xr pthread 3 2428269e767SBrooks Davisfunctionality. 2438269e767SBrooks Davis.Sh HISTORY 2448269e767SBrooks DavisThe 2458269e767SBrooks Davis.Fn thr_new 2468269e767SBrooks Davissystem call first appeared in 2478269e767SBrooks Davis.Fx 5.2 . 248