Open gparmer opened 7 years ago
Notes on the options presented here:
1) This option sounds nice, but I don't think it really works. We have to call libc's thread initialization routines to set up pthread
self correctly, and so we basically don't get anything out of doing it this way.
2) I favor this option. We can just stub out all the methods to fail, and then add them incrementally. I've spent a lot of time in libc internals, and I think I could do this. Since "pthreads" is a fairly high level API, we should just get composite level performance with compatibility. Additionally, we can make the api work with threads created outside the api.
3) If we pursue this option we basically have to fix all the current issues with pthreads. This means making the libc initialization routine work, perfectly emulating complex linux system calls, and generally a whole lot of annoying work. (I actually think this is more work than 2.)
@Others thanks for the comment!
Agreed with option 1 that it doesn't solve the whole problem. We would still need to figure out how to work with pthreads. However, if we continue to have TLS issues, it is an orthogonal option. But lets not get side-tracked: you're right that this is a different thing.
Why do you think that option 3 is more work? I'd appreciate your context here. My concerns incluce: implementing condition variables alone would be a huge effort to get right; keys and all the varieties of join and attributes would be annoying; cancelling seems atrocious; RW locks seem painful. I think this requires actual evidence-based analysis, not solely high-level statements. It is 2700 LoC in musl. For your reference: http://cursuri.cs.pub.ro/~apc/2003/resources/pthreads/uguide/users-gu.htm#301162
For option three, emulating general system calls can often (but not always) be easier than emulating those with complex, particular semantics. Futex emulation, and clone emulate are the key parts of the Linux system call layer that would require emulation. Other smaller system calls for TLS, scheduling, etc, I'd imagine would be quite a bit easier (but I haven't though this through tempered with enough examples to say anything beyond a "feeling"). It is hard to convince myself this is more work than option 2. I recognize that there are fiddly bits around initialization and such, and getting the system calls right will not be nothing. But the amount of debugging involved in getting pthread emulation working is also pretty large.
Summary. I think that what we can say is that if parts of musl outside of the pthread code require pthread capabilities (i.e. if any part of the system outside of pthreads uses TLS), then option 2 is difficult. We'd end up in a situation where we have our own library, but libc code assumes pthreads are used. errno
is one example where this might be an issue. I don't have faith in my imagination to predict others. There is some evidence that the rest of the library does rely on pthreads: pthread-specific code is executed at initialization.
If this is not an issue, and pthreads are a "drop in" part of the library (i.e. the library works completely without them), then we might be able to get a pared down version of option 2 working. I'd guess this is what @Others has in mind.
I very much don't like the requirement of TLS in each component, so Option 2 is very appealing to me. But there has to be an evidence-based discussion about the options. Given that, a list of quesitons:
Is TLS
the main problem with many different runtimes, libraries that we foresee to support?
I think we need to talk in a meeting to get broader context and be able to understand for ex: why we moved to musl
from diet
or even diet
in the first place. Moreover, I don't know if this problem is easier to solve or requires less work if we use a different libc
. I'm still trying to understand this problem thoroughly.
Given the above context and options, I think Option 3 is the most viable one. Though it's not as easy and we'll have obstacles, if we end up using pthread
and fixing all of it's issues, we might be able to use it for broader runtimes, usecases etc.
@phanikishoreg I agree a meeting is in order. Perhaps Wednesday?
It feels like our options for a pthread emulation library vs. a syscall emulation library comes down to a single factor: what rust crates do we want to support, and how controlled is the rust code. I'll illuminate what I mean by this with examples:
We want to write a low-level component in rust. We want to use the standard containers, concurrency facilities (channels, Arcs, and Mutexes), and threads. We do not care about supporting general crate execution, and instead focus on a low-level environment for OS code.
We want to run most crates (where possible), and get full-featured Rust up and running. All rust, all the time. But because this environment will have a lot of dependencies on the surrounding system (memory management, FS, networking, scheduling, etc...), it is unlikely to be appropriate for OS-level components.
I believe our discussion has been conflating these environments. I believe these environments require different designs. Without thinking too deeply about the details, it seems to me that we'd want:
So in pictures, we have
+-------------+----------+------------+
| Rust crate |Rust crate| native app |
+---------+---+----------+--+---------+
| Option 1| | |
+---------+ + |
| | musl libc | |
+ +-----------------+ |
| | Opt 2/libposix | |
+ +-----------------+ |
| cos_kernel_api/sl/... |
+-------------------------------------+
| Composite Kernel/VK |
+-------------------------------------+
Please note that this discussion is broader than Rust. If we want to use our own threading libraries and libc, we'll have this issue (see the "native app" above). This is more of an issue of supporting general legacy code vs. supporting our own abstractions. Note that there is likely more interesting research for the latter.
@gparmer Wednesday sounds good to me!
In RTEMS we use newlib with custom threading. I don't know if switching your libc is an option for you to consider.
@Others, when porting Rust to composite, has run into an issue with
musl
andpthread
s. To set up thread local storage (TLS) for each thread, we must executepthread
code. Parts of the rust standard library assume that they can use TLS. This starts taking us down a path where we just use libc interfaces for pretty much everything, instead of raw, low-overhead composite interfaces. So is there a design that gives us all benefits: use of TLS as expected for Rust and other run-times, efficient use of low-level composite interfaces when required, and low software eng. requirements (up-front, and in maintenance)?A non-exhaustive set of options include:
__pthread_self
inmusl
to avoid using TLS, and instead use a global variable. This can essentially just be a field in the currentsl_thread
. Unfortunately, this might fixpthread
s inmusl
(as in, they won't use TLS anymore), but if Rust uses TLS in its own standard library (or in crate x, y, or z), then we aren't left with many options. Thus, this might work well for musl, but not necessarily for libraries stacked on top. Are these problems we want to solve separately? Perhaps.pthread
library. The API is pretty broad so this would be a pain. It isn't clear how much ofmusl
would break if we do this. How much of it depends on the current implementation ofpthread
s? Or are threads kept somewhat isolated?pthread
API for our components that require higher level services in languages such as rust. Unfortunately, the appeal of a language like rust is that it is low-level enough that we should be able to avoid a lot of POSIX BS when possible.Are there any options that I'm missing?
This is complicated by the fact that components will execute in different environments. Sometimes with access to many resources, sometimes with access solely to invocations to a manger (see #251 for more details). The backends for how we handle different
musl
system calls will need to be different depending on the environment. This adds another dimension of complexity to this issue.@phanikishoreg @ryuxin