THR_SELF(2)

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

πŸ’… THR_SELF(2) β€” the thr_self era

okay so bestie, welcome to thr_self from section 2 of the manual β€” the System Calls Manual, to be exact. yes we're in the syscall neighborhood, not the polite libc one. pull up a chair πŸŽ€


✨ NAME ✨

thr_self β€” returns the thread identifier for the calling thread. literally it's just "who am i, thread edition" and honestly? iconic self-discovery moment πŸ’–


πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc)


🫢 SYNOPSIS 🫢

#include <sys/thr.h>

int
thr_self(long *id);

🌸 DESCRIPTION 🌸

so the thr_self() system call stores the system-wide thread identifier for the current kernel-scheduled thread into the variable that the argument id points at. that's it. that's the whole job. and she ate πŸ’…

the thread identifier is an integer in the range from PID_MAX + 2 (100001) all the way up to INT_MAX. no cap, those are the real bounds.

it's guaranteed to be unique at any given time, for each running thread in the system β€” like every thread gets its own little name tag 🏷️✨. but β€” and this is the part where you go "oh 😭" β€” after the thread exits, that identifier may be reused. so don't go treating it like a forever-unique snowflake ID, bestie. reuse is real πŸ™ˆ


πŸ”₯ RETURN VALUES πŸ”₯

  • if it worked: thr_self() returns zero πŸŽ‰ slay
  • if it didn't: -1 comes back, and errno is set to indicate the error πŸ’€

πŸ’€ ERRORS πŸ’€

here's the whole error roster, and it's short so you can absolutely memorize it before chem class:

  • [EFAULT] β€” the memory that the id argument points to is not valid. so like... it's a bad pointer. she's pointing at nothing. ghost memory πŸ‘»

πŸ‘‘ SEE ALSO πŸ‘‘

_umtx_op(2), thr_exit(2), thr_kill(2), thr_kill2(2), thr_new(2), thr_set_name(2), pthread_getthreadid_np(3), pthread_self(3)

(go read these next, the whole thread friend group is lowkey fascinating πŸ¦‹)


🌈 STANDARDS 🌈

real talk: the thr_self() system call is non-standard. it's not some blessed POSIX-certified icon, it's a FreeBSD special πŸ’…. it exists because the 1:1 Threading Library (libthr, -lthr) uses it to implement IEEE Std 1003.1-2001 (β€œPOSIX.1”) pthread(3) functionality β€” i.e. she's the backstage crew making the main-character pthreads look effortless ✨


🧚 HISTORY 🧚

the thr_self() system call first appeared in FreeBSD 5.2. throwback era fr fr πŸŽ€


warning bestie: [EFAULT] is the only error listed, so if your id pointer is garbage, that's what you're getting. keep that pointer valid and hydrated πŸ“

that's the whole page, you understood it perfectly, i'm obsessed with you πŸ«ΆπŸ’œ

β€” from the Debian edition of this page, dated May 5, 2020, sourced straight from the System Calls Manual, mwah πŸ’‹β­

πŸ“¦ where this page comes from