-
-
Notifications
You must be signed in to change notification settings - Fork 645
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply fixup to cthread initialization (#301)
Cosmopolitan Threads are currently Linux-only (with some NetBSD and Windows support too!). This change ensures we only initialize the high-level threading runtime when Cosmopolitan Threads are used.
- Loading branch information
Showing
3 changed files
with
13 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91d7833
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jart We need to be careful that thread local variables should still work without cthread (
%fs
should be set correctly). Maybe we use the.tdata
and.tbss
directly in that case. It would add the benefit to never fail.Another solution would be to tell the compiler that thread locals are just plain old globals, but I don't know how to do it.
91d7833
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lemaitre Programs that need TLS can work around it for the time being by using the static yoink. Keep in mind thread locals are going to be problematic in general. Other operating systems don't let us change the segment registers. Worst of all, x86-64-linux-gnu doesn't have a flag for generating TLS code that doesn't assume %fs is configured. I've considered solving the problem by having tool/build/package.c rewrite binary objects post-compilation to change the TLS instructions into something more manageable. But that only helps code in the Cosmopolitan codebase. We might have to rewrite the binary in memory at runtime using third_party/xed/ in order to fully support TLS.
91d7833
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't care much about TLS performance, we could most likely tell gcc to generate thread locals for the dynamic local model in order to have a call to
__tls_get_addr
in which we can have the magic we need.Also, we can tell gcc that object is relative to either %fs or %gs with an attribute (even though, it is not TLS as far as the compiler is aware).
Regarding to other systems, I think it will be manageable in Windows : https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreadcontext
I don't know about BSD or MacOS.
91d7833
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do those flags exist and do they work? Where does Windows store the TLS data? I know for example it needs %gs for its PEB internals. XNU lets us change %gs but not %fs. OpenBSD lets us change %fs but not %gs. FreeBSD and Linux let us set either. Haven't checked NetBSD yet. If we can figure out a way to control a segment register on Windows, then we can have fast TLS on all platforms. But we'd need to have Xed change the 0x64 segment prefixes to 0x65 at startup on XNU. That should be reasonably inexpensive since it doesn't need to relocate instructions.