0

I spent 2 days on compiling my C++ project with enabled pthreads via emscripten to WASM without success.

It compiles fine. I used options -s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=4.

But when I execute my index.html via "emrun index.html" - it even doesn't reach main function and stuck on pthreads pool preallocation: pthread sent an error! undefined:undefined: undefined

Obviously if I don't specify pthreads pools - it starts my main function but throws the same error when I reach pthread creation in my code.

Unfortunately I didn't find any answers in the google related to this problem. I run this in my browser: https://alex-wasm.appspot.com/threads/index.html And it reports that browser fully supports pthreads in WebAssembly.

Today I found example WASM project which is used pthreads but surprisingly when I start it with emrun I get exactly the same pthreads error in the console.

After this I tried to run it without emrun and got different error:

Uncaught DOMException: Failed to construct 'Worker': Script at 'file:///D:/engine/src/main/Emscripten/wengine.worker.js' cannot be accessed from origin 'null'. at Object.allocateUnusedWorker (file:///D:/engine/src/main/Emscripten/wengine.js:2221:36) at Object.initMainThreadBlock ...

I get the same error in both my project and example project when I run without emrun...

P.S. I need emrun to access my files without deploying the project on the server (for fast debugging).

So can you please give me the hints for this 2 questions:

  1. Is it possible to run webassembly build with pthreads support using emrun?
  2. Why it doesn't work even without emrun with this Uncaught DOMException ?
Andrey Honich
  • 41
  • 1
  • 4
  • 2
    What browser do you use? Firefox e.g. when run app from the link prints that it is ok but at the same time one get in the console: ```Current environment does not support SharedArrayBuffer, pthreads are not available!``` (whilst Chrome logs other error). – Ryszard Grzesica Feb 04 '21 at 11:28
  • Since Firefox doesn't support SAB (due to Spectre/Meltdown prevention) it most likely will not work for emrun too. Also did you set `--emrun` linker flag in your project - see https://emscripten.org/docs/compiling/Running-html-files-with-emrun.html? – Ryszard Grzesica Feb 04 '21 at 11:39
  • If you want to allow Firefox to load files from the file:// scheme, you need to change a preference or else it will block them (CORS). In the address bar type about:config, then search for security.fileuri.strict_origin_policy . Change that to false and a page loaded from a file:// address will be allowed to load others from the file system. – S Waye Feb 05 '21 at 21:32
  • And recent version of Firefox support shared memory, check for the javascript.options.shared_memory setting also from about:config – S Waye Feb 05 '21 at 21:33
  • Yes it is supporting but only with COOP/COEP, whilst for time being (due to site isolation and will also change) you may run in Chrome without it. – Ryszard Grzesica Feb 06 '21 at 07:31

1 Answers1

0
  1. See the Emscripten docs on pthread support, which state:

    Browsers that have implemented and enabled SharedArrayBuffer, are gating it behind Cross Origin Opener Policy (COOP) and Cross Origin Embedder Policy (COEP) headers. Pthreads code will not work in deployed environment unless these headers are correctly set. For more information click this.

After this I tried to run it without emrun and got different error:

Uncaught DOMException: Failed to construct 'Worker': Script at 'file:///D:/engine/src/main/Emscripten/wengine.worker.js' cannot be accessed from origin 'null'. at Object.allocateUnusedWorker (file:///D:/engine/src/main/Emscripten/wengine.js:2221:36) at Object.initMainThreadBlock ...

Chrome doesn't let you load web workers when running scripts from a local file.

starball
  • 20,030
  • 7
  • 43
  • 238