-
Notifications
You must be signed in to change notification settings - Fork 202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add pthread.h in wasi sysroot to support pthread #209
Comments
Furthermore, I'm trying to build some C++ workload which use
I noticed that the Thanks a lot ! |
The current situation is that there is a consensus among the people working on threads support in the WebAssembly core spec is that the current semantics are not yet suitable for use outside of Web contexts. See the following issues for background: WebAssembly/threads#8 Following this guidance, WASI libc does not yet attempt to support pthreads. This guidance is of course open for discussion. If you're looking to do something WAMR-specific and don't need anything in |
The lack of pthread headers becomes problematic when attempting to use higher-level APIs in WASI SDK, such as C++ |
BTW, with llvm 11 there will be no need for undefined-symbols.txt at all anymore since symbols can be marked as imported in the source code.. so using a recent build of llvm should make that issue at least go away. |
It is a shame that Are you sure it would require a rebuild of libc rather than just the addition headers? I think this issue (allowing atomics to work in single-threads builds) is somewhat of a separate issue to actually add pthreads and/or compiling with threading/shared memory. |
Could you elaborate please what do you mean by "the addition headers" here? This is the error that I get currently without the pthread header, building wasi-libc with
|
When we initially set up all these builds, we configured them for "no threads" mode, because we care about code size and have since the beginning, and some libraries have smaller code size than if they are built in threads mode and we just lower atomics. I don't know if that's specifically true of libcxx though, so if someone could investigate that, that'd be a good start. If it does turn out that libcxx is bigger when compiled in threads mode, a possible option would be to produce two builds, one where everything is built with threads disabled, and one where threads are ostensibly enabled but atomics are lowered. |
Circling back to this issue, It appears that thread-local storage is needed to activate multithreading at this time. As a POSIX newbie, what does it take to generate a single-threaded pthreads implementation that doesn't need TLS? |
AFAIK clang supports |
Thanks for the prompt reply! |
|
Did you try adding the suggested |
I didn't know the two could work together. I'll try again. Thanks a bunch! |
I got the same error as before. |
I think LLVM/clang would need some modifications to support |
Ugh. Ok, thanks. |
To clarify, the motivation is that TLS support for WebAssembly should be implemented once on clang/LLVM level, instead of adding it separately every time in all client code that depends on TLS, which would ultimately save us a lot of effort in the long run. |
Oh ok. |
I found out I added the flags to the wrong spot. Now I got farther. |
I'm really close. It compiles and links but fails the test with:
|
what kind of error messages do you see? |
2 errors:
|
I'm really close! Adding |
Adding that symbol to the expected results clears the test! Now I just need to test it and update the submodules in my fork of wasi-sdk to use my fork of wasi-libc! |
I may have identified a rare bug in asyncify: If there are two yields in the same function does resuming the second yield call return to the correct place? |
@SamuraiCrow That should work in Asyncify. The mechanism is that we serialize an ID for the call location in the function, so we know how to return to the right place. We have tests for this that pass, but perhaps you've found a corner case somehow where we are buggy - if you can get a testcase that would be good. |
Perhaps I was looking at an oversimplified example of how Asyncify was supposed to work. If its design already accounts for the corner cases, it should meet my needs. |
Emscripten has a PThread stub that may accomplish what I was trying to do here instead of Asyncify. Asyncify looks rather involved. |
Does you code actually need to do threading? Or can it run in a single thread if needs be (i.e. can run on a system where pthread_create() always fails)? The pthread stubs in emscripten are a version of the pthread API that doesn't do anything and doesn't allow for the creation of any threads.. just FYI. Its uses in the case where you compile a thread-aware program but you are targeting a single-threaded runtime. |
There's a conditional compilation flag that allows single-threaded execution but I didn't know about it until recently. |
Any news to this issue? I am trying to figure out as well how to compile code with threads with WASI. |
There is ongoing work to support building with threading support. See #311 |
Fix 'pthread.h not found' error by disabling thread support on DCMTK. Set DCMTK_WITH_THREADS = OFF to disable threads. Refer to [1] for more details. [1] WebAssembly/wasi-libc#209
@sbc100
I found https://stackoverflow.com/questions/70180309/how-to-link-wasi-libc-with-shared-memory-flag, where you mentioned this error stems from the fact that the wasi-libc distribution isn't build with With #311 merged, does that mean it is now possible to build a wasi-libc with |
Looks like However, when trying to compile a simple pthread test program, I get errors for many undefined symbols:
Do the thread-enabled sysroot's libraries assume that the user defines these? cc @abrown |
I don't think the "threads in wasi-libc" story is quite ready for use yet; there are still big pieces that need additional work. However, progress is being made: #311 is a start and the recent merging of #325 is an important step forward. Some of the pieces that still need work: finish compiling in the remainder of the
Probably not; I would guess that this is part of finishing out the compilation of more |
@abrown Thank you for the update, that helps understanding what to expect. That's really great work! Over the weekend, I had done some research into how pthreads support could be added to https://github.com/turbolent/w2c2 and tried to piece together the puzzle of what the libc would need to do, how the runtime could implement it, etc. just to realize there actually is a WASI proposal now, and that an implementation is in progress. Exciting! I'm looking forward to implementing the wasi-threads proposal once the libc has support for it. Unfortunately I'm not really familiar with the implementation details on that side. |
Fix 'pthread.h not found' error by disabling thread support on DCMTK. Set DCMTK_WITH_THREADS = OFF to disable threads. Refer to [1] for more details. [1] WebAssembly/wasi-libc#209
Fix 'pthread.h not found' error by disabling thread support on DCMTK. Set DCMTK_WITH_THREADS = OFF to disable threads. Refer to [1] for more details. [1] WebAssembly/wasi-libc#209
Fix 'pthread.h not found' error by disabling thread support on DCMTK. Set DCMTK_WITH_THREADS = OFF to disable threads. Refer to [1] for more details. [1] WebAssembly/wasi-libc#209
Fix 'pthread.h not found' error by disabling thread support on DCMTK. Set DCMTK_WITH_THREADS = OFF to disable threads. Refer to [1] for more details. [1] WebAssembly/wasi-libc#209
Fix 'pthread.h not found' error by disabling thread support on DCMTK. Set DCMTK_WITH_THREADS = OFF to disable threads. Refer to [1] for more details. [1] WebAssembly/wasi-libc#209
Fix 'pthread.h not found' error by disabling thread support on DCMTK. Set DCMTK_WITH_THREADS = OFF to disable threads. Refer to [1] for more details. [1] WebAssembly/wasi-libc#209
Fix 'pthread.h not found' error by disabling thread support on DCMTK. Set DCMTK_WITH_THREADS = OFF to disable threads. Refer to [1] for more details. [1] WebAssembly/wasi-libc#209
Fix 'pthread.h not found' error by disabling thread support on DCMTK. Set DCMTK_WITH_THREADS = OFF to disable threads. Refer to [1] for more details. [1] WebAssembly/wasi-libc#209
Fix 'pthread.h not found' error by disabling thread support on DCMTK. Set DCMTK_WITH_THREADS = OFF to disable threads. Refer to [1] for more details. [1] WebAssembly/wasi-libc#209
Fix 'pthread.h not found' error by disabling thread support on DCMTK. Set DCMTK_WITH_THREADS = OFF to disable threads. Refer to [1] for more details. [1] WebAssembly/wasi-libc#209
Fix 'pthread.h not found' error by disabling thread support on DCMTK. Set DCMTK_WITH_THREADS = OFF to disable threads. Refer to [1] for more details. [1] WebAssembly/wasi-libc#209
Fix 'pthread.h not found' error by disabling thread support on DCMTK. Set DCMTK_WITH_THREADS = OFF to disable threads. Refer to [1] for more details. [1] WebAssembly/wasi-libc#209
Fix 'pthread.h not found' error by disabling thread support on DCMTK. Set DCMTK_WITH_THREADS = OFF to disable threads. Refer to [1] for more details. [1] WebAssembly/wasi-libc#209
Fix 'pthread.h not found' error by disabling thread support on DCMTK. Set DCMTK_WITH_THREADS = OFF to disable threads. Refer to [1] for more details. [1] WebAssembly/wasi-libc#209
Fix 'pthread.h not found' error by disabling thread support on DCMTK. Set DCMTK_WITH_THREADS = OFF to disable threads. Refer to [1] for more details. [1] WebAssembly/wasi-libc#209
Hi,
The pthread library has been supported in wasm micro runtime. The pthread related APIs in wasm application should be imported from the runtime, such as pthread_create, pthread_join, etc. so the runtime can create an actual thread to execute the wasm function.
For example, suppose we have such a C code:
The
pthread_create
should be compiled as an import function:In this situation, I think there should be a simple way to support pthread in wasi-sdk as we only need a few things:
To achieve 1 and 2, we just need a
pthread.h
undersysroot/include
, just like WAMR's pthread.h, we also need to undef some macros like__NEED_pthread_t
to avoid duplicate definition withsysroot/include/bits/alltypes.h
And to achieve 3, we need to add pthread API symbols into
sysroot/share/wasm32-wasi/undefined-symbols.txt
just like wasi APIsIn this way, we don't need to provide libpthread.a in wasi-libc, as the pthread related APIs should be implemented by the runtime.
Do you think this is a reasonable way to support pthread ?
The text was updated successfully, but these errors were encountered: