emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.4k stars 3.25k forks source link

Pthreads custom wrapper #14452

Open jozefchutka opened 3 years ago

jozefchutka commented 3 years ago

Hi @sbc100 @kripken

following our conversation on another issue https://github.com/emscripten-core/emscripten/issues/14312#issuecomment-854913896 , I am now building the ffmpeg with -s ENVIRONMENT=worker -s BUILD_AS_WORKER=1, on JS side I spin up my custom worker.js (that creates the module) and calls module._main(). I am able to call main, but I need to call it with pthread wrapper I guess.

When calling module._main(argc, argv);, I see console logging Tried to spawn a new thread, but the thread pool is exhausted. so I increased pool size -s PTHREAD_POOL_SIZE=16, but event after log errors are gone the further process / logs hangs and is never finished. So my guess I need to properly wrap main()?

I wonder if this is possible on JS side or needs to be done in C. I tried to copy over https://github.com/emscripten-core/emscripten/blob/bdc97fe0de1daf9e66aea4fa1d30b3dc3bd907c3/system/lib/pthread/library_pthread.c#L935-L951 into my ffmpeg.c where main() exists, but obviously that throws compiler errors. I am not a C developer, so if possible, I would like to handle as much as possible on JS side.

I have looked into https://github.com/emscripten-core/emscripten/blob/2.0.24/src/library_pthread.js if there is anything that would guide me but no luck so far.

kripken commented 3 years ago

BUILD_AS_WORKER is an unrelated option to pthreads and the pthread pool. BUILD_AS_WORKER can use Web Workers without pthreads or shared memory, using a simple message-passing API (emscripten_call_worker etc.). That option is not mean to be used with pthreads (as they are much more efficient, using shared memory), and perhaps trying to use both at once is hitting a problem.