THR_NEW(2)

πŸ“¦ version 15.1, in freebsd 14.5, freebsd 15.1

okay so bestie, welcome to the THR_NEW(2) page from the System Calls Manual β€” this is all about thr_new, and we're gonna make it make sense πŸ’…βœ¨

✨ NAME ✨

  • thr_new β€” create new thread of execution (yes, a whole new thread, slay πŸ§΅πŸ‘‘)

πŸŽ€ LIBRARY πŸŽ€

Standard C Library (libc, -lc) β€” she lives in libc, bestie 🫢

πŸ¦‹ SYNOPSIS πŸ¦‹

#include <sys/thr.h>

int
thr_new(struct thr_param *param, int param_size);

πŸ’… DESCRIPTION πŸ’…

okay so the tea is: this function is intended for implementing threading. Normal applications should call pthread_create(3) instead. Like, unless you're building the threading machinery itself, this is not your everyday hangout spot, bestie πŸ’…

The thr_new() system call creates a new kernel-scheduled thread of execution in the context of the current process. The newly created thread shares all attributes of the process with the existing kernel-scheduled threads in the process, but has private processor execution state. The machine context for the new thread is copied from the creating thread's context, including coprocessor state. FPU state and specific machine registers are excluded from the copy. These are set according to ABI requirements and syscall parameters. The FPU state for the new thread is reinitialized to clean. so fresh, so clean ✨

The param structure supplies parameters affecting the thread creation. The structure is defined in the <sys/thr.h> header as follows:

struct thr_param {
    void          (*start_func)(void *);
    void          *arg;
    char          *stack_base;
    size_t        stack_size;
    char          *tls_base;
    size_t        tls_size;
    long          *child_tid;
    long          *parent_tid;
    int           flags;
    struct rtprio *rtp;
};

and it contains the following fields:

  • start_func β€” Pointer to the thread entry function. The kernel arranges for the new thread to start executing the function upon the first return to userspace. first-return-to-userspace realness πŸ‘‘
  • arg β€” Opaque argument supplied to the entry function.
  • stack_base β€” Stack base address. The stack must be allocated by the caller. On some architectures, the ABI might require that the system put information on the stack to ensure the execution environment for start_func.
  • stack_size β€” Stack size.
  • tls_base β€” TLS base address. The value of TLS base is loaded into the ABI-defined machine register in the new thread context.
  • tls_size β€” TLS size.
  • child_tid β€” Address to store the new thread identifier, for the child's use.
  • parent_tid β€” Address to store the new thread identifier, for the parent's use.

Both child_tid and parent_tid are provided, with the intent that child_tid is used by the new thread to get its thread identifier without issuing the thr_self(2) syscall, while parent_tid is used by the thread creator. The latter is separate from child_tid because the new thread might exit and free its thread data before the parent has a chance to execute far enough to access it. timing is literally everything 😭

  • flags β€” Thread creation flags. The flags member may specify the following flags:
    • THR_SUSPENDED β€” Create the new thread in the suspended state. The flag is not currently implemented.
    • THR_SYSTEM_SCOPE β€” Create the system scope thread. The flag is not currently implemented.
    • THR_C_RUNTIME β€” Indicate that the new thread is created by the C language runtime. It has architecture-specific meaning. On amd64, the flag requests that the specified tls_base was loaded into the %fsbase register before calling a signal handler.
  • rtp β€” Real-time scheduling priority for the new thread. May be NULL to inherit the priority from the creating thread.

The param_size argument should be set to the size of the param structure. no cap, just set it right 🫢

After the first successful creation of an additional thread, the process is marked by the kernel as multi-threaded. In particular, the P_HADTHREADS flag is set in the process' p_flag (visible in the ps(1) output), and several operations are executed in multi-threaded mode. For instance, the execve(2) system call terminates all threads but the calling one on successful execution. iconic but also kind of dramatic πŸ’€

🌸 RETURN VALUES 🌸

If successful, thr_new() will return zero, otherwise -1 is returned, and errno is set to indicate the error. slay when it works, cry when it doesn't 😭

😭 ERRORS 😭

The thr_new() operation returns the following errors:

  • [EFAULT] β€” The memory pointed to by the param argument is not valid.
  • [EFAULT] β€” The memory pointed to by the param structure child_tid, parent_tid or rtp arguments is not valid.
  • [EFAULT] β€” The specified stack base is invalid, or the kernel was unable to put required initial data on the stack.
  • [EINVAL] β€” The param_size argument specifies a negative value, or the value is greater than the largest struct param size the kernel can interpret.
  • [EINVAL] β€” The rtp member is not NULL and specifies invalid scheduling parameters.
  • [EINVAL] β€” The specified TLS base is invalid.
  • [EPERM] β€” The caller does not have permission to set the scheduling parameters or scheduling policy.
  • [EPROCLIM] β€” Creation of the new thread would exceed the RACCT_NTHR limit, see rctl_get_racct(2).
  • [EPROCLIM] β€” Creation of the new thread would exceed the kern.threads.max_threads_per_proc sysctl(3) limit.
  • [ENOMEM] β€” There was not enough kernel memory to allocate the new thread structures.

πŸ”₯ SEE ALSO πŸ”₯

ps(1), _umtx_op(2), execve(2), rctl_get_racct(2), thr_exit(2), thr_kill(2), thr_kill2(2), thr_self(2), thr_set_name(2), pthread_create(3) β€” go read these too, bestie 🫢

πŸ‘‘ STANDARDS πŸ‘‘

The thr_new() system call is non-standard and is used by the 1:1 Threading Library (libthr, -lthr) to implement IEEE Std 1003.1-2001 (β€œPOSIX.1”) pthread(3) functionality. so she's the lowkey engine behind your pthreads, iykyk πŸ‘‘

⭐ HISTORY ⭐

The thr_new() system call first appeared in FreeBSD 5.2. vintage but still relevant, periodt ✨


πŸ’– cute little sign-off: this page came from Debian, dated May 5, 2020, and it's the THR_NEW(2) manual page. love u, bye bestie! πŸŽ€πŸ«Ά

πŸ“¦ where this page comes from