emscripten-core / emscripten-core/emscripten
Strange behaviour when detecting ENVIRONMENT_IS_PTHREAD
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
```
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.61 (67fa4c16496b157a7fc3377afd69ee0445e8a6e3)
clang version 19.0.0git (https:/github.com/llvm/llvm-project 7cfffe74eeb68fbb3fb9706ac7071f8caeeb6520)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /home/jl/w/github/gst.wasm/build/gst.wasm_web_wasm32/emsdk/upstream/bin
```
I am porting a multithreaded library and when creating (and transferring the canvas to) a thread I get the following error:
```
pthread_create: could not find canvas with ID "#canvas" to transfer to thread!
pthread_create: cannot transfer canvas with ID "canvas" to thread, since the current thread does not have control over it!
```
My code is creating a new thread from another thread already created. If I use the main thread, everything works as expected.
I first thought that it was not possible to create a thread from another thread but there is this code:
```
// Synchronously proxy the thread creation to main thread if possible. If we
// need to transfer ownership of objects, then proxy asynchronously via
// postMessage.
if (ENVIRONMENT_IS_PTHREAD && (transferList.length === 0 || error)) {
return pthreadCreateProxied(pthread_ptr, attr, startRoutine, arg);
}
```
Which made me discard that idea, reviewing the .js code I see this:
```
} else if (!ENVIRONMENT_IS_PTHREAD) {
var canvas = (Module['canvas'] && Module['canvas'].id === name) ? Module['canvas'] : document.querySelector(name);
if (!canvas) {
err(`pthread_create: could not find canvas with ID "${name}" to transfer to thread!`);
error = 28;
break;
}
if (canvas.controlTransferredOffscreen) {
err(`pthread_create: cannot transfer canvas with ID "${name}" to thread, since the current thread does not have control over it!`);
error = 63; // Operation not permitted, some other thread is accessing the canvas.
break;
}
```
For some reason, the thread I'm creating the new thread on is evaluating to True in `ENVIRONMENT_IS_PTHREAD`
Any idea what is going on? My understanding is that it should evaluate to False and then recall this proxied from the main thread.
Important topic, this only happens with -sPROXY_TO_PTHREAD set
Contributor guide
Assessment
This issue has not been assessed yet.