125 points · by ingve · 3y ago · 51 comments
(1) It only works on one architecture. Even this architecture may change optimal alignment requirements in future, which glibc would transparently handle for you.
(2) It's very complex and error-prone compared to using the pthread_* APIs (and they themselves are not exactly easy to use correctly).
This article is a good overview of why using the futex syscall directly is hard: https://lwn.net/Articles/823513/
(3) You're missing future bug fixes and optimizations.
(4) The kernel may move to a different API in future and glibc will handle that for you, but this code will still be using the old API.
The kernel has changed its threading APIs, the clone API, and how syscalls are made several times in the past, and you cannot predict how they will change in future.
I thought "don't break userspace" was one of the key principles of Linux design?
A portable solution (on any POSIX system, actually) for the stack switching would be to jump to the new stack using sigaltstack and initialize a sigjmp_t context (i.e. setjmp) from the signal handler. After calling clone you conditionally invoke longjmp based on the return value. This works because setjmp and longjmp are compiler builtins; but if not, use __builtin_setjmp, __builtin_longjmp. (You can pass the sigjmp_t context using the same trick mentioned in the writeup. And of course this also assumes the compiler won't emit stack-dependent code between the syscall and longjmp.)
You still have the problem of implementing the syscalls, though. But maybe if we scrounge around we can find solutions. On some architectures the vdso Linux maps into the process address space has a syscall implementation. And maybe there are similar compiler builtins for other architectures, or macros in the Linux header files with the necessary inline assembly.
As a learning exercise this type of stuff is fun ;)
[0]: https://www.gnu.org/software/gnulib/manual/html_node/Multith...
> ... However, there are a few issues with that, as this pthread type was designed for C, not for Rust.
> In Rust, we move objects around all the time. ... The pthread types we discussed do not guarantee they are movable, which becomes quite a problem in Rust. Even a simple idiomatic Mutex::new() function is a problem: it would return a mutex object, which would move it into a new place in memory.
> A solution to this problem is to wrap the mutex in a Box. By putting the pthread mutex in its own allocation, it stays in the same location in memory, even if its owner is moved around. This is how std::sync::Mutex was implemented on all Unix platforms before Rust 1.62.
> The downside of this approach is the overhead: every mutex now gets its own allocation, adding significant overhead to creating, destroying, and using the mutex. Another downside is that it prevents the new function from being const, which gets in the way of having a static mutex. Even if pthread_mutex_t was movable, a const fn new could only initialize it with default settings, which results in undefined behavior when locking recursively. There is no way to design a safe interface that prevents locking recursively, so this means we’d need to make the lock function unsafe to make the user promise they won’t do that.
> A problem that remains with our Box approach occurs when dropping a locked mutex. ... pthread specifies that calling pthread_mutex_destroy() on a locked mutex is not guaranteed to work and might result in undefined behavior. One work-around is to first attempt to lock (and unlock) the pthread mutex when dropping our Mutex, and panic (or leak the Box) when it is already locked, but that adds even more overhead. These issues don’t just apply to pthread_mutex_t, but to the other types we discussed as well. Overall, the design of the pthread synchronization primitives is fine for C, but just not a great fit for Rust.
I could see an argument for small shims injected by a vDSO (AIUI this is what Fuchsia does), but clearly the 80s-style "libc is the OS boundary" is obsolete.
Funny. “Optimality” is not a criticism I’ve ever heard levied often about glibc.
Examples of the same concept in different contexts: WTF lock Folly locks ABSEIL locks Rust mutexes
I have more interesting projects to work on than glibc maintenance.
> Practical Libc-free threading on Linux
> This article will demonstrate a simple, practical, and robust approach to spawning and managing threads using only raw system calls.
This is neither practical nor simple nor robust.
It is a good write-up on what happens behind the scenes when you set up threads in your language of choice with your library of choice. Great if you have that understanding.
Bring this into any kind of production code base (that isn't itself a threading library) and it's grounds for dismissal, because it makes very evident a severe lack of insight and judgement on what tools to use for a given problem at hand.
Linux system calls are one of the most stable interfaces out there, maybe even more so than glibc. "WE DO NOT BREAK USERSPACE" and all that jazz.
BTW, this is a pet peeve of mine. Having different syscall (and errno) numbers on different architectures is an historical mistake from Linux's first non-x86 port, the DEC alpha. My understanding is that Linus used DEC UNIX to bootstrap Linux, and needed to make Linux compatible with DEC's syscalls initially. Rather than make a new ABI for DEC UNIX binaries in advance, or clean up later, everything was left in place leading to a mess of 19 errno.h files in the kernel today.
For comparison, FreeBSD has 5. 4 of which are related to linux compat..
Still, this stuff is very cool and I would love to have time to play with it.
case '0' ... '9':This is a very good idea... since I decided to do exactly that for many of my personal projects and I started years ago.
That said, "clone" syscall is the easy part. For newcomer, a gentle introduction and explanation of the volontary cpu yielding "futex" synchronization primitive is kind of much more important, or we'll end up with spinlocks everywhere. Often, you need 2 "futexes" to build higher level synchronization primitives. One of the good things here is educational: it shows how much expensive a cpu yielding synchronization primitive is since usually it is "hidden" by the POSIX API.