emscripten-core / emscripten-core/emscripten

Fetch API with pthreads has missing symbols

Open
#7,490 18 comments 0 reactions 0 assignees View on GitHub
multithreading
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

I've been attempting to port some of our projects to support Emscripten (v1.38.16), and I've started to encounter `Uncaught ReferenceError`s when both the worker thread and another thread are allocating at the same time. If a thread is already allocating when the Fetch worker attempts to allocate, it will fail the second conditional here: https://github.com/kripken/emscripten/blob/4bba062e95fb319ae9ba89797c88a0eef3dea1be/system/lib/libc/musl/src/thread/pthread_mutex_lock.c#L7-L9

The Fetch worker will then attempt to call `__pthread_mutex_timedlock()` which will then result in this error `Uncaught ReferenceError: ___pthread_mutex_timedlock is not defined`. I found the `tools/shared.py` script which generates the `fetch-worker.js` file and began adding all of the missing functions (as the reference errors came up) and ended up with:
```python
funcs_to_import = ['alignUp', 'getTotalMemory', 'stringToUTF8', 'intArrayFromString', 'lengthBytesUTF8', 'stringToUTF8Array', '_emscripten_is_main_runtime_thread', '_emscripten_futex_wait', '_emscripten_futex_wake', '_pthread_self']
asm_funcs_to_import = ['_malloc', '_free', '_sbrk', '___pthread_mutex_lock', '___pthread_mutex_unlock', '___pthread_mutex_timedlock', '___pthread_mutex_trylock', '___timedwait', '___pthread_setcancelstate']
```
Unfortunately, `_pthread_self()` requires `__pthread_ptr` which I believe requires the usage of `pthread_create()`? It seems like the solution is to make the worker thread more like a pthread as suggested in #7024?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.