Imagine main()
creates two threads. Each thread gets it errno
(as a private function). There's a function in the program, which can be called by both of these two threads. It is made thread safe.
How compiler will arrange this function to ensure its same code calls appropriate errno
function? How "thread environment" is being passed to the same piece of code?
Edit. The question is not about errno
per se. Question is about how this common function knows which errno
to call when it gets execution. From where the same piece of code gets different thread-specific info on errno
function?
Edit: does it assume that if I pass 3 variables to the function, there're actually several more variables passed behind the scenes, and one of them is this FS
processor register, which points to thread environment? If I used say errno
in this common function, how this function knows that it must search for errno
in its environment - is this mechanism embedded in errno
definition?
Conclusion: due to declaration of errno
as __thread
compiler knows that this entity (variable or whatever) is thread-specific, and to access this compiler uses another "background" input to the function - for example, processor's FS
register which always contains pointer to thread environment.