3 ms·
One cool thing I like about Sortix is how it handles threading. TLS is set up by the kernel, so errno can be a thread local variable.
by abenedic 9y ago
One cool thing I like about Sortix is how it handles threading. TLS is set up by the kernel, so errno can be a thread local variable.
- jeffreyrogers 9y agoI think errno is thread local in most modern unix variants.
- sortie 9y agoAuthor here. POSIX requires errno to be thread local, but with few requirements. Commonly libc's #define errno (*__get_errno_location()). Why? It's because the thread local storage isn't online very early on. The libc literally has to open its own program executable and locate the the thread local storage and mmap a copy of it for each thread. Thread level storage comes online late in Linux, which bloats statically linked executables with code to parse their own executable. In Sortix, I just have the kernel set up the TLS for statically linked executables and just declare errno as: __thread int errno; That means errno is online from the very first instruction in _start in statically linked executables, without the need to parse the program file in early libc. There's no error conditions between _start and main. This is the kind of implementation quality I want to have in Sortix and part of why I develop Sortix.
- jeffreyrogers 9y agoAh, that's very neat. Thanks for explaining.
- jchw 9y agoHm. I thought libc traditionally handled errno, not the kernel, and the syscall return code was simply translated to errno by libc.
- sortie 9y agoAuthor here. Like in Linux, libc provides errno on Sortix. The difference is that glibc does "#define errno (*__get_errno_location())" while Sortix libc does "__thread int errno;". linux syscalls return a negative value containing the negated errno, while Sortix system calls use two output registers: The return value register and the errno register. That allows Sortix system calls to legitimately return -42 if they want, which is not possible on Linux. Sortix libc assigns the errno register to the thread local errno if the errno register is non-zero.