Skip to content

Using libc++ with the "emulated pthreads" mode #546

@whitequark

Description

@whitequark

Now that WebAssembly/wasi-libc#518 has been merged thanks to the excellent work by @ArcaneNibble, we have pthread.h exist dormantly in single-threaded builds, only triggering an assertion if someone was using a function like pthread_create which is definitely missing. However, the original point of that PR was to enable the use of <atomic>, <mutex>, and other such headers in single-threaded WASI targets.

Libc++ can be built in single-threaded or multi-threaded mode, which changes a #define it embeds in a header, and also changes code generated for libc++.{a,so}. This is a problem given the direction chosen for handling pthread.h: right now, if I adjust the wasi-sdk build scripts to enable multi-threaded mode at all times, it becomes impossible to build any C++ code without also passing in -D_WASI_EMULATED_PTHREAD. This is obviously not a feasible way forward.

I can see three potential paths forward here:

  1. Add a target variant, let's say wasm32-wasi-emuthreads, that has libc++ compiled in the multi-threaded mode and linked with the emulated pthreads library.
  2. Ship a multi-threaded libc++ with the existing wasm32-wasi target, but modify the pthread integration code such that -D_WASI_EMULATED_PTHREAD isn't needed to build code that only needs e.g. <optional> but not <atomic>. This would involve extensive hacking at libc++ because right now e.g. <optional> depends on <atomic> (and many other seemingly non-threading-related headers depend, transitively, on pthreads functions).
  3. Give up. Applications like Clang or Slang (a SystemVerilog frontend in C++) will not be buildable with upstream WASI SDK for single-threaded targets, limiting their deployability. I will likely switch to using my fork of the SDK and start to recommend it to anyone porting complex C++ applications.

To me, (2) seems not really a feasible solution given the resource constraints in practice, but I can also see that (1) may be viewed as objectionable. I could put in some effort towards implementing either (I'm halfway through solution 1) but I would like clear guidance from the upstream first.

cc @sbc100 @alexcrichton

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions