
A Deep Dive into Implicit Thread-Local Storage - pplonski86
https://chao-tic.github.io/blog/2018/12/25/tls
======
chrisseaton
I don't understand why the linker and kernel need to be involved? Why isn't
storing the address of the thread-local memory in the register enough? That
only needs to involve the compiler to know about the register and the runtime
to set it. The article talks about needing the kernel to set a privileged
register, but we've got tons of general purpose registers we could store the
thread-local address in.

~~~
bdonlan
In 32-bit x86 there aren't very many registers to play with, and there were
legacy ABIs that didn't preserve registers for TLS, so the segment registers
were used instead. When x86-64 arrived, it had a lot more registers (but still
less than many other architectures) and a clean ABI break, but it kept enough
segment register functionality to use for TLS, so it continued to be used to
free up that one register - why not, after all?

~~~
chrisseaton
> why not, after all?

Because it seems to need the kernel to set it? But I guess that's only done
once each time you create a new thread and you're already doing a syscall in
that case, so it's probably better than taking up a general purpose register.

------
terrelln
I've generally found that thread-local storage is easily misused. My general
rule of thumb is to avoid thread-locals wherever possible, but if they are
required keep thread-local state constant sized, not one per object. The
memory usage can grow rapidly when your library is used with many objects and
many threads. In places where you would want one thread-local per object,
instead try core-locals.

~~~
quotemstr
Sure. Sometimes, people use TLS to implement Lisp-style "dynamic scope" in an
effort to reduce the amount of information passed from call-frame to call-
frame via explict parameters. This approach suffers from the same drawback
that dynamic scope does, and it's usually (but not always!) better to be
explicit.

~~~
saagarjha
> TLS

Not the clearest abbreviation ;)

